Mudanças entre as edições de "Migrar XEN KVM"

De Slacam_Wiki
Ir para: navegação, pesquisa
Linha 1: Linha 1:
== Migrate Paravirtualized Xen to KVM under RHEL ==
+
== Migrate Paravirtualized Xen to KVM under RHEL ==
  
Posted by Roel Gloudemans on 9 July 2009 | 0 Comments
+
Posted by Roel Gloudemans on 9 July 2009 | 0 Comments  
  
Tags:
+
[http://www.gloudemans.info/migrate-paravirtualized-xen-to-kvm-under-rhel/ http://www.gloudemans.info/migrate-paravirtualized-xen-to-kvm-under-rhel/]
  
'''Update July 11, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Re-registering VMs at RHN uses an extra entitlement with RHEL5.4Beta<br>'''Update July 15, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Swap usage, clock and disk cache of the virtual machine<br>'''Update July 16, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Replace virsh create with virsh define &amp;amp; start to create a managed domain and not a transient one<br>'''Update September 2, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Re-registering with RHN works<br>'''Update September 2, 2009:'''<span class="Apple-converted-space">&nbsp;</span>RHEL5.4 has been released. Added a note about services on the physical host<br>'''Update September 6, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Updating TimeKeeping and Hugepages<br>'''Update April 1, 2010:'''<span class="Apple-converted-space">&nbsp;</span>Hugepages configuration for RHEL 5.5<br>'''Update July 3, 2010:'''<span class="Apple-converted-space">&nbsp;</span>Make Hugepages mountpoint persistent<br>RedHat Enterprise Linux version 5.4 is out. It heralds the arrival of KVM as RedHat's official hypervisor. RedHat will be supporting Xen for the rest of the RHEL5 life cycle, so for the moment, there is no need to migrate to KVM.
+
Tags:  
  
However migrating to KVM has some advantages. For one KVM looks simpler from the outside, another is that it works with a normal kernel, meaning that all drivers that work on a normal kernel work as well. This not only encompasses display drivers, but CPU scaling (dynamically adapting the speed of the CPU) as well. This is not only very "green" but makes a difference is your or the companies wallet as well.
+
'''Update July 11, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Re-registering VMs at RHN uses an extra entitlement with RHEL5.4Beta<br>'''Update July 15, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Swap usage, clock and disk cache of the virtual machine<br>'''Update July 16, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Replace virsh create with virsh define &amp;amp; start to create a managed domain and not a transient one<br>'''Update September 2, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Re-registering with RHN works<br>'''Update September 2, 2009:'''<span class="Apple-converted-space">&nbsp;</span>RHEL5.4 has been released. Added a note about services on the physical host<br>'''Update September 6, 2009:'''<span class="Apple-converted-space">&nbsp;</span>Updating TimeKeeping and Hugepages<br>'''Update April 1, 2010:'''<span class="Apple-converted-space">&nbsp;</span>Hugepages configuration for RHEL 5.5<br>'''Update July 3, 2010:'''<span class="Apple-converted-space">&nbsp;</span>Make Hugepages mountpoint persistent<br>RedHat Enterprise Linux version 5.4 is out. It heralds the arrival of KVM as RedHat's official hypervisor. RedHat will be supporting Xen for the rest of the RHEL5 life cycle, so for the moment, there is no need to migrate to KVM.  
  
RedHat put a lot of work into making Xen easier to manage in RHEL5.0-5.3. As a result Xen uses a single disk image from which it can boot. The format of this image is the same as for KVM. One would suspect that migrating from one Hypervisor to another would be easy and it is. This blog will describe a step-by-step scenario on how to do it.
+
However migrating to KVM has some advantages. For one KVM looks simpler from the outside, another is that it works with a normal kernel, meaning that all drivers that work on a normal kernel work as well. This not only encompasses display drivers, but CPU scaling (dynamically adapting the speed of the CPU) as well. This is not only very "green" but makes a difference is your or the companies wallet as well.  
  
The starting situation is a RHEL5.3 Physical host with RHEL5.3 paravirtualized guests. The guests have two networking interfaces, one bridged to the physical network interface, and one bridged to a dummy network interface for an internal host network. Note that the minimum requirement to run with virtio is RHEL5.3.
+
RedHat put a lot of work into making Xen easier to manage in RHEL5.0-5.3. As a result Xen uses a single disk image from which it can boot. The format of this image is the same as for KVM. One would suspect that migrating from one Hypervisor to another would be easy and it is. This blog will describe a step-by-step scenario on how to do it.  
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Note:</span>'''<br>I had some trouble with selinux in the rhel 5.4 beta. It is related to the attributes on /var/lib/libvirt. I do not use this directory to store the images, bit I use raw LVM volumes. To get my system running again, I just disabled selinux.
+
The starting situation is a RHEL5.3 Physical host with RHEL5.3 paravirtualized guests. The guests have two networking interfaces, one bridged to the physical network interface, and one bridged to a dummy network interface for an internal host network. Note that the minimum requirement to run with virtio is RHEL5.3.  
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Configure the virtio drivers</span>'''<br>Open<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/etc/modprobe.conf</span><span class="Apple-converted-space">&nbsp;</span>in the editor. In our case /etc/modprobe.conf contains the following lines:<br>
+
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Note:</span>'''<br>I had some trouble with selinux in the rhel 5.4 beta. It is related to the attributes on /var/lib/libvirt. I do not use this directory to store the images, bit I use raw LVM volumes. To get my system running again, I just disabled selinux.
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
alias eth0<span class="Apple-converted-space">&nbsp;</span>'''xennet'''<br>alias eth1<span class="Apple-converted-space">&nbsp;</span>'''xennet'''<br>alias scsi_hostadapter<span class="Apple-converted-space">&nbsp;</span>'''xenblk'''
+
  
''change it to''
+
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Configure the virtio drivers</span>'''<br>Open<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/etc/modprobe.conf</span><span class="Apple-converted-space">&nbsp;</span>in the editor. In our case /etc/modprobe.conf contains the following lines:<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
alias eth0<span class="Apple-converted-space">&nbsp;</span>'''xennet'''<br>alias eth1<span class="Apple-converted-space">&nbsp;</span>'''xennet'''<br>alias scsi_hostadapter<span class="Apple-converted-space">&nbsp;</span>'''xenblk'''  
  
alias eth0<span class="Apple-converted-space">&nbsp;</span>'''virtio_net'''<br>alias eth1<span class="Apple-converted-space">&nbsp;</span>'''virtio_net'''<br>alias scsi_hostadapter<span class="Apple-converted-space">&nbsp;</span>'''virtio_blk'''
+
''change it to''
 +
 
 +
alias eth0<span class="Apple-converted-space">&nbsp;</span>'''virtio_net'''<br>alias eth1<span class="Apple-converted-space">&nbsp;</span>'''virtio_net'''<br>alias scsi_hostadapter<span class="Apple-converted-space">&nbsp;</span>'''virtio_blk'''  
 +
</div>
 +
Now add the virtio drivers to the kernel boot image (modify this lane to mirror the latest kernel version)
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
mkinitrd -f --with=virtio_blk --with=virtio_pci --builtin=xenblk initrd-2.6.18-128.1.16.el5.img 2.6.18-128.1.16.el5<br>''The --builtin is necessary only when currently running under a xen kernel in paravirtualized mode''
 +
</div>
 +
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Internal clock</span>'''<br>The internal clock of KVM is less stable than the clock under Xen. Heavy loads have been know to cause clock drift. There are two workarounds:<br>
 +
 
 +
*Boot with<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">divider=10 notsc</span><span class="Apple-converted-space">&nbsp;</span>(see earlier) and start ntpd at boot (chkconfig --level 2345 ntpd on; configure ntp first)<br>
 +
*Use the -no-kvm-pit-reinjection option with qemu-kvm. One of the improvements added to the final version is that libvirt seems to add this option by default now, so everything should work out of the box. You still need to start ntp though.<br>
 +
 
 +
<br> Also see ( [https://bugzilla.redhat.com/show_bug.cgi?id=507834 https://bugzilla.redhat.com/show_bug.cgi?id=507834] )
 +
 
 +
'''Now shut down the virtual system (shutdown -h now)'''
 +
 
 +
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; font-size: 15pt;"><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">'''''Updating the host'''''</span></span><br>The physical host needs some updating as well. First, before you start, make sure all virtual systems are stopped (xm list) and that you are logged on as root. If RHEL5.4 is already released, yum will update the system automatically to this version. If now, the system needs to be subscribed to the RHEL5.4 beta channel. You can do this at<span class="Apple-converted-space">&nbsp;</span>[https://rhn.redhat.com/ RedHat network], if your system is subscribed to rhn. Also make sure the system has access to the Virtual Platform channel beta. Aside from the updates, some new packages need to be installed as well and all virtualization services must be disabled at boot time until we are ready with the configuration work.<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
yum clean all<span class="Apple-converted-space">&nbsp;</span>''#for safety''<br>yum update<br>yum install kernel kvm kvm-tools kmod-kvm kvm-qemu-img bridge-utils<br>chkconfig --level 2345 xend off<br>chkconfig --level 2345 xendomains off<br>chkconfig --level 2345 rhn-virtualization-host off
 +
</div>
 +
Edit<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/boot/grub/menu.lst</span><span class="Apple-converted-space">&nbsp;</span>and set the default boot kernel to the newest non-xen kernel (see example grub config)
 +
 
 +
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Network configuration</span>'''<br>By default only a network that is connected via NAT to the outside world is created. There are three options, leave it as is, but check that the IP range does not conflict with anything on the local network, change the IP range, or convert it to a host only network. I left the network, but adapted the IP range and created a new network for host-only networking. Be sure to change the uuid of the network. The format of the uuid should not change. Change any hex number [0-9|a-f] in the uuid string.<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
''/etc/libvirt/qemu/networks/default.xml''<br>&lt;network&gt;&lt;name&gt;default&lt;/name&gt;&lt;uuid&gt;cc06c2a2-0766-45ee-baaa-896e04c7a3be&lt;/uuid&gt;&lt;forward mode="nat"/&gt;&lt;bridge name="virbr0" stp="on" forwarddelay="0"/&gt;&lt;ip address="a.b.c.d" netmask="255.255.255.0"&gt;&lt;dhcp&gt;&lt;range start="a.b.c.e" end="a.b.c.f"/&gt;&lt;/dhcp&gt;&lt;/ip&gt;&lt;/network&gt;<br>''/etc/libvirt/qemu/networks/hostonly.xml''<br>&lt;network&gt;&lt;name&gt;hostonly&lt;/name&gt;&lt;uuid&gt;04255669-803e-d8f6-352a-086fa45ae09d&lt;/uuid&gt;&lt;bridge name="virbr1" stp="on" forwarddelay="0"/&gt;&lt;ip address="a.b.g.h" netmask="255.255.255.0"&gt;&lt;dhcp&gt;&lt;range start="a.b.g.i" end="a.b.g.j"/&gt;&lt;/dhcp&gt;&lt;/ip&gt;&lt;/network&gt;
 
</div>
 
</div>
Now add the virtio drivers to the kernel boot image (modify this lane to mirror the latest kernel version)
+
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
 
mkinitrd -f --with=virtio_blk --with=virtio_pci --builtin=xenblk initrd-2.6.18-128.1.16.el5.img 2.6.18-128.1.16.el5<br>''The --builtin is necessary only when currently running under a xen kernel in paravirtualized mode''
+
The host-only network should be started at boot, so <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">ln -s /etc/libvirt/qemu/networks/hostonly.xml /etc/libvirt/qemu/networks/autostart</span> . Note that his network will replace the network coupled to the dummy0 interface, so dummy0 should not start up after a reboot. To do this, move <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/etc/sysconfig/network-scripts/ifcfg-dummy0</span><span class="Apple-converted-space">&nbsp;</span> to a safe location, or edit it and change the ONBOOT option from "yes" to "no".
 +
 
 +
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Note:</span>'''<br>If you run any services on the physical host, which are bound to the network interface of the host only network, you need to watch the boot order. Most services are started before libvirtd. The Virtual bridges only exist<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">after</span><span class="Apple-converted-space">&nbsp;</span>libvirtd has been started. Any services started before libvirtd will not be able to bind to the virbrX interface. Named (bind) for instance binds to the interfaces. If you use the host only network to access a nameserver on the physical hosts, you need to restart named after boot (of the physical host), or the guests cannot access the nameserver.
 +
 
 +
The bridged network is a bit more complex. Use the configuration file of eth0 as a basis.<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0</span>. Remove the lines crossed out below and change/add the bold statements.<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
&amp;#13;<br>''/etc/sysconfig/network-scripts/ifcfg-eth0''<br>DEVICE=eth0<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">BOOTPROTO=static</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">BROADCAST=a.b.c.255</del><br>HWADDR=ab:cd:ef:gh:ij:kl<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">IPADDR=a.b.c.d</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">NETMASK=255.255.255.0</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">NETWORK=a.b.c.0</del><br>'''BRIDGE=br0'''<br>ONBOOT=yes<br>''/etc/sysconfig/network-scripts/ifcfg-br0''<br>DEVICE='''br0'''<br>BOOTPROTO=static<br>BROADCAST=a.b.c.255<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif;">HWADDR=ab:cd:ef:gh:ij:kl</del><br>IPADDR=a.b.c.d<br>NETMASK=255.255.255.0<br>NETWORK=a.b.c.0<br>ONBOOT=yes<br>'''TYPE=Bridge'''  
 
</div>
 
</div>
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Internal clock</span>'''<br>The internal clock of KVM is less stable than the clock under Xen. Heavy loads have been know to cause clock drift. There are two workarounds:<br>
 
  
*Boot with<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">divider=10 notsc</span><span class="Apple-converted-space">&nbsp;</span>(see earlier) and start ntpd at boot (chkconfig --level 2345 ntpd on; configure ntp first)<br>
 
*Use the -no-kvm-pit-reinjection option with qemu-kvm. One of the improvements added to the final version is that libvirt seems to add this option by default now, so everything should work out of the box. You still need to start ntp though.<br>
 
<br>
 
Also see (
 
[https://bugzilla.redhat.com/show_bug.cgi?id=507834 https://bugzilla.redhat.com/show_bug.cgi?id=507834]
 
)
 
  
'''Now shut down the virtual system (shutdown -h now)'''
+
Now br0 can be used as a bridge interface. All traffic over the bridge interface is subject to filtering by IPtables. I think this is a great feature and allows you to centralize firewalling on each host. Even better is that the firewall rules are now susceptible to change if the virtual machine is ever compromised. However, Xen worked in a different fashion. Our Xen based images will have their own firewall rules. To skip the firewall rules for the physical host do:
  
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; font-size: 15pt; "><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">'''''Updating the host'''''</span></span><br>The physical host needs some updating as well. First, before you start, make sure all virtual systems are stopped (xm list) and that you are logged on as root. If RHEL5.4 is already released, yum will update the system automatically to this version. If now, the system needs to be subscribed to the RHEL5.4 beta channel. You can do this at<span class="Apple-converted-space">&nbsp;</span>[https://rhn.redhat.com/ RedHat network], if your system is subscribed to rhn. Also make sure the system has access to the Virtual Platform channel beta. Aside from the updates, some new packages need to be installed as well and all virtualization services must be disabled at boot time until we are ready with the configuration work.<br>
+
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
yum clean all<span class="Apple-converted-space">&nbsp;</span>''#for safety''<br>yum update<br>yum install kernel kvm kvm-tools kmod-kvm kvm-qemu-img bridge-utils<br>chkconfig --level 2345 xend off<br>chkconfig --level 2345 xendomains off<br>chkconfig --level 2345 rhn-virtualization-host off
+
echo net.bridge.bridge-nf-call-ip6tables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf<br>echo net.bridge.bridge-nf-call-iptables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf<br>echo net.bridge.bridge-nf-call-arptables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf
 +
</div>
 +
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Swap usage and caching</span>'''<br>If your physical machine is only running Virtual Machines and the memory is not oversubscribed (all VM's together use not more than 80-90%) of total memory, you might want to limit swapfile usage. Since the kernel sees the VMs as a process, rules for processes apply as well. One of those rules means that pages that are not referenced for a while are paged out to swap. The purpose is to free up memory to use for other processes or cache. This speeds up things that are being used. For a VM this is unwanted behavior. On a dedicated host nothing else does run<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">and</span><span class="Apple-converted-space">&nbsp;</span>I don't want my VMs being cached, since that is already happening<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">inside</span><span class="Apple-converted-space">&nbsp;</span>the VM. Double caching gives inconsistent performance behavior, let alone the effects when the host crashes.  
 +
 
 +
There are two ways to put a stop to paging and swapping. The first is not to create a swapfile at all. The second one is to set the kernel dwappiness parameter to a low value. I've set it to 0.<br>  
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
echo vm.swappiness = 0 &amp;gt;&amp;gt; /etc/sysctl.conf
 
</div>
 
</div>
Edit<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/boot/grub/menu.lst</span><span class="Apple-converted-space">&nbsp;</span>and set the default boot kernel to the newest non-xen kernel (see example grub config)
 
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Network configuration</span>'''<br>By default only a network that is connected via NAT to the outside world is created. There are three options, leave it as is, but check that the IP range does not conflict with anything on the local network, change the IP range, or convert it to a host only network. I left the network, but adapted the IP range and created a new network for host-only networking. Be sure to change the uuid of the network. The format of the uuid should not change. Change any hex number [0-9|a-f] in the uuid string.<br>
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
 
''/etc/libvirt/qemu/networks/default.xml''<br>&lt;network&gt;&lt;name&gt;default&lt;/name&gt;&lt;uuid&gt;cc06c2a2-0766-45ee-baaa-896e04c7a3be&lt;/uuid&gt;&lt;forward mode="nat"/&gt;&lt;bridge name="virbr0" stp="on" forwarddelay="0"/&gt;&lt;ip address="a.b.c.d" netmask="255.255.255.0"&gt;&lt;dhcp&gt;&lt;range start="a.b.c.e" end="a.b.c.f"/&gt;&lt;/dhcp&gt;&lt;/ip&gt;&lt;/network&gt;<br>''/etc/libvirt/qemu/networks/hostonly.xml''<br>&lt;network&gt;&lt;name&gt;hostonly&lt;/name&gt;&lt;uuid&gt;04255669-803e-d8f6-352a-086fa45ae09d&lt;/uuid&gt;&lt;bridge name="virbr1" stp="on" forwarddelay="0"/&gt;&lt;ip address="a.b.g.h" netmask="255.255.255.0"&gt;&lt;dhcp&gt;&lt;range start="a.b.g.i" end="a.b.g.j"/&gt;&lt;/dhcp&gt;&lt;/ip&gt;&lt;/network&gt;
 
</div><br>
 
The host-only network should be started at boot, so
 
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">ln -s /etc/libvirt/qemu/networks/hostonly.xml /etc/libvirt/qemu/networks/autostart</span>
 
. Note that his network will replace the network coupled to the dummy0 interface, so dummy0 should not start up after a reboot. To do this, move
 
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/etc/sysconfig/network-scripts/ifcfg-dummy0</span><span class="Apple-converted-space">&nbsp;</span>
 
to a safe location, or edit it and change the ONBOOT option from "yes" to "no".
 
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Note:</span>'''<br>If you run any services on the physical host, which are bound to the network interface of the host only network, you need to watch the boot order. Most services are started before libvirtd. The Virtual bridges only exist<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">after</span><span class="Apple-converted-space">&nbsp;</span>libvirtd has been started. Any services started before libvirtd will not be able to bind to the virbrX interface. Named (bind) for instance binds to the interfaces. If you use the host only network to access a nameserver on the physical hosts, you need to restart named after boot (of the physical host), or the guests cannot access the nameserver.
+
See the virtual machine config file on how to turn off disk caching for virtual machines.  
  
The bridged network is a bit more complex. Use the configuration file of eth0 as a basis.<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0</span>. Remove the lines crossed out below and change/add the bold statements.<br>
+
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Converting the virtual machine configuration file</span>'''<br>There are two ways of converting to KVM. The easiest one is to use virt-manager and create a new virtual machine with exactly the same details as the old one, but point it to a different virtual disk (smallest possible) to prevent overwriting any existing data. Then stop the machine (no need to really install anything) and change the configuration file in<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/etc/libvirt/qemu</span><span class="Apple-converted-space">&nbsp;</span>by hand to point at the right disk image. This method requires you to reboot first. Else the configuration tools wont see the networks we just created.  
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
 
&amp;#13;<br>''/etc/sysconfig/network-scripts/ifcfg-eth0''<br>DEVICE=eth0<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">BOOTPROTO=static</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">BROADCAST=a.b.c.255</del><br>HWADDR=ab:cd:ef:gh:ij:kl<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">IPADDR=a.b.c.d</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">NETMASK=255.255.255.0</del><br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">NETWORK=a.b.c.0</del><br>'''BRIDGE=br0'''<br>ONBOOT=yes<br>''/etc/sysconfig/network-scripts/ifcfg-br0''<br>DEVICE='''br0'''<br>BOOTPROTO=static<br>BROADCAST=a.b.c.255<br><del style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; ">HWADDR=ab:cd:ef:gh:ij:kl</del><br>IPADDR=a.b.c.d<br>NETMASK=255.255.255.0<br>NETWORK=a.b.c.0<br>ONBOOT=yes<br>'''TYPE=Bridge'''
+
The other method is to convert the virtual machine definition by hand. Below is an old Xen definition file<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">(/etc/xen/test1</span>:<br>  
</div><br>
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
Now br0 can be used as a bridge interface. All traffic over the bridge interface is subject to filtering by IPtables. I think this is a great feature and allows you to centralize firewalling on each host. Even better is that the firewall rules are now susceptible to change if the virtual machine is ever compromised. However, Xen worked in a different fashion. Our Xen based images will have their own firewall rules. To skip the firewall rules for the physical host do:
+
name = "test1"<br>uuid = "4a07fde8-f244-2a6d-9603-85ff2179a9bb"<br>maxmem = 512<br>memory = 512<br>vcpus = 2<br>bootloader = "/usr/bin/pygrub"<br>on_poweroff = "destroy"<br>on_reboot = "restart"<br>on_crash = "restart"<br>vfb = [ "type=vnc,vncunused=1,keymap=en-us" ]<br>disk = [ "tap:aio:/var/lib/xen/images/test1.img,xvda,w" ]<br>vif = [ "mac=00:16:3e:1a:d0:96,bridge=xenbr0", "mac=00:16:3e:1a:d0:97,bridge=xenbr1" ]
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
echo net.bridge.bridge-nf-call-ip6tables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf<br>echo net.bridge.bridge-nf-call-iptables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf<br>echo net.bridge.bridge-nf-call-arptables = 0 &amp;gt;&amp;gt; /etc/sysctl.conf
+
 
</div>
 
</div>
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Swap usage and caching</span>'''<br>If your physical machine is only running Virtual Machines and the memory is not oversubscribed (all VM's together use not more than 80-90%) of total memory, you might want to limit swapfile usage. Since the kernel sees the VMs as a process, rules for processes apply as well. One of those rules means that pages that are not referenced for a while are paged out to swap. The purpose is to free up memory to use for other processes or cache. This speeds up things that are being used. For a VM this is unwanted behavior. On a dedicated host nothing else does run<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">and</span><span class="Apple-converted-space">&nbsp;</span>I don't want my VMs being cached, since that is already happening<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">inside</span><span class="Apple-converted-space">&nbsp;</span>the VM. Double caching gives inconsistent performance behavior, let alone the effects when the host crashes.
 
  
There are two ways to put a stop to paging and swapping. The first is not to create a swapfile at all. The second one is to set the kernel dwappiness parameter to a low value. I've set it to 0.<br>
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
 
echo vm.swappiness = 0 &amp;gt;&amp;gt; /etc/sysctl.conf
 
</div><br>
 
See the virtual machine config file on how to turn off disk caching for virtual machines.
 
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Converting the virtual machine configuration file</span>'''<br>There are two ways of converting to KVM. The easiest one is to use virt-manager and create a new virtual machine with exactly the same details as the old one, but point it to a different virtual disk (smallest possible) to prevent overwriting any existing data. Then stop the machine (no need to really install anything) and change the configuration file in<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/etc/libvirt/qemu</span><span class="Apple-converted-space">&nbsp;</span>by hand to point at the right disk image. This method requires you to reboot first. Else the configuration tools wont see the networks we just created.
+
This information can be converted into a KVM configuration file <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">(/etc/libvirt/qemu/test1.xml</span> . Take care to use the same MAC addresses for the network interfaces or else they won't be recognized when the virtual machine is booted. Also watch the serial and console arguments to not point to the same serial port for multiple VMs. You could use <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">virsh list</span><span class="Apple-converted-space">&nbsp;</span> and <span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">virsh dumpxml</span><span class="Apple-converted-space">&nbsp;</span> as a starting point. However you must do this <span class="Apple-converted-space">&nbsp;</span>'''before'''<span class="Apple-converted-space">&nbsp;</span> starting with this howto.  
  
The other method is to convert the virtual machine definition by hand. Below is an old Xen definition file<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">(/etc/xen/test1</span>:<br>
+
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
name = "test1"<br>uuid = "4a07fde8-f244-2a6d-9603-85ff2179a9bb"<br>maxmem = 512<br>memory = 512<br>vcpus = 2<br>bootloader = "/usr/bin/pygrub"<br>on_poweroff = "destroy"<br>on_reboot = "restart"<br>on_crash = "restart"<br>vfb = [ "type=vnc,vncunused=1,keymap=en-us" ]<br>disk = [ "tap:aio:/var/lib/xen/images/test1.img,xvda,w" ]<br>vif = [ "mac=00:16:3e:1a:d0:96,bridge=xenbr0", "mac=00:16:3e:1a:d0:97,bridge=xenbr1" ]
+
&lt;domain type="kvm"&gt;&lt;name&gt;test1&lt;/name&gt;&lt;uuid&gt;48156322-4e0c-b658-b80a-1bf3b608b49d&lt;/uuid&gt;&lt;memory&gt;524288&lt;/memory&gt;&lt;currentmemory&gt;524288&lt;/currentmemory&gt;&lt;vcpu&gt;2&lt;/vcpu&gt;&lt;os&gt;&lt;type arch="x86_64" machine="pc"&gt;hvm&lt;/type&gt;&lt;boot dev="hd"/&gt;&lt;/os&gt;&lt;features&gt;&lt;acpi/&gt;&lt;apic/&gt;&lt;pae/&gt;&lt;/features&gt;&lt;clock offset="utc"/&gt;&lt;on_poweroff&gt;destroy&lt;/on_poweroff&gt;&lt;on_reboot&gt;restart&lt;/on_reboot&gt;&lt;on_crash&gt;restart&lt;/on_crash&gt;&lt;devices&gt;&lt;emulator&gt;/usr/libexec/qemu-kvm&lt;/emulator&gt;&lt;disk type="file" device="disk"&gt;&lt;driver name="qemu" cache="none"/&gt;&lt;source file="/var/lib/xen/images/test1.img"/&gt;&lt;target dev="vda" bus="virtio"/&gt;&lt;/disk&gt;&lt;interface type="bridge"&gt;&lt;mac address="00:16:3e:1a:d0:96"/&gt;&lt;source bridge="br0"/&gt;&lt;model type="virtio"/&gt;&lt;/interface&gt;&lt;interface type="network"&gt;&lt;mac address="00:16:3e:1a:d0:97"/&gt;&lt;source network="hostonly"/&gt;&lt;model type="virtio"/&gt;&lt;/interface&gt;&lt;serial type="pty"&gt;&lt;source path="/dev/pts/2"/&gt;&lt;target port="0"/&gt;&lt;/serial&gt;&lt;console type="pty"&gt;&lt;source path="/dev/pts/2"/&gt;&lt;target port="0"/&gt;&lt;/console&gt;&lt;input type="mouse" bus="ps2"/&gt;&lt;graphics type="vnc" port="-1" autoport="yes" keymap="en-us"/&gt;&lt;/devices&gt;&lt;/domain&gt;  
</div><br>
+
This information can be converted into a KVM configuration file
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">(/etc/libvirt/qemu/test1.xml</span>
+
. Take care to use the same MAC addresses for the network interfaces or else they won't be recognized when the virtual machine is booted. Also watch the serial and console arguments to not point to the same serial port for multiple VMs. You could use
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">virsh list</span><span class="Apple-converted-space">&nbsp;</span>
+
and
+
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">virsh dumpxml</span><span class="Apple-converted-space">&nbsp;</span>
+
as a starting point. However you must do this
+
<span class="Apple-converted-space">&nbsp;</span>'''before'''<span class="Apple-converted-space">&nbsp;</span>
+
starting with this howto.
+
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
&lt;domain type="kvm"&gt;&lt;name&gt;test1&lt;/name&gt;&lt;uuid&gt;48156322-4e0c-b658-b80a-1bf3b608b49d&lt;/uuid&gt;&lt;memory&gt;524288&lt;/memory&gt;&lt;currentmemory&gt;524288&lt;/currentmemory&gt;&lt;vcpu&gt;2&lt;/vcpu&gt;&lt;os&gt;&lt;type arch="x86_64" machine="pc"&gt;hvm&lt;/type&gt;&lt;boot dev="hd"/&gt;&lt;/os&gt;&lt;features&gt;&lt;acpi/&gt;&lt;apic/&gt;&lt;pae/&gt;&lt;/features&gt;&lt;clock offset="utc"/&gt;&lt;on_poweroff&gt;destroy&lt;/on_poweroff&gt;&lt;on_reboot&gt;restart&lt;/on_reboot&gt;&lt;on_crash&gt;restart&lt;/on_crash&gt;&lt;devices&gt;&lt;emulator&gt;/usr/libexec/qemu-kvm&lt;/emulator&gt;&lt;disk type="file" device="disk"&gt;&lt;driver name="qemu" cache="none"/&gt;&lt;source file="/var/lib/xen/images/test1.img"/&gt;&lt;target dev="vda" bus="virtio"/&gt;&lt;/disk&gt;&lt;interface type="bridge"&gt;&lt;mac address="00:16:3e:1a:d0:96"/&gt;&lt;source bridge="br0"/&gt;&lt;model type="virtio"/&gt;&lt;/interface&gt;&lt;interface type="network"&gt;&lt;mac address="00:16:3e:1a:d0:97"/&gt;&lt;source network="hostonly"/&gt;&lt;model type="virtio"/&gt;&lt;/interface&gt;&lt;serial type="pty"&gt;&lt;source path="/dev/pts/2"/&gt;&lt;target port="0"/&gt;&lt;/serial&gt;&lt;console type="pty"&gt;&lt;source path="/dev/pts/2"/&gt;&lt;target port="0"/&gt;&lt;/console&gt;&lt;input type="mouse" bus="ps2"/&gt;&lt;graphics type="vnc" port="-1" autoport="yes" keymap="en-us"/&gt;&lt;/devices&gt;&lt;/domain&gt;
+
</div><br>
+
If you are using a partition as a virtual disk the Xen configuration
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">disk = [ "phy:/dev/vgvm/lvmyvolume,xvda,w" ]</span>
+
translates to:
+
<span class="Apple-converted-space">&nbsp;</span><br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
&lt;disk device="disk" type="block"&gt;&lt;driver cache="none"/&gt;&lt;source dev="/dev/vgvm/lvmyvolume"/&gt;&lt;target dev="vda" bus="virtio"/&gt;&lt;/disk&gt;
+
</div><br>
+
If you want to bind the virtual cpu to a physical one use the following vcpu syntax:
+
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
&lt;vcpu cpuset="cpu1,cpu2,cpu3"&gt;virtual cpus&lt;/vcpu&gt;<br>''for example''<br>&lt;vcpu cpuset="0,1"&gt;4&lt;/vcpu&gt;
+
</div><br>
+
Also see
+
<span class="Apple-converted-space">&nbsp;</span>[http://libvirt.org/formatdomain.html http://libvirt.org/formatdomain.html]<span class="Apple-converted-space">&nbsp;</span>
+
If you want to verify that the xml file is correct, use the
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">virt-xml-validate</span>
+
command.
+
<br>'''now reboot the host'''
+
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Starting the virtual machines</span>'''<br>You can now start the virtual machines by using the virsh command. Open a console directly after starting the domain to monitor boot progress. You also might want to start the machine after booting.<br>
+
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
virsh define /etc/libvirt/qemu/[mymachine.xml]<br>virsh list<br>virsh start [mymachines ID]<br>virsh console [mymachines ID]<br>virsh autostart [mymachines ID]
+
 
</div>
 
</div>
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; font-size: 15pt; ">'''''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">Improving Performance with Hugepages</span>'''''</span><br><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">'''Note:'''</span><br>There could be some unwanted interaction with SELinux here. If you run into problems, either don't use Hugepages or turn SELinux off
 
  
KVM uses 4kB memory pages by default, just like any other process. One of the main differences between a normal average process and a kvm virtual machine process is the amount of memory allocated to it. Virtual machines normally use hundreds or even gigabytes of memory. This means a lot of overhead when the CPU switches between virtual machines since large memory tables need to be updated each time.&amp;#13;
 
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">RHEL 5.4 and Hugepages</span>'''<br>Linux also has Hugepages, special memory pages that are 1,2 or 4MB in size, shortening the list of memory pages dramatically and improving performance up to 10%. Sadly, support for Hugepages hasn't been implemented into libvirt. There is work on it in Fedora 12, but I don't expect to see those developments in RHEL5. There is a way however. First lets start by reserving the Hugepages. The file<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/proc/meminfo</span><span class="Apple-converted-space">&nbsp;</span>should contain the Hugepage size of the system somewhere in the last lines.
+
If you are using a partition as a virtual disk the Xen configuration <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">disk = [ "phy:/dev/vgvm/lvmyvolume,xvda,w" ]</span> translates to:
  
Now calculate the amount of Hugepages needed for the virtual machines and add at least 6 pages extra for each virtual machines. If you do not reserve enough pages, your virtual machine won't start. KVM uses some additional pages when starting up the VM, so if you don't add those 6 pages, the last VM will not start. Add the total of Hugepages to your kernel configuration by doing:<br>
+
<span class="Apple-converted-space">&nbsp;</span>
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
echo vm.nr_hugepages = XXXX &amp;gt;&amp;gt; /etc/sysctl.conf
+
&lt;disk device="disk" type="block"&gt;&lt;driver cache="none"/&gt;&lt;source dev="/dev/vgvm/lvmyvolume"/&gt;&lt;target dev="vda" bus="virtio"/&gt;&lt;/disk&gt;
</div><br>
+
</div>
Make the Hugepages accessible to KVM
+
 
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
 
mkdir /hugepages<br>echo hugetlbfs /hugepages hugetlbfs defaults 0 0
+
If you want to bind the virtual cpu to a physical one use the following vcpu syntax:
</div><br>
+
 
Now the Hugepages are set-up (they become accessible after a system reboot). Lets rig libvirt so the Hugepages are actually used after a system reboot. To do this we need to move the
+
 
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">qemu-kvm</span><span class="Apple-converted-space">&nbsp;</span>
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
binary and replace it with a script of our own. The binary is located in
+
&lt;vcpu cpuset="cpu1,cpu2,cpu3"&gt;virtual cpus&lt;/vcpu&gt;<br>''for example''<br>&lt;vcpu cpuset="0,1"&gt;4&lt;/vcpu&gt;
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/usr/libexec</span>
+
</div>
. Execute
+
 
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">mv /usr/libexec/qemu-kvm /usr/libexec/qemu-kvm2</span>
+
 
. Now create the script
+
Also see <span class="Apple-converted-space">&nbsp;</span>[http://libvirt.org/formatdomain.html http://libvirt.org/formatdomain.html]<span class="Apple-converted-space">&nbsp;</span> If you want to verify that the xml file is correct, use the <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">virt-xml-validate</span> command. <br>'''now reboot the host''' '''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Starting the virtual machines</span>'''<br>You can now start the virtual machines by using the virsh command. Open a console directly after starting the domain to monitor boot progress. You also might want to start the machine after booting.<br>
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/usr/libexec/qemu-kvm</span><span class="Apple-converted-space">&nbsp;</span>
+
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
with the following contents:
+
virsh define /etc/libvirt/qemu/[mymachine.xml]<br>virsh list<br>virsh start [mymachines ID]<br>virsh console [mymachines ID]<br>virsh autostart [mymachines ID]
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
</div>
 +
<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; font-size: 15pt;">'''''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">Improving Performance with Hugepages</span>'''''</span><br><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">'''Note:'''</span><br>There could be some unwanted interaction with SELinux here. If you run into problems, either don't use Hugepages or turn SELinux off
 +
 
 +
KVM uses 4kB memory pages by default, just like any other process. One of the main differences between a normal average process and a kvm virtual machine process is the amount of memory allocated to it. Virtual machines normally use hundreds or even gigabytes of memory. This means a lot of overhead when the CPU switches between virtual machines since large memory tables need to be updated each time.&amp;#13;
 +
 
 +
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">RHEL 5.4 and Hugepages</span>'''<br>Linux also has Hugepages, special memory pages that are 1,2 or 4MB in size, shortening the list of memory pages dramatically and improving performance up to 10%. Sadly, support for Hugepages hasn't been implemented into libvirt. There is work on it in Fedora 12, but I don't expect to see those developments in RHEL5. There is a way however. First lets start by reserving the Hugepages. The file<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/proc/meminfo</span><span class="Apple-converted-space">&nbsp;</span>should contain the Hugepage size of the system somewhere in the last lines.
 +
 
 +
Now calculate the amount of Hugepages needed for the virtual machines and add at least 6 pages extra for each virtual machines. If you do not reserve enough pages, your virtual machine won't start. KVM uses some additional pages when starting up the VM, so if you don't add those 6 pages, the last VM will not start. Add the total of Hugepages to your kernel configuration by doing:<br>  
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
echo vm.nr_hugepages = XXXX &amp;gt;&amp;gt; /etc/sysctl.conf  
 +
</div>
 +
 
 +
 
 +
Make the Hugepages accessible to KVM  
 +
 
 +
 
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
mkdir /hugepages<br>echo hugetlbfs /hugepages hugetlbfs defaults 0 0  
 +
</div>
 +
 
 +
 
 +
Now the Hugepages are set-up (they become accessible after a system reboot). Lets rig libvirt so the Hugepages are actually used after a system reboot. To do this we need to move the <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">qemu-kvm</span><span class="Apple-converted-space">&nbsp;</span> binary and replace it with a script of our own. The binary is located in <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/usr/libexec</span> . Execute <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">mv /usr/libexec/qemu-kvm /usr/libexec/qemu-kvm2</span> . Now create the script <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/usr/libexec/qemu-kvm</span><span class="Apple-converted-space">&nbsp;</span> with the following contents:  
 +
 
 +
 
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 
#!/bin/bash<br>exec /usr/libexec/qemu-kvm2 -mem-path /hugepages "$@"
 
#!/bin/bash<br>exec /usr/libexec/qemu-kvm2 -mem-path /hugepages "$@"
</div><br>
+
</div>
Now reboot the system and start your virtual machines like normal.
+
<br><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">'''Note:'''</span><br>
+
Be careful when updating the libvirt package. An update will overwrite our script, so you need to reapply the change after each libvirt update.
+
  
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline; ">RHEL 5.5 and Hugepages</span>'''<br>RHEL 5.5 has native support for Hugepages. First make sure that the<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">libhugetlbfs</span><span class="Apple-converted-space">&nbsp;</span>package is installed. Then execute the<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">huge_page_setup_helper</span><span class="Apple-converted-space">&nbsp;</span>command and answer the questions.<br>
 
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
 
[root@aurora ~]# rpm -qa | grep huge<br>libhugetlbfs-1.3-7.el5<br>libhugetlbfs-1.3-7.el5<br>[root@aurora ~]# huge_page_setup_helper.py<br>Current configuration:<br>* Total System Memory......: 7909 MB<br>* Shared Mem Max Mapping...: 7100 MB<br>* System Huge Page Size....: 2 MB<br>* Number of Huge Pages.....: 3550<br>* Total size of Huge Pages.: 7100 MB<br>* Remaining System Memory..: 809 MB<br>* Huge Page User Group.....: root (0)
 
  
How much memory would you like to allocate for huge pages? (input in MB, unless postfixed with GB):
+
Now reboot the system and start your virtual machines like normal. <br><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">'''Note:'''</span><br> Be careful when updating the libvirt package. An update will overwrite our script, so you need to reapply the change after each libvirt update.  
</div><br>
+
Now add the Hugepages mountpoint to
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/etc/fstab</span><br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
mkdir /dev/hugepages<br>echo hugetlbfs /dev/hugepages hugetlbfs defaults 0 0
+
</div><br>
+
On next reboot there will be a problem, as /dev is governed by udev. That means that the hugepages mountpoint disappears automatically on reboot. To fix this a patch must be applied to /sbin/start_udev. To make sure thuis patch stays in place, even after rolling out a udev patch, a script has been created to see if the patch has been applied and if not, applies it.
+
  
Download the patch<span class="Apple-converted-space">&nbsp;</span>[http://www.gloudemans.info/publish/udev.patch here]<span class="Apple-converted-space">&nbsp;</span>and place it in /usr/local/bin<br>Download the init script<span class="Apple-converted-space">&nbsp;</span>[http://www.gloudemans.info/publish/libvirt_hugepages here]<span class="Apple-converted-space">&nbsp;</span>and place it in /etc/init.d
+
'''<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; text-decoration: underline;">RHEL 5.5 and Hugepages</span>'''<br>RHEL 5.5 has native support for Hugepages. First make sure that the<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">libhugetlbfs</span><span class="Apple-converted-space">&nbsp;</span>package is installed. Then execute the<span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">huge_page_setup_helper</span><span class="Apple-converted-space">&nbsp;</span>command and answer the questions.<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
[root@aurora ~]# rpm -qa | grep huge<br>libhugetlbfs-1.3-7.el5<br>libhugetlbfs-1.3-7.el5<br>[root@aurora ~]# huge_page_setup_helper.py<br>Current configuration:<br>* Total System Memory......: 7909 MB<br>* Shared Mem Max Mapping...: 7100 MB<br>* System Huge Page Size....: 2 MB<br>* Number of Huge Pages.....: 3550<br>* Total size of Huge Pages.: 7100 MB<br>* Remaining System Memory..: 809 MB<br>* Huge Page User Group.....: root (0)
  
Then do:<br>
+
How much memory would you like to allocate for huge pages? (input in MB, unless postfixed with GB):  
<div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
chkconfig --add libvirt_hugepages<br>chkconfig libvirt_hugepages on
+
</div><br>
+
As a last step, add the following to the virtual machile XML config files in
+
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">/etc/libvirt/qemu</span><span class="Apple-converted-space">&nbsp;</span>
+
(on the same level as &lt;memory&gt;)
+
<br><div style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial; " class="codesnippet">
+
&lt;memorybacking&gt;&lt;hugepages/&gt;&lt;/memorybacking&gt;
+
 
</div>
 
</div>
Now reboot the system and the virtual machines should be started using Hugepages memory. You can verify this by looking at the qemu-kvm command in the process list. It should contain a<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">-mem-path</span><span class="Apple-converted-space">&nbsp;</span>parameter now. If the Hugepages mountpoint is added after the system has rebooted, restart<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue; ">libvirtd</span>, or else libvirt won't see the Hugepages.&lt;/memory&gt;
+
 
 +
 
 +
Now add the Hugepages mountpoint to
 +
 
 +
<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/etc/fstab</span>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
mkdir /dev/hugepages<br>echo hugetlbfs /dev/hugepages hugetlbfs defaults 0 0
 +
</div>
 +
 
 +
 
 +
On next reboot there will be a problem, as /dev is governed by udev. That means that the hugepages mountpoint disappears automatically on reboot. To fix this a patch must be applied to /sbin/start_udev. To make sure thuis patch stays in place, even after rolling out a udev patch, a script has been created to see if the patch has been applied and if not, applies it.
 +
 
 +
Download the patch<span class="Apple-converted-space">&nbsp;</span>[http://www.gloudemans.info/publish/udev.patch here]<span class="Apple-converted-space">&nbsp;</span>and place it in /usr/local/bin<br>Download the init script<span class="Apple-converted-space">&nbsp;</span>[http://www.gloudemans.info/publish/libvirt_hugepages here]<span class="Apple-converted-space">&nbsp;</span>and place it in /etc/init.d
 +
 
 +
Then do:<br>
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
chkconfig --add libvirt_hugepages<br>chkconfig libvirt_hugepages on
 +
</div>
 +
 
 +
 
 +
As a last step, add the following to the virtual machile XML config files in <span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">/etc/libvirt/qemu</span><span class="Apple-converted-space">&nbsp;</span> (on the same level as &lt;memory&gt;)
 +
 
 +
 
 +
<div class="codesnippet" style="margin-top: 10px; margin-right: 10px; margin-bottom: 10px; margin-left: 10px; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; font-family: courier, monospace; font-size: 12px; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(221, 221, 221); background-position: initial initial; background-repeat: initial initial;">
 +
&lt;memorybacking&gt;&lt;hugepages/&gt;&lt;/memorybacking&gt;
 +
</div>
 +
Now reboot the system and the virtual machines should be started using Hugepages memory. You can verify this by looking at the qemu-kvm command in the process list. It should contain a<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">-mem-path</span><span class="Apple-converted-space">&nbsp;</span>parameter now. If the Hugepages mountpoint is added after the system has rebooted, restart<span class="Apple-converted-space">&nbsp;</span><span style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; font-family: Verdana, Lucida, Arial, sans-serif; color: blue;">libvirtd</span>, or else libvirt won't see the Hugepages.&lt;/memory&gt;

Edição das 01h48min de 1 de dezembro de 2011

Migrate Paravirtualized Xen to KVM under RHEL

Posted by Roel Gloudemans on 9 July 2009 | 0 Comments

http://www.gloudemans.info/migrate-paravirtualized-xen-to-kvm-under-rhel/

Tags:

Update July 11, 2009: Re-registering VMs at RHN uses an extra entitlement with RHEL5.4Beta
Update July 15, 2009: Swap usage, clock and disk cache of the virtual machine
Update July 16, 2009: Replace virsh create with virsh define &amp; start to create a managed domain and not a transient one
Update September 2, 2009: Re-registering with RHN works
Update September 2, 2009: RHEL5.4 has been released. Added a note about services on the physical host
Update September 6, 2009: Updating TimeKeeping and Hugepages
Update April 1, 2010: Hugepages configuration for RHEL 5.5
Update July 3, 2010: Make Hugepages mountpoint persistent
RedHat Enterprise Linux version 5.4 is out. It heralds the arrival of KVM as RedHat's official hypervisor. RedHat will be supporting Xen for the rest of the RHEL5 life cycle, so for the moment, there is no need to migrate to KVM.

However migrating to KVM has some advantages. For one KVM looks simpler from the outside, another is that it works with a normal kernel, meaning that all drivers that work on a normal kernel work as well. This not only encompasses display drivers, but CPU scaling (dynamically adapting the speed of the CPU) as well. This is not only very "green" but makes a difference is your or the companies wallet as well.

RedHat put a lot of work into making Xen easier to manage in RHEL5.0-5.3. As a result Xen uses a single disk image from which it can boot. The format of this image is the same as for KVM. One would suspect that migrating from one Hypervisor to another would be easy and it is. This blog will describe a step-by-step scenario on how to do it.

The starting situation is a RHEL5.3 Physical host with RHEL5.3 paravirtualized guests. The guests have two networking interfaces, one bridged to the physical network interface, and one bridged to a dummy network interface for an internal host network. Note that the minimum requirement to run with virtio is RHEL5.3.

Note:
I had some trouble with selinux in the rhel 5.4 beta. It is related to the attributes on /var/lib/libvirt. I do not use this directory to store the images, bit I use raw LVM volumes. To get my system running again, I just disabled selinux.

Configure the virtio drivers
Open /etc/modprobe.conf in the editor. In our case /etc/modprobe.conf contains the following lines:

alias eth0 xennet
alias eth1 xennet
alias scsi_hostadapter xenblk

change it to

alias eth0 virtio_net
alias eth1 virtio_net
alias scsi_hostadapter virtio_blk

Now add the virtio drivers to the kernel boot image (modify this lane to mirror the latest kernel version)

mkinitrd -f --with=virtio_blk --with=virtio_pci --builtin=xenblk initrd-2.6.18-128.1.16.el5.img 2.6.18-128.1.16.el5
The --builtin is necessary only when currently running under a xen kernel in paravirtualized mode

Internal clock
The internal clock of KVM is less stable than the clock under Xen. Heavy loads have been know to cause clock drift. There are two workarounds:

  • Boot with divider=10 notsc (see earlier) and start ntpd at boot (chkconfig --level 2345 ntpd on; configure ntp first)
  • Use the -no-kvm-pit-reinjection option with qemu-kvm. One of the improvements added to the final version is that libvirt seems to add this option by default now, so everything should work out of the box. You still need to start ntp though.


Also see ( https://bugzilla.redhat.com/show_bug.cgi?id=507834 )

Now shut down the virtual system (shutdown -h now)

Updating the host
The physical host needs some updating as well. First, before you start, make sure all virtual systems are stopped (xm list) and that you are logged on as root. If RHEL5.4 is already released, yum will update the system automatically to this version. If now, the system needs to be subscribed to the RHEL5.4 beta channel. You can do this at RedHat network, if your system is subscribed to rhn. Also make sure the system has access to the Virtual Platform channel beta. Aside from the updates, some new packages need to be installed as well and all virtualization services must be disabled at boot time until we are ready with the configuration work.

yum clean all #for safety
yum update
yum install kernel kvm kvm-tools kmod-kvm kvm-qemu-img bridge-utils
chkconfig --level 2345 xend off
chkconfig --level 2345 xendomains off
chkconfig --level 2345 rhn-virtualization-host off

Edit /boot/grub/menu.lst and set the default boot kernel to the newest non-xen kernel (see example grub config)

Network configuration
By default only a network that is connected via NAT to the outside world is created. There are three options, leave it as is, but check that the IP range does not conflict with anything on the local network, change the IP range, or convert it to a host only network. I left the network, but adapted the IP range and created a new network for host-only networking. Be sure to change the uuid of the network. The format of the uuid should not change. Change any hex number [0-9|a-f] in the uuid string.

/etc/libvirt/qemu/networks/default.xml
<network><name>default</name><uuid>cc06c2a2-0766-45ee-baaa-896e04c7a3be</uuid><forward mode="nat"/><bridge name="virbr0" stp="on" forwarddelay="0"/><ip address="a.b.c.d" netmask="255.255.255.0"><dhcp><range start="a.b.c.e" end="a.b.c.f"/></dhcp></ip></network>
/etc/libvirt/qemu/networks/hostonly.xml
<network><name>hostonly</name><uuid>04255669-803e-d8f6-352a-086fa45ae09d</uuid><bridge name="virbr1" stp="on" forwarddelay="0"/><ip address="a.b.g.h" netmask="255.255.255.0"><dhcp><range start="a.b.g.i" end="a.b.g.j"/></dhcp></ip></network>


The host-only network should be started at boot, so  ln -s /etc/libvirt/qemu/networks/hostonly.xml /etc/libvirt/qemu/networks/autostart . Note that his network will replace the network coupled to the dummy0 interface, so dummy0 should not start up after a reboot. To do this, move  /etc/sysconfig/network-scripts/ifcfg-dummy0  to a safe location, or edit it and change the ONBOOT option from "yes" to "no".

Note:
If you run any services on the physical host, which are bound to the network interface of the host only network, you need to watch the boot order. Most services are started before libvirtd. The Virtual bridges only exist after libvirtd has been started. Any services started before libvirtd will not be able to bind to the virbrX interface. Named (bind) for instance binds to the interfaces. If you use the host only network to access a nameserver on the physical hosts, you need to restart named after boot (of the physical host), or the guests cannot access the nameserver.

The bridged network is a bit more complex. Use the configuration file of eth0 as a basis. cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0. Remove the lines crossed out below and change/add the bold statements.

&#13;
/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
BROADCAST=a.b.c.255
HWADDR=ab:cd:ef:gh:ij:kl
IPADDR=a.b.c.d
NETMASK=255.255.255.0
NETWORK=a.b.c.0
BRIDGE=br0
ONBOOT=yes
/etc/sysconfig/network-scripts/ifcfg-br0
DEVICE=br0
BOOTPROTO=static
BROADCAST=a.b.c.255
HWADDR=ab:cd:ef:gh:ij:kl
IPADDR=a.b.c.d
NETMASK=255.255.255.0
NETWORK=a.b.c.0
ONBOOT=yes
TYPE=Bridge


Now br0 can be used as a bridge interface. All traffic over the bridge interface is subject to filtering by IPtables. I think this is a great feature and allows you to centralize firewalling on each host. Even better is that the firewall rules are now susceptible to change if the virtual machine is ever compromised. However, Xen worked in a different fashion. Our Xen based images will have their own firewall rules. To skip the firewall rules for the physical host do:


echo net.bridge.bridge-nf-call-ip6tables = 0 &gt;&gt; /etc/sysctl.conf
echo net.bridge.bridge-nf-call-iptables = 0 &gt;&gt; /etc/sysctl.conf
echo net.bridge.bridge-nf-call-arptables = 0 &gt;&gt; /etc/sysctl.conf

Swap usage and caching
If your physical machine is only running Virtual Machines and the memory is not oversubscribed (all VM's together use not more than 80-90%) of total memory, you might want to limit swapfile usage. Since the kernel sees the VMs as a process, rules for processes apply as well. One of those rules means that pages that are not referenced for a while are paged out to swap. The purpose is to free up memory to use for other processes or cache. This speeds up things that are being used. For a VM this is unwanted behavior. On a dedicated host nothing else does run and I don't want my VMs being cached, since that is already happening inside the VM. Double caching gives inconsistent performance behavior, let alone the effects when the host crashes.

There are two ways to put a stop to paging and swapping. The first is not to create a swapfile at all. The second one is to set the kernel dwappiness parameter to a low value. I've set it to 0.

echo vm.swappiness = 0 &gt;&gt; /etc/sysctl.conf


See the virtual machine config file on how to turn off disk caching for virtual machines.

Converting the virtual machine configuration file
There are two ways of converting to KVM. The easiest one is to use virt-manager and create a new virtual machine with exactly the same details as the old one, but point it to a different virtual disk (smallest possible) to prevent overwriting any existing data. Then stop the machine (no need to really install anything) and change the configuration file in/etc/libvirt/qemu by hand to point at the right disk image. This method requires you to reboot first. Else the configuration tools wont see the networks we just created.

The other method is to convert the virtual machine definition by hand. Below is an old Xen definition file (/etc/xen/test1:

name = "test1"
uuid = "4a07fde8-f244-2a6d-9603-85ff2179a9bb"
maxmem = 512
memory = 512
vcpus = 2
bootloader = "/usr/bin/pygrub"
on_poweroff = "destroy"
on_reboot = "restart"
on_crash = "restart"
vfb = [ "type=vnc,vncunused=1,keymap=en-us" ]
disk = [ "tap:aio:/var/lib/xen/images/test1.img,xvda,w" ]
vif = [ "mac=00:16:3e:1a:d0:96,bridge=xenbr0", "mac=00:16:3e:1a:d0:97,bridge=xenbr1" ]


This information can be converted into a KVM configuration file  (/etc/libvirt/qemu/test1.xml . Take care to use the same MAC addresses for the network interfaces or else they won't be recognized when the virtual machine is booted. Also watch the serial and console arguments to not point to the same serial port for multiple VMs. You could use  virsh list  and virsh dumpxml  as a starting point. However you must do this  before  starting with this howto.


<domain type="kvm"><name>test1</name><uuid>48156322-4e0c-b658-b80a-1bf3b608b49d</uuid><memory>524288</memory><currentmemory>524288</currentmemory><vcpu>2</vcpu><os><type arch="x86_64" machine="pc">hvm</type><boot dev="hd"/></os><features><acpi/><apic/><pae/></features><clock offset="utc"/><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"><driver name="qemu" cache="none"/><source file="/var/lib/xen/images/test1.img"/><target dev="vda" bus="virtio"/></disk><interface type="bridge"><mac address="00:16:3e:1a:d0:96"/><source bridge="br0"/><model type="virtio"/></interface><interface type="network"><mac address="00:16:3e:1a:d0:97"/><source network="hostonly"/><model type="virtio"/></interface><serial type="pty"><source path="/dev/pts/2"/><target port="0"/></serial><console type="pty"><source path="/dev/pts/2"/><target port="0"/></console><input type="mouse" bus="ps2"/><graphics type="vnc" port="-1" autoport="yes" keymap="en-us"/></devices></domain>


If you are using a partition as a virtual disk the Xen configuration  disk = [ "phy:/dev/vgvm/lvmyvolume,xvda,w" ] translates to:

 

<disk device="disk" type="block"><driver cache="none"/><source dev="/dev/vgvm/lvmyvolume"/><target dev="vda" bus="virtio"/></disk>


If you want to bind the virtual cpu to a physical one use the following vcpu syntax:


<vcpu cpuset="cpu1,cpu2,cpu3">virtual cpus</vcpu>
for example
<vcpu cpuset="0,1">4</vcpu>


Also see  http://libvirt.org/formatdomain.html  If you want to verify that the xml file is correct, use the  virt-xml-validate command.
now reboot the host Starting the virtual machines
You can now start the virtual machines by using the virsh command. Open a console directly after starting the domain to monitor boot progress. You also might want to start the machine after booting.

virsh define /etc/libvirt/qemu/[mymachine.xml]
virsh list
virsh start [mymachines ID]
virsh console [mymachines ID]
virsh autostart [mymachines ID]

Improving Performance with Hugepages
Note:
There could be some unwanted interaction with SELinux here. If you run into problems, either don't use Hugepages or turn SELinux off

KVM uses 4kB memory pages by default, just like any other process. One of the main differences between a normal average process and a kvm virtual machine process is the amount of memory allocated to it. Virtual machines normally use hundreds or even gigabytes of memory. This means a lot of overhead when the CPU switches between virtual machines since large memory tables need to be updated each time.&#13;

RHEL 5.4 and Hugepages
Linux also has Hugepages, special memory pages that are 1,2 or 4MB in size, shortening the list of memory pages dramatically and improving performance up to 10%. Sadly, support for Hugepages hasn't been implemented into libvirt. There is work on it in Fedora 12, but I don't expect to see those developments in RHEL5. There is a way however. First lets start by reserving the Hugepages. The file /proc/meminfo should contain the Hugepage size of the system somewhere in the last lines.

Now calculate the amount of Hugepages needed for the virtual machines and add at least 6 pages extra for each virtual machines. If you do not reserve enough pages, your virtual machine won't start. KVM uses some additional pages when starting up the VM, so if you don't add those 6 pages, the last VM will not start. Add the total of Hugepages to your kernel configuration by doing:

echo vm.nr_hugepages = XXXX &gt;&gt; /etc/sysctl.conf


Make the Hugepages accessible to KVM


mkdir /hugepages
echo hugetlbfs /hugepages hugetlbfs defaults 0 0


Now the Hugepages are set-up (they become accessible after a system reboot). Lets rig libvirt so the Hugepages are actually used after a system reboot. To do this we need to move the  qemu-kvm  binary and replace it with a script of our own. The binary is located in  /usr/libexec . Execute  mv /usr/libexec/qemu-kvm /usr/libexec/qemu-kvm2 . Now create the script  /usr/libexec/qemu-kvm  with the following contents:


  1. !/bin/bash
    exec /usr/libexec/qemu-kvm2 -mem-path /hugepages "$@"


Now reboot the system and start your virtual machines like normal.
Note:
Be careful when updating the libvirt package. An update will overwrite our script, so you need to reapply the change after each libvirt update.

RHEL 5.5 and Hugepages
RHEL 5.5 has native support for Hugepages. First make sure that the libhugetlbfs package is installed. Then execute thehuge_page_setup_helper command and answer the questions.

[root@aurora ~]# rpm -qa | grep huge
libhugetlbfs-1.3-7.el5
libhugetlbfs-1.3-7.el5
[root@aurora ~]# huge_page_setup_helper.py
Current configuration:
* Total System Memory......: 7909 MB
* Shared Mem Max Mapping...: 7100 MB
* System Huge Page Size....: 2 MB
* Number of Huge Pages.....: 3550
* Total size of Huge Pages.: 7100 MB
* Remaining System Memory..: 809 MB
* Huge Page User Group.....: root (0)

How much memory would you like to allocate for huge pages? (input in MB, unless postfixed with GB):


Now add the Hugepages mountpoint to

 /etc/fstab

mkdir /dev/hugepages
echo hugetlbfs /dev/hugepages hugetlbfs defaults 0 0


On next reboot there will be a problem, as /dev is governed by udev. That means that the hugepages mountpoint disappears automatically on reboot. To fix this a patch must be applied to /sbin/start_udev. To make sure thuis patch stays in place, even after rolling out a udev patch, a script has been created to see if the patch has been applied and if not, applies it.

Download the patch here and place it in /usr/local/bin
Download the init script here and place it in /etc/init.d

Then do:

chkconfig --add libvirt_hugepages
chkconfig libvirt_hugepages on


As a last step, add the following to the virtual machile XML config files in  /etc/libvirt/qemu  (on the same level as <memory>)


<memorybacking><hugepages/></memorybacking>

Now reboot the system and the virtual machines should be started using Hugepages memory. You can verify this by looking at the qemu-kvm command in the process list. It should contain a -mem-path parameter now. If the Hugepages mountpoint is added after the system has rebooted, restart libvirtd, or else libvirt won't see the Hugepages.</memory>