#!/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