Setting up virtual machines with CentOS, KVM, Socat and RealVNC

Jephe Wu - http://linuxtechres.blogspot.com

Objective: use open source softwares to set up a virutal Windows XP machine inside Linux with excellent remote access performance
Environment: Dell OptiPlex 780 as host server running CentOS 5.5 64bit with 8G RAM, both KVM and Xen virtualzation installed, socat, realvnc, Windows XP virtual machine with bridged network br0 and Qumranet paravirtualized ethernet adapter, as well as RedHat VirtIO SCSI Disk Device driver.


Concept:
1. use CentOS 5.5 with KVM instead of Xen as KVM has the future. 
You don't have to change anything after inital KVM setup to enable bridged network, For NATed Windows machine, you can use 'socat' for tcp port forwarder on the VNC ports to make inside Windows XP virtual machine accessable to outside world, but vnc response speed is quite slow.

To gain better network, hard disk and graphic drawing speed, you can:
2. use realvnc server on virtual Windows XP SP3 machine instead of KVM builtin vnc server for better response speed, so called VNC mirror driver
3. use the latest virtio-win ISO package to install qumranet paravirtualized ethernet adapter driver instead of the default realtek network adapter for better network performance
4. use bridged network interface instead of the default NAT for Windows virtual machine for even better network performance and easier configuration, this requires some configuration after initial setup.
5. use RedHat VirtIO SCSI Disk Device driver instead of the default IDE driver for better block device performance.

Steps
1. Installation of CentOS 5.5
Download CentOS 5.5 DVD and burn it for local installation, or use NFS network installation if you are using DVD iso file.

Choose KVM and Gnome during installation.

If you have only CentOS 5 lower version, after install, run 'yum update' to upgrade to the latest.

2. KVM configuration
If you have installed both KVM and Xen, server will boot from Xen kernel, you will need to vi /etc/grub.conf to change it to boot from the plain Linux kernel which has KVM support.

By default, when you configure KVM virtual machine, it use NATed network only because it doesn't have bridged network for you to use. After that, the localhost will be listening at port 5900 for vnc connection, but it cannot be connected remotely since it's only listening at localhost.

You can use 'socat' (http://www.dest-unreach.org/socat/) as proxy to forward the vnc port by following the steps below:

# Install DAG rpmforge package
# rpm -Uhv http://apt.sw.be/redhat/el5/en/x86_64/rpmforge/RPMS//rpmforge-release0.3.6-1.el5.rf.x86_64.rpm

# yum install socat

# add local2 to /etc/syslog.conf
[root@jephe ~]# grep socat /etc/syslog.conf
local2.*                        /var/log/socat.log

# socat -d -d -lmlocal2 tcp4-listen:5900,bind=10.0.0.1,su=nobody,reuseaddr,fork tcp4:127.0.0.1:5900 &
note:
This will enable inside KVM virtual machine to accept connection from network.
a. log will be recorded in local2 which is /var/log/socat.log according to /etc/syslog.conf
b. listening on 10.0.0.1:5900 (host server tcp socket instead of the KVM builtin one which is localhost:5900)
c. su as nobody
d. receiving vnc connection from network then forward to inside Windows XP SP3 virtual machine at 192.168.100.230:5900

You can put above socat command into /etc/rc.d/rc.local.

Issues:
With above configuration, the VNC response speed is very slow. You can improve it by using realvnc or tightvnc on virtual machine itself and change network adapter to bridged mode as well as using Redhat VirtIO ethernet adapter and RedHat VirtIO SCSI Disk Device driver.

3. Installing Windows KVM guest
a. When installing Windows 7, use 'Windows vista' option. When you choose 'local CDROM install' option, the installation path part might become grey, you can check if you enabled 'messagebus' daemon and 'haldeamon' daemon:

service autofs start
service messagebus start
service haldaemon start

So that the auto mount will work under gnome desktop, check 'ls -l /dev/cdrom' which should be symbolic linked to /dev/scd0.

4. Improving performance  - RealVNC with mirror driver
installing realvnc or tightvnc latest version (both are supporting so-called mirror driver)
realvnc server free edition can enable bi-directional cut and paste but there's no password protection for configuration change once login
tightvnc has password protection for console admin but has no support for pasting out text from virtual machine, only one way pasting.

After installing realvnc or tightvnc, you can use socat like this:

# socat -d -d -lmlocal2 tcp4-listen:5900,bind=10.0.0.1,su=nobody,reuseaddr,fork tcp4:192.168.100.230:5900 &

5. Improving performance - Bridged network


Refer to http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/5/html/Virtualization/sect-Virtualization-Network_Configuration-Bridged_networking_with_libvirt.html

Here are my steps according to above RHEL doc website:

vi /etc/xen/xend-config.sxp and changing the line:

 (network-script network-bridge)

To:

 (network-script /bin/true)

# chkconfig NetworkManager off
# service NetworkManager stop

# cd /etc/sysconfig/network-scripts
Add the following line to ifcfg-eth0 which will be used for bridge.
BRIDGE=br0
MTU=9000

add the following lines to ifcfg-br0
[root@jephe network-scripts]# more ifcfg-br0
DEVICE=br0
TYPE=Bridge
BOOTPROTO=static
ONBOOT=yes
DELAY=0
BROADCAST=10.0.0.255
IPADDR=10.0.0.1
NETMASK=255.255.255.0
NETWORK=10.0.0.0

After configuring, restart networking or reboot.

# service network restart

Configure iptables to allow all traffic to be forwarded across the bridge.

# iptables -I FORWARD -m physdev --physdev-is-bridged -j ACCEPT
# service iptables save
# service iptables restart

 Restart the libvirt daemon.

# service libvirtd reload

Exit virt-manger, then start it again. Delete storage device hda , then add again, choose 'virtio' type. You should now have a "shared physical device", which guests can be attached and have full LAN access. Verify your new bridge:

# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.000000000000       yes
br0             8000.000e0cb30550       no              eth0

Note, the bridge is completely independent of the virbr0 bridge. Do not attempt to attach a physical device to virbr0. The virbr0 bridge is only for Network Address Translation (NAT) connectivity.

6. Improving performance - Virtio ethernet adapter driver
download the latest Windows guest network drivers from http://www.linux-kvm.com/  (left side of web page, under 'Windows Guest Drivers' - download the driver CD.

mounted as local cdrom image in virt-manager before rebooting Windows virtual machine.
After booting up, it found the new ethernet adapter, then I used cdrom to install driver.

After that, shutdown virtual machine, delete the existing network card in virt-manager, then add one more network card with virtio type, now you should be able to see bridged network br0 option

7. Improving performance - Windows block driver (RedHat VirtIO SCSI Disk Device driver)
In my case, I use Windows XP SP3 32bit as virtual machine, so I have done the following:
Visiting website http://www.linux-kvm.com/ , at the left side to download the latest Windows guest drivers CD or floppy (during installation)

and check
http://www.linux-kvm.com/content/block-driver-updates-install-drivers-during-windows-installation

Important: You cannot just delete hda then create vda, then startup guest, you need to add a small vda storage first without deleting the main hda, after that, boot into guest to install Redhat virtio scsi disk driver, after that, shutdown, delete vda and hda, then create vda to use the existing image.
otherwise, it will not be able to boot Windows. Here are the steps:


Shutdown virtual machine, use virt-manager to add one more storage device ( use dd if=/dev/zero of=/root/testfile bs=1k count=1000 to create a small file first as vd0 just for installing virtio disk driver)


After booting up virtual machine again, Windows XP detected a new scsi hard disk, then use above driver inside the cdrom to install it.


Shutdown again, use virt-manager to delete vd0 and hd0, after that ,create another storage device, use the existing hard disk image file, boot up again, this time, hard disk will become RedHat VirtIO SCSI Disk Device driver

Now, use vnc client to connect to Windows virtual machine and enjoy the fast speed.

Let's say you want to use floppy disk version after initial installation of virtual machine, you can use dd if=viostor-floppy.img of=/dev/fd0 to write to floppy disk. You also can mount the floppy img file as loop device as follows:

mkdir /mnt/viostor
cd /mnt/viostor
mount viostor-31-03-2010-floppy.img viostor/


8. Using virtio driver to improve network and disk performance when installing RHEL 5.4 guest

How can I improve network and disk performance of Red Hat Enterprise Linux KVM guests?- https://access.redhat.com/kb/docs/DOC-18756 

and

How do I set up virtio on a kvm guest to get better network and I/O performance on RHEL4 and RHEL5? - https://access.redhat.com/kb/docs/DOC-25441

If using the virt-manager graphical tool to install a new guest, select following options on the OS type selection page to enable virtio network and block device drivers for this new guest:

OS type: Linux
Version: Generic 2.6.25 or later kernel with virtio


9. High Resolution Graphic on KVM Windows guest 
For Windows 7 pro guest in KVM, you can use advanced mode option to choose monitor model up to resolution 1280x1024.



http://www.linux-kvm.com/content/using-high-resolution-graphics
http://www.linux-kvm.com/content/using-vmware-vga-kvm-windows-guests




10. How to clone the existing KVM guest
cd /var/lib/libvirt/images
virt-clone --original guest1 --name newguest1 --file newguest1.img

11. See Also
a. http://www.linux-kvm.org/page/Main_Page
b. http://virtualization.info/en/
c. http://www.linux-kvm.com/

d. KVM virtio network drivers

http://www.linux-kvm.com/content/tip-how-setup-windows-guest-paravirtual-network-drivers
http://www.linux-kvm.com/content/latest-release-windows-virtio-network-drivers
http://sourceforge.net/projects/kvm/files/kvm-driver-disc/

e. KVM Windows block drivers

http://www.linux-kvm.com/content/redhat-54-windows-virtio-drivers-part-2-block-drivers
http://www.linux-kvm.com/content/block-driver-updates-install-drivers-during-windows-installation