2 # SPDX-License-Identifier: LGPL-2.1-or-later
6 # shellcheck source=test/units/util.sh
7 .
"$(dirname "$0")"/util.sh
9 if ! command -v systemd-firstboot
>/dev
/null
; then
10 echo "systemd-firstboot not found, skipping the test"
15 if [[ -n "${ROOT:-}" ]]; then
17 grep -r .
"$ROOT/etc" ||
:
26 # Generated via `mkpasswd -m sha-512 -S foobarsalt password1`
27 # shellcheck disable=SC2016
28 ROOT_HASHED_PASSWORD1
='$6$foobarsalt$YbwdaATX6IsFxvWbY3QcZj2gB31R/LFRFrjlFrJtTTqFtSfn4dfOAg/km2k4Sl.a2g7LOYDo31wMTaEsCo9j41'
29 # Generated via `mkpasswd -m sha-512 -S foobarsalt password2`
30 # shellcheck disable=SC2016
31 ROOT_HASHED_PASSWORD2
='$6$foobarsalt$q.P2932zYMLbKnjFwIxPI8y3iuxeuJ2BgE372LcZMMnj3Gcg/9mJg2LPKUl.ha0TG/.fRNNnRQcLfzM0SNot3.'
33 if [[ -f /etc
/locale.conf
]]; then
34 cp /etc
/locale.conf
/tmp
/locale.conf.bak
37 # Debian/Ubuntu specific file
38 if [[ -f /etc
/default
/locale
]]; then
39 cp /etc
/default
/locale
/tmp
/default-locale.bak
42 if [[ -f /etc
/locale.gen
]]; then
43 cp /etc
/locale.gen
/tmp
/locale.gen.bak
46 # Make sure at least two locales exist (C.UTF-8 and en_US.UTF-8) as systemd-firstboot --prompt-locale will
47 # skip writing the locale if it detects only one is installed.
48 generate_locale en_US.UTF-8
50 # Debian and Ubuntu use /etc/default/locale instead of /etc/locale.conf. Make
51 # sure we use the appropriate path for locale configuration.
52 LOCALE_PATH
="/etc/locale.conf"
53 [ -e "$LOCALE_PATH" ] || LOCALE_PATH
="/etc/default/locale"
54 [ -e "$LOCALE_PATH" ] || systemd-firstboot
--locale=C.UTF-8
56 # Create a minimal root so we don't modify the testbed
59 # Dummy shell for --root-shell=
60 touch "$ROOT/bin/fooshell" "$ROOT/bin/barshell"
62 systemd-firstboot
--root="$ROOT" --locale=foo
63 grep -q "LANG=foo" "$ROOT$LOCALE_PATH"
64 rm -fv "$ROOT$LOCALE_PATH"
65 systemd-firstboot
--root="$ROOT" --locale-messages=foo
66 grep -q "LC_MESSAGES=foo" "$ROOT$LOCALE_PATH"
67 rm -fv "$ROOT$LOCALE_PATH"
68 systemd-firstboot
--root="$ROOT" --locale=foo
--locale-messages=bar
69 grep -q "LANG=foo" "$ROOT$LOCALE_PATH"
70 grep -q "LC_MESSAGES=bar" "$ROOT$LOCALE_PATH"
72 systemd-firstboot
--root="$ROOT" --keymap=foo
73 grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
75 systemd-firstboot
--root="$ROOT" --timezone=Europe
/Berlin
76 readlink
"$ROOT/etc/localtime" |
grep -q "Europe/Berlin"
78 systemd-firstboot
--root="$ROOT" --hostname "foobar"
79 grep -q "foobar" "$ROOT/etc/hostname"
81 systemd-firstboot
--root="$ROOT" --machine-id=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
82 grep -q "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "$ROOT/etc/machine-id"
84 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
85 systemd-firstboot
--root="$ROOT" --root-password=foo
86 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
87 grep -q "^root:[^!*]" "$ROOT/etc/shadow"
88 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
89 echo "foo" >root.passwd
90 systemd-firstboot
--root="$ROOT" --root-password-file=root.passwd
91 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
92 grep -q "^root:[^!*]" "$ROOT/etc/shadow"
93 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow" root.passwd
94 # Make sure the root password is set if /etc/passwd and /etc/shadow exist but
95 # don't have a root entry.
96 touch "$ROOT/etc/passwd" "$ROOT/etc/shadow"
97 systemd-firstboot
--root="$ROOT" --root-password=foo
98 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
99 grep -q "^root:[^!*]" "$ROOT/etc/shadow"
100 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
101 # If /etc/passwd and /etc/shadow exist, they will only be updated if the shadow
102 # password is !unprovisioned.
103 echo "root:x:0:0:root:/root:/bin/sh" >"$ROOT/etc/passwd"
104 echo "root:!test:::::::" >"$ROOT/etc/shadow"
105 systemd-firstboot
--root="$ROOT" --root-password=foo
106 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
107 grep -q "^root:!test:" "$ROOT/etc/shadow"
108 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
109 echo "root:x:0:0:root:/root:/bin/sh" >"$ROOT/etc/passwd"
110 echo "root:!unprovisioned:::::::" >"$ROOT/etc/shadow"
111 systemd-firstboot
--root="$ROOT" --root-password=foo
112 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
113 grep -q "^root:[^!*]" "$ROOT/etc/shadow"
114 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
115 systemd-firstboot
--root="$ROOT" --root-password-hashed="$ROOT_HASHED_PASSWORD1"
116 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
117 grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
118 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
119 systemd-firstboot
--root="$ROOT" --root-shell=/bin
/fooshell
120 grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
121 grep -q "^root:!\*:" "$ROOT/etc/shadow"
122 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
123 systemd-firstboot
--root="$ROOT" --root-password-hashed="$ROOT_HASHED_PASSWORD1" --root-shell=/bin
/fooshell
124 grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
125 grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
127 systemd-firstboot
--root="$ROOT" --kernel-command-line="foo.bar=42"
128 grep -q "foo.bar=42" "$ROOT/etc/kernel/cmdline"
130 # Configs should not get overwritten if they exist unless --force is used
131 systemd-firstboot
--root="$ROOT" \
132 --locale=locale-overwrite \
133 --locale-messages=messages-overwrite \
134 --keymap=keymap-overwrite \
135 --timezone=Europe
/Berlin \
136 --hostname=hostname-overwrite \
137 --machine-id=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \
138 --root-password-hashed="$ROOT_HASHED_PASSWORD2" \
139 --root-shell=/bin
/barshell \
140 --kernel-command-line="hello.world=0"
141 grep -q "LANG=foo" "$ROOT$LOCALE_PATH"
142 grep -q "LC_MESSAGES=bar" "$ROOT$LOCALE_PATH"
143 grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
144 readlink
"$ROOT/etc/localtime" |
grep -q "Europe/Berlin$"
145 grep -q "foobar" "$ROOT/etc/hostname"
146 grep -q "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "$ROOT/etc/machine-id"
147 grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
148 grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
149 grep -q "foo.bar=42" "$ROOT/etc/kernel/cmdline"
151 # The same thing, but now with --force
152 systemd-firstboot
--root="$ROOT" --force \
153 --locale=locale-overwrite \
154 --locale-messages=messages-overwrite \
155 --keymap=keymap-overwrite \
156 --timezone=Europe
/Berlin \
157 --hostname=hostname-overwrite \
158 --machine-id=bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb \
159 --root-password-hashed="$ROOT_HASHED_PASSWORD2" \
160 --root-shell=/bin
/barshell \
161 --kernel-command-line="hello.world=0"
162 grep -q "LANG=locale-overwrite" "$ROOT$LOCALE_PATH"
163 grep -q "LC_MESSAGES=messages-overwrite" "$ROOT$LOCALE_PATH"
164 grep -q "KEYMAP=keymap-overwrite" "$ROOT/etc/vconsole.conf"
165 readlink
"$ROOT/etc/localtime" |
grep -q "/Europe/Berlin$"
166 grep -q "hostname-overwrite" "$ROOT/etc/hostname"
167 grep -q "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" "$ROOT/etc/machine-id"
168 grep -q "^root:x:0:0:.*:/bin/barshell$" "$ROOT/etc/passwd"
169 grep -q "^root:$ROOT_HASHED_PASSWORD2:" "$ROOT/etc/shadow"
170 grep -q "hello.world=0" "$ROOT/etc/kernel/cmdline"
172 # Test that --reset removes all files configured by firstboot.
173 systemd-firstboot
--root="$ROOT" --reset
174 [[ ! -e "$ROOT/etc/locale.conf" ]]
175 [[ ! -e "$ROOT/etc/vconsole.conf" ]]
176 [[ ! -e "$ROOT/etc/localtime" ]]
177 [[ ! -e "$ROOT/etc/hostname" ]]
178 [[ ! -e "$ROOT/etc/machine-id" ]]
179 [[ ! -e "$ROOT/etc/kernel/cmdline" ]]
184 # Copy everything at once (--copy)
185 systemd-firstboot
--root="$ROOT" --copy
186 diff $LOCALE_PATH "$ROOT$LOCALE_PATH"
187 diff <(awk -F: '/^root/ { print $7; }' /etc
/passwd
) <(awk -F: '/^root/ { print $7; }' "$ROOT/etc/passwd")
188 diff <(awk -F: '/^root/ { print $2; }' /etc
/shadow
) <(awk -F: '/^root/ { print $2; }' "$ROOT/etc/shadow")
189 [[ -e /etc
/vconsole.conf
]] && diff /etc
/vconsole.conf
"$ROOT/etc/vconsole.conf"
190 [[ -e /etc
/localtime
]] && diff <(readlink
/etc
/localtime
) <(readlink
"$ROOT/etc/localtime")
193 # Copy everything at once, but now by using separate switches
194 systemd-firstboot
--root="$ROOT" --copy-locale --copy-keymap --copy-timezone --copy-root-password --copy-root-shell
195 diff $LOCALE_PATH "$ROOT$LOCALE_PATH"
196 diff <(awk -F: '/^root/ { print $7; }' /etc
/passwd
) <(awk -F: '/^root/ { print $7; }' "$ROOT/etc/passwd")
197 diff <(awk -F: '/^root/ { print $2; }' /etc
/shadow
) <(awk -F: '/^root/ { print $2; }' "$ROOT/etc/shadow")
198 [[ -e /etc
/vconsole.conf
]] && diff /etc
/vconsole.conf
"$ROOT/etc/vconsole.conf"
199 [[ -e /etc
/localtime
]] && diff <(readlink
/etc
/localtime
) <(readlink
"$ROOT/etc/localtime")
204 touch "$ROOT/bin/fooshell" "$ROOT/bin/barshell"
205 # Temporarily disable pipefail to avoid `echo: write error: Broken pipe
207 # We can do only limited testing here, since it's all an interactive stuff, so
208 # --prompt is skipped on purpose and only limited --prompt-root-password
209 # testing can be done.
210 echo -ne "\nfoo\nbar\n" | systemd-firstboot
--root="$ROOT" --prompt-locale
211 grep -q "LANG=foo" "$ROOT$LOCALE_PATH"
212 grep -q "LC_MESSAGES=bar" "$ROOT$LOCALE_PATH"
213 # systemd-firstboot in prompt-keymap mode requires keymaps to be installed so
214 # it can present them as a list to the user. As Debian does not ship/provide
215 # compatible keymaps (from the kbd package), skip this test if the keymaps are
217 if [ -d "/usr/share/keymaps/" ] ||
[ -d "/usr/share/kbd/keymaps/" ] ||
[ -d "/usr/lib/kbd/keymaps/" ] ; then
218 echo -ne "\nfoo\n" | systemd-firstboot
--root="$ROOT" --prompt-keymap
219 grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
221 echo -ne "\nEurope/Berlin\n" | systemd-firstboot
--root="$ROOT" --prompt-timezone
222 readlink
"$ROOT/etc/localtime" |
grep -q "Europe/Berlin$"
223 echo -ne "\nfoobar\n" | systemd-firstboot
--root="$ROOT" --prompt-hostname
224 grep -q "foobar" "$ROOT/etc/hostname"
225 # With no root password provided, a locked account should be created.
226 systemd-firstboot
--root="$ROOT" --prompt-root-password </dev
/null
227 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
228 grep -q "^root:!\*:" "$ROOT/etc/shadow"
229 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
230 echo -ne "\n/bin/fooshell\n" | systemd-firstboot
--root="$ROOT" --prompt-root-shell
231 grep -q "^root:.*:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
232 # Existing files should not get overwritten
233 echo -ne "\n/bin/barshell\n" | systemd-firstboot
--root="$ROOT" --prompt-root-shell
234 grep -q "^root:.*:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
235 # Now without the welcome screen but with force
236 echo -ne "/bin/barshell\n" | systemd-firstboot
--root="$ROOT" --force --prompt-root-shell --welcome=no
237 grep -q "^root:.*:0:0:.*:/bin/barshell$" "$ROOT/etc/passwd"
241 # --prompt-* options with credentials. Unfortunately, with --root the
242 # --systemd.firstboot kernel command line option is ignored, so that can't be
246 touch "$ROOT/bin/fooshell" "$ROOT/bin/barshell"
247 systemd-run
--wait --pipe --service-type=exec \
248 -p SetCredential
=firstboot.locale
:foo \
249 -p SetCredential
=firstboot.locale-messages
:bar \
250 -p SetCredential
=firstboot.keymap
:foo \
251 -p SetCredential
=firstboot.timezone
:Europe
/Berlin \
252 -p SetCredential
=passwd.hashed-password.root
:"$ROOT_HASHED_PASSWORD1" \
253 -p SetCredential
=passwd.shell.root
:/bin
/fooshell \
259 --prompt-root-password \
260 --prompt-root-shell \
262 grep -q "LANG=foo" "$ROOT$LOCALE_PATH"
263 grep -q "LC_MESSAGES=bar" "$ROOT$LOCALE_PATH"
264 grep -q "KEYMAP=foo" "$ROOT/etc/vconsole.conf"
265 readlink
"$ROOT/etc/localtime" |
grep -q "Europe/Berlin$"
266 grep -q "^root:x:0:0:.*:/bin/fooshell$" "$ROOT/etc/passwd"
267 grep -q "^root:$ROOT_HASHED_PASSWORD1:" "$ROOT/etc/shadow"
273 systemd-firstboot
--root="$ROOT" --setup-machine-id
274 grep -E "[a-z0-9]{32}" "$ROOT/etc/machine-id"
275 rm -fv "$ROOT/etc/machine-id"
277 systemd-firstboot
--root="$ROOT" --delete-root-password
278 grep -q "^root:x:0:0:" "$ROOT/etc/passwd"
279 grep -q "^root::" "$ROOT/etc/shadow"
280 rm -fv "$ROOT/etc/passwd" "$ROOT/etc/shadow"
282 (! systemd-firstboot
--root="$ROOT" --root-shell=/bin
/nonexistentshell
)
283 (! systemd-firstboot
--root="$ROOT" --machine-id=invalidmachineid
)
284 (! systemd-firstboot
--root="$ROOT" --timezone=Foo
/Bar
)