031-create-win-vm
#!/bin/bash
#
# Create a Windows 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=2048
VB_VM_VRAM=128
VB_VM_DISK_SIZE_GB=100
VB_VM_NAME="Win1"
VB_VM_TYPE="Windows7_64"
VB_VM_VRDEPORT=5000
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="82540EM"
VB_VM_PISS="PostInstall" # Post Install Snap Shot Name
 
VB_VM_GUESTADD_ISO="VBoxGuestAdditions_4.3.20.iso"
#
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_CDRIVE_SIZE=$VB_VM_DISK_SIZE
#
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"
echo "The Windows install DVD should be loaded in the Host DVD drive"
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
# Modify audio
VBoxManage modifyvm "${VB_VM_NAME}" --audio alsa --audiocontroller hda
# 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}_cdrive.vdi \
  --size $VB_VM_DISK_CDRIVE_SIZE
# Define storage controller
VBoxManage storagectl ${VB_VM_NAME} --name "SATAController" \
 --add sata --bootable on --portcount 4 --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}_cdrive.vdi
# Attach the host DVD to Guest
# Use the "VBoxManage list hostdvds" command to list host dvd
# HARD-CODED below are /dev/sr0
VBoxManage storageattach ${VB_VM_NAME} \
 --storagectl "SATAController" --port 0 --device 0 \
 --type dvddrive --medium host:/dev/sr0
#
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
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"
echo "Shutdown VM at this point"
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 "Start VM again"
echo "Please login after VM starts"
read -p "Press [Enter] key to continue or [Ctrl-C] to break ..."
echo
#
echo "Proceeding to install Guest Additions ...
 
Use the file explorer to run the VBoxWindowsAdditions executable
"
 
echo "Waiting to complete Guest Additions Install"
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 "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:031-create-win-vm (generated for current page)