Skip to content

Instantly share code, notes, and snippets.

@emandret
Created September 20, 2020 18:18
Show Gist options
  • Select an option

  • Save emandret/ed593cfefdbceaf76dc19f129d9554ce to your computer and use it in GitHub Desktop.

Select an option

Save emandret/ed593cfefdbceaf76dc19f129d9554ce to your computer and use it in GitHub Desktop.

Revisions

  1. emandret created this gist Sep 20, 2020.
    41 changes: 41 additions & 0 deletions qemu.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/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