#!/bin/bash function fatal_error { echo "$1" exit $2 } ### MAIN ### Self=$(basename $0) ## Set defaults for memory (4Gb), disk size 350Gb, and Mac Address ### test -z "$Processors" && Processors=4 test -z "$Memory" && Memory=8192 test -z "$DiskSize" && DiskSize=400000 test -z "$MacAddress" && MacAddress=080027D13CED test -z "$NIC1" && NIC1=$(ip route get 8.8.8.8 | grep ^8.8.8.8 | cut -d' ' -f5) ### Check for the folder where virtual machines live called the "common folder" in the virtualBox docs $### test -z "$CommonFolder" && CommonFolder="${HOME}/VirtualBox VMs" if [ ! -d "$CommonFolder" ]; then Answer=yes if [ -z "$Force" ]; then echo -n "Common folder, $CommonFolder does not exist, create it? [default $Default]: " read Answer fi if [ "$Answer" == "Y" -o "$Answer" == "y" -o "$Answer" == "yes" ]; then echo "Creating common folder, $CommonFolder" mkdir "$CommonFolder" fi fi test ! -d "$CommonFolder" && fatal_error "Common folder, $CommonFolder, does not exist." echo "Common folder is $CommonFolder" ### First command line parameter is VMNAME, defaults to script name ### test -z "$VMNAME" && VMNAME=$1 if [ -z "$VMNAME" ]; then Default="$Self" if [ -z "$Force" ]; then echo -n "Virtual machine name [default $Default]: " read VMNAME fi test -z "$VMNAME" && VMNAME="$Default" fi echo "Virtual machine name is $VMNAME" ### Second command line parameter is location of boot image ### test ! -z "$2" && BootImage="$2" if [ -z "$BootImage" ]; then Default="${CommonFolder}/Files/${VMNAME}.iso" test ! -f "$Default" && Default=none if [ -z "$Force" ]; then echo -n "Boot image? [default $Default]: " read BootImage fi test -z "$BootImage" && BootImage="$Default" fi if [ "$BootImage" == "none" ]; then AnswerFile="none" else test ! -f "$BootImage" && fatal_error "Boot image, $BootImage, does not exist." fi echo "Boot image is $BootImage" ### Third command line parameter is location of answer file ### test ! -z "$3" && AnswerFile="$3" if [ -z "$AnswerFile" ]; then Default="${CommonFolder}/Files/${VMNAME}.xml" test ! -f "$Default" && Default=none if [ -z "$Force" ]; then echo -n "Answer file? [default $Default]: " read AnswerFile fi test -z "$AnswerFile" && AnswerFile="$Default" fi test "$AnswerFile" != "none" -a ! -f "$AnswerFile" && fatal_error "Answer file, $AnswerFile, does not exist." echo "Answer file is $AnswerFile" if [ "$AnswerFile" != "none" ]; then DisketteImage="${CommonFolder}/Files/${VMNAME}.flp" echo "Creating diskette image as $DisketteImage" TempFile="/tmp/${VMNAME}.flp" test -f "$TempFile" && rm -f "$TempFile" dd if=/dev/zero of="$TempFile" bs=512 count=2880 Label=$(echo "$VMNAME" | cut -b1-11) mkfs.vfat -n "$Label" "$TempFile" MountPoint=/tmp/diskette0 test ! -d "$MountPoint" && mkdir "$MountPoint" sudo mount -o loop "$TempFile" "$MountPoint" cp "$AnswerFile" /tmp/junk; sudo cp /tmp/junk "${MountPoint}/autounattend.xml"; rm /tmp/junk sudo umount "$MountPoint" cp "$TempFile" "$DisketteImage" fi ### Fourth command line parameter is OS type ### test ! -z "$4" && OSType="$4" if [ -z "$OSType" ]; then Okay=$(vboxmanage list ostypes | grep -ce "ID:\s*${VMNAME}") if [ $Okay -gt 0 ]; then Default="$VMNAME" else Default=None fi if [ -z "$Force" ]; then echo -n "OS type [default $Default]: " read OSType fi test -z "$OSType" && OSType="$Default" fi echo "OS type is $OSType" MachineFolder="${CommonFolder}/${VMNAME}" if [ -d "$MachineFolder" ]; then echo "Machine folder exists" Default=yes if [ -z "$Force" ]; then echo -n "Unregister existing vm? [default $Default]: " read Answer fi test -z "$Answer" && Answer="$Default" if [ "$Answer" == "Y" -o "$Answer" == "y" -o "$Answer" == "yes" ]; then vboxmanage unregistervm $VMNAME --delete rm -rf "$MachineFolder" fi fi # Create the vm vboxmanage createvm --name "$VMNAME" --ostype "$OSType" --register test ! -d "$MachineFolder" && fatal_error "Failed to create virtual machine, $VMNAME." #Create a dynamic disk. cd "$MachineFolder" vboxmanage createhd --filename $VMNAME.vdi --size "$DiskSize" # Add a SATA controller with the dynamic disk attached. vboxmanage storagectl $VMNAME --name "SATA Controller" --add sata --controller IntelAHCI vboxmanage storageattach $VMNAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium $VMNAME.vdi # Add an IDE controller anda DVD drive vboxmanage storagectl $VMNAME --name "IDE Controller" --add ide vboxmanage storageattach $VMNAME --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium "$BootImage" # If a diskette image with the answer file has been specified, add a floppy drive and insert the diskette if [ ! -z "$DisketteImage" ]; then vboxmanage storagectl $VMNAME --name "Floppy Controller" --add floppy vboxmanage storageattach $VMNAME --storagectl "Floppy Controller" --port 0 --device 0 --type fdd --medium "$DisketteImage" fi # Misc system settings. vboxmanage modifyvm $VMNAME --ioapic on vboxmanage modifyvm $VMNAME --hwvirtex on vboxmanage modifyvm $VMNAME --usb on vboxmanage modifyvm $VMNAME --boot1 dvd --boot2 disk --boot3 net --boot4 none vboxmanage modifyvm $VMNAME --cpus "$Processors" vboxmanage modifyvm $VMNAME --memory "$Memory" --vram 256 vboxmanage modifyvm $VMNAME --nic1 bridged --bridgeadapter1 "$NIC1" test ! -z "$MacAddress" && vboxmanage modifyvm $VMNAME --macaddress1 "$MacAddress" ### Add a sound card. Seems as if these setting change with ever virtualBox and/or Windows version. #vboxmanage modifyvm $VMNAME --audiocontroller sb16 --audiocodec sb16 #vboxmanage modifyvm $VMNAME --audio pulse --audiocontroller sb16 --audiocodec sb16 #vboxmanage modifyvm $VMNAME --audio alsa --audiocontroller ac97 #vboxmanage modifyvm $VMNAME --audio pulse --audiocontroller hda vboxmanage modifyvm $VMNAME --audio alsa --audiocontroller sb16 vboxmanage modifyvm $VMNAME --audioout on vboxmanage modifyvm $VMNAME --audioin on # Boot it up! If you’ve done this on a remote machine, you can RDP to the console via vboxhost:3389. vboxmanage startvm $VMNAME ### Some useful commands # VBoxHeadless -s $VMNAME # vboxmanage showvminfo $VMNAME #vboxmanage getextradata global GUI/Input/HostKeyCombination # vboxmanage setextradata global GUI/Input/HostKeyCombination 65300 #vboxmanage snapshot $VMNAME take snapshot`date +"%F"` --description "Snapshot, "`date` # ### # To mount the virtual disk: # sudo modprobe nbd max_part=8 #sudo qemu-nbd -c /dev/nbd0 "/home/john/VirtualBox VMs/$VMNAME/$VMNAME.vdi" # sudo mount /dev/nbd0p2 /mnt/vdi # ### # EOF