722-add-disk
#!/bin/bash
#
# Script to add an aditional disk to a VM
#
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
#
if [ $# -ne 1 ]; then
  echo "Usage for $0 : VM Name required as argument."
  exit 1
fi
#
# Virtual Machine Name
VB_VM_NAME=$1
#
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
#
VM_POWERED_OFF=`VBoxManage showvminfo ${VB_VM_NAME}|\
 grep State|grep -c "powered off"`
if [ $VM_POWERED_OFF -eq 0 ]; then
  echo "VM $VB_VM_NAME is up or saved. Please power-down VM to continue"
  echo "Exiting ..."
  exit 1
fi
#
# Determine Name of SATA Controller
# This script not tested with multiple controllers!
VB_VM_SATA=`VBoxManage showvminfo ${VB_VM_NAME}| \
grep "Storage Controller Name"|cut -d":" -f2|sed 's/^[ ]*//'`
#
echo "
List of attached disks below ...
"
VBoxManage showvminfo ${VB_VM_NAME}|grep "${VB_VM_SATA}"
 
echo
read -e -p "Enter the new disk size (GB) :" -i "100" VB_VM_DISK_SIZE_GB
echo "Note: Disk name is prefixed with ${VB_VM_DISK}_"
read -e -p "Enter the new disk name (VDI):" -i "extradata" VB_VM_DISK_NAME
#
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:
"
SATAC=$VB_VM_SATA
read -e -p "Sata Controller : " -i "$SATAC" SATAC
read -e -p "Sata Port #     : " -i "$SATAP" SATAP
read -e -p "Sata Device #   : " -i "$SATAD" SATAD
#
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
#
# Disk related variables to create disk
VB_VM_DISK="disk" # Prefix .vdi files
VB_VM_PATH=${VB_VMS_LOC}/${VB_VM_NAME}
# Disk Sizes - Adjust as required, sizes are in MB
VB_VM_DISK_SIZE=$(echo "$VB_VM_DISK_SIZE_GB * 1000"|bc)
#
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_${VB_VM_DISK_NAME}.vdi \
  --size $VB_VM_DISK_SIZE
#
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "${SATAC}" --port $SATAP --device $SATAD \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_${VB_VM_DISK_NAME}.vdi
#
echo
echo "Disk created and attached to VM.
Next step will start VM.  Safe to break out of script at this point.
"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo "
Starting VM in headless mode.
Login to configure new disk for use by O/S.
"
VBoxManage startvm ${VB_VM_NAME} --type headless
#
echo "Done!"
exit

QR Code
QR Code scripts:722-add-disk (generated for current page)