Thursday, January 13, 2011

VirtualBox 4.0: Creating a Virtual Machine on a headless server

The following instructions may give you an idea how to create a virtual machine on a headless server over a network connection. We will create a virtual machine, establish an RDP connection and install a guest operating system -- all without having to touch the headless server. All you need is the following:

1. VirtualBox on a server machine with a supported host operating system. The VirtualBox extension pack for the VRDP server must be installed For the following example, we will assume a Linux server.

2. An ISO file accessible from the server, containing the installation data for the guest operating system to install (we will assume Windows XP in the following example).

3. A terminal connection to that host through which you can access a command line (e.g. via ssh).

4. An RDP viewer on the remote client rdesktop, rdesktop-vrdp, Microsoft mstsc

Note again that on the server machine, since we will only use the headless server, neither Qt nor SDL nor the X Window system will be needed.

1. On the headless server, create a new virtual machine:

VBoxManage createvm --name "Windows XP" --ostype WindowsXP --register

Note that if you do not specify --register, you will have to manually use the registervm command later.

Note further that you do not need to specify --ostype, but doing so selects some sane default values for certain VM parameters, for example the RAM size and the type of the virtual network device. To get a complete list of supported operating systems you can use

VBoxManage list ostypes

2. Make sure the settings for this VM are appropriate for the guest operating system that we will install. For example:


VBoxManage modifyvm "Windows XP" --memory 256 --acpi on --boot1 dvd --nic1 nat

3. Create a virtual hard disk for the VM (in this case, 10GB in size):


VBoxManage createhd --filename "WinXP.vdi" --size 10000

4. Add an IDE Controller to the new VM:

VBoxManage storagectl "Windows XP" --name "IDE Controller" --add ide --controller PIIX4

5. Set the VDI file created above as the first virtual hard disk of the new VM:

VBoxManage storageattach "Windows XP" --storagectl "IDE Controller" --port 0 --device 0 --type hdd --medium "WinXP.vdi"

6. Attach the ISO file that contains the operating system installation that you want to install later to the virtual machine, so the machine can boot from it:


VBoxManage storageattach "Windows XP" --storagectl "IDE Controller" --port 0 --device 1 --type dvddrive --medium /full/path/to/iso.iso

7. Start the virtual machine using VBoxHeadless:


VBoxHeadless --startvm "Windows XP"

If everything worked, you should see a copyright notice. If, instead, you are returned to the command line, then something went wrong.
  
8. On the client machine, fire up the RDP viewer and try to connect to the server (see the section called “Common third-party RDP viewers” above for how to use various common RDP viewers).

You should now be seeing the installation routine of your guest operating system remotely in the RDP viewer.

To stop a VM
VBoxManage controlvm "Windows XP" poweroff

 To pause a VM
 VBoxManage controlvm "Windows XP" pause

Allow multi connections:
VBoxManage modifyvm "Windows XP" --vrdemulticon on

Listen on different port
VBoxHeadless --startvm "Windows XP" --vrdeproperty "TCP/Ports"=5001


Installing Guest Addition on Headless VM

1. First attach the Guest Addition .iso to VM (vm should be poweroff)

VBoxManage storageattach "Windows XP" --storagectl "IDE Controller" --port 1 --device 0 --type dvddrive --medium /usr/share/virtualbox/VBoxGuestAdditions.iso

2. Start VM and now you can explorer and install the guest addition.

6 comments:

  1. Great man, just what i was looking for,

    Thanks for Sharing

    ReplyDelete
  2. Using virtual machines, we can deploy our test platform in minutes, not hours, and feel confident they are safe and ready for use. In fact, we can achieve many of these ready to go to the operating environments with us when we travel to high-capacity external disk systems. Normally, the size of a paperback book regularly.

    ReplyDelete
  3. Thanks mate, excellent instructions. Really easy to follow and they worked a treat.

    ReplyDelete