vbox (4933B)
1 #!/bin/sh 2 die() { 3 echo "$1" >&2 4 exit 1 5 } 6 command -v VBoxManage >/dev/null 2>&1 || die "VBoxManage cannot be found, please install it." 7 command -v gstat >/dev/null 2>&1 && statcmd="gstat" 8 start() { 9 [ $# -eq 1 ] || die "Only one argument: VM name" 10 VBoxManage startvm --type headless "$1" 11 } 12 13 startgui() { 14 [ $# -eq 1 ] || die "Only one argument: VM name" 15 VBoxManage startvm --type gui "$1" 16 } 17 18 stop_vm() { 19 [ $# -eq 1 ] || die "Only one argument: VM name" 20 VBoxManage controlvm "$1" acpipowerbutton 21 } 22 23 open() { 24 [ $# -eq 1 ] || die "Only one argument: VM name" 25 VBoxManage startvm --type separate "$1" 26 } 27 28 pause() { 29 [ $# -eq 1 ] || die "Only one argument: VM name" 30 VBoxManage controlvm "$1" pause 31 } 32 suspend_vm() { 33 [ $# -eq 1 ] || die "Only one argument: VM name" 34 VBoxManage controlvm "$1" savestate 35 } 36 resume() { 37 [ $# -eq 1 ] || die "Only one argument: VM name" 38 vminfo="$(VBoxManage showvminfo "$1")" 39 if [ "$(printf '%s' "$vminfo" | awk '/^State/ { print $2 }')" = "saved" ]; then 40 snapshots="$(printf '%s' "$vminfo" | awk '/^Snapshot folder/ { print $3 }')" 41 snapfile="$(find "$snapshots" -name "*.sav" -exec "${statcmd:-stat}" -c "%y %n" {} + | sort -r | head -n1 | cut -d " " -f 4-)" 42 43 [ -n "$snapfile" ] \ 44 && echo "VM suspended, resuming from saved state..." \ 45 && VBoxManage startvm "$1" --type headless 46 else 47 echo "VM paused, resuming..." 48 VBoxManage controlvm "$1" resume 49 fi 50 } 51 list() { 52 VBoxManage list vms 53 } 54 55 running() { 56 VBoxManage list runningvms 57 } 58 59 info() { 60 VBoxManage showvminfo "$1" 61 } 62 63 status() { 64 VBoxManage showvminfo "$1" | awk -F' +' '/^State/ { print $2 }' 65 } 66 67 # share /folder/path vmname /mount/point 68 share() { 69 [ $# -eq 3 ] || die "Not enough arguments" 70 [ -d "$1" ] || die "$1 is not a directory or does not exist" 71 if ! VBoxManage sharedfolder add "$2" --name "$(basename "$(realpath "$1")")" --hostpath "$(realpath "$1")" --automount --auto-mount-point "$3"; then 72 echo "Could not add shared folder, machine is probably running." 73 echo "Stop the machine to add a permanent folder, or use sharetmp to add a transient folder." 74 fi 75 } 76 77 unshare() { 78 [ $# -eq 2 ] || die "Not enough arguments" 79 [ -d "$1" ] || die "$1 is not a directory or does not exist" 80 VBoxManage sharedfolder remove "$2" --name "$(basename "$(realpath "$1")")" --transient 81 } 82 83 sharetmp() { 84 [ $# -eq 3 ] || die "Not enough arguments" 85 [ -d "$1" ] || die "$1 is not a directory or does not exist" 86 VBoxManage sharedfolder add "$2" --name "$(basename "$(realpath "$1")")" --hostpath "$(realpath "$1")" --automount --auto-mount-point "$3" --transient 87 } 88 89 shared() { 90 [ $# -eq 1 ] || die "Not enough arguments" 91 VBoxManage showvminfo "$1" | grep '^Name: .*Host path: ' 92 } 93 PARAMS="" 94 while [ $(($#)) -ne 0 ]; do 95 case "$1" in 96 -h|--help) 97 echo "Usage:" 98 echo "start vmname start a VM" 99 echo "stop vmname stop a VM" 100 echo "open vmname open a VM, starting it if necessary" 101 echo "list, ls list VMs" 102 echo "running list running VMs" 103 echo "share /local/path vmname /mount/point share a local folder" 104 echo "unshare /local/path vmname unshare a local folder" 105 echo "sharetmp /local/path vmname /mount/point temporarily share a local folder" 106 echo "shared vmname list shared folders" 107 echo "pause vmname pause a running VM" 108 echo "resume vmname resume a paused VM" 109 echo "info vmname get information about a VM" 110 echo "status vmname print a VM's status" 111 exit 0 112 ;; 113 --) # end arg parsing 114 shift 115 break 116 ;; 117 -*) # unsupported flags 118 echo "Unsupported flag $1" >&2 119 exit 1 120 ;; 121 *) # preserve positional arguments 122 PARAMS="$PARAMS $1" 123 shift 124 ;; 125 esac 126 done 127 eval set -- "$PARAMS" 128 129 [ $# -ge 1 ] || die "Not enough arguments provided." 130 131 case "$1" in 132 "start") 133 shift; 134 start "$@"; 135 ;; 136 "startgui") 137 shift; 138 startgui "$@"; 139 ;; 140 "stop") 141 shift; 142 stop_vm "$@"; 143 ;; 144 "open") 145 shift; 146 open "$@"; 147 ;; 148 "ls"|"list") 149 list; 150 ;; 151 "running") 152 running; 153 ;; 154 "share") 155 shift; 156 share "$@"; 157 ;; 158 "unshare") 159 shift; 160 unshare "$@"; 161 ;; 162 "sharetmp") 163 shift; 164 sharetmp "$@"; 165 ;; 166 "shared") 167 shift; 168 shared "$@"; 169 ;; 170 "info") 171 shift; 172 info "$@"; 173 ;; 174 "status") 175 shift; 176 status "$@"; 177 ;; 178 "pause") 179 shift; 180 pause "$@"; 181 ;; 182 "suspend") 183 shift; 184 suspend_vm "$@"; 185 ;; 186 "resume") 187 shift; 188 resume "$@"; 189 ;; 190 *) 191 die "Unsupported command $1"; 192 ;; 193 esac