71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering#!/bin/bash
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering# -*- mode: shell-script; indent-tabs-mode: nil; sh-basic-offset: 4; -*-
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering# ex: ts=8 sw=4 sts=4 et filetype=sh
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringTEST_DESCRIPTION="cryptsetup systemd setup"
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
fff87a35d9e26c0d4ea41273a963c0eb20e18da4Zbigniew Jędrzejewski-Szmek. $TEST_BASE_DIR/test-functions
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevaliercheck_result_qemu() {
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering ret=1
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mkdir -p $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mount ${LOOPDEV}p1 $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering [[ -e $TESTDIR/root/testok ]] && ret=0
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier [[ -f $TESTDIR/root/failed ]] && cp -a $TESTDIR/root/failed $TESTDIR
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cryptsetup luksOpen ${LOOPDEV}p2 varcrypt <$TESTDIR/keyfile
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mount /dev/mapper/varcrypt $TESTDIR/root/var
278d5115470919319c514ea37d7b14e3f7d0580bEvgeny Vereshchagin cp -a $TESTDIR/root/var/log/journal $TESTDIR
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root/var
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cryptsetup luksClose /dev/mapper/varcrypt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier [[ -f $TESTDIR/failed ]] && cat $TESTDIR/failed
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering ls -l $TESTDIR/journal/*/*.journal
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering test -s $TESTDIR/failed && ret=$(($ret+1))
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering return $ret
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering}
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poetteringtest_run() {
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier if run_qemu; then
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier check_result_qemu || return 1
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering else
c6a77179a4097df355f0f04b8f3260c76b5e515cRonny Chevalier dwarn "can't run QEMU, skipping"
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering fi
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering return 0
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering}
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poetteringtest_setup() {
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier create_empty_image
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering echo -n test >$TESTDIR/keyfile
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cryptsetup -q luksFormat ${LOOPDEV}p2 $TESTDIR/keyfile
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cryptsetup luksOpen ${LOOPDEV}p2 varcrypt <$TESTDIR/keyfile
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mkfs.ext3 -L var /dev/mapper/varcrypt
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mkdir -p $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mount ${LOOPDEV}p1 $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mkdir -p $TESTDIR/root/var
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering mount /dev/mapper/varcrypt $TESTDIR/root/var
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering # Create what will eventually be our root filesystem onto an overlay
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering (
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering LOG_LEVEL=5
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering eval $(udevadm info --export --query=env --name=/dev/mapper/varcrypt)
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering eval $(udevadm info --export --query=env --name=${LOOPDEV}p2)
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier setup_basic_environment
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering # setup the testsuite service
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cat >$initdir/etc/systemd/system/testsuite.service <<EOF
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering[Unit]
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringDescription=Testsuite service
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringAfter=multi-user.target
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering[Service]
5c404f1ab8e96efedb983806443ca982a1b2a372Evgeny VereshchaginExecStart=/bin/sh -x -c 'systemctl --failed --no-legend --no-pager > /failed ; echo OK > /testok'
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringType=oneshot
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringEOF
c90feab4ff8bc23d88d4f9c67d9652ba189cb51bMichal Schmidt
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier setup_testsuite
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier install_dmevent
1a6dc6534e329f21aa2dcde5b8af4138bb4c11f1Ronny Chevalier generate_module_dependencies
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier cat >$initdir/etc/crypttab <<EOF
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier$DM_NAME UUID=$ID_FS_UUID /etc/varkey
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart PoetteringEOF
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier echo -n test > $initdir/etc/varkey
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier cat $initdir/etc/crypttab | ddebug
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier cat >>$initdir/etc/fstab <<EOF
889a90422dd47284dffa32b9234a6e58991b000cRonny Chevalier/dev/mapper/varcrypt /var ext3 defaults 0 1
889a90422dd47284dffa32b9234a6e58991b000cRonny ChevalierEOF
cd949ee7348982d90fb8bdf4a37128928b82925fEvgeny Vereshchagin ) || return 1
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering ddebug "umount $TESTDIR/root/var"
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root/var
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering cryptsetup luksClose /dev/mapper/varcrypt
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering ddebug "umount $TESTDIR/root"
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering}
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poetteringtest_cleanup() {
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root/var 2>/dev/null
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering [[ -b /dev/mapper/varcrypt ]] && cryptsetup luksClose /dev/mapper/varcrypt
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering umount $TESTDIR/root 2>/dev/null
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering [[ $LOOPDEV ]] && losetup -d $LOOPDEV
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering return 0
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering}
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poettering
71dc3ed1d95dfe246004018c2ca85156fc73c146Lennart Poetteringdo_test "$@"