Using Xen With LVM-Based VMs Instead Of Image-Based VMs (Debian Etch)
Version 1.0
Author: Falko Timme
This guide explains how you can set up LVM-based virtual machines on a Xen host running on Debian Etch instead of virtual machines that use disk images. Virtual machines that use disk images are very slow and heavy on disk IO.
I do not issue any guarantee that this will work for you!
1 Preliminary Note
In this example I'm using a Debian Etch host with the LVM volume group /dev/vg0 that has about 50GB of space. /dev/vg0 contains two logical volumes, /dev/vg0/root and /dev/vg0/swap_1 that consume about 7GB of space - the rest is not allocated and can be used to create logical volumes for our virtual machines:
vgdisplay
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 49.76 GB
PE Size 4.00 MB
Total PE 12738
Alloc PE / Size 1792 / 7.00 GB
Free PE / Size 10946 / 42.76 GB
VG UUID JbjZw7-aOGY-3iA4-iW0M-KAhu-86fz-Ap3wLw
server1:~#
lvdisplay
server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/vg0/root
VG Name vg0
LV UUID HH15eU-MoDJ-NfPg-HMZ6-Fczf-uPQO-I0NDzp
LV Write Access read/write
LV Status available
# open 1
LV Size 5.00 GB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:0
--- Logical volume ---
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID GV0SC0-oZLA-rQZS-pe3k-fi2N-YU9q-A73rm3
LV Write Access read/write
LV Status available
# open 2
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:1
server1:~#
I'm assuming that you've already set up Xen - e.g. as described in the following two guides:
2 Creating LVM-Based Virtual Machines (domU)
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. xen-tools are available as a Debian Etch package, so we install that one right now:
apt-get install xen-tools
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
[...] lvm = vg0 [...] dist = etch # Default distribution to install. [...] gateway = 192.168.0.1 netmask = 255.255.255.0 [...] passwd = 1 [...] kernel = /boot/vmlinuz-2.6.18-6-xen-vserver-686 initrd = /boot/initrd.img-2.6.18-6-xen-vserver-686 [...] mirror = http://ftp2.de.debian.org/debian/ [...] |
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!
The passwd = 1 line makes that you can specify a root password when you create a new guest domain.
In the kernel and initrd lines you must specify the domU kernel and initrd that you want to use for your guest domains. Normally this is /boot/vmlinuz- + the output of uname -r and /boot/initrd.img- + the output of uname -r, so if
uname -r
displays 2.6.18-6-xen-vserver-686, then this translates to /boot/vmlinuz-2.6.18-6-xen-vserver-686 and /boot/initrd.img-2.6.18-6-xen-vserver-686. You can find out which kernels and initrds are available by running
ls -l /boot/
In the mirror line specify a Debian mirror close to you.
Make sure you specify a gateway and netmask. 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!
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=10Gb --swap=512Mb --ip=192.168.0.101 --force --memory=256Mb --arch=i386 --debootstrap
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.
(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:
server1:~# xen-create-image --hostname=xen1.example.com --size=10Gb --swap=512Mb --ip=192.168.0.101 --force --memory=256Mb --arch=i386 --debootstrap
General Infomation
--------------------
Hostname : xen1.example.com
Distribution : etch
Fileystem Type : ext3
Size Information
----------------
Image size : 10Gb
Swap size : 512Mb
Image type : full
Memory size : 256Mb
Kernel path : /boot/vmlinuz-2.6.18-6-xen-vserver-686
Initrd path : /boot/initrd.img-2.6.18-6-xen-vserver-686
Networking Information
----------------------
IP Address 1 : 192.168.0.101
Netmask : 255.255.255.0
Gateway : 192.168.0.1
Creating ext3 filesystem on /dev/vg0/xen1.example.com-disk
Done
Installing your system with debootstrap mirror http://ftp2.de.debian.org/debian/
Done
Running hooks
Done
No role script specified. Skipping
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
server1:~#
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:
server1:~# lvdisplay
--- Logical volume ---
LV Name /dev/vg0/root
VG Name vg0
LV UUID HH15eU-MoDJ-NfPg-HMZ6-Fczf-uPQO-I0NDzp
LV Write Access read/write
LV Status available
# open 1
LV Size 5.00 GB
Current LE 1280
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:0
--- Logical volume ---
LV Name /dev/vg0/swap_1
VG Name vg0
LV UUID GV0SC0-oZLA-rQZS-pe3k-fi2N-YU9q-A73rm3
LV Write Access read/write
LV Status available
# open 1
LV Size 2.00 GB
Current LE 512
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:1
--- Logical volume ---
LV Name /dev/vg0/xen1.example.com-disk
VG Name vg0
LV UUID yya7Uj-klRJ-hN9z-nKVk-YgIV-vI1j-KXT53a
LV Write Access read/write
LV Status available
# open 0
LV Size 10.00 GB
Current LE 2560
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:2
--- Logical volume ---
LV Name /dev/vg0/xen1.example.com-swap
VG Name vg0
LV UUID pJcl71-YFPf-t0mr-hSIN-4s8J-xkWD-BEvtNv
LV Write Access read/write
LV Status available
# open 0
LV Size 512.00 MB
Current LE 128
Segments 1
Allocation inherit
Read ahead sectors 0
Block device 254:3
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 on # Sat Dec 13 02:01:35 2008. # # # Kernel + memory size # kernel = '/boot/vmlinuz-2.6.18-6-xen-vserver-686' ramdisk = '/boot/initrd.img-2.6.18-6-xen-vserver-686' memory = '256' # # Disk device(s). # root = '/dev/sda1 ro' disk = [ 'phy:vg0/xen1.example.com-disk,sda1,w', 'phy:vg0/xen1.example.com-swap,sda2,w' ] # # Hostname # name = 'xen1.example.com' # # Networking # vif = [ 'ip=192.168.0.101' ] # # Behaviour # on_poweroff = 'destroy' on_reboot = 'restart' on_crash = 'restart' |
(If we had used disk images instead of logical volumes, the disk line would look similar to this one:
disk = [ 'file:/path/to/xen1.example.com/disk.img,hda1,w', 'file:/path/to/xen1.example.com/swap.img,hda2,w' ]
)
To start the virtual machine, run
xm create /etc/xen/xen1.example.com.cfg
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:
server1:~# xm list
Name ID Mem(MiB) VCPUs State Time(s)
Domain-0 0 747 1 r----- 1402.9
xen1.example.com 1 256 1 -b---- 55.8
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:
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
server1:~# xen-list-images
Name: xen1.example.com
Memory: 256
IP: 192.168.0.101
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
3 Links
- Xen: http://www.xensource.com/xen/
- xen-tools: http://xen-tools.org/software/xen-tools
- Debian: http://www.debian.org/