Updated: Running a virtual machine in virtualbox 3.1 from the command line
Posted in Linux, Ubuntu, VirtualBox on February 2nd, 2010 by alex – Be the first to commentAfter upgrading to VirtualBox 3.1.2 I noticed some changes in the command line interface when it comes to creating and managing virtual machines. Because of that, I decided to update my previous post.
Some terms in this post:
host – machine on which VirtualBox runs, your “real” server
guest – virtual machine running in VirtualBox, your virtual server
- Install or Update VirtualBox:
- Download it from the site: http://www.virtualbox.org/wiki/Downloads
- For Debian based systems there are detailed instructions on the linux download page: http://www.virtualbox.org/wiki/Linux_Downloads
- For this post I’m using VirtualBox 3.1.2
- Create the virtual machine and register it with VBox. The name of the vm is vbuntu.
user@host:~$ VBoxManage createvm --name vbuntu --ostyle "Ubuntu" --register
- Create a SATA controller which we will use to control the disk image.
user@host:~$ VBoxManage storagectl vbuntu --name "SATA Controller" --add sata --controller IntelAhci
- Create and register the virtual disk image, basically the virtual disk from which the vm runs. Give it a filename and a size in MB, here my virtual disk image is name vbuntu.vdi and it has 30,000 MB.
user@host:~$ VBoxManage createhd --filename vdesktop.vdi --size 30000 --variant Standard
- Link the vdi to the vm’s SATA controller we previously created. Connect it to the first port and make it the first device, basically sda1
user@host:~$ VBoxManage storageattach vbuntu --storagectl "SATA Controller" --port 0 --device 0 --type hdd --medium vdesktop.vdi
- Create an IDE controller which we will use to control the virtual dvd drive from which we will install the OS.
user@host:~$ VBoxManage storagectl vbuntu --name "IDE" --add ide
- Register the install disk image of ubuntu server with VBox and mount the install disk on your vm.
user@host:~$ VBoxManage storageattach vbuntu --storagectl "IDE" --port 0 --device --0 --type dvddrive --medium /path/to/ubuntu-9.04-server-amd64.iso
- Modify the boot order for the virtual machine so it boots from the install dvd first.
user@host:~$ VBoxManage modifyvm vbuntu --boot1 dvd --boot2 disk
- Modify the default options for the vm. I gave the vm 1GB of ram, turned on ACPI for power control, turned on PAE (ubuntu server requires it, it enabled 32bit hosts to use more than 4GB of ram) and I also turned on Enable VT-x/AMD-V which are the technologies Intel and AMD processors use for hardware virtualization extensions. To use these extensions your host must have a processor supporting them and then enable them from the BIOS.
user@host:~$ VBoxManage modifyvm vbuntu --memory 1024MB --acpi on --pae on --hwvirtex on
- For networking I’m using bridged networking. First set the vm nic to bridged mode, then bridge it with one of the host’s nic. Here I bridged it over my host’s eth1 nic.
user@host:~$ VBoxManage modifyvm vbuntu --nic1 bridged user@host:~$ VBoxManage modifyvm vbuntu --bridgeadapter1 eth1
- I also changed the port for the remote desktop protocol to 33891 because I’m running multiple vm’s, so each vm has its own port.
user@host:~$ VBoxManage modifyvm vbuntu --vrdpport 33891
- Start the vm in the background, so remember to add the ampersand at the end of the command. After that you can RDP into the vm by connecting to the IP of your host system on the above set port. Now you can install the guest OS.
user@host:~$ VBoxHeadless -startvm vbuntu &
- To shut off the vm use this command.
user@host:~$ VBoxManage controlvm vbuntu poweroff - Once done with the install, you can unregister the install disk and have the vm boot from its own hdd.
user@host:~$ VBoxManage storageattach vbuntu --storagectl "IDE Controller" --port 0 --device 0 --medium none user@host:~$ VBoxManage modifyvm vbuntu --boot1 disk --boot2 none
- Optionally here are the steps to get rid of the vm and its disk image if you don’t need it anymore.
user@host:~$ VBoxManage modifyvm vbuntu --boot1 none user@host:~$ VBoxManage unregistervm --delete vbuntu user@host:~$ VBoxManage unregisterimage disk "vbuntu.vdi"
Let me know if you have any suggestions or comments as to the process I just described.