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