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