Sunday, January 30, 2011

Creating KVM Guests using virsh tool

An example XML file to install an operating system from an ISO image The following example XML file can be modified to create a KVM and boot to any operating system from an ISO image or a CD-ROM drive.

<domain type='kvm'>
   <name>kvm1</name>
   <uuid>a1e40383-2dfb-47f7-9cf6-589fc9679aa7</uuid>
   <memory>524288</memory>
   <currentMemory>524288</currentMemory>
   <vcpu>1</vcpu>
   <os>
     <type arch='x86_64' machine='pc'>hvm</type>
     <boot dev='cdrom'/>
 </os>
 <features>
    <acpi/>
    <apic/>
     <pae/>
 </features>
 <clock offset='localtime'/>
 <on_poweroff>destroy</on_poweroff>
 <on_reboot>restart</on_reboot>
 <on_crash>restart</on_crash>
 <devices>
     <emulator>/usr/libexec/qemu-kvm</emulator>
     <disk type='file' device='disk'>
       <source file='/var/lib/libvirt/images/kvm1.img'/>
       <target dev='hda' bus='ide'/>
     </disk>
    <disk type='file' device='cdrom'>
     <source file='/home/askarali/.bb/Fedora-14-x86_64-Live/Fedora-14-x86_64-Live-Desktop.iso'/>
     <target dev='hdc' bus='ide'/>
     <readonly/>
     </disk>
     <interface type='network'>
      <mac address='54:52:00:2a:58:0d'/>
      <source network='default'/>
    </interface>
     <input type='mouse' bus='ps2'/>
     <graphics type='vnc' port='-1' autoport='yes' keymap='en-us'/>
 </devices>
 </domain>

Edit the following before proceeding
a) Each guest needs a universal unique identifier (uuid), You must generate one for your guest by running the following command, and then copy and paste the identifier into the XML file <uuid>HERE</uuid>

# uuidgen

b) Replace <memory>524288</memory> and <currentMemory>524288</currentMemory> tag to memory you want to allocate to your guest.

c) Replace <mac address='54:52:00:2a:58:0d'/> with the mac address, To generate unique mac address, get the python script from http://www.centos.org/docs/5/html/5.2/Virtualization/sect-Virtualization-Tips_and_tricks-Generating_a_new_unique_MAC_address.html

Creating Storage Image file
# qemu-img create -f qcow2 /var/lib/libvirt/images/kvm1.img 10G

Using virsh to create a KVM
1. Define your KVM by running:
# virsh define kvm1.xml

2. Start the KVM so that the installation of its operating system can begin:
# virsh start kvm1

If your KVM does not start, complete one of the following to redefine the KVM:

# virsh edit <Name of KVM>

After editing the definition file, try to start guest vm again

# virsh start vmName

Tips for installing your guest operating system (This should be run on mother host

To Connect from your PC/laptop

1. install virt-viewer
2. Upload your ssh public key to KVM host /root/.ssh/authorized_keys (For this you should allow root login in /etc/ssh/sshd_config)

From your laptop, run

$ virt-viewer -c qemu+ssh://root@KVMHostIP/system vmName

To connect KVMs from the Host

To connect using the virt-viewer tool, run the following command:
# virt-viewer vmName

To connect using the virt-manager tool, run the following command:
# virt-manager vmName

Editing the KVM definition file after operating system installation

To boot to the installed guest after the guest operating system installation is complete, revise the guest
definition so that it will boot from its hard disk drive.

# virsh edit vmName

1.  Replace <boot dev='cdrom'/> line with <boot dev='hd'/> (To boot from hard disk)
2.  and remove the ISO disk definitions are removed because they are no longer needed.

Propagating your KVMs
For example, if your task is to create 10 identical KVMs that have simple configurations, you can propagate existing KVMs rather than manually installing 10 identical KVMs.

1. Stop the template KVM with the virsh tool:

# virsh shutdown templateKVM

2. Make copies of the KVM disk image by using the qemu-image command with the convert option:

# qemu-img convert templateKVM.img -O qcow2 NewKVM1.img

3. Make copies of the templateKVM XML definition file. You can find it in the /etc/libvirt/qemu/directory or you can run the following command to see the definition file.

# virsh dumpxml templateKVM

4. Edit the definition files for name, memory, mac address, source file etc to define the new KVMs. It is important that a unique MAC address is used in the definition file to make sure that the network functions without a problem.

Networking in Bridge Mode

1. Configure the bridge interface on host operating system
2. replace <interface type='network'> section in kvm.xml with the following

<interface type='bridge'>
      <mac address='00:16:3e:56:40:ad'/>
      <source bridge='br0'/>
</interface>

3. Install the guest OS, after installation configure the IP on guest.

Auto start KVM VMs on Host system boot

The following command will mark the VM for auto start on system reboot

# virsh autostart vmName

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.

Wednesday, January 12, 2011

VirtualBox VBoxHeadless, the remote desktop server

Which produces no visible output on the host at all, but instead only delivers VRDP data. This front-end has no dependencies on the X Window system on Linux and Solaris hosts, this is usefull on servers where the only purpose is to run VMs and access it remotely.

Prerequisite:
On Virtualbox 4.0 you must have extension pack installed.

To start a virtual machine with VBoxHeadless, you have two options:

VBoxManage startvm "VM name" --type headless

The extra --type option causes VirtualBox to use VBoxHeadless as the front-end to the internal virtualization engine instead of the Qt front-end.

The alternative is to use VBoxHeadless directly, as follows:

VBoxHeadless --startvm

This way of starting the VM is preferred because you can see more detailed error messages, especially for early failures before the VM execution is started. If you have trouble with VBoxManage startvm, it can help greatly to start VBoxHeadless directly to diagnose the problem cause.

Note that when you use VBoxHeadless to start a VM, since the headless server has no other means of output, the VRDP (VirtualBox Remote Desktop Protocol) server will always be enabled, regardless of whether you had enabled the VRDP server in the VM's settings

Accessing the VM remotely:

On Linux host you can test the Remote display of VM using rdesktop, or rdesktop-vrdp which is installed with VirtualBox

rdesktop -a 16 -N 1.2.3.4:3339

As said for the Microsoft viewer above, replace "1.2.3.4" with the host IP address, and 3389 with a different port if necessary. The -a 16 option requests a color depth of 16 bits per pixel, which we recommend. (For best performance, after installation of the guest operating system, you should set its display color depth to the same value). The -N option enables use of the NumPad keys.

rdesktop-vrdp 127.0.0.2:3339

When connecting to localhost in order to test the connection, the addresses localhost and 127.0.0.1 might not work. Instead, the address 127.0.0.2[:3389] has to be used.

On Windows, you can use the Microsoft Terminal Services Connector (mstsc.exe) that ships with Windows. You can start it by bringing up the "Run" dialog (press the Windows key and "R") and typing "mstsc". You can also find it under "Start" -> "All Programs" -> "Accessories" -> "Remote Desktop Connection". If you use the "Run" dialog, you can type in options directly:

mstsc 1.2.3.4[:3389]

VirtualBox Remote display (VRDP support)

VirtualBox can display virtual machines remotely, meaning that a virtual machine can execute on one machine even though the machine will be displayed on a second computer, and the machine will be controlled from there as well, as if the virtual machine was running on that second computer.

Prerequisites:
VirtualBox Extentions Pack

You can also check my eariier post for how to install VirtualBox and extention pack.

Enabling VRDP

Even when the extension is installed, the VRDP server is disabled by default. It can easily be enabled on a per-VM basis either in the VirtualBox Manager in the "Display" settings or with VBoxManage:

 VBoxManage modifyvm "VM name" --vrde on

By default, VRDP uses TCP port 3389. You will need to change the default port if you run more than one VRDP server, since the port can only be used by one server at a time; you might also need to change it on Windows hosts since the default port might already be used by the RDP server that is built into Windows itself. Ports 5000 through 5050 are typically not used and might be a good choice.

Accessing the VM remotely:

On Linux host you can test the Remote display of VM using rdesktop, or rdesktop-vrdp which is installed with VirtualBox

rdesktop -a 16 -N 1.2.3.4:3339

As said for the Microsoft viewer above, replace "1.2.3.4" with the host IP address, and 3389 with a different port if necessary. The -a 16 option requests a color depth of 16 bits per pixel, which we recommend. (For best performance, after installation of the guest operating system, you should set its display color depth to the same value). The -N option enables use of the NumPad keys.

rdesktop-vrdp 127.0.0.2:3339

When connecting to localhost in order to test the connection, the addresses localhost and 127.0.0.1 might not work using mstsc.exe. Instead, the address 127.0.0.2[:3389] has to be used.


On Windows, you can use the Microsoft Terminal Services Connector (mstsc.exe) that ships with Windows. You can start it by bringing up the "Run" dialog (press the Windows key and "R") and typing "mstsc". You can also find it under "Start" -> "All Programs" -> "Accessories" -> "Remote Desktop Connection". If you use the "Run" dialog, you can type in options directly:
mstsc 1.2.3.4[:3389]

Tuesday, January 11, 2011

Installing VirtualBox 4.0 on Fedora 13

Oracle recently released VirtualBox 4.0, a new major release. It now has an open architecture using extension packs and the base package is under GPLv2 and lots of new cool features. So its worth to install the VB 4.0

This article assume that you have already older version of VirtualBox installed along with all the dependencies.

1.       First install the DKMS package
$ sudo yum install dkms

2.       Download the VB 4.0 for Fedora 13 64bit

3.       Uninstall the older version of VB (make sure no virtual machine is running)
$ rpm -qa | grep VirtualBox <- this will give you the package name of installed VB
$ sudo rpm –e PackageName (replace package name with the name got from earlier command)

4.       Install VirtualBox 4.0
sudo rpm –Uvh VirtualBox-4.0-4.0.0_69151_fedora13-1.x86_64.rpm

Download and install the extension pack as follows:

$ wget http://download.virtualbox.org/virtualbox/4.0.0/Oracle_VM_VirtualBox_Extension_Pack-4.0.0-69151.vbox-extpack

$ sudo VBoxManage extpack install Oracle_VM_VirtualBox_Extension_Pack-4.0.0-69151.vbox-extpack