chamesle-vm (6250B)
1 #!/bin/sh 2 # chamesle-vm: runs the Chamesle ISO or an installed disk in qemu, in this terminal, no window. 3 # chamesle-vm iso boot the live ISO with an empty disk attached, to install onto 4 # chamesle-vm disk boot the disk that was installed 5 # chamesle-vm install wipe the disk and put the live system on it, typing the steps into the vm for you 6 # chamesle-vm reset throw the disk away and make an empty one 7 # chamesle-vm -g iso same, but with a window 8 # the console is this terminal: ctrl-a x leaves qemu, ctrl-a c switches to the qemu monitor. 9 # the disk is UEFI, so /boot is the EFI partition and limine goes to EFI/BOOT/BOOTX64.EFI. 10 # STAGE=url chamesle-vm install unpack a stage tarball from url instead of copying the live system 11 # ISO=path DISK=path SIZE=16G MEM=2048 CPUS=2 chamesle-vm iso 12 die() { echo "chamesle-vm: $*" >&2; exit 1; } 13 dir=${VMDIR:-$HOME/chamesle-build/vm} 14 : "${ISO:=$HOME/chamesle-build/iso-out/chamesle.iso}" 15 : "${DISK:=$dir/disk.img}" 16 : "${SIZE:=16G}" 17 : "${MEM:=2048}" 18 : "${CPUS:=2}" 19 : "${FW:=/usr/share/edk2-ovmf}" 20 gui= 21 if [ "$1" = -g ]; then gui=1; shift; fi 22 mode=${1:-iso} 23 24 mkdir -p "$dir" 25 # a disk that a running qemu has open must not be touched 26 inuse() { 27 for p in $(pgrep qemu-system); do 28 for f in /proc/$p/fd/*; do [ "$(readlink "$f" 2>/dev/null)" = "$DISK" ] && return 0; done 29 done 30 return 1 31 } 32 if inuse; then die "a vm is already running on $DISK, leave it with ctrl-a x first"; fi 33 [ -f "$FW/OVMF_CODE.fd" ] && [ -f "$FW/OVMF_VARS.fd" ] || die "no OVMF in $FW (set FW)" 34 case $mode in 35 reset) rm -f "$DISK" "$dir/vars.fd"; echo "disk removed"; exit 0;; 36 iso|disk|install) ;; 37 *) echo "usage: chamesle-vm [-g] iso|disk|install|reset" >&2; exit 2;; 38 esac 39 [ "$mode" = install ] && rm -f "$DISK" "$dir/vars.fd" 40 [ -f "$DISK" ] || { qemu-img create -f raw "$DISK" "$SIZE" >/dev/null || die "cannot make $DISK"; echo "new empty disk: $DISK ($SIZE)"; } 41 [ -f "$dir/vars.fd" ] || cp "$FW/OVMF_VARS.fd" "$dir/vars.fd" 42 43 set -- -m "$MEM" -smp "$CPUS" -machine q35 \ 44 -drive if=pflash,format=raw,readonly=on,file="$FW/OVMF_CODE.fd" \ 45 -drive if=pflash,format=raw,file="$dir/vars.fd" \ 46 -drive file="$DISK",format=raw,if=none,id=d0 -device virtio-blk-pci,drive=d0,bootindex=0 \ 47 -nic user,model=virtio-net-pci -device virtio-rng-pci 48 [ -w /dev/kvm ] && set -- "$@" -accel kvm -cpu host 49 if [ "$mode" = iso ] || [ "$mode" = install ]; then 50 [ -f "$ISO" ] || die "no ISO at $ISO (set ISO)" 51 set -- "$@" -drive file="$ISO",media=cdrom,readonly=on -boot order=d 52 else 53 [ -n "$(head -c 512 "$DISK" | tr -d '\000')" ] || die "the disk is empty, install onto it first (chamesle-vm iso)" 54 fi 55 if [ "$mode" = install ]; then 56 rm -f "$dir"/ser.*; mkfifo "$dir/ser.in" "$dir/ser.out" 57 qemu-system-x86_64 "$@" -display none -serial pipe:"$dir/ser" -monitor none & 58 qpid=$! 59 cat "$dir/ser.out" > "$dir/ser.log" & 60 exec 3> "$dir/ser.in" 61 # run CMD in the vm and wait until it has finished, stop if it failed. the sentinel is computed, so the echoed command line never matches it 62 run() { 63 echo "==> $1" 64 printf '%s; echo __R$((0+$?))_X\n' "$2" >&3 65 t=0 66 while ! grep -q '__R[0-9]*_X' "$dir/ser.log"; do 67 sleep 2; t=$((t + 2)) 68 [ $t -lt "${3:-300}" ] || { kill $qpid; die "'$1' took too long, see $dir/ser.log"; } 69 done 70 grep -q '__R0_X' "$dir/ser.log" || { tail -n 8 "$dir/ser.log" >&2; kill $qpid; die "'$1' failed"; } 71 : > "$dir/ser.log" 72 } 73 echo "==> booting the ISO (a minute or two without kvm)" 74 # any key stops the boot menu's countdown, so say nothing until the kernel is up 75 n=0 76 until grep -q 'Linux version' "$dir/ser.log"; do 77 sleep 2; n=$((n + 2)) 78 [ $n -lt 240 ] || { kill $qpid; die "the kernel did not start, see $dir/ser.log"; } 79 done 80 sleep 20 81 n=0 82 while ! grep -q '__U42_X' "$dir/ser.log"; do 83 printf 'echo __U$((6*7))_X\n' >&3; sleep 5; n=$((n + 5)) 84 [ $n -lt 300 ] || { kill $qpid; die "the live system did not come up, see $dir/ser.log"; } 85 done 86 : > "$dir/ser.log" 87 run "partitioning" "printf 'label: gpt\nsize=512M, type=U\ntype=L\n' | sfdisk /dev/vda >/dev/null && sleep 2" 88 run "boot partition" "modprobe vfat; mkfs.fat -F32 /dev/vda1 >/dev/null" 89 run "zfs pool" "modprobe zfs && zpool create -f -o ashift=12 -O compression=zstd -O acltype=posixacl -O xattr=sa -O mountpoint=none -R /mnt rpool /dev/vda2 && zfs create -o mountpoint=/ -o canmount=noauto rpool/root && zfs mount rpool/root && zfs create -o mountpoint=/home rpool/home && zfs create -o mountpoint=/var rpool/var && zpool set bootfs=rpool/root rpool && mkdir -p /mnt/boot && mount -t vfat /dev/vda1 /mnt/boot" 90 if [ -n "$STAGE" ]; then 91 run "unpacking the stage" "curl -sf -o /mnt/stage.tar.xz $STAGE && xz -t /mnt/stage.tar.xz && xz -dc /mnt/stage.tar.xz | tar -C /mnt --exclude ./etc/fstab -xpf - && rm /mnt/stage.tar.xz && mkdir -p /mnt/proc /mnt/sys /mnt/dev /mnt/run /mnt/tmp /mnt/var/tmp /mnt/var/log /mnt/var/cache /mnt/home && chmod 1777 /mnt/tmp /mnt/var/tmp && cp /run/live/rootfs/etc/fstab /mnt/etc/fstab && cp /run/live/medium/boot/initrd /mnt/boot/initrd && cp /run/live/rootfs/boot/vmlinuz-* /mnt/boot/ && cp /mnt/boot/vmlinuz-* /mnt/boot/vmlinuz" 1800 92 else 93 run "copying the system" "tar -C /run/live/rootfs -cf - . | tar -C /mnt -xpf - && cp /run/live/medium/boot/initrd /mnt/boot/initrd && cp /mnt/boot/vmlinuz-* /mnt/boot/vmlinuz" 900 94 fi 95 run "fstab, password, boot entry" "printf 'UUID=%s /boot vfat rw,noatime 0 2\n' \$(blkid -s UUID -o value /dev/vda1) >> /mnt/etc/fstab && sed -i 's/^root:x:/root:/' /mnt/etc/passwd && mkdir -p /mnt/boot/EFI/BOOT && cp /mnt/usr/share/limine/BOOTX64.EFI /mnt/boot/EFI/BOOT/ && printf 'timeout: 3\nserial: yes\n\n/Chamesle\n protocol: linux\n path: boot():/vmlinuz\n module_path: boot():/initrd\n cmdline: root=ZFS=rpool/root rw console=tty0 console=ttyS0,115200\n' > /mnt/boot/limine.conf" 96 run "unmounting" "umount -R /mnt && zpool export rpool" 97 echo "==> shutting down" 98 printf 'poweroff\n' >&3 99 wait $qpid 100 rm -f "$dir"/ser.in "$dir"/ser.out 101 echo "installed. start it with: chamesle-vm disk (root has no password)" 102 exit 0 103 fi 104 if [ -n "$gui" ]; then 105 exec qemu-system-x86_64 "$@" -device qemu-xhci -device usb-tablet -monitor unix:"$dir/mon",server,nowait 106 fi 107 exec qemu-system-x86_64 "$@" -nographic