XEN DOMU CENTOS

Embed Size (px)

Citation preview

1.Creating and installing a CentOS 5 domU instance 2.Installing Xen On CentOS 5.0 (i386)-Libernix 3.Installing and using a fully-virtualized Xen guest 4.move a native CentOS-4 installation into a paravirtualized Xen3 guest.

CreatingandinstallingaCentOS5domUinstanceNote: This write-up is of a general nature for each successive point release of CentOS 5, and is not tied to or specific for any particular 'point' respin of CentOS in the 5 series. Setup and usage of KVM is covered elsewhere.

IntroductionThis howto describes how you can create a CentOS 5 Xen domU instance, without using virt-manager, or its console variant virt-install. Usually, instances require more customization than these tools provide, so it is often better to make instances using the tools that lie underneath. The last section of this howto describes how you can perform the same steps with virsh, a generic tool for management of virtualized domains. This howto assumes that Xen is already installed, and that CentOS is running as dom0 (the privileged administrative domain). Put simply, this can be done by installing the xen and kernel-xen packages, and booting the Xen flavor of the kernel. The matter is covered in careful detail in the Virtualization Guide Besides that this howto currently assumes that a disk image file will be used to hold the domU disk. Though, using a physical partition or volume should be trivial. To clear up some terminology that is often confusing to new Xen users: dom0 is the privileged administrative domain of which only one can run. domU is an unprivileged domain, of which many can run at the same time. Although it is an incorrect analogy, it often helps to think of dom0 as the host system, and domU as a guest system.

Creating an imageThe first step is to create an image that will hold the domU virtual disk. Since this can just be a raw zero-filled file, our usual friend dd becomes handy. In this howto we will put images in /srv/xen, although the semi-officially sanctioned location seems to be /var/lib/xen/images. If you would like to allocate disk blocks as the file grows, you can create a file with a hole. The following command will create a /srv/xen/mailserver.img file of 2048MB, although the actual data blocks are allocated in a lazy fashion:# dd if=/dev/zero of=/srv/xen/mailserver.img oflag=direct bs=1M seek=2047 count=1

If you want to reserve all the data blocks right away, you can also do that:# dd if=/dev/zero of=/srv/xen/mailserver.img oflag=direct bs=1M count=2048

This will avoid data block allocation problems if the volume that holds the image is full. If you are using SELinux, it is important to check that the image has the correct security context (xen_image_t), or access to the virtual disk will be denied to the domU system. You can check this with ls:# ls -Z /srv/xen/mailserver.img -rw-r--r-- root root user_u:object_r:xen_image_t /srv/xen/mailserver.img

If you are having trouble setting the right file context, please have a look at the Xen tips and tricks page. At any rate, turning off SELinux, as some howtos on this subject advise is a very poor workaround. Reading two manual pages (semanage(8) and restorecon(8)), for an extra layer of security is a good trade!

Preparing a Xen configuration file for the installationXen uses a configuration file per domain. The configuration for the domain will be slightly different during the installation, because we have to provide installation kernels, and possibly some boot parameters. You will need an domU installation initrd image and kernel. Depending on the machine architecture, both can be downloaded from: http://mirror.centos.org/centos/5/os/i386/images/xen/ or http://mirror.centos.org/centos/5/os/x86_64/images/xen/ You can put them in some sensible directory, and rename them appropriately. In this example, the kernel and initrd image will be named /boot/vmlinuzxen-install and /boot/initrd-xen-install respectively.

With the images in place, you can create the installation configuration file, named /etc/xen/mailserver here:kernel = "/boot/vmlinuz-xen-install" ramdisk = "/boot/initrd-xen-install" extra = "text ks=http://localserver/minimal-ks.cfg" name = "mailserver" memory = "256" disk = [ 'tap:aio:/srv/xen/mailserver.img,xvda,w', ] vif = [ 'bridge=xenbr0', ] vcpus=1 on_reboot = 'destroy' on_crash = 'destroy'

You may want to tune some of the parameters, like the amount of memory that is dedicated to the domU, or the name of the bridge, if you will be using a different bridge for this domU. Besides that, this configuration file uses a kickstart file to perform the installation automatically. Remove the ks=http://localserver/minimal-ks.cfg stanza if you would like to do an interactive installation. This example also explicitly uses a text installation - it is possible to do a graphical installation with a VNC viewer. But in my experience a text installation is much more comfortable, because you can attach a (text) console to the domain at any time, allowing you to use or watch Anaconda directly.

Example kickstart fileFor reference, this is an example kickstart file. If you want to use it, you *should* modify it for your specific situation (and at the very least modify the root password). This kickstart file makes a very minimal installation, which is handy if you would like to make copies of an image to deploy new instances rapidly.install url --url http://mirror.centos.org/centos/5/os/i386 lang en_US.UTF-8 network --device eth0 --bootproto dhcp # Bogus password, change to something sensible! rootpw bogus firewall --enabled --port= authconfig --enableshadow --enablemd5 selinux --enforcing port=22:tcp timezone --utc Europe/Amsterdam bootloader --location=mbr --driveorder=xvda --append="console=xvc0" reboot # Partitioning

clearpart --all --initlabel --drives=xvda part /boot --fstype ext3 --size=100 --ondisk=xvda part pv.2 --size=0 --grow --ondisk=xvda volgroup VolGroup00 --pesize=32768 pv.2 logvol / --fstype ext3 --name=LogVol00 --vgname=VolGroup00 --size=1024 --grow logvol swap --fstype swap --name=LogVol01 --vgname=VolGroup00 --size=256 --grow --maxsize=512 %packages @core

Starting the installationWith the installation configuration set up, you can launch the domU instance:# xm create mailserver

If you configured this domU correctly, the installation will happily start. If you chose to do a manual installation, or would like to see kickstart in action, you can attach a console to the domU:# xm console mailserver

After the installation, the domU will be rebooted and destroyed (since that is the default action for reboots, we will change that later).

Post-install configurationNow that the installation is finished, this can be a good time to make a copy of the instance image to use as a template. At this point the SSH keys are not generated yet, making it easier to give each instance a unique set of SSH keys. The installation configuration should now be modified for non-install use. This is the modified configuration:name = "mailserver" memory = "256" disk = [ 'tap:aio:/srv/xen/mailserver.img,xvda,w', ] vif = [ 'bridge=xenbr0', ] bootloader="/usr/bin/pygrub" vcpus=1 on_reboot = 'restart' on_crash = 'restart'

As you can see, this new configuration is not using the kernel and initrd images anymore. Instead, it is using pygrub as a bootloader. pygrub will try to look for a partition holding a filesystem that contains the GRUB configuration in the virtual disk image. If a GRUB configuration was found, this will be used to boot a kernel. This is very handy, because this will allow you to use/manage kernels in the domU. If yum update pulls in a security updated kernel in the domU, it will automatically be used during the next boot of the domain. Another change is that we have changed how to handle crashes and reboots. You'll usually want to reboot a domain if a crash or reboot occurs. With this configuration in place, you can test this domain:# xm create mailserver

If you have installed SSH, you can log in to that domain through SSH, or you can use the Xen console:# xm console mailserver

You can now administrate the domain as a usual CentOS machine. You can shut down the domain with:# xm shutdown mailserver

For more information on the options that xm provides, please refer to the xm manual page.

Automatically starting domainsIf you would like a domain to start automatically when the (dom0) system is started, move the domain configuration to the /etc/xen/auto directory. For instance:# mv /etc/xen/mailserver /etc/xen/auto

This will also shut down the domain properly when the system is shut down.

Using virsh/libvirtThe libvirt library is being developed to provide an abstraction API for the various virtual machine technology that is out there. An older version of libvirt that supports Xen is included in CentOS 5. Newer versions also provide abstraction for KVM and qemu. libvirt can be used from the shell with the virsh command. This command can be used to manage domains in a comparable fashion to xm. The domain creation and shutdown commands are the most important commands in the context of this article:virsh create FILE

virsh shutdown domain-name/id/uuid

libvirt uses XML descriptions for domains. The format of these domain descriptions is covered on the libvirt website. To get you started, here are the domain descriptions for respectively the installation configuration: mailserver linux /boot/vmlinuz-xen-install /boot/initrd-xen-install text ks=http://localserver/minimal-ks.cfg 262144 1 destroy destroy destroy

and the run-time configuration: mailserver /usr/bin/pygrub linux 262144

1 destroy restart restart

Suppose that these configuration files are named /srv/xen/mailserver.xml, you can create and shutdown the domain with:# virsh create /srv/xen/mailserver.xml # virsh shutdown mailserver

Installing Xen On CentOS 5.0 (i386)Dom0 - host operating system. DomUs - guest operating systems (*nix operating systems like Linux and FreeBSD), so called "virtual machines" Using Xen you can separate your applications into different virtual machines that are totally independent from each other

(e.g. a virtual machine for a mail server, a virtual machine for a high-traffic web site, virtual machine that serves your customers' web sites, a virtual machine for DNS, etc.), but still use the same hardware. This saves money, and what is even more important, it's more secure. If the virtual machine of your DNS server gets hacked, it has no effect on your other virtual machines. you can move virtual machines from one Xen server to the next one. I will use CentOS 5.0 (i386) for both the host OS (dom0) and the guest OS (domU).

1 Preliminary NoteI use the following partitions on my CentOS 5.0 host system (dom0): /boot 150 MB (ext3) swap 1GB / 3GB (ext3) /vm the rest (ext3)

for

virtual machines

Installing XenTo install Xen, we simply run [root@server1 ~]# yum install kernel-xen xen Before we can boot the system with the Xen kernel, we must tell the bootloader GRUB about it. We open /boot/grub/menu.lst: [root@server1 ~]# vi /boot/grub/menu.lst and add the following stanza above all other kernel stanzas:

[...] title CentOS (2.6.18-8.1.4.el5xen) root (hd0,0) kernel /xen.gz-2.6.18-8.1.4.el5 module /vmlinuz-2.6.18-8.1.4.el5xen ro root=/dev/VolGroup00/LogVol00 module /initrd-2.6.18-8.1.4.el5xen.img [...]

If you use a separate /boot the /boot/grub/menu.lst is[...] title CentOS (2.6.18-8.1.4.el5xen) root (hd0,0) kernel /boot/xen.gz-2.6.18-8.1.4.el5 module /boot/vmlinuz-2.6.18-8.1.4.el5xen ro root=/dev/VolGroup00/LogVol00 module /boot/initrd-2.6.18-8.1.4.el5xen.img [...]

[root@server1 ~]# shutdown -r now The system should now automatically boot the new Xen kernel. After the system has booted, we can check that by running [root@server1 ~]# uname -r 2.6.18-8.1.4.el5xen So it's really using the new Xen kernel! to check if Xen has started. It should list Domain-0 (dom0): [root@server1 ~]# xm list Name ID Mem(MiB) VCPUs State Time(s)

3 Creating A Virtual MachineVirt-install - can create virtual machines for Xen. [root@server1 ~]# virt-install The tools asks a few questions before it creates a virtual machine. I want to call my first virtual machine vm01, with 256MB RAM and a disk size of 4GB. I want to store it in the file /vm/vm01.img: What is the name of your virtual machine?