test/*: do not hardcode nbd device

This commit is contained in:
illiliti 2021-08-05 08:43:29 +03:00
parent 5adc1faf2b
commit 23c277b059
4 changed files with 38 additions and 34 deletions

View File

@ -5,13 +5,14 @@
cleanup() cleanup()
{ {
umount "${tmpdir}/root" || : umount "${tmpdir}/root" || :
qemu-nbd -d /dev/nbd0 || : qemu-nbd -d "$nbd" || :
rm -rf "$tmpdir" rm -rf "$tmpdir"
} }
set -ef set -ef
trap cleanup EXIT INT trap cleanup EXIT INT
nbd=${NBD:-/dev/nbd1}
devmgr=${DEVMGR:-proc} devmgr=${DEVMGR:-proc}
arch=${ARCH:-$(uname -m)} arch=${ARCH:-$(uname -m)}
kernel=${KERNEL:-$(uname -r)} kernel=${KERNEL:-$(uname -r)}
@ -25,7 +26,7 @@ image="${tmpdir}/root.qcow2"
initrd="${tmpdir}/initramfs-$(uname -r)" initrd="${tmpdir}/initramfs-$(uname -r)"
qemu-img create -f qcow2 "$image" 1G qemu-img create -f qcow2 "$image" 1G
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# o: Create MBR table. # o: Create MBR table.
# n: Add new partition to table. # n: Add new partition to table.
@ -34,7 +35,7 @@ qemu-nbd -c /dev/nbd0 "$image"
# newline: Use default value for first sector. # newline: Use default value for first sector.
# newline: Use default value for last sector. # newline: Use default value for last sector.
# w: Write changes and re-read partition table. # w: Write changes and re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
o o
n n
p p
@ -51,8 +52,8 @@ EOF
mkdir -p "$root" mkdir -p "$root"
mkfs.ext4 -L root /dev/nbd0p1 mkfs.ext4 -L root "${nbd}p1"
mount /dev/nbd0p1 "$root" mount "${nbd}p1" "$root"
( (
tmpdir=$root; cd "$tmpdir" tmpdir=$root; cd "$tmpdir"
@ -78,7 +79,7 @@ EOF
) )
umount "$root" umount "$root"
qemu-nbd -d /dev/nbd0 qemu-nbd -d "$nbd"
(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") (cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd")
@ -101,11 +102,11 @@ fi
"qemu-system-${arch}" "$@" "qemu-system-${arch}" "$@"
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# Re-read partition table. # Re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
w w
EOF EOF
[ "$(e2label /dev/nbd0p1)" = success ] [ "$(e2label "${nbd}p1")" = success ]

View File

@ -6,7 +6,7 @@ cleanup()
{ {
umount "${tmpdir}/root" || : umount "${tmpdir}/root" || :
cryptsetup close "$name" || : cryptsetup close "$name" || :
qemu-nbd -d /dev/nbd0 || : qemu-nbd -d "$nbd" || :
rm -rf "$tmpdir" rm -rf "$tmpdir"
} }
@ -15,6 +15,7 @@ command -v cryptsetup > /dev/null || exit 222
set -ef set -ef
trap cleanup EXIT INT trap cleanup EXIT INT
nbd=${NBD:-/dev/nbd2}
devmgr=${DEVMGR:-proc} devmgr=${DEVMGR:-proc}
arch=${ARCH:-$(uname -m)} arch=${ARCH:-$(uname -m)}
kernel=${KERNEL:-$(uname -r)} kernel=${KERNEL:-$(uname -r)}
@ -29,7 +30,7 @@ image="${tmpdir}/root.qcow2"
initrd="${tmpdir}/initramfs-$(uname -r)" initrd="${tmpdir}/initramfs-$(uname -r)"
qemu-img create -f qcow2 "$image" 1G qemu-img create -f qcow2 "$image" 1G
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# o: Create MBR table. # o: Create MBR table.
# n: Add new partition to table. # n: Add new partition to table.
@ -38,7 +39,7 @@ qemu-nbd -c /dev/nbd0 "$image"
# newline: Use default value for first sector. # newline: Use default value for first sector.
# newline: Use default value for last sector. # newline: Use default value for last sector.
# w: Write changes and re-read partition table. # w: Write changes and re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
o o
n n
p p
@ -50,13 +51,13 @@ EOF
dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key" dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key"
cryptsetup -qd "${tmpdir}/key" --pbkdf=pbkdf2 luksFormat /dev/nbd0p1 cryptsetup -qd "${tmpdir}/key" --pbkdf=pbkdf2 luksFormat "${nbd}p1"
cryptsetup -d "${tmpdir}/key" open /dev/nbd0p1 "$name" cryptsetup -d "${tmpdir}/key" open "${nbd}p1" "$name"
cat > "$config" << EOF cat > "$config" << EOF
hooks=$devmgr,luks hooks=$devmgr,luks
root=LABEL=root root=LABEL=root
luks_root=UUID=$(cryptsetup luksUUID /dev/nbd0p1) luks_root=UUID=$(cryptsetup luksUUID "${nbd}p1")
luks_key=${tmpdir}/key luks_key=${tmpdir}/key
EOF EOF
@ -90,7 +91,7 @@ EOF
umount "$root" umount "$root"
cryptsetup close "$name" cryptsetup close "$name"
qemu-nbd -d /dev/nbd0 qemu-nbd -d "$nbd"
(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") (cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd")
@ -113,13 +114,13 @@ fi
"qemu-system-${arch}" "$@" "qemu-system-${arch}" "$@"
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# Re-read partition table. # Re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
w w
EOF EOF
cryptsetup -d "${tmpdir}/key" open /dev/nbd0p1 "$name" cryptsetup -d "${tmpdir}/key" open "${nbd}p1" "$name"
[ "$(e2label "/dev/mapper/${name}")" = success ] [ "$(e2label "/dev/mapper/${name}")" = success ]

View File

@ -6,7 +6,7 @@ cleanup()
{ {
umount "${tmpdir}/root" || : umount "${tmpdir}/root" || :
vgchange -an "$vg" || : vgchange -an "$vg" || :
qemu-nbd -d /dev/nbd0 || : qemu-nbd -d "$nbd" || :
rm -rf "$tmpdir" rm -rf "$tmpdir"
} }
@ -15,6 +15,7 @@ command -v lvm > /dev/null || exit 222
set -ef set -ef
trap cleanup EXIT INT trap cleanup EXIT INT
nbd=${NBD:-/dev/nbd3}
devmgr=${DEVMGR:-proc} devmgr=${DEVMGR:-proc}
arch=${ARCH:-$(uname -m)} arch=${ARCH:-$(uname -m)}
kernel=${KERNEL:-$(uname -r)} kernel=${KERNEL:-$(uname -r)}
@ -30,7 +31,7 @@ image="${tmpdir}/root.qcow2"
initrd="${tmpdir}/initramfs-$(uname -r)" initrd="${tmpdir}/initramfs-$(uname -r)"
qemu-img create -f qcow2 "$image" 1G qemu-img create -f qcow2 "$image" 1G
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# o: Create MBR table. # o: Create MBR table.
# n: Add new partition to table. # n: Add new partition to table.
@ -39,7 +40,7 @@ qemu-nbd -c /dev/nbd0 "$image"
# newline: Use default value for first sector. # newline: Use default value for first sector.
# newline: Use default value for last sector. # newline: Use default value for last sector.
# w: Write changes and re-read partition table. # w: Write changes and re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
o o
n n
p p
@ -56,7 +57,7 @@ lvm_group=$vg
lvm_name=$lv lvm_name=$lv
EOF EOF
vgcreate "$vg" /dev/nbd0p1 vgcreate "$vg" "${nbd}p1"
lvcreate -l 100%FREE -n "$lv" "$vg" lvcreate -l 100%FREE -n "$lv" "$vg"
lvchange -ay "${vg}/${lv}" lvchange -ay "${vg}/${lv}"
@ -90,7 +91,7 @@ EOF
umount "$root" umount "$root"
vgchange -an "$vg" vgchange -an "$vg"
qemu-nbd -d /dev/nbd0 qemu-nbd -d "$nbd"
(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") (cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd")
@ -113,10 +114,10 @@ fi
"qemu-system-${arch}" "$@" "qemu-system-${arch}" "$@"
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# Re-read partition table. # Re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
w w
EOF EOF

View File

@ -5,7 +5,7 @@
cleanup() cleanup()
{ {
zpool export "$pool" || : zpool export "$pool" || :
qemu-nbd -d /dev/nbd0 || : qemu-nbd -d "$nbd" || :
rm -rf "$tmpdir" rm -rf "$tmpdir"
} }
@ -14,6 +14,7 @@ command -v zfs > /dev/null || exit 222
set -ef set -ef
trap cleanup EXIT INT trap cleanup EXIT INT
nbd=${NBD:-/dev/nbd4}
devmgr=${DEVMGR:-proc} devmgr=${DEVMGR:-proc}
arch=${ARCH:-$(uname -m)} arch=${ARCH:-$(uname -m)}
kernel=${KERNEL:-$(uname -r)} kernel=${KERNEL:-$(uname -r)}
@ -28,7 +29,7 @@ image="${tmpdir}/root.qcow2"
initrd="${tmpdir}/initramfs-$(uname -r)" initrd="${tmpdir}/initramfs-$(uname -r)"
qemu-img create -f qcow2 "$image" 1G qemu-img create -f qcow2 "$image" 1G
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# o: Create MBR table. # o: Create MBR table.
# n: Add new partition to table. # n: Add new partition to table.
@ -37,7 +38,7 @@ qemu-nbd -c /dev/nbd0 "$image"
# newline: Use default value for first sector. # newline: Use default value for first sector.
# newline: Use default value for last sector. # newline: Use default value for last sector.
# w: Write changes and re-read partition table. # w: Write changes and re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
o o
n n
p p
@ -49,7 +50,7 @@ EOF
dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key" dd bs=512 count=1 if=/dev/urandom of="${tmpdir}/key"
zpool create -m none "$pool" /dev/nbd0p1 zpool create -m none "$pool" "${nbd}p1"
zfs create \ zfs create \
-o mountpoint=legacy \ -o mountpoint=legacy \
-o canmount=noauto \ -o canmount=noauto \
@ -96,7 +97,7 @@ EOF
) )
zpool export "$pool" zpool export "$pool"
qemu-nbd -d /dev/nbd0 qemu-nbd -d "$nbd"
(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd") (cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd")
@ -119,13 +120,13 @@ fi
"qemu-system-${arch}" "$@" "qemu-system-${arch}" "$@"
qemu-nbd -c /dev/nbd0 "$image" qemu-nbd -c "$nbd" "$image"
# Re-read partition table. # Re-read partition table.
fdisk /dev/nbd0 << EOF fdisk "$nbd" << EOF
w w
EOF EOF
zpool import -Nd /dev/nbd0p1 "$pool" zpool import -Nd "${nbd}p1" "$pool"
[ "$(zfs get -Ho value success: "${pool}/root")" = true ] [ "$(zfs get -Ho value success: "${pool}/root")" = true ]