-
Notifications
You must be signed in to change notification settings - Fork 8
/
ignition-generator
executable file
·151 lines (129 loc) · 4.64 KB
/
ignition-generator
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
# ex: ts=8 sw=4 sts=4 et filetype=sh
set -e
# Generators don't have logging right now
# https://github.com/systemd/systemd/issues/15638
exec 1>/dev/kmsg; exec 2>&1
UNIT_DIR="${1:-/tmp}"
cmdline=( $(</proc/cmdline) )
cmdline_arg() {
local name="$1" value="$2"
for arg in "${cmdline[@]}"; do
if [[ "${arg%%=*}" == "${name}" ]]; then
value="${arg#*=}"
fi
done
echo "${value}"
}
cmdline_bool() {
local value=$(cmdline_arg "$@")
case "$value" in
""|0|no|off) return 1;;
*) return 0;;
esac
}
add_requires() {
local name="$1"; shift
local target="$1"; shift
local requires_dir="${UNIT_DIR}/${target}.requires"
mkdir -p "${requires_dir}"
ln -sf "../${name}" "${requires_dir}/${name}"
}
add_wants() {
local name="$1"
local wants_dir="${UNIT_DIR}/initrd.target.wants"
mkdir -p "${wants_dir}"
ln -sf "../${name}" "${wants_dir}/${name}"
}
# This can't be done with ConditionKernelCommandLine because that always
# starts the unit's dependencies. We want to start networking only on first
# boot.
if $(cmdline_bool flatcar.first_boot 0) || $(cmdline_bool coreos.first_boot 0); then
add_requires ignition-complete.target initrd.target
# Only try to mount the ESP if GRUB detected a first_boot file
if [[ $(cmdline_arg flatcar.first_boot) = "detected" ]] || [[ $(cmdline_arg coreos.first_boot) = "detected" ]]; then
add_requires ignition-quench.service initrd.target
fi
if [[ $(cmdline_arg flatcar.oem.id) == "packet" ]] || [[ $(cmdline_arg coreos.oem.id) == "packet" ]]; then
add_requires flatcar-static-network.service initrd.target
fi
# On EC2, shut down systemd-networkd if ignition fails so that the instance
# fails EC2 instance checks.
if [[ $(cmdline_arg flatcar.oem.id) == "ec2" ]] || [[ $(cmdline_arg coreos.oem.id) == "ec2" ]]; then
mkdir -p ${UNIT_DIR}/systemd-networkd.service.d
cat > ${UNIT_DIR}/systemd-networkd.service.d/10-conflict-emergency.conf <<EOF
[Unit]
Conflicts=emergency.target
Conflicts=emergency.service
Conflicts=dracut-emergency.service
EOF
fi
# Configure hostname from metadata through afterburn on platforms that don't set it via DHCP
add_wants flatcar-metadata-hostname.service
if [[ $(cmdline_arg flatcar.oem.id) == "openstack" ]] || [[ $(cmdline_arg coreos.oem.id) == "openstack" ]]; then
add_wants flatcar-openstack-hostname.service
fi
else
# If we're doing a non-Ignition (subsequent) boot, then
# queue a different target. This is necessary so that units
# like `ignition-ostree-mount-sysroot.service`
# can cleanly distinguish between the two.
add_requires ignition-subsequent.target initrd.target
fi
# Write ignition-setup.service customized for PXE/ISO or regular boot
pxe=
nopxe=
usr=$(cmdline_arg mount.usr "$(cmdline_arg usr)")
if [[ -z "${usr}" && -f /usr.squashfs ]]; then
# PXE-booted system, with or without persistent root
# (see 10diskless-generator)
pxe=1
else
nopxe=1
if $(cmdline_bool flatcar.first_boot 0) || $(cmdline_bool coreos.first_boot 0); then
add_requires ignition-diskful.target ignition-complete.target
else
add_requires ignition-diskful-subsequent.target ignition-subsequent.target
fi
fi
cat > ${UNIT_DIR}/ignition-setup-pre.service <<EOF
[Unit]
Description=Ignition env setup
DefaultDependencies=false
Requires=local-fs-pre.target
Before=local-fs-pre.target
OnFailure=emergency.target
OnFailureJobMode=isolate
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/ignition-setup-pre
EOF
cat > ${UNIT_DIR}/ignition-setup.service <<EOF
[Unit]
Description=Ignition (setup)
DefaultDependencies=false
# Flatcar: Load Ignition binary
Requires=sysusr-usr.mount ignition-setup-pre.service
After=sysusr-usr.mount ignition-setup-pre.service
Requires=local-fs-pre.target
Before=local-fs-pre.target
# pull in OEM device if it should exist
${nopxe:+Requires=dev-disk-by\x2dlabel-OEM.device
After=dev-disk-by\x2dlabel-OEM.device}
OnFailure=emergency.target
OnFailureJobMode=isolate
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/ignition-setup ${nopxe:+normal} ${pxe:+pxe}
EOF
# Call the disk UUID randomizer whenever we're not PXE booting
# The unit will check if it needs to act or not.
if [ "${nopxe}" = 1 ]; then
add_requires "disk-uuid.service" initrd.target
fi
if [[ $(cmdline_arg flatcar.oem.id) == "digitalocean" ]] || [[ $(cmdline_arg coreos.oem.id) == "digitalocean" ]] || [[ $(cmdline_arg flatcar.oem.id) == "proxmoxve" ]]; then
add_requires flatcar-afterburn-network.service initrd.target
fi