]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-14-MACHINE-ID/test.sh
test: check that we can boot with broken machine-id
[thirdparty/systemd.git] / test / TEST-14-MACHINE-ID / test.sh
1 #!/bin/bash
2 # -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
3 # ex: ts=8 sw=4 sts=4 et filetype=sh
4 TEST_DESCRIPTION="Basic systemd setup"
5 SKIP_INITRD=yes
6 . $TEST_BASE_DIR/test-functions
7
8 check_result_qemu() {
9 ret=1
10 mkdir -p $TESTDIR/root
11 mount ${LOOPDEV}p1 $TESTDIR/root
12 [[ -e $TESTDIR/root/testok ]] && ret=0
13 [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
14 cp -a $TESTDIR/root/var/log/journal $TESTDIR
15 umount $TESTDIR/root
16 [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
17 ls -l $TESTDIR/journal/*/*.journal
18 test -s $TESTDIR/failed && ret=$(($ret+1))
19 return $ret
20 }
21
22 test_run() {
23 if run_qemu; then
24 check_result_qemu || return 1
25 else
26 dwarn "can't run QEMU, skipping"
27 fi
28 return 0
29 }
30
31 test_setup() {
32 create_empty_image
33 mkdir -p $TESTDIR/root
34 mount ${LOOPDEV}p1 $TESTDIR/root
35
36 # Create what will eventually be our root filesystem onto an overlay
37 (
38 LOG_LEVEL=5
39 eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
40
41 setup_basic_environment
42 printf "556f48e837bc4424a710fa2e2c9d3e3c\ne3d\n" >$initdir/etc/machine-id
43 dracut_install mount cmp
44
45 # setup the testsuite service
46 cat >$initdir/etc/systemd/system/testsuite.service <<EOF
47 [Unit]
48 Description=Testsuite service
49 After=multi-user.target
50
51 [Service]
52 ExecStart=/bin/sh -e -x -c '/test-machine-id-setup.sh; systemctl --state=failed --no-legend --no-pager > /failed ; echo OK > /testok'
53 Type=oneshot
54 EOF
55
56 cat >$initdir/test-machine-id-setup.sh <<'EOF'
57 #!/bin/bash
58
59 set -e
60 set -x
61
62 function setup_root {
63 local _root="$1"
64 mkdir -p "$_root"
65 mount -t tmpfs tmpfs "$_root"
66 mkdir -p "$_root/etc" "$_root/run"
67 }
68
69 function check {
70 printf "Expected\n"
71 cat "$1"
72 printf "\nGot\n"
73 cat "$2"
74 cmp "$1" "$2"
75 }
76
77 r="$(pwd)/overwrite-broken-machine-id"
78 setup_root "$r"
79 systemd-machine-id-setup --print --root "$r"
80 echo abc >>"$r/etc/machine-id"
81 id=$(systemd-machine-id-setup --print --root "$r")
82 echo $id >expected
83 check expected "$r/etc/machine-id"
84
85 r="$(pwd)/transient-machine-id"
86 setup_root "$r"
87 systemd-machine-id-setup --print --root "$r"
88 echo abc >>"$r/etc/machine-id"
89 mount -o remount,ro "$r"
90 mount -t tmpfs tmpfs "$r/run"
91 transient_id=$(systemd-machine-id-setup --print --root "$r")
92 mount -o remount,rw "$r"
93 commited_id=$(systemd-machine-id-setup --print --commit --root "$r")
94 [[ "$transient_id" = "$commited_id" ]]
95 check "$r/etc/machine-id" "$r/run/machine-id"
96 EOF
97 chmod +x $initdir/test-machine-id-setup.sh
98
99 setup_testsuite
100 ) || return 1
101
102 # mask some services that we do not want to run in these tests
103 ln -s /dev/null $initdir/etc/systemd/system/systemd-hwdb-update.service
104 ln -s /dev/null $initdir/etc/systemd/system/systemd-journal-catalog-update.service
105 ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.service
106 ln -s /dev/null $initdir/etc/systemd/system/systemd-networkd.socket
107 ln -s /dev/null $initdir/etc/systemd/system/systemd-resolved.service
108
109 ddebug "umount $TESTDIR/root"
110 umount $TESTDIR/root
111 }
112
113 test_cleanup() {
114 umount $TESTDIR/root 2>/dev/null
115 [[ $LOOPDEV ]] && losetup -d $LOOPDEV
116 return 0
117 }
118
119 do_test "$@"