chiark / gitweb /
bootchart: rename log.c to store.c to aovid confusion with src/shared/log.c
[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 and the test ending service
126         cp $TEST_BASE_DIR/{testsuite.target,end.service} $initdir/etc/systemd/system/
127
128         # setup the testsuite service
129         cat >$initdir/etc/systemd/system/testsuite.service <<EOF
130 [Unit]
131 Description=Testsuite service
132 After=multi-user.target
133
134 [Service]
135 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;'
136 Type=oneshot
137 EOF
138
139         mkdir -p $initdir/etc/systemd/system/testsuite.target.wants
140         ln -fs ../testsuite.service $initdir/etc/systemd/system/testsuite.target.wants/testsuite.service
141         ln -fs ../end.service $initdir/etc/systemd/system/testsuite.target.wants/end.service
142
143         # make the testsuite the default target
144         ln -fs testsuite.target $initdir/etc/systemd/system/default.target
145         mkdir -p $initdir/etc/rc.d
146         cat >$initdir/etc/rc.d/rc.local <<EOF
147 #!/bin/bash
148 exit 0
149 EOF
150         chmod 0755 $initdir/etc/rc.d/rc.local
151         # install basic tools needed
152         dracut_install sh bash setsid loadkeys setfont \
153             login sushell sulogin gzip sleep echo mount umount cryptsetup
154         dracut_install dmsetup modprobe
155
156         instmods dm_crypt =crypto
157
158         type -P dmeventd >/dev/null && dracut_install dmeventd
159
160         inst_libdir_file "libdevmapper-event.so*"
161
162         inst_rules 10-dm.rules 13-dm-disk.rules 95-dm-notify.rules
163
164         # install libnss_files for login
165         inst_libdir_file "libnss_files*"
166
167         # install dbus and pam
168         find \
169             /etc/dbus-1 \
170             /etc/pam.d \
171             /etc/security \
172             /lib64/security \
173             /lib/security -xtype f \
174             | while read file; do
175             inst $file
176         done
177
178         # install dbus socket and service file
179         inst /usr/lib/systemd/system/dbus.socket
180         inst /usr/lib/systemd/system/dbus.service
181
182         # install basic keyboard maps and fonts
183         for i in \
184             /usr/lib/kbd/consolefonts/latarcyrheb-sun16* \
185             /usr/lib/kbd/keymaps/include/* \
186             /usr/lib/kbd/keymaps/i386/include/* \
187             /usr/lib/kbd/keymaps/i386/qwerty/us.*; do
188                 [[ -f $i ]] || continue
189                 inst $i
190         done
191
192         # some basic terminfo files
193         for _terminfodir in /lib/terminfo /etc/terminfo /usr/share/terminfo; do
194             [ -f ${_terminfodir}/l/linux ] && break
195         done
196         dracut_install -o ${_terminfodir}/l/linux
197
198         # softlink mtab
199         ln -fs /proc/self/mounts $initdir/etc/mtab
200
201         # install any Exec's from the service files
202         egrep -ho '^Exec[^ ]*=[^ ]+' $initdir/lib/systemd/system/*.service \
203             | while read i; do
204             i=${i##Exec*=}; i=${i##-}
205             inst $i
206         done
207
208         # install plymouth, if found... else remove plymouth service files
209         # if [ -x /usr/libexec/plymouth/plymouth-populate-initrd ]; then
210         #     PLYMOUTH_POPULATE_SOURCE_FUNCTIONS="$TEST_BASE_DIR/test-functions" \
211         #         /usr/libexec/plymouth/plymouth-populate-initrd -t $initdir
212         #         dracut_install plymouth plymouthd
213         # else
214         rm -f $initdir/{usr/lib,etc}/systemd/system/plymouth* $initdir/{usr/lib,etc}/systemd/system/*/plymouth*
215         # fi
216
217         # some helper tools for debugging
218         [[ $DEBUGTOOLS ]] && dracut_install $DEBUGTOOLS
219
220         # install ld.so.conf* and run ldconfig
221         cp -a /etc/ld.so.conf* $initdir/etc
222         ldconfig -r "$initdir"
223         ddebug "Strip binaeries"
224         find "$initdir" -perm +111 -type f | xargs strip --strip-unneeded | ddebug
225
226         # copy depmod files
227         inst /lib/modules/$KERNEL_VER/modules.order
228         inst /lib/modules/$KERNEL_VER/modules.builtin
229         # generate module dependencies
230         if [[ -d $initdir/lib/modules/$KERNEL_VER ]] && \
231             ! depmod -a -b "$initdir" $KERNEL_VER; then
232                 dfatal "\"depmod -a $KERNEL_VER\" failed."
233                 exit 1
234         fi
235     )
236     rm -fr $TESTDIR/nspawn-root
237     ddebug "cp -ar $TESTDIR/root $TESTDIR/nspawn-root"
238     cp -ar $TESTDIR/root $TESTDIR/nspawn-root
239     # we don't mount in the nspawn root
240     rm -fr $TESTDIR/nspawn-root/etc/fstab
241
242     ddebug "umount $TESTDIR/root/var"
243     umount $TESTDIR/root/var
244     cryptsetup luksClose /dev/mapper/varcrypt
245     ddebug "umount $TESTDIR/root"
246     umount $TESTDIR/root
247 }
248
249 test_cleanup() {
250     umount $TESTDIR/root/var 2>/dev/null
251     [[ -b /dev/mapper/varcrypt ]] && cryptsetup luksClose /dev/mapper/varcrypt
252     umount $TESTDIR/root 2>/dev/null
253     [[ $LOOPDEV ]] && losetup -d $LOOPDEV
254     return 0
255 }
256
257 . $TEST_BASE_DIR/test-functions
258 do_test "$@"