commit 6454ac0267cbd829f11b15cb555696fc38f34f20 parent 2dbe7184a7e0395d8ef7ecdd2a85efc4f6e1d24b Author: Alex Balgavy <a.balgavy@gmail.com> Date: Tue, 12 May 2020 17:48:06 +0200 vbox: a simpler wrapper for VBoxManage Former-commit-id: f225a3e2ead9bdfae83e8af4a899b20a298587a4 Diffstat:
A | scripts/vbox | | | 105 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
A | shell/zsh-completions-mine/_vbox | | | 40 | ++++++++++++++++++++++++++++++++++++++++ |
2 files changed, 145 insertions(+), 0 deletions(-)
diff --git a/scripts/vbox b/scripts/vbox @@ -0,0 +1,105 @@ +#!/usr/bin/env bash +die() { + echo "$1" >&2 + exit 1 +} + +start() { + [ $# -eq 1 ] || die "Only one argument: VM name" + VBoxManage startvm "$1" --type headless +} + +stop() { + [ $# -eq 1 ] || die "Only one argument: VM name" + VBoxManage controlvm "$1" acpipowerbutton +} + +list() { + VBoxManage list vms +} + +running() { + VBoxManage list runningvms +} + +info() { + VBoxManage showvminfo "$1" +} + +# share /folder/path vmname /mount/point +share() { + [ $# -eq 3 ] || die "Not enough arguments" + [ -d $1 ] || die "$1 is not a directory or does not exist" + VBoxManage sharedfolder add "$2" --name "$(basename $(realpath "$1"))" --hostpath "$1" --automount --auto-mount-point "$3" +} + + +sharetmp() { + [ $# -eq 3 ] || die "Not enough arguments" + [ -d $1 ] || die "$1 is not a directory or does not exist" + VBoxManage sharedfolder add "$2" --name "$(basename $(realpath "$1"))" --hostpath "$1" --automount --auto-mount-point "$3" --transient +} + +PARAMS="" + +while (( "$#" )); do + case "$1" in + -h|--help) + echo "Usage:" + echo "start vmname start a VM" + echo "stop vmname stop a VM" + echo "list list VMs" + echo "running list running VMs" + echo "share /local/path vmname /mount/point share a local folder" + echo "sharetmp /local/path vmname /mount/point temporarily share a local folder" + exit 0 + ;; + --) # end arg parsing + shift + break + ;; + -*) # unsupported flags + echo "Unsupported flag $1" >&2 + exit 1 + ;; + *) # preserve positional arguments + PARAMS="$PARAMS $1" + shift + ;; + esac +done +eval set -- "$PARAMS" + +[ $# -ge 1 ] || die "Not enough arguments provided." + +case "$1" in + "start") + shift; + start "$@"; + ;; + "stop") + shift; + stop "$@"; + ;; + "list") + list; + ;; + "running") + running; + ;; + "share") + shift; + share "$@"; + ;; + "sharetmp") + shift; + sharetmp "$@"; + ;; + "info") + shift; + info "$@"; + ;; + *) + die "Unsupported command $1"; + ;; +esac diff --git a/shell/zsh-completions-mine/_vbox b/shell/zsh-completions-mine/_vbox @@ -0,0 +1,40 @@ +#compdef vbox +_vboxmachines() { + VBoxManage list vms | egrep -o '^"[^"]+"' 2>/dev/null | sed -e 's|"||g' | while read machine; do + _wanted 'machine' expl 'machine' compadd $machine + done +} +_vbox() { + local -a arguments + arguments=( + 'start:start a VM' + 'stop:stop a VM' + 'list:list known VMs' + 'info:get information about a VM' + 'running:list currently running VMs' + 'share:share a local folder' + 'sharetmp:temporarily share a local folder' + ) + local context state line expl + local -A opt_args + _arguments '*:: :->subcmds' && return 0 + + if (( CURRENT == 1 )); then + _describe -t commands "vbox commands" arguments -V1 + return + fi + + case "$words[1]" in + share|sharetmp) + _arguments \ + ':hostpath:_files -/' \ + :machine:_vboxmachines \ + ':name: :' + ;; + start|stop|info) + _arguments \ + :machine:_vboxmachines + ;; + esac + return 1 +}