dotfiles

My personal shell configs and stuff
git clone git://git.alex.balgavy.eu/dotfiles.git
Log | Files | Refs | Submodules | README | LICENSE

vbox (4648B)


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