Skip to content

Instantly share code, notes, and snippets.

@andreafortuna
Created May 17, 2019 08:16
Show Gist options
  • Save andreafortuna/9c7e342dbff00edefbaebcbfd034193f to your computer and use it in GitHub Desktop.
Save andreafortuna/9c7e342dbff00edefbaebcbfd034193f to your computer and use it in GitHub Desktop.
Automatically create and start a Debian VM on VirtualBOX
#!/bin/bash
MACHINENAME=$1
# Download debian.iso
if [ ! -f ./debian.iso ]; then
wget https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.9.0-amd64-netinst.iso -O debian.iso
fi
#Create VM
VBoxManage createvm --name $MACHINENAME --ostype "Debian_64" --register --basefolder `pwd`
#Set memory and network
VBoxManage modifyvm $MACHINENAME --ioapic on
VBoxManage modifyvm $MACHINENAME --memory 1024 --vram 128
VBoxManage modifyvm $MACHINENAME --nic1 nat
#Create Disk and connect Debian Iso
VBoxManage createhd --filename `pwd`/$MACHINENAME/$MACHINENAME_DISK.vdi --size 80000 --format VDI
VBoxManage storagectl $MACHINENAME --name "SATA Controller" --add sata --controller IntelAhci
VBoxManage storageattach $MACHINENAME --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium `pwd`/$MACHINENAME/$MACHINENAME_DISK.vdi
VBoxManage storagectl $MACHINENAME --name "IDE Controller" --add ide --controller PIIX4
VBoxManage storageattach $MACHINENAME --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium `pwd`/debian.iso
VBoxManage modifyvm $MACHINENAME --boot1 dvd --boot2 disk --boot3 none --boot4 none
#Enable RDP
VBoxManage modifyvm $MACHINENAME --vrde on
VBoxManage modifyvm $MACHINENAME --vrdemulticon on --vrdeport 10001
#Start the VM
VBoxHeadless --startvm $MACHINENAME
@syreal17
Copy link

syreal17 commented Mar 2, 2021

This is a very handy script, thank you!

I had an issue with line 16... I think my shell (Focal Fossa bash) interpreted $MACHINENAME_DISK as a different, undefined bash var from $MACHINENAME. The resulting file created by vboxmanage was .vdi! This might have worked, as it was consistent with the rest of the script, but I'm unsure. I changed line 16 to:

VBoxManage createhd --filename `pwd`/$MACHINENAME/$MACHINENANE.vdi --size 80000 --format VDI

And subsequent references to the disk file to be consistent.

Cheers!
~~LT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment