035-upgrade-ga
#!/bin/bash
#
# Install/Upgrade GA on Guest
#
if [ "$UID" -eq 0 ]
  then echo "This script should NOT 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
#
# Guest Additions fallback Snapshot name
GASS=gass
#
# Sata Controller Name
SATAC="SATAController"
# Sata Controller Port
SATAP="0"
# Sata Controller Device
SATAD="0"
# Check if extension pack has been installed
VB_EXT_PACK_COUNT=`VBoxManage list extpacks | head -1 | cut -d":" -f2`
if [ $VB_EXT_PACK_COUNT -eq 0 ]
  then echo "Please install Extension Pack before proceeding"
  exit 1
fi
#
VB_VM_NAME=$1
if [[ $VB_VM_NAME == "" ]]; then
  echo "Install/Upgrade Guest Additions (on Guest
 
Choose the VirtualMachine from the list below:
"
  ${VB_BIN_LOC}/050-list-vm.bash
  read -p "Enter VM Name: " VB_VM_NAME
fi
#
VM_EXISTS=`$VB_BIN_LOC/110-vm-exists.bash "${VB_VM_NAME}"`
if [ $VM_EXISTS -ne 1 ]; then
  echo "Invalid VM Name: $VB_VM_NAME"
  exit 1
fi
#
echo "
Full list of $SATAC :"
VBoxManage showvminfo "${VB_VM_NAME}" | grep "$SATAC" 
echo "
Choose a Controller/Port/Device that is not in use.
Avoid knocking off an existing device.
It is possible you may need to increase # of ports.
 
Enter the SATA Controller details for CD/DVD Device:
"
read -e -p "Sata Controller : " -i "$SATAC" SATAC
read -e -p "Sata Port #     : " -i "$SATAP" SATAP
read -e -p "Sata Device #   : " -i "$SATAD" SATAD
#
# May need to update this to the correct ISO
VB_VM_GUESTADD_ISO="VBoxGuestAdditions_4.3.20.iso"
VB_VM_GUESTADD_ISO="MirantisOpenStack-5.1.1.iso"
#
VB_VM_GUESTADD_ISO_LOC="${VB_DVD_LOC}/${VB_VM_GUESTADD_ISO}"
if [ ! -f $VB_VM_GUESTADD_ISO_LOC ]; then
    echo
    echo "Guest Addition ISO not found at: $VB_VM_GUESTADD_ISO_LOC"
    echo "Update variable VB_VM_GUESTADD_ISO and/or download Guest additions"
    echo "List of downloaded Guest Additions ..."
    ls -1 $VB_DVD_LOC/VBoxGuestAdditions*
    echo
    exit 1
fi
#
SATACPD="$SATAC (${SATAP}, ${SATAD})"
SATACPD1="$SATAC[ ]*(${SATAP},[ ]*${SATAD}):"
INUSE=`VBoxManage showvminfo "${VB_VM_NAME}" | grep -c "$SATACPD1"`
 
if [ $INUSE -gt 0 ]; then
    echo "SATA: $SATACPD is already in use"
    echo
    read -e -p "Override (y/n) : " -i "n" SATAOV
    if [[ $SATAOV == "y" || $SATAOV == "Y" ]]; then
        echo "Overriding ..."
    else
      exit 1
    fi
fi
 
echo "Attaching media ${VB_VM_GUESTADD_ISO} on ${VB_VM_NAME}
"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
VM_RUNNING=`VBoxManage showvminfo ${VB_VM_NAME}|\
 grep State|grep -c "running"`
if [ $VM_RUNNING -eq 1 ]; then
  echo "
  VM $VB_VM_NAME is running ...
  Please powerdown VM before proceeding.
 
  Exiting ...
  "
  exit 1
fi
#
#echo "Taking safety snapshot before installing/upgrading GA ..."
#VBoxManage snapshot "${VB_VM_NAME}" take $GASS
#
echo "Attaching Guest Additions Media ..."
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "${SATAC}" --port $SATAP --device $SATAD \
 --type dvddrive --medium $VB_VM_GUESTADD_ISO_LOC
#
echo "
VM VRDE Port details (if any):"
VBoxManage showvminfo ${VB_VM_NAME}|grep "VRDE property: TCP/Ports"
echo "
Powering on VM ..."
VBoxManage startvm "${VB_VM_NAME}" --type headless
echo "Pausing for VM to fully power on ..."
echo "Proceed once VM is powered-on and you have logged into VM"
#echo "Note: If unaware of Guest IP, wait until full power-on and continue."
#read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#echo "
#Guest IP (in case you did not already know it):"
#VBoxManage guestcontrol "${VB_VM_NAME}" exec --image /sbin/ifconfig \
#  --username $VB_VM_GUEST_USER --password $VB_VM_GUEST_PASS \
#  --wait-stdout | grep "inet addr"
echo
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
echo "
Proceed to install Guest Additions:
Run the following commands first (optional) on Guest (as root) and
to upgrade OS and install DKMS."
echo "
aptitude update
aptitude upgrade
#aptitude dist-upgrade # May be required sometimes
reboot # if required
aptitude -y install dkms
#
#Waiting to complete upgrades and dkms install on guest
"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Install (Linux) Guest additions with these steps as root:"
echo "mount /dev/cdrom /mnt
cd /mnt
ls -l
sh ./VBoxLinuxAdditions.run
cd
umount /mnt"
echo
echo "Note: You can safely ignore the below error"
echo "-- Could not find the X.Org or XFree86 Window System, skipping. --"
echo
echo "Waiting to complete Guest Additions Install"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
echo "Please shutdown VM"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo "Waiting for complete shutdown"
echo "Sleeping 30 seconds ... 
Depending on your system it make take longer to shutdown your VM"
sleep 30
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Removing Guest Additions Media ..."
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "${SATAC}" --port $SATAP --device $SATAD \
 --type dvddrive --medium none
#
echo "Powering on VM ..."
VBoxManage startvm "${VB_VM_NAME}" --type headless
echo "
     Once VM is powered on optionally verify Guest Additions install.
     Run the following two commands to verify (root not required):
     1. lsmod | grep -i vboxguest
        Should return vboxguest ...
     2. modinfo vboxguest
        Should return detailed info including GA version
     "
#
echo "Done"
exit