-
Notifications
You must be signed in to change notification settings - Fork 1
/
qemurun.sh
executable file
·198 lines (166 loc) · 5.49 KB
/
qemurun.sh
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
## Helper script to run QEMU images from configuration file
## options:
## -c, --config <name> Configuration name (e.g. ubuntu-22.04)
## -n, --new Generate a new cfg file (to be used with --config to give file path)
## -s, --disk_size <size> Size of the disk IMG to be created (to be used with --new)
## -e, --edit Edit the configuration file before running qemu (to be used with --config to give file path)
## -i, --iso <path> Path to the ISO image to run (for installing the VM)
## -k, --kill Kill the running VM
# CLInt GENERATED_CODE: start
# info: https://github.com/clobrano/CLInt.git
# No-arguments is not allowed
[ $# -eq 0 ] && sed -ne 's/^## \(.*\)/\1/p' $0 && exit 1
# Converting long-options into short ones
for arg in "$@"; do
shift
case "$arg" in
"--config") set -- "$@" "-c";;
"--new") set -- "$@" "-n";;
"--disk_size") set -- "$@" "-s";;
"--edit") set -- "$@" "-e";;
"--iso") set -- "$@" "-i";;
"--kill") set -- "$@" "-k";;
*) set -- "$@" "$arg"
esac
done
function print_illegal() {
echo Unexpected flag in command line \"$@\"
}
# Parsing flags and arguments
while getopts 'hnekc:s:i:' OPT; do
case $OPT in
h) sed -ne 's/^## \(.*\)/\1/p' $0
exit 1 ;;
n) _new=1 ;;
e) _edit=1 ;;
k) _kill=1 ;;
c) _config=$OPTARG ;;
s) _disk_size=$OPTARG ;;
i) _iso=$OPTARG ;;
\?) print_illegal $@ >&2;
echo "---"
sed -ne 's/^## \(.*\)/\1/p' $0
exit 1
;;
esac
done
# CLInt GENERATED_CODE: end
if [[ -n $_kill ]]; then
kill $(cat /tmp/qemu.pid)
exit 0
fi
CONF_NAME=$_config
CONF=${CONF_NAME}/${CONF_NAME}.cfg
if [[ -n $_edit ]]; then
edit $CONF
echo "Run qemu with current $CONF? [ENTER/CTRL-C]"
read
fi
ARCH=`grep "ARCH=" "$CONF" 2>/dev/null | cut -d"=" -f2`
ARCH=${ARCH:-"x86_64"}
BZIMAGE=`grep "BZIMAGE=" "$CONF" 2>/dev/null | cut -d"=" -f2`
DISK=`grep "DISK=" "$CONF" 2>/dev/null | cut -d"=" -f2`
DISK=${DISK:-"30G"}
HEADLESS=`grep "HEADLESS=" "$CONF" 2>/dev/null | cut -d"=" -f2`
HEADLESS=${HEADLESS:-"false"}
IMG=`grep "IMG=" "$CONF" 2>/dev/null | cut -d"=" -f2`
IMG=${IMG:-"$CONF_NAME".img}
RAM=`grep "RAM=" "$CONF" 2>/dev/null | cut -d"=" -f2`
RAM=${RAM:-"4G"}
ROOT=`grep "ROOT=" "$CONF" 2>/dev/null | cut -d"=" -f2`
ROOT=${ROOT:-"/dev/sda3"}
SPICE=`grep "SPICE=" "$CONF" 2>/dev/null | cut -d"=" -f2`
SPICE=${SPICE:-"true"}
SPICY=`grep "SPICY=" "$CONF" 2>/dev/null | cut -d"=" -f2`
SPICY=${SPICY:-"false"}
SSHPORTNO=`grep "SSHPORTNO=" "$CONF" 2>/dev/null | cut -d"=" -f2`
SSHPORTNO=${SSHPORTNO:-"2222"}
USBPASSTHROUGH=`grep "USBPASSTHROUGH=" "$CONF" 2>/dev/null | cut -d"=" -f2`
USBPASSTHROUGH=${USBPASSTHROUGH:-""}
if [[ -n $_new ]]; then
echo "[+] Creating new configuration file $CONF"
[[ -f "$CONF" ]] && echo "[!] $CONF file exists already!" && exit 1
[[ ! -d "$CONF_NAME" ]] && mkdir "$CONF_NAME"
if [[ -n $_disk_size ]]; then
DISK=$_disk_size
qemu-img create -f qcow2 ${CONF_NAME}/$IMG $_disk_size
fi
echo "ARCH=$ARCH" > $CONF
echo "BZIMAGE=$BZIMAGE" >> $CONF
echo "DISK=$DISK" >> $CONF
echo "HEADLESS=$HEADLESS" >> $CONF
echo "IMG=$IMG" >> $CONF
echo "RAM=$RAM" >> $CONF
echo "ROOT=$ROOT" >> $CONF
echo "SPICE=$SPICE" >> $CONF
echo "SSHPORTNO=$SSHPORTNO" >> $CONF
if [[ -n $USBPASSTHROUGH ]]; then
echo "USBPASSTHROUGH=$USBPASSTHROUGH" >> $CONF
fi
exit 0
fi
OPTS=()
if [[ -n $_iso ]]; then
OPTS+=(-cdrom ${_iso})
fi
# PIDFILE to be able to shutdown the VM easily
OPTS+=(-pidfile /tmp/qemu.pid)
# Enable KVM
OPTS+=(-enable-kvm)
# Set RAM
OPTS+=( -m $RAM)
# Set number of Cores
OPTS+=( -smp 4)
# Using Host cpu flags (?)
OPTS+=( -cpu host)
if [[ -n "$BZIMAGE" ]]; then
# Kernel image to load and configurations
OPTS+=(-kernel $BZIMAGE)
# No need to use much RAM in HEADLESS mode
OPTS+=( -append \"root=$ROOT console=ttyS0 rw\")
OPTS+=(-serial mon:stdio)
OPTS+=(-display none)
fi
# Set the IMG to use
OPTS+=(-drive file="$CONF_NAME"/"$IMG",if=virtio)
echo $SPICE
if [[ $SPICE = "true" ]]; then
# Spice configuration
OPTS+=(-vga qxl)
OPTS+=(-spice port=5900,addr=127.0.0.1,disable-ticketing=on
-device virtio-serial-pci
-device virtserialport,chardev=spicechannel0,name=com.redhat.spice.0
-chardev spicevmc,id=spicechannel0,name=vdagent)
# enable usb passthroug
OPTS+=(-device qemu-xhci,id=spicepass
-chardev spicevmc,id=usbredirchardev1,name=usbredir
-device usb-redir,chardev=usbredirchardev1,id=usbredirdev1
-chardev spicevmc,id=usbredirchardev2,name=usbredir
-device usb-redir,chardev=usbredirchardev2,id=usbredirdev2
-chardev spicevmc,id=usbredirchardev3,name=usbredir
-device usb-redir,chardev=usbredirchardev3,id=usbredirdev3)
# filesystem sharing
PUBLIC=$(xdg-user-dir PUBLICSHARE)
PUBLIC_TAG="public-${USER,,}"
OPTS+=(-virtfs local,path="${PUBLIC}",mount_tag="${PUBLIC_TAG}",security_model=mapped-xattr)
fi
ARGS="${OPTS[*]}"
set -u
echo "[+] Running the following qemu line:"
echo ""
echo "qemu-system-$ARCH ${ARGS}"
echo " "
echo "[+] Press ENTER to continue, CTRL-C to stop"
read
RUNNER=/tmp/qemu-runner-script.sh
echo qemu-system-$ARCH ${ARGS[@]} > $RUNNER
chmod +x $RUNNER
$RUNNER &
if [[ $SPICY = "true" ]]; then
spicy -h 127.0.0.1 -p 5900
if [[ -f /tmp/qemu.pid ]]; then
kill $(cat /tmp/qemu.pid)
fi
fi