Skip to content

Instantly share code, notes, and snippets.

@emandret
Created September 20, 2020 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emandret/ed593cfefdbceaf76dc19f129d9554ce to your computer and use it in GitHub Desktop.
Save emandret/ed593cfefdbceaf76dc19f129d9554ce to your computer and use it in GitHub Desktop.
A simple bash script to launch and daemonize QEMU instances
#!/bin/bash
# daemonize a process by closing its file descriptors and detaching the process
# from its controlling terminal and the current session group
launch_daemon () {
$1 < /dev/null > /dev/null 2>&1 0<&- 1>&- 2>&- &
disown
}
# function to get the array key associated with one value
get_array_key () {
sed "s/$2//g" <<< $1 | wc -w | tr -d ' '
}
# populates options and qcow2 disk files arrays
opts=()
disk=()
# disks are placed in ~/QEMU/ubuntu-16.04-server/ubuntu-server-16.04.qcow2
for file in $(find ~/QEMU -name "*.qcow2"); do
opts[${#opts[@]}]=$(basename $(dirname "$file"))
disk[${#disk[@]}]=$file
done
# select the disk file and run the virtual machine
echo "Select one disk to continue:"
select opt in ${opts[@]}; do
file=${disk[$(get_array_key "$opts" "$opt")]}
launch_daemon "qemu-system-x86_64 -m 1024 \
-vga virtio \
-display default,show-cursor=on \
-usb \
-device usb-tablet \
-enable-kvm \
-drive file=$file,if=virtio \
-machine accel=hvf \
-cpu Penryn,kvm=on,vendor=GenuineIntel \
-device e1000,netdev=net0 \
-netdev user,id=net0,hostfwd=tcp::2222-:22"
exit
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment