810-cfg-autostart
#!/bin/bash
# Initial setup of configuring VM autostart
#
if [ "$UID" -ne 0 ]
  then echo "This script should be run as root."
  exit 1
fi
#
# Source config file
if [ ! -f ~/.vbconfig ]; then
    echo "Config File ~/.vbconfig not found!"
    echo "Run: set-folders to create .vbconfig"
    exit 1
fi
. ~/.vbconfig
# Get VM User
read -e -p "Enter VM User : " -i "$SUDO_USER" VB_USER
#
CNT_RUNNING=`su - $VB_USER -c "VBoxManage list runningvms|wc -l"`
if [ $CNT_RUNNING -gt 0 ]; then
  echo
  echo "Caution: You have VMs running."
  read -p "Press [Enter] key to continue or Ctrl-C to break ..."
fi
#
AUTOSTART_DIR="/etc/default"
AUTOSTARTDB_DIR="/etc/vbox"
AUTOSHUTDOWN_TYPE=savestate
AUTOSTART_CFG="$AUTOSTART_DIR/virtualbox"
VBOXGRP=vboxusers
#
mkdir -p $AUTOSTART_DIR
if [ ! -f $AUTOSTART_CFG ]; then
  echo "VBOXAUTOSTART_DB=$AUTOSTARTDB_DIR
VBOXAUTOSTART_CONFIG=$AUTOSTARTDB_DIR/autostart.cfg
SHUTDOWN=$AUTOSHUTDOWN_TYPE
SHUTDOWN_USERS=$VB_USER" > $AUTOSTART_CFG
fi
#
# Source config file variables
. $AUTOSTART_CFG
# Recheck variables and set missing values
if [[ $VBOXAUTOSTART_DB == "" ]]; then
  echo "VBOXAUTOSTART_DB=$AUTOSTARTDB_DIR" >> $AUTOSTART_CFG
fi
if [[ $VBOXAUTOSTART_CONFIG == "" ]]; then
  echo "VBOXAUTOSTART_CONFIG=$AUTOSTARTDB_DIR/autostart.cfg" >> $AUTOSTART_CFG
fi
if [[ $SHUTDOWN == "" ]]; then
  echo "SHUTDOWN=$AUTOSHUTDOWN_TYPE" >> $AUTOSTART_CFG
fi
if [[ $SHUTDOWN_USERS == "" ]]; then
  echo "SHUTDOWN_USERS=$VB_USER" >> $AUTOSTART_CFG
fi
# Source config file variables (again)
. $AUTOSTART_CFG
#
if [ ! -f $VBOXAUTOSTART_CONFIG ]; then
  echo "default_policy = allow" > $VBOXAUTOSTART_CONFIG
fi
#
echo "Setting permissions"
chgrp $VBOXGRP $VBOXAUTOSTART_DB
chmod 1775 $VBOXAUTOSTART_DB
usermod -aG $VBOXGRP $VB_USER
# This runs as $VB_USER
echo "Setting Auto start db path"
su - $VB_USER -c "VBoxManage setproperty autostartdbpath $VBOXAUTOSTART_DB"
 
# Reset run levels
# Remove
echo "Resetting run levels"
update-rc.d -f vboxdrv remove
update-rc.d -f virtualbox remove
update-rc.d -f vboxautostart-service remove
update-rc.d -f vboxweb-service remove
update-rc.d -f vboxballoonctrl-service remove
# Add
update-rc.d -f vboxdrv defaults 21 79
update-rc.d -f virtualbox defaults 22 78
update-rc.d -f vboxautostart-service defaults 91 09
update-rc.d -f vboxweb-service defaults 23 77
update-rc.d -f vboxballoonctrl-service defaults 23 77
#
echo "
Use toggle-autostart script to enable/disable autostart
on individual VMs
"
exit

QR Code
QR Code scripts:810-cfg-autostart (generated for current page)