VirtualBox

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 comment

After 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

  1. Install or Update VirtualBox:
  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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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 &
  13. To shut off the vm use this command.
    user@host:~$ VBoxManage controlvm vbuntu poweroff
  14. 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
  15. 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.

Share

VirtualBox 3.1 new feature: Snapshots

Posted in Ubuntu, VirtualBox on December 1st, 2009 by alex – Be the first to comment

A new feature in VirtualBox 3.1 is the ability to take a snapshot of a VM.
Each snapshot saves the state of the VM and you’re able to go back to it, just like version control.
This means that if you go back to a snapshot, you will lose all the changes you made after the snapshot.
Snapshots can be taken while the VM is running, so you can take a snapshot before installing a new program to play around with.
If something goes wrong with the installation, just start the snapshot and you’re back in the game.
A snapshot does not create a separate VM, so you’re still using the original VM, just modifying what’s on it.
Remember that if you go back to a snapshot, all the changes you made since taking the snapshot will be lost, so before going back, take another snapshot of the current state.
Now for the practice part.

The scenario is this: you have a fresh install of ubuntu on a VM called vbuntu.
You want to install something on it, but want to create a snapshot in case something goes wrong.

Look at the previous posts to see how to create the initial VM.
Once that is done, this is how you create a snapshot of the original VM called vbuntu, and call the snapshot initial_install

user@server:~$ VBoxManage snapshot vbuntu take initial_install

Then you do your install. If something goes wrong, you want to go back to the initial_install snapshot, here’s how to

user@server:~$ VBoxManage snapshot vbuntu restore initial_install

If your install goes well and want to create another snapshot before you install another program, you should take another snapshot, like above.

For more information about VirtualBox 3.1 snapshots check out the VirtualBox site.

Share

Running a virtual machine in virtualbox 3.0 from the command line

Posted in Linux, Ubuntu, VirtualBox on July 12th, 2009 by alex – Be the first to comment

VirtualBox has a great user interface but sometimes it’s just not necessary, especially if you want to create/start/stop virtual machines remotely or if you are just running a server without a windowing system.
Luckily VBox allows you to do everything from the command line interface (cli) that you can do from the user interface and it also allows you to remote desktop connect to the virtual machine.
In this post I’ll show you how to install VirtualBox on an ubuntu linux server, then set up and run a virtual machine from the cli.

Some terms in this post:
host – machine on which VirtualBox runs, your “real” server
guest – virtual machine running in VirtualBox, your virtual server

  1. Install VirtualBox:
  2. Create the virtual machine and register it with VBox. The name of the vm is vbuntu.
    user@host:~$ VBoxManage createvm -name vbuntu -register
  3. Change the boot adapter to the virtual dvd drive so we can boot the OS install disk.
    user@host:~$ VBoxManage modifyvm vbuntu --boot1 dvd
  4. 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 createvdi -filename "vbuntu.vdi" -size 30000 -register
  5. Link the vdi to the vm, here hda is the hdd off which the vm boots.
    user@host:~$ VBoxManage modifyvm vbuntu -hda vbuntu.vdi
  6. Register the install disk image of ubuntu server with VBox. Then mount the install disk on your vm.
    user@host:~$ VBoxManage registerimage dvd /path/to/ubuntu-9.04-server-amd64.iso
    user@host:~$ VBoxManage modifyvm vbuntu -dvd /path/to/ubuntu-9.04-server-amd64.iso
  7. 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
  8. 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
  9. 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
  10. 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 &
  11. To shut off the vm use this command.
    user@host:~$ VBoxManage controlvm vbuntu poweroff
  12. Once done with the install, you can unregister the install disk and have the vm boot from its own hdd.
    user@host:~$ VBoxManage modifyvm vbuntu -dvd none
    user@host:~$ VBoxManage unregisterimage dvd /path/to/ubuntu-9.04-server-amd64.iso
    user@host:~$ VBoxManage modifyvm vbuntu -boot1 hda
  13. 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 --hda 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.

Share