Skip to content

Instantly share code, notes, and snippets.

@lloydroc
Last active August 23, 2016 14:09
Show Gist options
  • Save lloydroc/8470d2c8bce42d996ed5561475205462 to your computer and use it in GitHub Desktop.
Save lloydroc/8470d2c8bce42d996ed5561475205462 to your computer and use it in GitHub Desktop.
Script to create virtual machines using the VirtualBox commands on the cli
# Useful commands
# VBoxManage list vms
# VBoxManage showmediuminfo avm/dvd/dsl-4.4.10-x86.vdi
# VBoxManage list hostonlyifs
# VBoxManage list intnets
# VBoxManage list ostypes | more
# VBoxManage showvminfo f-1
# VBoxManage list hostdvds
if [ ! -f ~/.vbconfig ]; then
echo "Config file ~/.vbconfig not found"
echo "Run vbconfig.sh file"
exit 1
fi
. ~/.vbconfig
# Check the DVD File is there
#VB_VM_VDI=dsl-4.4.10-x86.vdi
VB_VM_VDI=ubuntu-16.04.1-desktop-amd64.iso
VDI_LOC=${VB_DVD_LOC}/${VB_VM_VDI}
if [ ! -f $VDI_LOC ]; then
echo "DVD file $VDI_LOC does not exist ... checking downloads"
if [ -f ${VB_DLS_LOC}/${VB_VM_VDI} ]; then
echo "Found in downloads ... copying"
cp ${VB_DLS_LOC}/${VB_VM_VDI} $VB_DVD_LOC
else
echo "Couldn't find file ... exiting"
exit 1
fi
fi
VB_VM_NAME=v3
echo "Creating VM $VB_VM_NAME"
VBoxManage createvm --name "${VB_VM_NAME}" --register
if [ $? ]; then
echo "Powering down VM"
VBoxManage controlvm "${VB_VM_NAME}" poweroff
echo "Unregistering VM $VB_VM_NAME"
# --delete will also remove the files
VBoxManage unregistervm "${VB_VM_NAME}" --delete
echo "Re-registering VM $VB_VM_NAME"
VBoxManage createvm --name "${VB_VM_NAME}" --register
fi
# See types with VBoxManage list ostypes
#VBoxManage modifyvm "${VB_VM_NAME}" --ostype Linux24 --memory 128 --acpi on --ioapic on --cpus 1 --boot1 disk --nic1 bridged --bridgeadapter1 en0 --vrde on
VBoxManage modifyvm "${VB_VM_NAME}" --ostype Ubuntu_64 --memory 512 --acpi on --ioapic on --cpus 1 --boot1 disk --vrde on
VBoxManage modifyvm "${VB_VM_NAME}" --nic1 nat
VBoxManage modifyvm "${VB_VM_NAME}" --natpf1 "guestssh,tcp,,2222,,22"
VBoxManage modifyvm "${VB_VM_NAME}" --nic2 hostonly --hostonlyadapter2 vboxnet0
VBoxManage storagectl "${VB_VM_NAME}" --name "SATAController" --add sata --bootable on --portcount 1 --hostiocache off
VBoxManage storagectl "${VB_VM_NAME}" --name "IDEController" --add ide --bootable on --portcount 2 --hostiocache off --controller PIIX4
if [ ! $? ]; then
echo "Unable to create SATAController ... exiting"
exit 1
fi
HD=${VB_VMS_LOC}/${VB_VM_NAME}/${VB_VM_NAME}.vdi
if [ -f $HD ]; then
VBoxManage unregistervm "${VB_VM_NAME}" --delete
echo "HD $HD already exists ... deleting VM and exiting"
exit 1
fi
VBoxManage createhd --filename ${VB_VMS_LOC}/${VB_VM_NAME}/${VB_VM_NAME}.vdi --size 12000
if [ ! $? ]; then
echo "Unable to create HD ... exiting"
exit 1
fi
VBoxManage storageattach "${VB_VM_NAME}" --storagectl "IDEController" --port 0 --device 0 --medium $VDI_LOC --type dvddrive
VBoxManage storageattach "${VB_VM_NAME}" --storagectl "SATAController" --port 0 --device 0 --medium $HD --type hdd
#VBoxManage modifyvm "${VB_VM_NAME}" --nictype1 virtio
#VBoxManage modifyvm "${VB_VM_NAME}" --nic1 hostonly --hostonlyadapter1 vboxnet0
VBoxManage startvm "${VB_VM_NAME}" --type headless
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment