diff options
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/vm | 69 |
1 files changed, 51 insertions, 18 deletions
@@ -5,7 +5,7 @@ set -eu usage() { cat <<-'EOF' Usage: - vm [-G] [-S] [-v] ACTION [OS] + vm [-G] [-n] [-S] [-v] ACTION [OS [-- QEMU_OPTIONS...]] vm -h EOF } @@ -15,16 +15,18 @@ help() { Options: - -G use graphics - -S don't write to VM image (snapshot on) - -v verbose mode - -h, --help show this message + -G use graphics + -n dry-run: don't execute any code, assumes -v + -S don't write to VM image (snapshot on) + -v verbose mode + -h, --help show this message - ACTION one of: - - up - - down - - status - OS the name of the OS to be acted upon + ACTION one of: + - up + - down + - status + OS the name of the OS to be acted upon + QEMU_OPTIONS command line options to be given verbatim to QEMU Manage the state of known virtual machines. @@ -78,14 +80,19 @@ for flag in "$@"; do esac done +DRY_RUN=false GRAPHICS=false SNAPSHOT=false VERBOSE=false -while getopts 'GSvh' flag; do +while getopts 'GnSvh' flag; do case "$flag" in G) GRAPHICS=true ;; + n) + DRY_RUN=true + VERBOSE=true + ;; S) SNAPSHOT=true ;; @@ -150,6 +157,7 @@ ACTION="${1:-}" OS="${2:-}" eval "$(assert-arg "$ACTION" 'ACTION')" +shift FLAGS='' @@ -180,6 +188,11 @@ case "$ACTION" in ;; up) eval "$(assert-arg "$OS" 'OS')" + shift + if [ "${1:-}" = '--' ]; then + shift + fi + OS="$(guess_name "$OS")" PID_F="$RUNDIR/$OS.pid" PORT_F="$RUNDIR/$OS.port" @@ -192,16 +205,25 @@ case "$ACTION" in PORT="$(free-port)" QCOW="$QCOW_DIR"/"$OS".qcow2 - if [ "$VERBOSE" = true ]; then - set -x - fi + # shellcheck disable=2086 - qemu-system-x86_64 \ + set -- qemu-system-x86_64 \ -m 1G \ -nic user,model=virtio,hostfwd=tcp::"$PORT"-:22 \ -drive file="$QCOW",media=disk,if=virtio \ -enable-kvm \ - $FLAGS 1>>"$LOGS" 2>&1 & + $FLAGS "$@" + + if [ "$DRY_RUN" = true ]; then + echo "$@" + exit + fi + + if [ "$VERBOSE" = true ]; then + set -x + fi + + "$@" 1>>"$LOGS" 2>&1 & PID=$! set +x printf '%s' "$PID" > "$PID_F" @@ -211,6 +233,11 @@ case "$ACTION" in ;; down) eval "$(assert-arg "$OS" 'OS')" + shift + if [ "${1:-}" = '--' ]; then + shift + fi + OS="$(guess_name "$OS")" PID_F="$RUNDIR/$OS.pid" PORT_F="$RUNDIR/$OS.port" @@ -221,8 +248,14 @@ case "$ACTION" in fi PID="$(cat "$PID_F")" - rm -f "$PID_F" "$PORT_F" - kill "$PID" + + if [ "$DRY_RUN" = true ]; then + echo rm -f "$PID_F" "$PORT_F" + echo kill "$PID" + else + rm -f "$PID_F" "$PORT_F" + kill "$PID" + fi write_ssh_config ;; |