030-create-vm
#!/bin/bash
#
# Create the VM and install the Guest OS
#
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
#
# 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
#
# Update New VM parameters below
#
VB_VM_CPU=1
VB_VM_MEM=512
VB_VM_VRAM=1 # Just 1M (default) for console
VB_VM_DISK_SIZE_GB=100
VB_VM_NAME="Trusty"
VB_VM_TYPE="Ubuntu_64"
VB_VM_VRDEPORT=4000
VB_VM_HOST_NET_ADAPTER="eth0"
# Define adapter type as paravirtualized virtio
# Most Linux flavors have virtio drivers
# Choose NIC Type as "82540EM" for Windows or download from KVM project
VB_VM_NIC_TYPE="virtio"
VB_VM_PISS="PostInstall" # Post Install Snap Shot Name
VB_VM_INSTALL_ISO="ubuntu-14.04-server-amd64.iso"
VB_VM_GUESTADD_ISO="VBoxGuestAdditions_4.3.14.iso"
#
VB_VM_INSTALL_ISO_LOC="${VB_DVD_LOC}/${VB_VM_INSTALL_ISO}"
if [ ! -f $VB_VM_INSTALL_ISO_LOC ]; then
    echo "Install ISO not found at: $VB_VM_INSTALL_ISO_LOC"
    exit 1
fi
VB_VM_GUESTADD_ISO_LOC="${VB_DVD_LOC}/${VB_VM_GUESTADD_ISO}"
if [ ! -f $VB_VM_GUESTADD_ISO_LOC ]; then
    echo "Guest Addition ISO not found at: $VB_VM_GUESTADD_ISO_LOC"
    exit 1
fi
#
# Disk related variables
#
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)
VB_VM_DISK_BOOT_SIZE=$VB_VM_DISK_SIZE
VB_VM_DISK_ROOT_SIZE=$VB_VM_DISK_SIZE
VB_VM_DISK_VAR_SIZE=$VB_VM_DISK_SIZE
VB_VM_DISK_HOME_SIZE=$VB_VM_DISK_SIZE
VB_VM_DISK_DATA_SIZE=$VB_VM_DISK_SIZE
VB_VM_DISK_TMP_SIZE="12000"
VB_VM_DISK_SWAP_SIZE="4096"
#
# This is to query the VM Name (hardcoded) from inside the VM
VB_VM_GUEST_VM_CMD="/usr/local/bin/vmname"
#
echo "Starting VM Creation ..."
echo "Review key parameters before proceeding ..."
echo "CPU            : $VB_VM_CPU"
echo "Disk Size (GB) : $VB_VM_DISK_SIZE_GB"
echo "Memory (MB)    : $VB_VM_MEM"
echo "Video RAM (MB) : $VB_VM_VRAM"
echo "New VM Name    : $VB_VM_NAME"
echo "New VM Type    : $VB_VM_TYPE"
echo "VM VRDE Port   : $VB_VM_VRDEPORT"
echo "Host Adapter   : $VB_VM_HOST_NET_ADAPTER"
echo
echo "Review parameters to create VM"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
NOW=$(date +"%Y-%m-%d-%H-%M-%S")
VBoxManage createvm --name ${VB_VM_NAME} --register
# Set setextradata on when the VM was created for documentation
VBoxManage setextradata ${VB_VM_NAME} CreateDateTime $NOW
#
echo
echo "Modifying VM with $VB_VM_CPU CPU, $VB_VM_MEM MB Memory, etc."
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
# Set ostype, memory, cpu and video memory
VBoxManage modifyvm ${VB_VM_NAME} --ostype ${VB_VM_TYPE} \
  --memory ${VB_VM_MEM} --cpus ${VB_VM_CPU} --vram ${VB_VM_VRAM}
# Turn on acpi and ioapic
VBoxManage modifyvm ${VB_VM_NAME} --acpi on --ioapic on
# Set initial boot device as DVD
VBoxManage modifyvm ${VB_VM_NAME} --boot1 dvd
# Create a Bridged Adapter - this goes to eth0
VM_NIC=1
VBoxManage modifyvm ${VB_VM_NAME} --nic${VM_NIC} bridged \
  --bridgeadapter${VM_NIC} ${VB_VM_HOST_NET_ADAPTER}
# Set the default Network hardware
VBoxManage modifyvm "${VB_VM_NAME}" --nictype${VM_NIC} $VB_VM_NIC_TYPE
# Enable Remote Desktop of said port
VBoxManage modifyvm ${VB_VM_NAME} --vrde on --vrdeport ${VB_VM_VRDEPORT}
#
echo
echo "Adding Disk Controllers and Disk/DVD"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
# Create Hard Disks of given size
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_boot.vdi \
  --size $VB_VM_DISK_BOOT_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_root.vdi \
  --size $VB_VM_DISK_ROOT_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_var.vdi \
  --size $VB_VM_DISK_VAR_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_home.vdi \
  --size $VB_VM_DISK_HOME_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_data.vdi \
  --size $VB_VM_DISK_DATA_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_tmp.vdi \
  --size $VB_VM_DISK_TMP_SIZE
VBoxManage createhd --filename $VB_VM_PATH/${VB_VM_DISK}_swap.vdi \
  --size $VB_VM_DISK_SWAP_SIZE
# Create a SATA Controller with eight ports
VBoxManage storagectl ${VB_VM_NAME} --name "SATAController" \
 --add sata --bootable on --portcount 8 --hostiocache off
# Attach harddisk to available ports
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 1 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_boot.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 2 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_root.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 3 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_var.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 4 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_home.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 5 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_data.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 6 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_tmp.vdi
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 7 --device 0 \
 --type hdd --medium $VB_VM_PATH/${VB_VM_DISK}_swap.vdi
# Use Port 0 for Optical Drive, attach iso media
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 0 --device 0 \
 --type dvddrive --medium $VB_VM_INSTALL_ISO_LOC
#
echo
echo "VM created and configured. Starting VM in headless mode"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
VBoxManage startvm ${VB_VM_NAME} --type headless
#
echo
echo "VM Ready for O/S Install.  
Complete O/S Install and come back to the script BEFORE reboot"
echo " - Only install SSH server at this time"
echo " - Partition Disks to ONLY what is required - not to the full disk size"
echo
echo "Terminal RDP Info: `hostname`:${VB_VM_VRDEPORT}"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
echo
echo "About to remove install Media. If install is completed then proceed"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
#
echo "Replacing installation media with Guest Additions Media ..."
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 0 --device 0 \
 --type dvddrive --medium $VB_VM_GUESTADD_ISO_LOC
#
# Optionally save guest credentials on the host configuration
# This can be useful when many VMs are getting created
# Remember to update if cloning VM and changing users/passwords
read -p "Enter Guest Host Name: " GUESTHOSTNAME
VBoxManage setextradata ${VB_VM_NAME} GuestHostName $GUESTHOSTNAME
echo "Caustion: Security Risk!!!  Enter details below optionally"
read -p "Enter Guest User/Login id: " GUESTUSER
read -p "Enter Guest User password: " GUESTPASS
VBoxManage setextradata ${VB_VM_NAME} GuestUser $GUESTUSER
VBoxManage setextradata ${VB_VM_NAME} GuestPass $GUESTPASS
#
echo
echo "Let install Reboot system"
echo "Please login after reboot"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Proceeding to install Guest Additions ...
Run the following commands first on Guest 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:"
echo "mount /dev/cdrom /mnt
cd /mnt
ls -l
sh ./VBoxLinuxAdditions.run
cd
umount /mnt"
echo "Waiting to complete Guest Additions Install"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Save VM Name ($VB_VM_NAME) inside Guest for reference
This can be queried with Command: ${VB_VM_GUEST_VM_CMD} inside guest
Run the following (as root) in Guest:"
echo "
#
echo '#!/bin/bash' > ${VB_VM_GUEST_VM_CMD}
echo 'echo $VB_VM_NAME' >> ${VB_VM_GUEST_VM_CMD}
chmod 755 ${VB_VM_GUEST_VM_CMD}
#
"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Poweroff Guest at this time for remaing tasks including a Snapshot"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
echo "Changing boot to disk ..."
VBoxManage modifyvm ${VB_VM_NAME} --boot1 disk
echo "Removing Guest Additions Media ..."
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 0 --device 0 \
 --type dvddrive --medium none
#
echo
echo "Modifying Guest Additions time synchronization parameters options"
# Synchronize the time with the host every 60 seconds (Default 10 seconds)
VBoxManage guestproperty set "${VB_VM_NAME}" \
 "/VirtualBox/GuestAdd/VBoxService/--timesync-interval" 60000
# Adjust in drift increments of 1 second (Default 100 milliseconds)
VBoxManage guestproperty set "${VB_VM_NAME}" \
 "/VirtualBox/GuestAdd/VBoxService/--timesync-min-adjust" 1000
# Adjust if out of sync by more than 30 seconds (Default 20 minutes!)
VBoxManage guestproperty set "${VB_VM_NAME}" \
 "/VirtualBox/GuestAdd/VBoxService/--timesync-set-threshold" 30000
#
echo
echo "Taking Post Install snapshot of Guest with name $VB_VM_PISS"
VBoxManage snapshot ${VB_VM_NAME} take $VB_VM_PISS
#
echo
echo "Summary info for you VM saved in ExtraData ..."
VBoxManage getextradata $VB_VM_NAME enumerate
echo "For security reasons, feel free to delete some or all
of this user-defined info from the configuration."
echo
#
echo "Powering VM on ..."
VBoxManage startvm ${VB_VM_NAME} --type headless
echo
echo "All done. Enjoy your new VM!"
#
exit

QR Code
QR Code scripts:030-create-vm (generated for current page)