summaryrefslogtreecommitdiff
path: root/.local/bin/livevm
blob: f52b45ce08cffd5316ab19b17d2b17c37e421602 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh

data_dir="${XDG_DATA_HOME:-${HOME}/.local/share}/vm"
storage="${data_dir}/livevm.qcow2"
# allocate half of available memory and cores to vm
mem="$(($(grep MemAvailable /proc/meminfo | grep -o '[0-9]*')/2))"
cores="$(($(nproc)/2))"
cmd="qemu-system-x86_64 -boot menu=on -enable-kvm -device intel-hda \
	-device hda-duplex -m ${mem}k -cpu host -smp $cores -drive file=$storage"

[ ! -d "${data_dir}" ] &&
	mkdir -p "${data_dir}"
[ ! -f "$storage" ] &&
	qemu-img create -f qcow2 "$storage" 30G

if [ "$#" -eq 1 ] && [ -f "$1" ]
then
	cmd="${cmd} -cdrom ${1}"
	shift
fi

while getopts "wehc:d:u:q:r:" f
do
	case "$f" in
	w)
		cmd="${cmd} -device qxl -display gtk,show-menubar=off"
		;;
	e)
		[ ! -f "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd" ] &&
			die "ovmf not found, install it"

		[ ! -f "${data_dir}/OVMF_VARS.fd" ] &&
			cp "/usr/share/edk2-ovmf/x64/OVMF_CODE.fd" "${data_dir}/OVMF_VARS.fd"

		cmd="${cmd} -drive if=pflash,format=raw,readonly=on,file=/usr/share/edk2-ovmf/x64/OVMF_CODE.fd"
		cmd="${cmd} -drive if=pflash,format=raw,file=${data_dir}/OVMF_VARS.fd"
		;;
	c)
		cmd="${cmd} -cdrom ${OPTARG}"
		;;
	d)
		cmd="${cmd} -drive file=${OPTARG}"
		;;
	u)
		cmd="${cmd} -device qemu-xhci,id=xhci -device usb-host,hostdevice=/dev/bus/usb/${OPTARG}"
		;;
	q)
		cmd="${cmd} -drive file=${OPTARG}"
		;;
	r)
		cmd="${cmd} -drive format=raw,file=${OPTARG}"
		;;
	h)
		cat <<- EOF
		Usage: ${0##*/} command
		a wrapper for qemu
		Commands:
		  -e	use efi
		  -w	run wangblows
		  -c	[cdrom]
		  -d	[drive]
		  -u	[host_bus/host_addr]
		  -q	[img.qcow2]
		  -r	[img.raw]
		EOF

		exit
		;;
	?)
		printf "%s%s\n" "usage: ${0##*/} [ -weh ] [ -c cdrom ] [ -d drive ]" \
			"[ -u host_bus/host_addr ] [ -q img.qcow2 ] [ -r img.raw ]" 1>&2
		exit 1
		;;
	esac
done

# use virgl by default if wangblows is not set
[ -n "${cmd##*device qxl*}" ] &&
	cmd="${cmd} -device virtio-vga-gl -display gtk,gl=on,show-menubar=off"

eval "$cmd"