A very appreciated feature of VirtualBox is the possibility to be used in a headless environment, without a GUI.

So, today I want to share the workflow I use for creating virtual machines using only the command line.



1. Create the VM

This command uses as base folder the current directory:

VBoxManage createvm --name [MACHINE NAME] --ostype [Os Type, ex: "Debian_64"] --register --basefolder `pwd` 

2. Set memory and network

In this example: 1Gb RAM and 1 network card behind NAT.

VBoxManage modifyvm [MACHINE NAME] --ioapic on                     
VBoxManage modifyvm [MACHINE NAME] --memory 1024 --vram 128       
VBoxManage modifyvm [MACHINE NAME] --nic1 nat 

3. Create the Disk and connect the CD ISO

One 80Gb SATA HD, and one CDROM (with Debian ISO) drive attached to an IDE Controller.

VBoxManage createhd --filename `pwd`/[MACHINENAME]/[MACHINE NAME]_DISK.vdi --size 80000 --format VDI                     
VBoxManage storagectl [MACHINE NAME] --name "SATA Controller" --add sata --controller IntelAhci       
VBoxManage storageattach [MACHINE NAME] --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium  `pwd`/[MACHINE NAME]/[MACHINE NAME]_DISK.vdi                
VBoxManage storagectl [MACHINE NAME] --name "IDE Controller" --add ide --controller PIIX4       
VBoxManage storageattach [MACHINE NAME] --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium `pwd`/debian.iso       
VBoxManage modifyvm [MACHINE NAME] --boot1 dvd --boot2 disk --boot3 none --boot4 none 

4. Set RDP access and start the VM

Remote Desktop enabled on port 10001 (useful for administration on an headless environment).

VBoxManage modifyvm [MACHINE NAME] --vrde on                  
VBoxManage modifyvm [MACHINE NAME] --vrdemulticon on --vrdeport 10001

VBoxHeadless --startvm [MACHINE NAME] 


Script example: automatically create a Debian VM

Here a script that i usually use to deploy a simple Debian VM, automatically download the Debian NetInst ISO.

It's a really base and readable code that can be simply customized:



Usage:

root@Lucille:VM# ./createdebian.sh TestDebian
 Virtual machine 'TestDebian' is created and registered.
 UUID: 953074b7-0e7e-4d34-82e4-f187cf8fa1f7
 Settings file: '/mnt/data/VM/TestDebian/TestDebian.vbox'
 0%…10%…20%…30%…40%…50%…60%…70%…80%…90%…100%
 Medium created. UUID: dd5d2efd-205d-4d9e-b464-d82cb792698c
 Oracle VM VirtualBox Headless Interface 5.1.38
  (C) 2008-2018 Oracle Corporation
  All rights reserved.
  VRDE server is listening on port 10001.


References