2021-07-31 22:55:47 +05:30
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
. ../lib/common.sh
|
|
|
|
|
|
|
|
cleanup()
|
|
|
|
{
|
|
|
|
umount "${tmpdir}/root" || :
|
|
|
|
vgchange -an "$vg" || :
|
2021-08-05 11:13:29 +05:30
|
|
|
qemu-nbd -d "$nbd" || :
|
2021-07-31 22:55:47 +05:30
|
|
|
rm -rf "$tmpdir"
|
|
|
|
}
|
|
|
|
|
|
|
|
set -ef
|
|
|
|
trap cleanup EXIT INT
|
|
|
|
|
2021-08-05 11:13:29 +05:30
|
|
|
nbd=${NBD:-/dev/nbd3}
|
2021-07-31 22:55:47 +05:30
|
|
|
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
|
2021-08-05 11:13:29 +05:30
|
|
|
qemu-nbd -c "$nbd" "$image"
|
2021-07-31 22:55:47 +05:30
|
|
|
|
2021-08-05 11:26:22 +05:30
|
|
|
sleep 1
|
|
|
|
|
2021-07-31 22:55:47 +05:30
|
|
|
# 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.
|
2021-08-05 11:13:29 +05:30
|
|
|
fdisk "$nbd" << EOF
|
2021-07-31 22:55:47 +05:30
|
|
|
o
|
|
|
|
n
|
|
|
|
p
|
|
|
|
1
|
|
|
|
|
|
|
|
|
|
|
|
w
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > "$config" << EOF
|
|
|
|
hooks=$devmgr,lvm
|
|
|
|
root=LABEL=root
|
|
|
|
lvm_group=$vg
|
|
|
|
lvm_name=$lv
|
|
|
|
EOF
|
|
|
|
|
2021-08-05 11:13:29 +05:30
|
|
|
vgcreate "$vg" "${nbd}p1"
|
2021-07-31 22:55:47 +05:30
|
|
|
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
|
|
|
|
|
2021-08-15 13:05:09 +05:30
|
|
|
ln -s lib usr/lib64
|
|
|
|
ln -s usr/lib lib64
|
2021-07-31 22:55:47 +05:30
|
|
|
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"
|
2021-08-05 11:13:29 +05:30
|
|
|
qemu-nbd -d "$nbd"
|
2021-07-31 22:55:47 +05:30
|
|
|
|
|
|
|
(cd .. && ./tinyramfs -lk "$kernel" -c "$config" "$initrd")
|
|
|
|
|
|
|
|
set -- \
|
2021-08-15 13:07:16 +05:30
|
|
|
-m 1G \
|
2021-07-31 22:55:47 +05:30
|
|
|
-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}" "$@"
|
|
|
|
|
2021-08-05 11:13:29 +05:30
|
|
|
qemu-nbd -c "$nbd" "$image"
|
2021-07-31 22:55:47 +05:30
|
|
|
|
2021-08-05 11:26:22 +05:30
|
|
|
sleep 1
|
|
|
|
|
2021-07-31 22:55:47 +05:30
|
|
|
# Re-read partition table.
|
2021-08-05 11:13:29 +05:30
|
|
|
fdisk "$nbd" << EOF
|
2021-07-31 22:55:47 +05:30
|
|
|
w
|
|
|
|
EOF
|
|
|
|
|
|
|
|
lvchange -ay "${vg}/${lv}"
|
|
|
|
|
|
|
|
[ "$(e2label "/dev/mapper/${vg}-${lv}")" = success ]
|