在 Ubuntu 上安装和配置 Xen

服务器硬盘已经升级了,还在等内存,内存是和硬盘一起订的货不知道怎么还没到,终于发现前天 RAID 0 的测试结果为什么不正常,因为有个硬盘有问题,昏~新买的刚拆包装的硬盘居然就有问题。现在大多数 SUN 服务器上只有 2GB-8GB 的内存,升级后应该达到 8GB-32GB,这样就可以给每个 Xen 虚拟机更多的内存了。我们有一些操作系统还是32位的,现在也必须升级到64位以应付大内存的到来。今天在一台 Sun Fire X2200 M2 上最小化安装64位的 Ubuntu,升级更新后开始安装和配置 Xen. 下面的安装步骤和配置过程基于 Ubuntu 9.04 Server 版本,CentOS 版本可以参看:在 CentOS 上安装和配置 Xen,Debian 版本可以参看:在 Debian 上安装和配置 Xen,OpenSolaris 版本参看:在 OpenSolaris 上安装和配置 Xen,NetBSD 版本参看:在 NetBSD 上安装和配置 Xen.

安装 Xen

安装支持 Xen 的相关工具:

$ sudo apt-get install ubuntu-xen-server

下载和安装支持 Xen 的 Linux 内核:

http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/linux-modules-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb
http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/linux-image-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb

$ sudo dpkg -i linux-modules-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb 
$ sudo dpkg -i linux-image-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb

安装成功后,检查 Xen 内核是否已经装好:

$ sudo vi /boot/grub/menu.lst

...
title       Xen 3.3 / Ubuntu 9.04, kernel 2.6.26-2-xen-amd64
root        (hd0,0)
kernel      /xen-3.3.gz
module      /vmlinuz-2.6.26-2-xen-amd64 root=/dev/mapper/nvidia_igcjceec3 ro console=tty0
module      /initrd.img-2.6.26-2-xen-amd64
...

重启系统后检查一下是否是 Xen 的内核以及 Xen 是否启动:

$ uname -r
2.6.26-2-xen-amd64

$ sudo xm list
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1879     2     r-----      6.6

创建和安装 guest

如果把正在运行的 Xen 内核的操作系统看成 host 的话,那么运行在 host 上面的操作系统就可以看成 guest。先建一个目录用来存放所有的 guest 操作系统的 image,vm01.img,vm02.img,等等,1个 guest 对应1个 image. 使用 xen-create-image 后会在 /vm 下生成以 hostname 命名的目录,这个目录下会有 vm01.vpsee.com/disk.img,vm01.vpsee.com/swap.img 两个镜像文件。

$ sudo mkdir /vm

$ sudo xen-create-image --hostname=vm01.vpsee.com \
--ip=172.16.39.101 --gateway=172.16.38.1 --netmask=255.255.254.0  \
--dist=feisty --mirror=http://archive.ubuntu.com/ubuntu --size=10gb \
--memory=512mb --swap=512mb --arch=amd64 --dir=/vm \
--boot --passwd --role udev --dir=/etc/xen --force

可以根据需要调整 /etc/xen-tools/xen-tools.conf

进入 guest

安装成功后就可以登录 guest 了,

$ sudo xm console vm01

如果要退出 guest 的话,用 Ctrl + ],就会回到 host。

如果想每次启动 host 时自动启动 guest 的话:

$ sudo ln -s /etc/xen/vm01 /etc/xen/auto

更懒的办法

如果觉得安装 guest 操作系统太麻烦,可以到 http://stacklet.com/ 下载一个已经装好的镜像文件,调整 Xen guest 的配置文件,然后在 host 下启动 guest 镜像就可以了。

$ wget http://stacklet.com/sites/default/files/ubuntu/
ubuntu.9-04.x86-64.20090514.img.tar.bz2

$ tar jxvf ubuntu.9-04.x86-64.20090514.img.tar.bz2
$ sudo mv ubuntu.9-04.x86-64.xen3.pygrub.cfg /etc/xen/ubuntu.9-04.cfg
$ sudo vi /etc/xen/ubuntu.9-04.cfg
$ sudo mv ubuntu.9-04.x86-64.img /vm/ubuntu.9-04.img

$ sudo xm create -f ubuntu.9-04.cfg
Using config file "/etc/xen/ubuntu.9-04.cfg".
Error: Device 2049 (vbd) could not be connected. Failed to find an unused loop device

$ sudo vi /etc/modules
loop max_loop=64

$ sudo xm list
$ sudo xm console ubuntu.9-04

从 http://stacklet.com/ 下载的镜像文件的初始用户名和密码为:root/password

增加 swap

如何给 guest 系统(domU)增加 swap,这里有介绍。

评论 (12 Comments)

  1. 您好,看了您网站的文档,受益颇多。现看这篇文章里面最后一段介绍安装Guest OS的最简单的一种方法,就是把已经安装好了的客户机镜像文件上传至服务器,然后用Xen启动之,不知道我的理解是否正确。现我这样操作不知能否成功:CentOS5.4上已经建好了一个虚拟机,现我关闭其虚拟机,然后拷贝其*.img文件至另外目录中并重命名,然后用XM命令创建新虚拟机,不知道这种操作是否正确,请指教。

  2. 对的,你的想法完全正确。

  3. 那个with-data的partition 文件怎么来的?目录下找不到会报错

  4. 用 –partitions 的方式很麻烦,需要事先在 with-data 文件中指定分区信息:
    # vi /etc/xen-tools/partitions.d/with-data
    [root]
    size=10G
    type=ext3
    mountpoint=/
    options=sync,errors=remount-ro

    [swap]
    size=512M
    type=swap

    简单的话可以用 −dir=/xen(指定目录)或者 −−lvm=myvolumegroup(指定 LVM)的方式来指定 Linux 系统装在 loopback 镜像文件还是 LVM 区上。

  5. 您好 我按照您的步骤安装好了 可是在启动DomU的时候卡住了 我的host是ubuntu 10.04 安装的也是10.04
    root@Xen:~# xm create -c /etc/xen/xentest.cfg
    Using config file “/etc/xen/xentest.cfg”.
    Started domain xentest (id=12)
    [ 0.000000] Initializing cgroup subsys cpuset
    [ 0.000000] Linux version 2.6.31.8 (root@Xen) (gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5) ) #1 SMP Thu Feb 2 12:54:58 CST 2012
    [ 0.000000] Command line: root=/dev/xvda2 ro ip=:127.0.255.255::::eth0:dhcp
    [ 0.000000] KERNEL supported cpus:
    [ 0.000000] Intel GenuineIntel
    [ 0.000000] AMD AuthenticAMD
    [ 0.000000] Centaur CentaurHauls
    [ 0.000000] Xen-provided physical RAM map:
    [ 0.000000] Xen: 0000000000000000 – 0000000020800000 (usable)
    [ 0.000000] last_pfn = 0x20800 max_arch_pfn = 0x80000000
    [ 0.000000] init_memory_mapping: 0000000000000000-0000000020800000
    [ 0.000000] RAMDISK: 00b44000 – 0bbd0000
    [ 0.000000] ACPI in unprivileged domain disabled
    [ 0.000000] (3 early reservations) ==> bootmem [0000000000 – 0020000000]
    [ 0.000000] #0 [0000b44000 – 000bd36000] Xen provided ==> [0000b44000 – 000bd36000]
    [ 0.000000] #1 [0000200000 – 0000b2364c] TEXT DATA BSS ==> [0000200000 – 0000b2364c]
    [ 0.000000] #2 [000bd36000 – 000be3c000] PGTABLE ==> [000bd36000 – 000be3c000]
    [ 0.000000] Zone PFN ranges:
    [ 0.000000] DMA 0x00000000 -> 0x00001000
    [ 0.000000] DMA32 0x00001000 -> 0x00100000
    [ 0.000000] Normal 0x00100000 -> 0x00100000
    [ 0.000000] Movable zone start PFN for each node
    [ 0.000000] early_node_map[2] active PFN ranges
    [ 0.000000] 0: 0x00000000 -> 0x00020000
    [ 0.000000] 0: 0x00020800 -> 0x00020800
    [ 0.000000] NR_CPUS:256 nr_cpumask_bits:256 nr_cpu_ids:1 nr_node_ids:1
    [ 0.000000] PERCPU: Allocated 12 4k pages, static data 48096 bytes
    [ 0.000000] Swapping MFNs for PFN 9c6 and bbdb (MFN 10a8ef and 1b716e)
    [ 0.000000] Built 1 zonelists in Zone order, mobility grouping on. Total pages: 129252
    [ 0.000000] Kernel command line: root=/dev/xvda2 ro ip=:127.0.255.255::::eth0:dhcp
    [ 0.000000] PID hash table entries: 2048 (order: 11, 16384 bytes)
    [ 0.000000] Dentry cache hash table entries: 65536 (order: 7, 524288 bytes)
    [ 0.000000] Inode-cache hash table entries: 32768 (order: 6, 262144 bytes)
    [ 0.000000] Initializing CPU#0
    [ 0.000000] allocated 5324800 bytes of page_cgroup
    [ 0.000000] please try ‘cgroup_disable=memory’ option if you don’t want memory cgroups
    [ 0.000000] Software IO TLB disabled
    [ 0.000000] Memory: 317824k/532480k available (5182k kernel code, 8192k absent, 206012k reserved, 2744k data, 364k init)
    [ 0.000000] SLUB: Genslabs=13, HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
    [ 0.000000] Hierarchical RCU implementation.
    [ 0.000000] NR_IRQS:4672 nr_irqs:600
    [ 0.000000] Xen reported: 2400.134 MHz processor.
    [ 0.010000] Console: colour dummy device 80×25
    [ 0.010000] console [tty0] enabled
    [ 0.010000] console [xvc-1] enabled
    [ 0.750001] Calibrating delay using timer specific routine.. 4825.53 BogoMIPS (lpj=24127666)
    [ 0.750027] Security Framework initialized
    [ 0.750033] SELinux: Disabled at boot.
    [ 0.750040] Mount-cache hash table entries: 256
    [ 0.750123] Initializing cgroup subsys ns
    [ 0.750129] Initializing cgroup subsys cpuacct
    [ 0.750133] Initializing cgroup subsys memory
    [ 0.750139] Initializing cgroup subsys devices
    [ 0.750143] Initializing cgroup subsys freezer
    [ 0.750146] Initializing cgroup subsys net_cls
    [ 0.750170] CPU: L1 I cache: 32K, L1 D cache: 32K
    [ 0.750175] CPU: L2 cache: 256K
    [ 0.750177] CPU: L3 cache: 12288K
    [ 0.750187] SMP alternatives: switching to UP code
    [ 0.774691] Freeing SMP alternatives: 38k freed
    [ 0.774708] ftrace: converting mcount calls to 0f 1f 44 00 00
    [ 0.774714] ftrace: allocating 19165 entries in 76 pages
    [ 0.780115] Brought up 1 CPUs
    [ 0.780355] regulator: core version 0.5
    [ 0.800535] Time: 165:165:165 Date: 165/165/65
    [ 0.800581] NET: Registered protocol family 16
    [ 0.801455] Brought up 1 CPUs
    [ 0.801946] PCI: Fatal: No config space access function found
    [ 0.801951] PCI: setting up Xen PCI frontend stub
    [ 0.802373] bio: create slab at 0
    [ 0.802442] ACPI: Interpreter disabled.
    [ 0.802677] suspend: event channel 9
    [ 0.803168] xen_mem: Initialising balloon driver.
    [ 0.804216] SCSI subsystem initialized
    [ 0.804330] usbcore: registered new interface driver usbfs
    [ 0.804342] usbcore: registered new interface driver hub
    [ 0.804363] usbcore: registered new device driver usb
    [ 0.804439] PCI: System does not support PCI
    [ 0.804444] PCI: System does not support PCI
    [ 0.804490] NetLabel: Initializing
    [ 0.804494] NetLabel: domain hash size = 128
    [ 0.804496] NetLabel: protocols = UNLABELED CIPSOv4
    [ 0.804510] NetLabel: unlabeled traffic allowed by default
    [ 0.805487] pnp: PnP ACPI: disabled
    [ 0.805593] NET: Registered protocol family 2
    [ 0.805653] IP route cache hash table entries: 16384 (order: 5, 131072 bytes)
    [ 0.805837] TCP established hash table entries: 65536 (order: 8, 1048576 bytes)
    [ 0.806012] TCP bind hash table entries: 65536 (order: 8, 1048576 bytes)
    [ 0.806176] TCP: Hash tables configured (established 65536 bind 65536)
    [ 0.806182] TCP reno registered
    [ 0.806230] NET: Registered protocol family 1
    [ 0.806262] Trying to unpack rootfs image as initramfs…
    [ 0.886545] Freeing initrd memory: 180784k freed
    [ 0.921748] platform rtc_cmos: registered platform RTC device (no PNP device found)
    [ 0.921914] audit: initializing netlink socket (disabled)
    [ 0.921928] type=2000 audit(1328176143.710:1): initialized
    [ 0.930405] VFS: Disk quotas dquot_6.5.2
    [ 0.930448] Dquot-cache hash table entries: 512 (order 0, 4096 bytes)
    [ 0.930888] fuse init (API version 7.12)
    [ 0.930947] msgmni has been set to 1377
    [ 0.931079] alg: No test for stdrng (krng)
    [ 0.931123] Block layer SCSI generic (bsg) driver version 0.4 loaded (major 253)
    [ 0.931128] io scheduler noop registered
    [ 0.931131] io scheduler anticipatory registered
    [ 0.931134] io scheduler deadline registered (default)
    [ 0.931144] io scheduler cfq registered
    [ 0.931174] pci_hotplug: PCI Hot Plug PCI Core version: 0.5
    [ 0.931192] pciehp: PCI Express Hot Plug Controller Driver version: 0.4
    [ 0.931812] Linux agpgart interface v0.103
    [ 0.932357] brd: module loaded
    [ 0.932613] loop: module loaded
    [ 0.932665] input: Macintosh mouse button emulation as /devices/virtual/input/input0
    [ 0.932718] Xen virtual console successfully installed as xvc0
    [ 0.932739] Event-channel device installed.
    [ 0.935486] blktap_device_init: blktap device major 252
    [ 0.935494] blktap_ring_init: blktap ring major: 251
    [ 0.943614] netfront: Initialising virtual ethernet driver.
    [ 0.946186] xen-vbd: registered block device major 202
    [ 0.946200] blkfront: xvda2: barriers enabled
    [ 0.957193] blkfront: xvda1: barriers enabled
    [ 0.963022] usbcore: registered new interface driver usbback
    [ 0.965089] Fixed MDIO Bus: probed
    [ 0.965118] PPP generic driver version 2.4.2
    [ 0.965138] tun: Universal TUN/TAP device driver, 1.6
    [ 0.965141] tun: (C) 1999-2004 Max Krasnyansky
    [ 0.965194] ehci_hcd: USB 2.0 ‘Enhanced’ Host Controller (EHCI) Driver
    [ 0.965210] ohci_hcd: USB 1.1 ‘Open’ Host Controller (OHCI) Driver
    [ 0.965222] uhci_hcd: USB Universal Host Controller Interface driver
    [ 0.965281] PNP: No PS/2 controller found. Probing ports directly.
    [ 0.966113] i8042.c: No controller found.
    [ 0.966157] mice: PS/2 mouse device common for all mice
    [ 0.966222] rtc_cmos rtc_cmos: rtc core: registered rtc_cmos as rtc0
    [ 0.966349] device-mapper: uevent: version 1.0.3
    [ 0.966445] device-mapper: ioctl: 4.15.0-ioctl (2009-04-01) initialised: dm-devel@redhat.com
    [ 0.966526] device-mapper: multipath: version 1.1.0 loaded
    [ 0.966532] device-mapper: multipath round-robin: version 1.0.0 loaded
    [ 0.966987] TCP cubic registered
    [ 0.967109] NET: Registered protocol family 10
    [ 0.967530] lo: Disabled Privacy Extensions
    [ 0.968055] NET: Registered protocol family 17
    [ 0.968106] Bridge firewalling registered
    [ 0.968183] registered taskstats version 1
    [ 0.968209] PCI IO multiplexer device installed.
    [ 0.968227] Magic number: 1:252:3141
    [ 0.968243] XENBUS: Device with no driver: device/console/0
    [ 0.968249] drivers/rtc/hctosys.c: unable to open rtc device (rtc0)
    [ 0.968255] BIOS EDD facility v0.16 2004-Jun-25, 0 devices found
    [ 0.968260] EDD information not available.
    [ 0.968406] Freeing unused kernel memory: 364k freed
    [ 0.968570] Write protecting the kernel read-only data: 7364k
    [ 6.116634] kjournald starting. Commit interval 5 seconds
    [ 6.116703] EXT3-fs: mounted filesystem with ordered data mode.
    麻烦帮忙看下

  6. 请问在ubuntu12.04上可以同时运行xen和kvm的虚拟机吗?

  7. @jin
    不能同时运行。

  8. 想问一下,现在有一个centos的镜像,想mount 然后chroot去做adduser user -p password,为什么在vm启动的时候会提示错误,新的用户不能登陆

  9. http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/linux-modules-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb

    http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/linux-image-2.6.26-2-xen-amd64_2.6.26-17lenny2_amd64.deb

    这2个文件已经无法下载了,我使用这个2个可以么》
    http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/linux-image-2.6.32-5-xen-amd64_2.6.32-44_amd64.deb
    http://security.debian.org/debian-security/pool/updates/main/l/linux-2.6/xen-linux-system-2.6.32-5-xen-amd64_2.6.32-44_amd64.deb

  10. 楼主你好,我在安装的时候有以下错误,能麻烦您给看看吗?谢谢!!!!
    无法完成安装:’POST操作失败: xend_post:来自 xen 守护进程的错误:’

    Traceback (most recent call last):
    File “/usr/share/virt-manager/virtManager/asyncjob.py”, line 45, in cb_wrapper
    callback(asyncjob, *args, **kwargs)
    File “/usr/share/virt-manager/virtManager/create.py”, line 1909, in do_install
    guest.start_install(False, meter=meter)
    File “/usr/lib/python2.7/dist-packages/virtinst/Guest.py”, line 1236, in start_install
    noboot)
    File “/usr/lib/python2.7/dist-packages/virtinst/Guest.py”, line 1304, in _create_guest
    dom = self.conn.createLinux(start_xml or final_xml, 0)
    File “/usr/lib/python2.7/dist-packages/libvirt.py”, line 2166, in createLinux
    if ret is None:raise libvirtError(‘virDomainCreateLinux() failed’, conn=self)
    libvirtError: POST操作失败: xend_post:来自 xen 守护进程的错误:

  11. 无法完成安装:’POST操作失败: xend_post:来自 xen 守护进程的错误:Fault 3: ‘win7’

  12. 我想问问如果输入sudo xl list出现了domain0是否证明它安装完成,正在运行;但是如入uname -a显示的是3.16.0-30-generic 并不是xen的内核;domain0怎么会运行?
    我安装了一个virt-manager虚拟系统管理器 ,打开后没有显示domain0 怎么回事呢

发表评论