Install Xen 4 with Libvirt / XL on CentOS 6 (2013)
It can be installed on your CentOS 6 machine via running the following commands:
yum install centos-release-xen && yum install xen libvirt python-virtinst libvirt-daemon-xen sh /usr/bin/grub-bootxen.sh rebootThe above commands will install the official Xen 4 packages along with the libvirt toolstack, load the correct kernel into your GRUB boot-loader, and reboot into your Xen kernel.
Once your system boots, ensure that you are running the Xen 4 Kernel via:
uname -rNow that Xen 4 has been installed, you can skip to section 6 at the bottom of this guide for installing your first Virtual Machine (VM) on CentOS Xen.
First things first, update your CentOS install via the following command:
yum -y update
1. Disable SElinux
SElinux can really interfere with Xen, so we’ll disable SElinux by performing the following:sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config setenforce 0
2. Create a Network Bridge
To achieve network sharing and segmentation between your Xen instances, we need to install bridge-utils for CentOS 6.x.2.1. Install Bridge Utilities
This step can be skipped unless you have a minimal install of CentOS 6.x:yum -y install bridge-utils
2.2. Configuring the Network Bridge
Next, we can configure the network bridge. To get started, copy the contents of your current active interface (typically eth0 / eth1 or wlan1 / wlan2) to another file called ifcfg-br0:cp /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-br0Now, modify the contents of our new file (ifcfg-br0) to resemble the following configuration:
The important settings here are ‘Device’, and ‘Type’.
/etc/sysconfig/network-scripts/ifcfg-br0:
DEVICE="br0" NM_CONTROLLED="yes" ONBOOT="yes" TYPE="Bridge" BOOTPROTO="none" IPADDR="192.168.0.50" # Inherited from your current active interface. PREFIX="24" GATEWAY="192.168.0.1" # Inherited from your current active interface. DNS1="8.8.8.8" DNS2="8.8.4.4" DEFROUTE="yes" NAME="Bridge Network br0"Next, we will make a few adjustments to the previous interface to ensure the new network bridge configuration is used here-out.
The most important setting here is ‘Bridge’.
/etc/sysconfig/network-scripts/ifcfg-eth0:
DEVICE="eth0" NM_CONTROLLED="yes" ONBOOT=yes TYPE="Ethernet" #BOOTPROTO=none #IPADDR=192.168.0.100 #PREFIX=24 #GATEWAY=192.168.0.1 #DNS1=8.8.8.8 #DNS2=8.8.4.4 #DEFROUTE=yes NAME="Ethernet Network eth0" BRIDGE="br0"To ensure that your new network bridge will take effect, simply restart your network interface:
service network restartTo check if your new network bridge is being utilised, run the following command:
ifconfig br0If you see br0 show up, you’re good to go.
3. Install Xen & Libvirt on CentOS 6.x.
To get started, install Xen and lib-virt on your CentOS 6.3 box:cd /etc/yum.repos.d/ wget http://drewsymo.com/files/xen-centos6.repo yum groupinstall -y "Development Tools" yum install -y transfig texi2html libaio-devel dev86 glibc-devel e2fsprogs-devel gitk mkinitrd iasl xz-devel bzip2-devel pciutils-libs pciutils-devel SDL-devel libX11-devel gtk2-devel PyXML qemu-img mercurial texinfo libidn-devel yajl yajl-devel ocaml ocaml-findlib ocaml-findlib-devel python-devel uuid-devel libuuid-devel openssl-devel libcgroup gnutls-utils radvd ebtables cyrus-sasl-md5 augeas nfs-utils libnetcf python-lxml nc lzop libnl-devel xhtml1-dtds libudev-devel libpciaccess-devel libpcap-devel avahi-devel parted-devel device-mapper-devel numactl-devel netcf-devel bridge-utils iscsi-initiator-utils qemu-kvm numad virt-install yum --disablerepo "*" --enablerepo "xen-stable" -y install xen* kernel-xen libvirt*
4. Add Xen to Your CentOS Boot Loader (GRUB)
To boot into the Xen kernel, you will need to update a few lines in your GRUB menu configuration.As an added convenience, by installing the Xen package via Yum earlier, your GRUB menu has already been preloaded with the new Xen kernel, however, a few adjustments will need to be made in order to ensure successful boot of Xen.
At the moment, you will see something like this in your GRUB configuration.
/boot/grub/menu.lst:
title CentOS (3.4.36-6.el6.centos.alt.x86_64) root (hd0,0) kernel /vmlinuz-3.4.36-6.el6.centos.alt.x86_64 ro root=UUID=7f49f046-616b-445d-8744-78761ad09dd8 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-3.4.36-6.el6.centos.alt.x86_64.imgYou will need to modify these lines to resemble the following configuration.
/boot/grub/menu.lst:
title CentOS (3.4.36-6.el6.centos.alt.x86_64) root (hd0,0) kernel /xen.gz dom0_mem=1024M cpufreq=xen dom0_max_vcpus=1 dom0_vcpus_pin module /vmlinuz-3.4.36-6.el6.centos.alt.x86_64 ro root=UUID=7f49f046-616b-445d-8744-78761ad09dd8 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet module /initramfs-3.4.36-6.el6.centos.alt.x86_64.imgYou will notice that we have added a new line for the Xen kernel, and prepended the existing lines with module.
5. Boot into Xen Kernel
Now this is starting to get a little exciting. Reboot your CentOS box to boot into your new Xen Kernel:rebootAssuming everything has gone to plan, you will now be in your new Xen Kernel. If you’re getting errors, it might be a good idea to adjust dom0_mem - or – dom0_max_vcpus to a different value. Alternatively, enable Kernel Debugging or check /var/dmesg for errors.
To verify that you’re in the new Xen Kernel, run the following command:
uname -r && xl infoThis should output the name of your kernel, the list of VM’s currently running and information about your new Xen hypervisor:
3.7.10-1.el6xen.x86_64 Name ID Mem VCPUs State Time(s) Domain-0 0 1024 1 r----- 64.7
6. Creating your First Virtual Machine
6.1. Create the VM Storage Device
To get started, we’ll create a 20GB LVM where our VM’s will be stored. In this example, we’ll be creating the LVM and Volume Group (vg0/debian6) on an existing physical volume (dev/sdb). To ascertain which physical volume to use, simply use the fdisk -l command.pvcreate -ff /dev/sdb vgcreate vg0 /dev/sdb lvcreate -L 20G -n debian6 vg0Next, we’ll scan the system for available LVM’s to verify succesfull creation:
lvscanThis should output:
ACTIVE '/dev/vg0/debian6' [20.00 GiB] inherit
6.2. Option 1: Create Debian VM using Xen XL and Prebuilt Debian Image
For this option, we’ll be using the XL toolstack to create the VM, along with a prebuilt Debian image from the OrionVM mirror.Next, we can obtain the necessary configuration files and format the VM image for provisioning:
wget http://mirror.orionvm.com.au/pub/debian/6/debian6.img.bz2 bzcat debian6.img.bz2 | dd bs=1M of=/dev// cd /etc/xen && wget http://mirror.orionvm.com.au/pub/debian/6/debian6.cfgNow, you will need to configure the debian6.cfg file to suit your requirements. Adjust the necessary parameters such as ‘memory’ and ‘vcpus’ to suit your requirements. By default, this premade debian6 config file will allocate 4096MB of memory and 8 vcpus.
Once this has been done, you can proceed with the provisioning of the Debian VM:
xl create -c debian6.cfgIf succesfull, you should now be connected to the Virtual Machine console. To exit the console, simply press:
Ctrl + ]
To view a list of Virtual Machines running, you can type:
xl list
6.3. Option 2: Create a Fedora VM using Libvirt Toolstack
Simply run the following commands to get started with your own Fedora VM via Libvirt:Note: We’ll use the debian6 example LVM we created earlier for brevity.
virt-install --virt-type xen -n fedora-vm-1 -r 512 --vcpus=2 -f /dev/vg0/debian6 --location http://mirror.internode.on.net/pub/fedora/linux/releases/18/Fedora/x86_64/os/ --os-type linux --accelerate --nographics --network=bridge:br0This method will pull all the necessary files for the Fedora installation via the Internode Mirror (as such, this can take some time)
You will now need to connect to the VM’s console and finish the installation of Fedora. This can be done by issuing:
virsh console fedora-vm-1To view a list of Virtual Machines running, you can type:
virsh list