个人工具

UbuntuHelp:XenVirtualMachine

来自Ubuntu中文

Wikibot讨论 | 贡献2007年12月8日 (六) 10:03的版本

跳转至: 导航, 搜索

This page is mostly old junk, and the useful parts are being migrated to Xen.

Using loopback-mounted-file

  1. Generate loopback file as sparse file (not written entirely to disk but filled on demand) (replace 'edgy.ext3' with whatever name you want for it, ie. 'system1.ext3':
     sudo dd if=/dev/zero of=/usr/local/edgy.ext3 bs=1024k seek=4096 count=0 
  2. Create filesystem within (replace 'edgy.ext3' with what the name you used in step 1):
     sudo mkfs.ext3 /usr/local/edgy.ext3 

Mkfs will tell you that this is no block device. Since you already know that (do you?) you can tell it to create the filesystem nevertheless. (so say Yes)

  1. Loopback mount filesystem (replace 'edgy' and 'edgy.ext3' with the name you used in step 1):
     sudo mkdir /mnt/edgy
     sudo mount /usr/local/edgy.ext3 /mnt/edgy -o loop 
  2. Install edgy base into it (replace 'edgy' with the release-name (version) of ubuntu you want (dapper, edgy, feisty, etc), replace '/mnt/edgy' with the name you used in step 1):

32bit Host O/S & 32bit virtual host sudo debootstrap edgy /mnt/edgy 64bit Host O/S & 64bit virtual host sudo debootstrap --arch amd64 edgy /mnt/edgy

  1. Give it the kernel modules (the module name depends on whether you use the desktop or server edition) (this implies you are installing the same release of ubuntu as you are currently running on the system as the host O/S, which is edgy throughout this example) (replace '/mnt/edgy' with what you used in step 1):

32bit Host OS & 32bit Virtual host: sudo cp -a /lib/modules/2.6.19-4-generic/ /mnt/edgy/lib/modules/ 64bit Host OS & 64bit Virtual host: sudo cp -a /lib/modules/2.6.19-4-generic-amd64/ /mnt/edgy/lib/modules/

  1. Change whatever you want to be changed before the first boot. Examples:

The network example below is somewhat confusing. Basically, whatever your Host O/S's (pre-existing) IP address is, put that in for the gateway, and make sure that the 'netmask' and 'address' put the virtual host into the same broadcast group as the host o/s (simpler example: address: 192.168.1.222 netmask: 255.255.255.0 gateway: 192.168.1.111, the 222 is the virtual host, the 111 is the host o/s's actual IP). If you followed the directions above regarding the network configuration you can run the following command: sudo nano /etc/network/interfaces and enter the following information

 cat /mnt/edgy/etc/network/interfaces 
 auto lo
 iface lo inet loopback

 auto eth0
 iface eth0 inet dhcp 

otherwise do the following:

 cat /mnt/edgy/etc/network/interfaces 
 auto lo
 iface lo inet loopback

 auto eth0
 iface eth0 inet static
 address 192.168.44.247
 netmask 255.255.252.0
 gateway 192.168.47.254 

Then configure the rest of the files:

 cat /mnt/edgy/etc/hosts
 127.0.0.1       localhost
 127.0.1.1       edgyvm 
 cat /mnt/edgy/etc/hostname 
 edgyvm 
 cat /mnt/edgy/etc/fstab
 proc            /proc           proc    defaults        0       0
 /dev/hda1       /               ext3    defaults,errors=remount-ro 0       1
 
  1. Umount the loopback partition
     
     sudo umount /mnt/edgy 
  2. Configure the guest, create /etc/xen/edgy-guest.cfg
     kernel = "/boot/vmlinuz-2.6.19-4-generic"
     ramdisk = "/boot/initrd.img-2.6.19-4-generic"
     builder='linux'
     memory = 128
     name = "edgy-guest"
     vcpus = 1
     vif = [ 'bridge=xenbr0' ]
     disk = [ 'file:/usr/local/edgy.ext3,ioemu:hda1,w' ]
     root = "/dev/hda1 ro"
     

Note: Change hda1 by sda1 if you are using some SATA or SCSI Controller

Note that root points to the virtual root partition of the domU and not of dom0. It is recommended to give each domU a swap partition, in such case your cfg-file may look as follows (don't forget to add it to the /etc/fstab of the virtual host, so it mounts it on boot-up):
disk = [ 'file:/usr/local/edgy.ext3,ioemu:hda1,w','phy:/dev/hda6,ioemu:hda2,w']
  1. Now you can create your xen domain
 
 sudo xm create edgy-guest.cfg 
  1. You can connect to the guest using the command below or any network based access that you install later on (openssh-server, vncserver, remote-X11, ...)
 
 sudo xm console edgy-guest 

Use crtl + ] to get out of console. If you logged in via putty use ctrl + 5 to leave the domu-console.

Hypervisor and dom0

The host for virtualized machines and the priviledged first guest

  1. The Xen kernel packages currently do not create an initrd image when they are installed. (This will probably be changed in the future). To create an initrd image run the following command:
    sudo mkinitramfs -o /boot/xen0-linux-2.6.17-6-generic-xen0.initrd.img 2.6.17-6-generic-xen0
    
    Respectively:

    sudo mkinitramfs -o /boot/xen0-linux-2.6.17-6-server-xen0.initrd.img 2.6.17-6-server-xen0

    if you are using the Ubuntu server distribution.
  2. Now that the kernel and initrd is installed, you have to setup grub. Add the following to your /boot/grub/menu.lst:
    title XEN/2.6.17
    root (hd0,0)
    kernel /boot/xen-3.0-i386-pae.gz
    module /boot/xen0-linux-2.6.17-6-generic-xen0 root=/dev/xxx ro
    module /boot/xen0-linux-2.6.17-6-generic-xen0.initrd.img
    

where xxx is the partition where your root is located. Edgy can use a new way of identifying you partitions by UUID. In this case, just use that syntax for root e.g. root=UUID=f5cf5b9a-ed1a-4d6a-a707-a606574e2b6f. You can obtain the UUIDs of your partitions using the command blkid (found in /sbin). If you are using the server edition, please make sure you select the right terms in the module lines (xen0-linux-2.6.17-6-server-xen0...). Note: On amd64, use xen-3.0-amd64.gz instead of xen-3.0-i386.gz

Note: kernel and modules may need to have the /boot removed from them to get this to work. ie
kernel /xen-3.0-i386.gz

domU

The guest machines running under the xen hypervisor Xen-tools can set up Ubuntu, Debian, CentOS systems, and also any other system if you give it a proper tarball. A domU guest system can be built using the same kernel image that was used for dom0.

  1. Decide whether to use a loopback-mounted file or a raw partition for a clients filesystem.

use loopback-mounted-file

  1. In case of loopback, proceed like:
    1. Generate loopback file as sparse file (not written entirely to disk but filled on demand:
      sudo dd if=/dev/zero of=/usr/local/diskimage-guest.ext3 bs=1024k seek=4096 count=0 
    2. Create filesystem within:
       
      sudo mkfs.ext3 /usr/local/diskimage-guest.ext3 
      Mkfs will tell you that this is no block device. Since you already know that (do you?) you can tell it to create the filesystem nevertheless.
    3. Loopback mount filesystem:
       
      sudo mount /usr/local/diskimage-guest.ext3 /mnt -o loop 
    4. Install debootstrap:
      sudo apt-get install debootstrap 
    5. Install edgy base into it:
       
      sudo debootstrap edgy /mnt 
    6. Give it the kernel modules (the module name depends on whether you use the desktop or server edition)
       
      sudo cp -a /lib/modules/2.6.17-6-generic-xen0/ /mnt/lib/modules 
    7. Change whatever you want to be changed before the first boot (/mnt/etc/apt/sources.list, /mnt/etc/network/interfaces, /mnt/etc/hostname, /mnt/etc/hosts)
    8. Unmount
       
      sudo umount /mnt 
    9. Configure the guest, create /etc/xen/edgy-guest.cfg
      kernel = "/boot/xen0-linux-2.6.17-6-generic-xen0"
      ramdisk = "/boot/xen0-linux-2.6.17-6-generic-xen0.initrd.img"
      builder='linux'
      memory = 128
      name = "edgy-guest"
      vcpus = 1
      vif = [ 'bridge=xenbr0' ]
      disk = [ 'file:/usr/local/diskimage-guest.ext3,ioemu:hda1,w' ]
      root = "/dev/hda1 ro"
      
      Note that root points to the virtual root partition of the domU and not of dom0. It is recommended to give each domU a swap partition, in such case your cfg-file may look as follows:
      disk = [ 'file:/usr/local/diskimage-guest.ext3,ioemu:hda1,w','phy:/dev/hda6,ioemu:hda2,w']
  2. Give it a try
  3. First start xend if it is not already started:
    xend start 
  4. Now you can create your xen domain
 
sudo xm create edgy-guest.cfg 
  1. You can connect to the guest using
     
    sudo xm console edgy-guest 
    or any network based access that you install later on (ssh or vnc or remote-X11)

use raw partition

  1. In case of a raw partition you want to use we need to create a file-system on it first.
    1. If e.g /dev/hda6 is our raw unformated partiton create a ext3-filesystem:
       
      mkfs -t ext3 /dev/hda6 
    2. The next step is to create a mountpoint and to mount it:
       
      sudo mkdir /media/hda6 && sudo mount /dev/hda6 /media/hda6 
    3. All what follows now is almost identical to "use loopback-mounted-file"
    4. Install edgy base into it:
       
      sudo debootstrap edgy /media/hda6 
    5. Give it the kernel modules:
       
      sudo cp -a /lib/modules/2.6.17-6-generic-xen0/ /media/hda6/lib/modules 
    6. Change whatever you want to be changed before the first boot (/media/hda6/etc/apt/sources.list, /media/hda6/etc/network/interfaces, /media/hda6/etc/hostname, /media/hda6/etc/hosts)
    7. Unmount
       
      sudo umount /media/hda6 
    8. Configure the guest, create /etc/xen/edgy-guest.cfg
      kernel = "/boot/xen0-linux-2.6.17-6-generic-xen0"
      ramdisk = "/boot/xen0-linux-2.6.17-6-generic-xen0.initrd.img"
      builder='linux'
      memory = 128
      name = "edgy-guest"
      vcpus = 1
      vif = [ 'bridge=xenbr0' ]
      disk = [ 'phy:/dev/hdX,hda1,w' ]
      root = "/dev/hda1 ro"
      
      Note that root points to the virtual root partition of the domU and not of dom0. It is recommended to give each domU a swap partition, in such case your cfg-file may look as follows:
      disk = [ 'phy:/dev/hdXX,hda1,w','phy:/dev/hdXY,hda2,w']
  2. Give it a try
  3. First start xend if it is not already started:
    xend start 
  4. Now you can create your xen domain
 
sudo xm create edgy-guest.cfg 
  1. You can connect to the guest using
     
    sudo xm console edgy-guest 
    or any network based access that you install later on (ssh or vnc or remote-X11)

Especially if you want to set up a larger number of virtual machines, it's strongly recommended to set up LVM and use logical volumes for your virtual machines.

Installing DomU w/ debootstrap

First, setup and configure lvm, create an ext3 and swap partition in lvm, and mount your lvm partition to /xen. apt-get install debootstrap I prefer to install from CD, as it's faster. Mount your CD-Rom to /media/cdrom, and mount your future domU root partition to /xen

debootstrap dapper /xen file:/media/cdrom

You should see lots of I: messages, and finally the prompt. Change directories to the root directory of your new filesystem.

Xen configuration options

Using your favorite editor (back in Dom0), create /etc/xen/dom1.cfg with the following text

kernel = "/boot/vmlinuz-2.6-xen"
memory = 128
name = "dom1"
vif = [ '' ]
dhcp = "dhcp"
disk = ['phy:/dev/xen_lvm/dom1,hda1,w','phy:/dev/xen_lvm/dom1_swap,hdb1,w']
root = "/dev/hda1 ro"
extra = "4"

Create a DomU

xm create dom1.cfg -c Login with root, no password (you want to change that!)

Changes Necessary on the DomU's Filesystem

mv ./lib/tls ./lib/tls.disabled in ./etc/network/interfaces (change the relevant numbers)

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
        address 192.168.3.101
        netmask 255.255.255.0
        network 192.168.3.0
        broadcast 192.168.3.255
        gateway 192.168.3.1
        # dns-* options are implemented by the resolvconf package, if installed
        dns-nameservers 1.2.3.4 1.2.3.5

Copy the Dom0's sources list: cp /etc/apt/sources.list ./etc/apt/sources.list and edit them if you wish. In ./etc/fstab

# /fstab: static file system information.
#
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
proc            /proc           proc    defaults        0       0
/dev/hda1       /               ext3    defaults,errors=remount-ro 0       1
/dev/hdb1       none            swap    sw              0       0

umount /dev/xen_lvm/dom1 (we don't want two operating systems writing to the same partition!) Locale setup: The locale isn't set up properly by default. Resolve this by:

Install base packages for your language:
  apt-get install language-pack-en-base

Install/Reconfigure localeconf via either:
  apt-get install localeconf
  dpkg-reconfigure localeconf

   * Manage locale configuration files with debconf? -> yes
   * Please select the default system locale. -> Your locale. Mine is en_US ISO-8859-1 for example.
   * Select the environment settings that should override the default locale.  -> Select All
   * In the following dialogs always select what you set for your locale above.

Install/reconfigure locales:
  apt-get install locales
  dpkg-reconfigure locales

   * Here, use the same locale you used in the localeconf setup.

Setting up your hostname

Manually edit /etc/hostname with your domU hostname.
Manually edit /etc/hosts (If you used debootstrap, you need to set this file up from scratch.)
  (set the 127.0.0.1 host in /etc/hosts to sync with the host in /etc/hostname)

At this point you want to set up sshd, so you can log into your new virtual machine remotely! You are ready!

lib tls error

This should only be on older versions of Ubuntu, or on domU guests maybe? If you receive a large warning message during boot-up about /lib/tls, you may do one of the following:

  1. mv /lib/tls /lib/tls.disabled (easy, but the issue may crop up again after an upgrade)
  2. Divert Xen using a Script (This makes sure upgrades don't put /lib/tls back again.
  3. Build your own glibc (Instructions from XenSource's Wiki)

Note: Disabling /lib/tls gives significantly poor performance for glibc. Find a way to install a Xen-friendly glibc. Removing /lib/tls is disabling the thread-local storage (tls) handling features of glibc by hiding them.

libcrypto error

Probably only on older versions of Ubuntu. If when starting xend, you get the error: blktapctrl: error while loading shared libraries: libcrypto.so.0.9.7: cannot open shared object file: No such file or directory Figure out the version of libcrypto you have: ls /usr/lib/libcrypto.so.* for example : /usr/lib/libcrypto.so.0.9.8 Make a symbolic link to it: ln -s /usr/lib/libcrypto.so.0.9.8 /usr/lib/libcrypto.so.0.9.7

ethx missing

When porting a physical system to a xen image file, eth0 was missing. Xen assigned eth2 as the first network interface. To correct this issue comment out eth0 in /etc/iftab. In fact this also happens if you move an installation to another hardware, because the numbers of the ethernet interfaces are bound to a specific MAC address (in the /etc/iftab table) by the ubuntu installer.

Windows DomU guest

In recent processors, Intel and AMD use an extension that gives xen the ability to run guest machines that are not aware of xen at all. The processor flag is called 'VMX' and is present in alle Core2 and some D9xx processors (called 'Vanderpool' or 'VT') as well as in AM2 processors ('called 'Pacifica'). With the help of qemu (a processor emulation project), xen is able to provide a complete boot process from bios. The necessary package is installed by apt-get install xen-ioemu-3.0 from universe repository.

Here is an example configuration (vmx-guest.cfg) using a bootable cdrom as iso-image under /usr/local/bootimage.iso and a loopback (see above) file /usr/local/diskimage-windows for the later harddisk image:
kernel = "/usr/lib/xen-ioemu-3.0/boot/hvmloader"
builder='hvm'
memory = 384
name = "vmx-guest"
vcpus=1
vif = [ 'bridge=xenbr0' ]
disk = ['file:/usr/local/diskimage-windows,ioemu:hda,w','file:/usr/local/bootimage.iso,hdc:cdrom,r']
device_model ='/usr/lib/xen-ioemu-3.0/bin/qemu-dm'
boot='d'
vnc=1
vncviewer=1
serial='pty'
ne2000=0

Note the new syntax for cdrom in xen 3.0.3! boot='d' lets the guest boot from the cd image, after finishing installation, the next boot should be launched from disk using boot='c' vnc=1 allows us to connect to the guest after a launch using the vncviewer software:

xm create vmx-guest.cfg
vncviewer localhost
  • You may face problems running other distributions than Edgy as domU, in such cases download the original XENsource archive and use the kernel version out of this packages:

wget http://bits.xensource.com/oss-xen/release/3.0.4-1/bin.tgz/xen-3.0.4_1-install-x86_32p.tgz tar zvfx xen-3.0.4_1-install-x86_32p.tgz cd dist

sudo ./install.sh
  • If you need a ramdisk, remember to create one with mkinitramfs before:

sudo depmod -v 2.6.16.33-xen cd /boot

sudo mkinitramfs -o /boot/initrd.img-2.6.16.33-xen 2.6.16.33-xen

Known Bugs

Steps

  • apt-get install ssh bridge-utils gcc make patch binutils zlib1g-dev python-dev libncurses-dev x-dev libssl-dev

The packages x-dev and libssl-dev seem to be necessary for building the xen-tools.

  • apt-get install bin86 bcc libc6-dev-i386

so you can build the hvmloader as well. Download source distribution, unpack, cd to xen-3.0.4_1-src and do

    # make prep-kernels
    # pico linux-2.6.16.33-xen/Makefile
        Add -fno-stack-protector to CFLAGS in the makefile above (I added it to line 343) - thanks to Peter Fokkinga for the tip.
    # make dist

Add cciss support to kernel in order for Xen to boot successfully on this HP box:

    # make linux-2.6-xen-config CONFIGMODE=menuconfig
        check "Device Drivers -> SCSI device support -> SCSI device support" - link statically.
        check "Device Drivers -> SCSI device support -> SCSI disk support" - link statically.
        check "Device Drivers -> SCSI device support -> SCSI generic support" - link statically.
        check "Device Drivers -> Block devices - >Compaq Smart Array 5xxx support" - link statically.
    # make linux-2.6-xen-build
    # make install

Add this to /boot/grub/menu.lst

title Xen 3.0.4 / XenLinux 2.6
kernel /boot/xen-3.0.gz console=vga
module /boot/vmlinuz-2.6-xen root=/dev/cciss/c0d0p1 ro console=tty0
savedefault
boot

and reboot. Now, if you try to export an LVM partition as "sda1" it will fail with a boot log containing "xen_blk: can't get major 8 with name sd" followed by a guest kernel panic. This is a known problem described in the XenFaq and the solution is to build a domU kernel w/o SCSI support. These are the steps to be done in directory xen-3.0.4_1-src/:

# make linux-2.6-xenU-config CONFIGMODE=menuconfig
  UNcheck "Device Drivers -> SCSI device support -> SCSI device support" completely
  remember to edit ...xenU/Makefile to get a no-stack-protection compile.
# make linux-2.6-xenU-build
# cp linux-2.6.16.33-xenU/vmlinuz /boot/vmlinuz-2.6-xenU

This config file then works for a Linux PV guest:

#  -*- mode: python; -*-

# Kernel image file.
kernel = "/boot/vmlinuz-2.6-xenU"
memory = 64
name = "debian1"
vif = [ '' ]
disk = [ 'phy:vm-disks/debian1,sda1,w', 'phy:vm-disks/debian1-swap,sda2,w' ]
root = "/dev/sda1 ro"
extra = "4"

and