diff --git a/test/README.md b/test/README.md new file mode 100644 index 0000000..7be50b4 --- /dev/null +++ b/test/README.md @@ -0,0 +1 @@ +TODO docs diff --git a/test/bare.test b/test/bare.test new file mode 100755 index 0000000..d680caa --- /dev/null +++ b/test/bare.test @@ -0,0 +1,111 @@ +#!/bin/sh + +. ../lib/common.sh + +cleanup() +{ + umount "${tmpdir}/root" || : + qemu-nbd -d /dev/nbd0 || : + rm -rf "$tmpdir" +} + +set -ef +trap cleanup EXIT INT + +devmgr=${DEVMGR:-proc} +arch=${ARCH:-$(uname -m)} +kernel=${KERNEL:-$(uname -r)} +vmlinuz=${VMLINUZ:-"/boot/vmlinuz-${kernel}"} + +mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/${0##*/}.$$}" + +root="${tmpdir}/root" +config="${tmpdir}/config" +image="${tmpdir}/root.qcow2" +initrd="${tmpdir}/initramfs-$(uname -r)" + +qemu-img create -f qcow2 "$image" 1G +qemu-nbd -c /dev/nbd0 "$image" + +# o: Create MBR table. +# n: Add new partition to table. +# p: Primary partition. +# 1: Partition number. +# newline: Use default value for first sector. +# newline: Use default value for last sector. +# w: Write changes and re-read partition table. +fdisk /dev/nbd0 << EOF +o +n +p +1 + + +w +EOF + +cat > "$config" << EOF +hooks=$devmgr +root=LABEL=root +EOF + +mkdir -p "$root" + +mkfs.ext4 -L root /dev/nbd0p1 +mount /dev/nbd0p1 "$root" + +( + tmpdir=$root; cd "$tmpdir" + + mkdir -p \ + dev sys tmp run proc \ + root usr/lib usr/bin + + ln -s usr/lib lib + ln -s usr/bin bin + ln -s usr/bin sbin + ln -s bin usr/sbin + + copy_exec sh + copy_exec e2label + + cat > sbin/init << EOF +#!/bin/sh +exec e2label /dev/disk/by-label/root success +EOF + + chmod +x sbin/init +) + +umount "$root" +qemu-nbd -d /dev/nbd0 + +(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") + +set -- \ + -no-reboot \ + -initrd "$initrd" \ + -kernel "$vmlinuz" \ + -device virtio-scsi \ + -drive file="$image",if=virtio + +if [ -c /dev/kvm ]; then + set -- -enable-kvm -cpu host "$@" +fi + +if [ "$DEBUG" ]; then + set -- -append 'panic=-1 rdpanic debug rddebug console=ttyS0' -nographic "$@" +else + set -- -append 'panic=-1 rdpanic' -display none "$@" +fi + +"qemu-system-${arch}" "$@" + +qemu-nbd -c /dev/nbd0 "$image" + +# Re-read partition table. +fdisk /dev/nbd0 << EOF +w +EOF + +[ "$(e2label /dev/nbd0p1)" = success ] diff --git a/test/check.sh b/test/check.sh new file mode 100755 index 0000000..6ebb058 --- /dev/null +++ b/test/check.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +if [ "$(id -u)" != 0 ]; then + printf '%s: must be run as root' "$0" + exit 1 +fi + +if [ "$DEVMGR" ]; then + : +elif command -v mdev; then + DEVMGR=mdev +elif command -v mdevd; then + DEVMGR=mdevd +elif command -v udevd; then + DEVMGR=eudev +elif command -v /lib/systemd/systemd-udevd; then + DEVMGR=systemd-udev +elif [ -e /proc/sys/kernel/hotplug ]; then + DEVMGR=proc +else + printf '%s: device manager not found' "$0" >&2 + exit 1 +fi > /dev/null + +export DEVMGR + +if command -v modprobe > /dev/null; then + modprobe kvm + modprobe nbd + modprobe zfs +fi + +[ "$1" ] || set -- *.test + +for file; do + if [ "$DEBUG" ]; then + "./${file}" 2>&1 + else + "./${file}" > /dev/null 2>&1 + fi + + case $? in + 222) status=SKIP ;; + 0) status=PASS ;; + *) status=FAIL ;; + esac + + printf '%s: %s\n' "$file" "$status" >&2 +done diff --git a/test/luks.test b/test/luks.test new file mode 100755 index 0000000..37b1db7 --- /dev/null +++ b/test/luks.test @@ -0,0 +1,125 @@ +#!/bin/sh + +. ../lib/common.sh + +cleanup() +{ + umount "${tmpdir}/root" || : + cryptsetup close "$name" || : + qemu-nbd -d /dev/nbd0 || : + rm -rf "$tmpdir" +} + +command -v cryptsetup > /dev/null || exit 222 + +set -ef +trap cleanup EXIT INT + +devmgr=${DEVMGR:-proc} +arch=${ARCH:-$(uname -m)} +kernel=${KERNEL:-$(uname -r)} +vmlinuz=${VMLINUZ:-"/boot/vmlinuz-${kernel}"} + +mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/${0##*/}.$$}" + +name="luks$$" +root="${tmpdir}/root" +config="${tmpdir}/config" +image="${tmpdir}/root.qcow2" +initrd="${tmpdir}/initramfs-$(uname -r)" + +qemu-img create -f qcow2 "$image" 1G +qemu-nbd -c /dev/nbd0 "$image" + +# o: Create MBR table. +# n: Add new partition to table. +# p: Primary partition. +# 1: Partition number. +# newline: Use default value for first sector. +# newline: Use default value for last sector. +# w: Write changes and re-read partition table. +fdisk /dev/nbd0 << EOF +o +n +p +1 + + +w +EOF + +dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key" + +cryptsetup -qd "${tmpdir}/key" --pbkdf=pbkdf2 luksFormat /dev/nbd0p1 +cryptsetup -d "${tmpdir}/key" open /dev/nbd0p1 "$name" + +cat > "$config" << EOF +hooks=$devmgr,luks +root=LABEL=root +luks_root=UUID=$(cryptsetup luksUUID /dev/nbd0p1) +luks_key=${tmpdir}/key +EOF + +mkdir -p "$root" + +mkfs.ext4 -L root "/dev/mapper/${name}" +mount "/dev/mapper/${name}" "$root" + +( + tmpdir=$root; cd "$tmpdir" + + mkdir -p \ + dev sys tmp run proc \ + root usr/lib usr/bin + + ln -s usr/lib lib + ln -s usr/bin bin + ln -s usr/bin sbin + ln -s bin usr/sbin + + copy_exec sh + copy_exec e2label + + cat > sbin/init << EOF +#!/bin/sh +exec e2label /dev/disk/by-label/root success +EOF + + chmod +x sbin/init +) + +umount "$root" +cryptsetup close "$name" +qemu-nbd -d /dev/nbd0 + +(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") + +set -- \ + -no-reboot \ + -initrd "$initrd" \ + -kernel "$vmlinuz" \ + -device virtio-scsi \ + -drive file="$image",if=virtio + +if [ -c /dev/kvm ]; then + set -- -enable-kvm -cpu host "$@" +fi + +if [ "$DEBUG" ]; then + set -- -append 'panic=-1 rdpanic debug rddebug console=ttyS0' -nographic "$@" +else + set -- -append 'panic=-1 rdpanic' -display none "$@" +fi + +"qemu-system-${arch}" "$@" + +qemu-nbd -c /dev/nbd0 "$image" + +# Re-read partition table. +fdisk /dev/nbd0 << EOF +w +EOF + +cryptsetup -d "${tmpdir}/key" open /dev/nbd0p1 "$name" + +[ "$(e2label "/dev/mapper/${name}")" = success ] diff --git a/test/lvm.test b/test/lvm.test new file mode 100755 index 0000000..a02563c --- /dev/null +++ b/test/lvm.test @@ -0,0 +1,125 @@ +#!/bin/sh + +. ../lib/common.sh + +cleanup() +{ + umount "${tmpdir}/root" || : + vgchange -an "$vg" || : + qemu-nbd -d /dev/nbd0 || : + rm -rf "$tmpdir" +} + +command -v lvm > /dev/null || exit 222 + +set -ef +trap cleanup EXIT INT + +devmgr=${DEVMGR:-proc} +arch=${ARCH:-$(uname -m)} +kernel=${KERNEL:-$(uname -r)} +vmlinuz=${VMLINUZ:-"/boot/vmlinuz-${kernel}"} + +mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/${0##*/}.$$}" + +vg="vg$$" +lv="lv$$" +root="${tmpdir}/root" +config="${tmpdir}/config" +image="${tmpdir}/root.qcow2" +initrd="${tmpdir}/initramfs-$(uname -r)" + +qemu-img create -f qcow2 "$image" 1G +qemu-nbd -c /dev/nbd0 "$image" + +# o: Create MBR table. +# n: Add new partition to table. +# p: Primary partition. +# 1: Partition number. +# newline: Use default value for first sector. +# newline: Use default value for last sector. +# w: Write changes and re-read partition table. +fdisk /dev/nbd0 << EOF +o +n +p +1 + + +w +EOF + +cat > "$config" << EOF +hooks=$devmgr,lvm +root=LABEL=root +lvm_group=$vg +lvm_name=$lv +EOF + +vgcreate "$vg" /dev/nbd0p1 +lvcreate -l 100%FREE -n "$lv" "$vg" +lvchange -ay "${vg}/${lv}" + +mkdir -p "$root" + +mkfs.ext4 -L root "/dev/mapper/${vg}-${lv}" +mount "/dev/mapper/${vg}-${lv}" "$root" + +( + tmpdir=$root; cd "$tmpdir" + + mkdir -p \ + dev sys tmp run proc \ + root usr/lib usr/bin + + ln -s usr/lib lib + ln -s usr/bin bin + ln -s usr/bin sbin + ln -s bin usr/sbin + + copy_exec sh + copy_exec e2label + + cat > sbin/init << EOF +#!/bin/sh +exec e2label /dev/disk/by-label/root success +EOF + + chmod +x sbin/init +) + +umount "$root" +vgchange -an "$vg" +qemu-nbd -d /dev/nbd0 + +(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") + +set -- \ + -no-reboot \ + -initrd "$initrd" \ + -kernel "$vmlinuz" \ + -device virtio-scsi \ + -drive file="$image",if=virtio + +if [ -c /dev/kvm ]; then + set -- -enable-kvm -cpu host "$@" +fi + +if [ "$DEBUG" ]; then + set -- -append 'panic=-1 rdpanic debug rddebug console=ttyS0' -nographic "$@" +else + set -- -append 'panic=-1 rdpanic' -display none "$@" +fi + +"qemu-system-${arch}" "$@" + +qemu-nbd -c /dev/nbd0 "$image" + +# Re-read partition table. +fdisk /dev/nbd0 << EOF +w +EOF + +lvchange -ay "${vg}/${lv}" + +[ "$(e2label "/dev/mapper/${vg}-${lv}")" = success ] diff --git a/test/zfs.test b/test/zfs.test new file mode 100755 index 0000000..1b7565e --- /dev/null +++ b/test/zfs.test @@ -0,0 +1,131 @@ +#!/bin/sh + +. ../lib/common.sh + +cleanup() +{ + zpool export "$pool" || : + qemu-nbd -d /dev/nbd0 || : + rm -rf "$tmpdir" +} + +command -v zfs > /dev/null || exit 222 + +set -ef +trap cleanup EXIT INT + +devmgr=${DEVMGR:-proc} +arch=${ARCH:-$(uname -m)} +kernel=${KERNEL:-$(uname -r)} +vmlinuz=${VMLINUZ:-"/boot/vmlinuz-${kernel}"} + +mkdir -p "${tmpdir:=${TMPDIR:-/tmp}/${0##*/}.$$}" + +pool="pool$$" +root="${tmpdir}/root" +config="${tmpdir}/config" +image="${tmpdir}/root.qcow2" +initrd="${tmpdir}/initramfs-$(uname -r)" + +qemu-img create -f qcow2 "$image" 1G +qemu-nbd -c /dev/nbd0 "$image" + +# o: Create MBR table. +# n: Add new partition to table. +# p: Primary partition. +# 1: Partition number. +# newline: Use default value for first sector. +# newline: Use default value for last sector. +# w: Write changes and re-read partition table. +fdisk /dev/nbd0 << EOF +o +n +p +1 + + +w +EOF + +dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key" + +zpool create -m none "$pool" /dev/nbd0p1 +zfs create \ + -o mountpoint=legacy \ + -o canmount=noauto \ + -o encryption=on \ + -o keyformat=passphrase \ + -o keylocation="file://${tmpdir}/key" \ + "${pool}/root" + +cat > "$config" << EOF +hooks=$devmgr,zfs +root=${pool}/root +root_type=zfs +# required if mountpoint != legacy +#root_opts=zfsutil +zfs_key=${tmpdir}/key +# toybox/busybox blkid doesn't support zfs. fallback to /dev/* (unstable) +zfs_root=/dev/vda1 +EOF + +mkdir -p "$root" +mount -t zfs "${pool}/root" "$root" + +( + tmpdir=$root; cd "$tmpdir" + + mkdir -p \ + dev sys tmp run proc \ + root usr/lib usr/bin + + ln -s usr/lib lib + ln -s usr/bin bin + ln -s usr/bin sbin + ln -s bin usr/sbin + + copy_exec sh + copy_exec zfs + + cat > sbin/init << EOF +#!/bin/sh +exec zfs set success:=true "${pool}/root" +EOF + + chmod +x sbin/init +) + +zpool export "$pool" +qemu-nbd -d /dev/nbd0 + +(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") + +set -- \ + -no-reboot \ + -initrd "$initrd" \ + -kernel "$vmlinuz" \ + -device virtio-scsi \ + -drive file="$image",if=virtio + +if [ -c /dev/kvm ]; then + set -- -enable-kvm -cpu host "$@" +fi + +if [ "$DEBUG" ]; then + set -- -append 'panic=-1 rdpanic debug rddebug console=ttyS0' -nographic "$@" +else + set -- -append 'panic=-1 rdpanic' -display none "$@" +fi + +"qemu-system-${arch}" "$@" + +qemu-nbd -c /dev/nbd0 "$image" + +# Re-read partition table. +fdisk /dev/nbd0 << EOF +w +EOF + +zpool import -Nd /dev/nbd0p1 "$pool" + +[ "$(zfs get -Ho value success: "${pool}/root")" = true ]