From e236ec56ec9783c9e51c6190184d7e8b359d0c7b Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Mon, 4 Sep 2017 14:31:18 +0200 Subject: [PATCH] Genesis --- README.md | 24 ++++++++++++++++++++++++ _vb | 38 ++++++++++++++++++++++++++++++++++++++ virtualbox.plugin.zsh | 24 ++++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 README.md create mode 100644 _vb create mode 100644 virtualbox.plugin.zsh diff --git a/README.md b/README.md new file mode 100644 index 0000000..65e8e4f --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# VirtualBox ZSH Plugin + +This plugin provides several aliases for VirtualBox: + +* `vb list` - List all VMs +* `vb start ` - Start VM +* `vb stop ` - Send poweroff signal to VM via ACPI, call hard power on failure +* `vb gui ` - Attach to the GUI of VM +* `vb suspend ` - Suspend VM +* `vb restart ` - Restart VM (calls `vb stop`, then `vb start`) +* `vb reset ` - Send reset signal to VM +* `vb status [VM]` - Show status of VM (show all VMs if no VM name is passed) +* `vb snaplist ` - list snapshots for VM +* `vb snapshot [NAME]` - create snapshot for VM (optional: define a name for the snapshot) +* `vb snaprestore ` - restore snapshot for VM +* `vb snapdel ` - delete snapshot of VM + +Autocompletion for parameters and VMs and their snapshots is enabled. + +## Installation + +### Antigen + +`antigen bundle https://gitlab.com/smark/virtualbox.zshplugin.git` diff --git a/_vb b/_vb new file mode 100644 index 0000000..d7deff1 --- /dev/null +++ b/_vb @@ -0,0 +1,38 @@ +#compdef vb + +_vb() { + local state + + _arguments -C '1: :->actions' '2: :->vms' '3: :->snapshots' + + + case $state in + (actions) + local actions + actions=("list:List all VMs" + "start:Start VM" + "stop:Stop VM" + "suspend:Suspend VM" + "restart:Restart VM" + "reset:Reset VM" + "status:Show status" + "snapshot:Create new snapshot" + "snaprestore:Restore snapshots" + "snaplist:List snapshots" + "snapdel:Delete snapshot") + _describe 'actions' actions + ;; + (vms) + compadd -- $(vboxmanage list vms | awk '{print $1}' | xargs) + ;; + (snapshots) + local snapshot_list + [[ $line[1] =~ 'snapdel|snaprestore' ]] || return + snapshot_list=$(vboxmanage snapshot $line[2] list) + [[ $? -ne 0 ]] && return + compadd -- $(awk '{print $2}' <<<$snapshot_list) + ;; + esac +} + +_vb "$@" diff --git a/virtualbox.plugin.zsh b/virtualbox.plugin.zsh new file mode 100644 index 0000000..3b6606d --- /dev/null +++ b/virtualbox.plugin.zsh @@ -0,0 +1,24 @@ +vb() { + case $1 in + list) vboxmanage list vms | awk '{print $1}' | tr -d '"' | sort;; + start) vboxmanage startvm $2 --type headless;;; + gui) vboxmanage startvm $2 --type separate;; + stop) vboxmanage controlvm $2 acpipowerbutton || vboxmanage controlvm $2 poweroff;; + suspend) vboxmanage controlvm $2 savestate;; + restart) vb stop $2; vb start $2;; + reset) vboxmanage controlvm $2 reset;; + status) + VMS=$2 + [[ $# -eq 1 ]] && VMS=$(vb list) + for VM in ${=VMS}; do + echo -n "$VM: " + vboxmanage list runningvms | grep -q $VM && echo "running" || echo "stopped" + done + ;; + snapshot) vboxmanage snapshot $2 take ${3:-"$(date +%s)-$2"};; + snaprestore) vboxmanage snapshot $2 restore ${3};; + snaplist) vboxmanage snapshot $2 list | cut -d: -f2-;; + snapdel) vboxmanage snapshot $2 delete $3;; + *) echo "unknown parameter: $1"; return 1;; + esac +}