Various refactoring
[rrq/tiniest.git] / vm.sh
1 #!/bin/bash
2 #
3 # QEMU setup for trial VM
4
5 N="12345678"
6 if [ ${#1} = 1 ] && [ "${N#*$1}" != "$N" ] ; then
7     REPLY=$1
8 else
9     VARIANT=(
10         "use bootimage.raw as harddisk with legacy boot (and boot menu)"
11         "use bootimage.raw as harddisk with UEFI boot"
12         "use tiniest.iso as harddisk with legacy boot (and boot menu)"
13         "use tiniest.iso as harddisk with UEFI boot"
14         "use tiniest.iso as cdrom with legacy boot (and boot menu)"
15         "use tiniest.iso as cdrom with UEFI boot"
16         "use tiniest.iso as USB disk with legacy boot (and boot menu)"
17         "use tiniest.iso as USB disk with UEFI boot"
18     )
19
20     select V in "${VARIANT[@]}" ; do
21         [ -z "$V" ] || break
22     done
23 fi
24 [ -z "$REPLY" ] && exit 0
25
26 FILE=bootimage.raw
27 [ $REPLY -ge 3 ] && FILE=tiniest.iso
28 MEDIA=disk
29 [ $REPLY -ge 5 ] && MEDIA=cdrom
30 [ $REPLY -ge 7 ] && MEDIA=usb
31
32 case "$MEDIA" in
33     disk)
34         DISK2="-drive index=1,format=raw,media=$MEDIA,file=$FILE"
35         ;;
36     usb)
37         USB="
38         -device qemu-xhci,id=xhci
39         -drive if=none,id=stick,format=raw,media=disk,file=${FILE}
40         -device usb-storage,bus=xhci.0,port=1,drive=stick
41         "
42         ;;
43     cdrom*)
44         DISK2="-drive index=1,format=raw,media=$MEDIA,file=$FILE"
45         ;;
46 esac
47
48 BOOT="-boot menu=on,splash=boot.jpg,splash-time=60000"
49 [ $(( $REPLY % 2 )) -eq 0 ] &&  BOOT="-bios /usr/share/OVMF/OVMF_CODE.fd"
50
51 case "$NET" in
52     vde)
53         NET="-net nic,model=e1000 -net vde,sock=/run/vde.ctl"
54         ;;
55     *)
56         NET=
57         ;;
58 esac
59
60 [ -e disk.raw ] || dd if=/dev/zero of=disk.raw bs=1G count=0 seek=8
61
62 ARGS=(
63     -m 2G -M pc,accel=kvm -cpu host -vga qxl
64     -serial mon:stdio -echr 0x1c # Using ^\ as meta character
65     $BOOT
66     -drive index=0,id=disk,media=disk,format=raw,file=disk.raw
67     $DISK2
68     $USB
69     $NET
70 )
71 echo "${ARGS[*]}" | sed "s/ -/\n -/g"
72
73 exec qemu-system-x86_64 ${ARGS[@]}