Paravirtualization With Xen 4.0 On Debian Squeeze (AMD64) - Page 2
On this page
4 Creating LVM-Based Virtual Machines
This chapter explains how you can set up LVM-based virtual machines instead of virtual machines that use disk images. Virtual machines that use disk images are very slow and heavy on disk IO.
In this example I'm using a Debian Squeeze host with the LVM volume group /dev/vg0 that has about 500GB of space. /dev/vg0 contains two logical volumes, /dev/vg0/root and /dev/vg0/swap_1 that consume about 100GB of space - the rest is not allocated and can be used to create logical volumes for our virtual machines:
vgdisplay
root@server1:~# vgdisplay
--- Volume group ---
VG Name vg0
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 465.29 GiB
PE Size 4.00 MiB
Total PE 119115
Alloc PE / Size 24794 / 96.85 GiB
Free PE / Size 94321 / 368.44 GiB
VG UUID YZruwf-nqxs-H47I-0WUE-RDJ7-SmEP-XU1RbH
root@server1:~#
lvdisplay
root@server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/vg0/root
VG Name vg0
LV UUID 0dXvE5-gjHP-0q7c-qpq6-veOx-faSg-p11vUp
LV Write Access read/write
LV Status available
# open 1
LV Size 93.13 GiB
Current LE 23841
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
--- Logical volume ---
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID FqnT46-fLHS-NLqC-XGOR-fNPe-NYyT-TEw8bR
LV Write Access read/write
LV Status available
# open 1
LV Size 3.72 GiB
Current LE 953
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1
root@server1:~#
We will use xen-tools to create virtual machines. xen-tools make it very easy to create virtual machines - please read this tutorial to learn more: https://www.howtoforge.com/xen_tools_xen_shell_argo. We've already installed xen-tools in chapter 2.
Next we edit /etc/xen-tools/xen-tools.conf. This file contains the default values that are used by the xen-create-image script unless you specify other values on the command line. I changed the following values and left the rest untouched:
vi /etc/xen-tools/xen-tools.conf
[...] # dir = /home/xen [...] lvm = vg0 [...] dist = `xt-guess-suite-and-mirror --suite` # Default distribution to install. [...] gateway = 192.168.0.1 netmask = 255.255.255.0 broadcast = 192.168.0.255 [...] passwd = 1 [...] kernel = /boot/vmlinuz-`uname -r` initrd = /boot/initrd.img-`uname -r` [...] mirror = `xt-guess-suite-and-mirror --mirror` [...] serial_device = hvc0 #default [...] disk_device = xvda #default [...] |
Make sure that you uncomment the lvm line and fill in the name of your volume group (vg0 in my case). At the same time make sure that the dir line is commented out (these settings are mutually exclusive)!
dist specifies the distribution to be installed in the virtual machines (if you run the command
xt-guess-suite-and-mirror --suite
on the command line, you will see that it translates to squeeze, so in this case Debian Squeeze would be installed unless you specify anything else on the command line).
To find out which distributions you can install in a virtual machine, run:
gunzip /usr/share/doc/xen-tools/README.gz
cat /usr/share/doc/xen-tools/README
The passwd = 1 line makes that you can specify a root password when you create a new guest domain.
The kernel and initrd lines specify the kernel and ramdisk that get installed in a virtual machine. If you want to install Debian Squeeze, please use the settings as shown above (otherwise the Debian Squeeze guest might not boot). If you want to install Ubuntu Maverick, for example, you can use the same settings, or you comment out both lines, in which case the default Ubuntu kernel would be used (yes, Xen 4.0 allows you to use non-Xen kernels in a guest!).
Make sure you specify a gateway, netmask, and broadcast address. If you don't, and you don't specify a gateway and netmask on the command line when using xen-create-image, your guest domains won't have networking even if you specified an IP address!
The mirror line specifies the mirror to use (the command
xt-guess-suite-and-mirror --mirror
translates to a Debian mirror by default. Of course, you can specify another mirror, e.g. as follows:
mirror = http://ftp.de.debian.org/debian/
or
mirror = http://archive.ubuntu.com/ubuntu (for Ubuntu; in this case make sure that you specify an Ubuntu version in the dist line, e.g. dist = maverick)
)
It is very important that you add the line serial_device = hvc0 because otherwise your virtual machines might not boot properly!
disk_device = xvda makes that the virtualized disks are named xvda1, xvda2, etc. This is the correct setting for a Debian Squeeze guest; Ubuntu guests, especially when using a non-Xen kernel, might not boot with this setting, but you can override it with the --scsi switch that you pass to the xen-create-image command - in this case the virtualized disks will be named sda1, sda2, etc.
To summarize, the above settings are perfect for Debian Squeeze guests; if you want to install Ubuntu Maverick instead, you will have to override some of the settings in /etc/xen-tools/xen-tools.conf (--dist, --mirror, --scsi switches; maybe also comment out kernel and initrd) - I'll come to that in a moment.
Now let's create our first guest domain, xen1.example.com, with the IP address 192.168.0.101:
xen-create-image --hostname=xen1.example.com --size=4Gb --swap=256Mb --ip=192.168.0.101 --memory=256Mb --arch=amd64 --role=udev
Options that you specify on the command line override the settings in /etc/xen-tools/xen-tools.conf. Options that are not specified on the command line are taken from /etc/xen-tools/xen-tools.conf. Please make sure that you add --role=udev, or your virtual machine might not boot properly!
(To learn more about the available options, take a look at the xen-create-image man page:
man xen-create-image
)
The xen-create-image command will now create the xen1.example.com virtual machine for us. This can take a few minutes. The output should be similar to this one:
root@server1:~# xen-create-image --hostname=xen1.example.com --size=4Gb --swap=256Mb --ip=192.168.0.101 --memory=256Mb --arch=amd64 --role=udev
General Information
--------------------
Hostname : xen1.example.com
Distribution : squeeze
Mirror : http://ftp.de.debian.org/debian/
Partitions : swap 256Mb (swap)
/ 4Gb (ext3)
Image type : full
Memory size : 256Mb
Kernel path : /boot/vmlinuz-2.6.32-5-xen-amd64
Initrd path : /boot/initrd.img-2.6.32-5-xen-amd64
Networking Information
----------------------
IP Address 1 : 192.168.0.101 [MAC: 00:16:3E:E8:61:97]
Netmask : 255.255.255.0
Broadcast : 192.168.0.255
Gateway : 192.168.0.1
Creating swap on /dev/vg0/xen1.example.com-swap
Done
Creating ext3 filesystem on /dev/vg0/xen1.example.com-disk
Done
Installation method: debootstrap
Done
Running hooks
Done
Role: udev
File: /etc/xen-tools/role.d/udev
Role script completed.
Creating Xen configuration file
Done
Setting up root password
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
All done
Logfile produced at:
/var/log/xen-tools/xen1.example.com.log
Installation Summary
---------------------
Hostname : xen1.example.com
Distribution : squeeze
IP-Address(es) : 192.168.0.101
RSA Fingerprint : 6b:88:61:9a:ea:8a:7b:0d:24:34:ae:ee:2e:75:d6:53
Root Password : N/A
root@server1:~#
For an Ubuntu Maverick guest, you should use this command instead:
xen-create-image --hostname=xen1.example.com --size=4Gb --swap=256Mb --ip=192.168.0.101 --memory=256Mb --arch=amd64 --role=udev --scsi --dist maverick --mirror=http://archive.ubuntu.com/ubuntu
(To use the default Ubuntu kernel instead of Debian's Xen kernel in the guest, you can also comment out the kernel and initrd lines in /etc/xen-tools/xen-tools.conf.)
As you see from the output, xen-create-image has created a new logical volume for our VM in the vg0 volume group, /dev/vg0/xen1.example.com-disk, for the VM's root filesystem. Take a look at
lvdisplay
and you will see that it has also created a second logical volume, /dev/vg0/xen1.example.com-swap, for the VM's swap:
root@server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/vg0/root
VG Name vg0
LV UUID 0dXvE5-gjHP-0q7c-qpq6-veOx-faSg-p11vUp
LV Write Access read/write
LV Status available
# open 1
LV Size 93.13 GiB
Current LE 23841
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:0
--- Logical volume ---
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID FqnT46-fLHS-NLqC-XGOR-fNPe-NYyT-TEw8bR
LV Write Access read/write
LV Status available
# open 1
LV Size 3.72 GiB
Current LE 953
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:1
--- Logical volume ---
LV Name /dev/vg0/xen1.example.com-swap
VG Name vg0
LV UUID TIc1fp-Lfkm-Ulm9-ElaP-zM4F-LEjc-cVw2Jk
LV Write Access read/write
LV Status available
# open 0
LV Size 256.00 MiB
Current LE 64
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:2
--- Logical volume ---
LV Name /dev/vg0/xen1.example.com-disk
VG Name vg0
LV UUID ocZI2U-drBa-1iCC-DMy1-toWk-5hex-utJ4FA
LV Write Access read/write
LV Status available
# open 0
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 254:3
root@server1:~#
There should now be a xen1.example.com configuration file - /etc/xen/xen1.example.com.cfg. The disk line contains physical devices (the two logical volumes created by xen-create-image) instead of disk images:
cat /etc/xen/xen1.example.com.cfg
# # Configuration file for the Xen instance xen1.example.com, created # by xen-tools 4.2 on Fri Mar 25 16:56:49 2011. # # # Kernel + memory size # kernel = '/boot/vmlinuz-2.6.32-5-xen-amd64' ramdisk = '/boot/initrd.img-2.6.32-5-xen-amd64' vcpus = '1' memory = '256' # # Disk device(s). # root = '/dev/xvda2 ro' disk = [ 'phy:/dev/vg0/xen1.example.com-disk,xvda2,w', 'phy:/dev/vg0/xen1.example.com-swap,xvda1,w', ] # # Physical volumes # # # Hostname # name = 'xen1.example.com' # # Networking # vif = [ 'ip=192.168.0.101,mac=00:16:3E:E8:61:97' ] # # Behaviour # on_poweroff = 'destroy' on_reboot = 'restart' on_crash = 'restart' |
(If we had used disk images instead of logical volumes, the disk part would look similar to this one:
disk = [
'file:/home/xen/domains/xen1.example.com/disk.img,xvda2,w',
'file:/home/xen/domains/xen1.example.com/swap.img,xvda1,w',
]
)
(Please note: if you have a dual-core or quad-core CPU and want the virtual machine to use all CPU cores, please change the vcpus line to vcpus = '2' or vcpus = '4'.)
To start the virtual machine, run
xm create /etc/xen/xen1.example.com.cfg
root@server1:~# xm create /etc/xen/xen1.example.com.cfg
Using config file "/etc/xen/xen1.example.com.cfg".
Started domain xen1.example.com (id=1)
root@server1:~#
Run
xm console xen1.example.com
to log in on that virtual machine (type CTRL+] if you are at the console, or CTRL+5 if you're using PuTTY to go back to dom0), or use an SSH client to connect to it (192.168.0.101).
To get a list of running virtual machines, type
xm list
The output should look like this:
root@server1:~# xm list
Name ID Mem VCPUs State Time(s)
Domain-0 0 2811 2 r----- 136.1
xen1.example.com 2 256 1 -b---- 1.7
root@server1:~#
To shut down xen1.example.com, do this:
xm shutdown xen1.example.com
If you want xen1.example.com to start automatically at the next boot of the system, then do this:
mkdir /etc/xen/auto
ln -s /etc/xen/xen1.example.com.cfg /etc/xen/auto
Here are the most important Xen commands:
xm create -c /path/to/config - Start a virtual machine.
xm shutdown <name> - Stop a virtual machine.
xm destroy <name> - Stop a virtual machine immediately without shutting it down. It's as if you switch off the power button.
xm list - List all running systems.
xm console <name> - Log in on a virtual machine.
xm help - List of all commands.
A list of all virtual machines that were created with the xen-create-image command is available under
xen-list-images
root@server1:~# xen-list-images
Name: xen1.example.com
Memory: 256
IP: 192.168.0.101
root@server1:~#
To learn more about what you can do with xen-tools, take a look at this tutorial: https://www.howtoforge.com/xen_tools_xen_shell_argo
5 Links
- Xen: http://www.xensource.com/xen/
- xen-tools: http://xen-tools.org/software/xen-tools
- Debian: http://www.debian.org/