Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically download Ubuntu 15.10 iso and creates a virtual machine with VirtualBox to install it.
# !/bin/env bash
name="Ubuntu15.10-auto"
image="http://releases.ubuntu.com/15.10/ubuntu-15.10-desktop-amd64.iso"
# To show list of os types available, type "VBoxManage list ostypes"
ostype="Ubuntu_64"
cpu="2"
memory="2048"
cpuexecutioncap="80"
image_basename=`basename $image`
#image_basename_no_ext="${image_basename%.*}"
# Checks that virtual machine does not already exist
exists=`VBoxManage list vms | grep -c "$name"`
if [ "$exists" = "1" ]
then
echo "$name already exists"
exit 1
fi
# Downloads Ubuntu 15.10
outputname="$HOME/Downloads/$image_basename"
if [ -e "$outputname" ]
then
echo "Skipping download, $outputname already available on local machine"
else
curl -L $image -o ~/Downloads/$image_basename
fi
# Creates VM
VBoxManage createvm --name $name --register --ostype $ostype
# Create Hard drive
# If we don't manually get the machine folder, VBoxManage will create the virtual disk in the current working directory
machinefolder="$(VBoxManage list systemproperties | sed -n 's/Default machine folder: *//p')"
machinefolder_sed=`echo $machinefolder | sed "s/ /\\\ /g"`
# Make sure it is not already in the virtual media manager
exists=`VBoxManage list hdds| grep -c "$machinefolder_sed/$name/$name.vdi"`
if [ "$exists" = "1" ]
then
echo "Virtual hard drive already exists. Remove it first (using the Virtual Media Manager)."
exit 3
fi
# 25GB * 1024 = 25600MB
VBoxManage createhd --filename "$machinefolder/$name/$name.vdi" --size 25600 --variant Standard
# Create IDE controller to attach a virtual CD
VBoxManage storagectl $name --name "IDE Controller" --add ide --controller PIIX4 --bootable on
# Attach ISO of OS to install
VBoxManage storageattach $name --storagectl "IDE Controller" --port 0 --device 0 --type dvddrive --medium ~/Downloads/$image_basename
# Create SATA controller for hard drive
VBoxManage storagectl $name --name "SATA Controller" --add sata --controller IntelAhci --bootable on
# Attach virtual disk to SATA controller
VBoxManage storageattach $name --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium "$machinefolder/$name/$name.vdi"
# setup networking
# Public network vboxnet0 (10.1.0.0/16)
#VBoxManage hostonlyif create
#VBoxManage hostonlyif ipconfig vboxnet0 --ip 10.1.0.254 --netmask 255.255.0.0
#VBoxManage modifyvm "$name" --nic1 hostonly --hostonlyadapter2 vboxnet0
#VBoxManage modifyvm "$name" --nic2 nat
# VirtualBox CPU and Memory
VBoxManage modifyvm "$name" --cpus $cpu --cpuexecutioncap $cpuexecutioncap --memory $memory
# Miscellaneous
VBoxManage modifyvm "$name" --clipboard bidirectional
VBoxManage modifyvm "$name" --draganddrop bidirectional
VBoxManage modifyvm "$name" --vram 32
# Start VM
#VBoxManage startvm $name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment