chiark / gitweb /
TEST-01-BASIC, TEST-02-CRYPTSETUP: fixed strip
[elogind.git] / test / TEST-02-CRYPTSETUP / 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="cryptsetup systemd setup"
5
6 . $TEST_BASE_DIR/test-functions
7
8 # Uncomment this to debug failures
9 #DEBUGFAIL="systemd.unit=multi-user.target"
10 DEBUGTOOLS="df free ls stty cat ps ln ip route dmesg dhclient mkdir cp ping dhclient strace less grep id tty touch du sort"
11
12 run_qemu() {
13     # TODO: qemu wrapper script: http://www.spinics.net/lists/kvm/msg72389.html
14     qemu-kvm \
15         -hda $TESTDIR/rootdisk.img \
16         -m 512M -nographic \
17         -net none -kernel /boot/vmlinuz-$KERNEL_VER \
18         -append "root=/dev/sda1 systemd.log_level=debug raid=noautodetect loglevel=2 init=/usr/lib/systemd/systemd ro console=ttyS0,115200n81 selinux=0 $DEBUGFAIL" || return 1
19
20     ret=1
21     mkdir -p $TESTDIR/root
22     mount ${LOOPDEV}p1 $TESTDIR/root
23     [[ -e $TESTDIR/root/testok ]] && ret=0
24     cp -a $TESTDIR/root/failed $TESTDIR
25     cryptsetup luksOpen ${LOOPDEV}p2 varcrypt <$TESTDIR/keyfile
26     mount /dev/mapper/varcrypt $TESTDIR/root/var
27     cp -a $TESTDIR/root/var/log/journal $TESTDIR
28     umount $TESTDIR/root/var
29     umount $TESTDIR/root
30     cryptsetup luksClose /dev/mapper/varcrypt
31     cat $TESTDIR/failed
32     ls -l $TESTDIR/journal/*/*.journal
33     test -s $TESTDIR/failed && ret=$(($ret+1))
34     return $ret
35 }
36
37
38 test_run() {
39     if check_qemu ; then
40         run_qemu || return 1
41     else
42         dwarn "can't run qemu-kvm, skipping"
43     fi
44     return 0
45 }
46
47 test_setup() {
48     rm -f $TESTDIR/rootdisk.img
49     # Create the blank file to use as a root filesystem
50     dd if=/dev/null of=$TESTDIR/rootdisk.img bs=1M seek=200
51     LOOPDEV=$(losetup --show -P -f $TESTDIR/rootdisk.img)
52     [ -b $LOOPDEV ] || return 1
53     echo "LOOPDEV=$LOOPDEV" >> $STATEFILE
54     sfdisk -C 6400 -H 2 -S 32 -L $LOOPDEV <<EOF
55 ,3200
56 ,
57 EOF
58
59     mkfs.ext3 -L systemd ${LOOPDEV}p1
60     echo -n test >$TESTDIR/keyfile
61     cryptsetup -q luksFormat ${LOOPDEV}p2 $TESTDIR/keyfile
62     cryptsetup luksOpen ${LOOPDEV}p2 varcrypt <$TESTDIR/keyfile
63     mkfs.ext3 -L var /dev/mapper/varcrypt
64     mkdir -p $TESTDIR/root
65     mount ${LOOPDEV}p1 $TESTDIR/root
66     mkdir -p $TESTDIR/root/run
67     mkdir -p $TESTDIR/root/var
68     mount /dev/mapper/varcrypt $TESTDIR/root/var
69
70     # Create what will eventually be our root filesystem onto an overlay
71     (
72         LOG_LEVEL=5
73         initdir=$TESTDIR/root
74
75         # create the basic filesystem layout
76         setup_basic_dirs
77
78         # install compiled files
79         (cd ../..; make DESTDIR=$initdir install)
80
81         # remove unneeded documentation
82         rm -fr $initdir/usr/share/{man,doc,gtk-doc}
83
84         # install possible missing libraries
85         for i in $initdir/{sbin,bin}/* $initdir/lib/systemd/*; do
86             inst_libs $i
87         done
88
89         # make a journal directory
90         mkdir -p $initdir/var/log/journal
91
92         # install some basic config files
93         inst /etc/sysconfig/init
94         inst /etc/passwd
95         inst /etc/shadow
96         inst /etc/group
97         inst /etc/shells
98         inst /etc/nsswitch.conf
99         inst /etc/pam.conf
100         inst /etc/securetty
101         inst /etc/os-release
102         inst /etc/localtime
103         # we want an empty environment
104         > $initdir/etc/environment
105         > $initdir/etc/machine-id
106
107         # set the hostname
108         echo  systemd-testsuite > $initdir/etc/hostname
109
110         eval $(udevadm info --export --query=env --name=/dev/mapper/varcrypt)
111         eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
112
113         cat >$initdir/etc/crypttab <<EOF
114 $DM_NAME UUID=$ID_FS_UUID /etc/varkey
115 EOF
116         echo -n test > $initdir/etc/varkey
117         cat $initdir/etc/crypttab | ddebug
118
119         cat >$initdir/etc/fstab <<EOF
120 LABEL=systemd           /       ext3    rw 0 1
121 /dev/mapper/varcrypt    /var    ext3    defaults 0 1
122 EOF
123
124         # setup the testsuite target and the test ending service
125         cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
126
127         # setup the testsuite service
128         cat >$initdir/etc/systemd/system/testsuite.service <<EOF
129 [Unit]
130 Description=Testsuite service
131 After=multi-user.target
132
133 [Service]
134 ExecStart=/bin/bash -c 'set -x; ( systemctl --failed --no-legend --no-pager; systemctl status --failed ) > /failed ; echo OK > /testok; while : ;do systemd-cat echo "testsuite service waiting for /var/log/journal" ; echo "testsuite service waiting for journal to move to /var/log/journal" > /dev/console ; for i in /var/log/journal/*;do [ -d "\$i" ] && echo "\$i" && break 2; done; sleep 1; done; sleep 1; exit 0;'
135 Type=oneshot
136 EOF
137
138         mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
139         ln -fs ../testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
140         ln -fs ../end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
141
142         # make the testsuite the default target
143         ln -fs testsuite.target $initdir/etc/systemd/system/default.target
144         mkdir -p $initdir/etc/rc.d
145         cat >$initdir/etc/rc.d/rc.local <<EOF
146 #!/bin/bash
147 exit 0
148 EOF
149         chmod 0755 $initdir/etc/rc.d/rc.local
150         # install basic tools needed
151         dracut_install sh bash setsid loadkeys setfont \
152             login sushell sulogin gzip sleep echo mount umount cryptsetup
153         dracut_install dmsetup modprobe
154
155         instmods dm_crypt =crypto
156
157         type -P dmeventd >/dev/null && dracut_install dmeventd
158
159         inst_libdir_file "libdevmapper-event.so*"
160
161         inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
162
163         # install libnss_files for login
164         inst_libdir_file "libnss_files*"
165
166         # install dbus and pam
167         find \
168             /etc/dbus-1 \
169             /etc/pam.d \
170             /etc/security \
171             /lib64/security \
172             /lib/security -xtype f \
173             | while read file; do
174             inst $file
175         done
176
177         # install dbus socket and service file
178         inst /usr/lib/systemd/system/dbus.socket
179         inst /usr/lib/systemd/system/dbus.service
180
181         # install basic keyboard maps and fonts
182         for i in \
183             /usr/lib/kbd/consolefonts/latarcyrheb-sun16* \
184             /usr/lib/kbd/keymaps/include/* \
185             /usr/lib/kbd/keymaps/i386/include/* \
186             /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
187                 [[ -f $i ]] || continue
188                 inst $i
189         done
190
191         # some basic terminfo files
192         for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
193             [ -f ${_terminfodir}/l/linux ] && break
194         done
195         dracut_install -o ${_terminfodir}/l/linux
196
197         # softlink mtab
198         ln -fs /proc/self/mounts $initdir/etc/mtab
199
200         # install any Execs from the service files
201         egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
202             | while read i; do
203             i=${i##Exec*=}; i=${i##-}
204             inst $i
205         done
206
207         # install plymouth, if found... else remove plymouth service files
208         # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
209         #     PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
210         #         /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
211         #         dracut_install plymouth plymouthd
212         # else
213         rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
214         # fi
215
216         # some helper tools for debugging
217         [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
218
219         # install ld.so.conf* and run ldconfig
220         cp -a /etc/ld.so.conf* $initdir/etc
221         ldconfig -r "$initdir"
222         ddebug "Strip binaeries"
223         find "$initdir" -executable -not -path '*/lib/modules/*.ko' -type f | xargs strip --strip-unneeded | ddebug
224
225         # copy depmod files
226         inst /lib/modules/$KERNEL_VER/modules.order
227         inst /lib/modules/$KERNEL_VER/modules.builtin
228         # generate module dependencies
229         if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
230             ! depmod -a -b "$initdir" $KERNEL_VER; then
231                 dfatal "\"depmod -a $KERNEL_VER\" failed."
232                 exit 1
233         fi
234     )
235     rm -fr $TESTDIR/nspawn-root
236     ddebug "cp -ar $TESTDIR/root $TESTDIR/nspawn-root"
237     cp -ar $TESTDIR/root $TESTDIR/nspawn-root
238     # we don't mount in the nspawn root
239     rm -fr $TESTDIR/nspawn-root/etc/fstab
240
241     ddebug "umount $TESTDIR/root/var"
242     umount $TESTDIR/root/var
243     cryptsetup luksClose /dev/mapper/varcrypt
244     ddebug "umount $TESTDIR/root"
245     umount $TESTDIR/root
246 }
247
248 test_cleanup() {
249     umount $TESTDIR/root/var 2>/dev/null
250     [[ -b /dev/mapper/varcrypt ]] && cryptsetup luksClose /dev/mapper/varcrypt
251     umount $TESTDIR/root 2>/dev/null
252     [[ $LOOPDEV ]] && losetup -d $LOOPDEV
253     return 0
254 }
255
256 do_test "$@"