HOWTO Xen and Gentoo
出自Gentoo Linux Wiki
| 安装方法 • 架构/平台 • 内核 & 硬件 • Portage • 系统相关 • 网络 & 服务器 • X服务器 • 软件 • 游戏 • 非x86平台 |
目录 |
[编辑] 介绍
Xen 是一个基于x86架构的开源的类虚拟化 (para-virtualizing) 的虚拟机监视器(VMM),或者叫管理程序 (hypervisor)。Xen 可以在一台物理机器上安全的执行多个虚拟机,而且接近本地速度。Xen 提供了以下的商业级别的功能,包括了:
- 接近本地硬件速度的虚拟机。
- 虚拟机在不同的机器之间可以实时迁移。
- 每个客户虚拟机支持最多32个虚拟CPU,并且支持 虚拟CPU 热插拔。
- 支持 x86/32, 带 PAE 的 x86/32 和 x86/64 平台。
- 利用 Intel 虚拟技术 (VT-x) 可以不修改客户操作系统而直接运行在 Xen 上 (包括 MS Windows)。
- 出色的硬件支持 (支持几乎所有的 Linux 硬件驱动)。
[编辑] Xen 的适用范围包括:
- 服务器的统一
- 把多个服务器放到一个物理主机上,为不同的虚拟机提供性能和故障的隔离。
- 不依赖硬件
- 允许遗留程序和操作系统使用新硬件。
- 多系统配置
- 同时运行多个操作系统。方便开发或者测试。
- 内核开发
- 在一个沙箱虚拟机中测试和调试修改的内核 -- 不需要单独的测试机器了。
- 平行计算
- 在虚拟机中管理比在多个物理主机之间管理更方便, but better control and isolation than single-system image solutions, particularly by using live migration for load balancing.
- 各种系统的硬件支持
- 除了 Linux 已经支持的各种硬件之外,你还可以开发新的系统。
[编辑] Xen 的技术词汇
- 域
- 单独的虚拟机。
- 0域
- Xen 管理程序第一个加载的域。0域是特权域,可以控制其他的域,一般是用来直接访问机器的硬件。
- U域
- 0域的用户建立的所有其他非特权域,都是U域。
[编辑] 在开始前
确保你运行最新的 Gentoo (当前版本是 2006.0)。可以用以下语句检查:
| 代码: 检查你的 Profile |
ls -l /etc/make.profile lrwxrwxrwx 1 root root 46 Feb 28 09:27 /etc/make.profile -> /usr/portage/profiles/default-linux/x86/2006.0 |
这个版本可以保证你所用的 Glibc 比较新而且带有 NPTL。
这个连接会告诉你如何升级 Profile: http://www.gentoo.org/doc/en/gentoo-upgrading.xml
有一些软件,尤其是 Glibc 的 TLS 库,在 Xen 中执行某些操作的时候效率低下。在运行多线程程序的时候会造成大概 50% 的性能降低。要解决这个问题,你必须用 '-mno-tls-direct-seg-refs' 来编译你的系统。
修改你的 /etc/make.conf。 把 '-mno-tls-direct-seg-refs' 添加到你的 CFLAGS。"注意:这个操作和 Xen 提供的命令 "mv /usr/lib/tls /usr/lib/tls.disabled" 很相似,但是这个将会把所有的包都更新,而不只是 Glibc 自己。如果使用了 -Os 这个优化参数的话 (<gcc-4,gcc-4 之前的版本),请改成 -O2,因为编译在产生代码的时候会出问题的,这个是已经验证了的。
| 代码: Fixing compiler issues for all profiles |
echo 'sys-libs/glibc nptlonly' >> /etc/portage/package.use # Rebuild every package with your new CFLAGS emerge --emptytree --deep --ask --verbose world |
Note: You will also need to fix the CFLAGS for each domain U you install! In practice, however, you will do this only once and save the result as your 'skeleton base' for all your domain Us.
[编辑] 建立管理程序和应用程序
Xen 仍然被 ~arch 屏蔽。要解封的话,在文件中加入:
| 文件: /etc/portage/package.keywords |
app-emulation/xen ~x86 app-emulation/xen-tools ~x86 sys-devel/dev86 ~x86 sys-kernel/xen-sources ~x86 |
安装管理程序和应用程序。
| 代码: 安装管理程序和应用程序 |
emerge --ask --verbose app-emulation/xen app-emulation/xen-tools /sbin/rc-update add xend default |
xen 这个 ebuild 会安装管理程序(/boot/xen.gz),而 xen-tools 这个ebuild 则安装 xend 守护程序以及其他的命令行工具。Xend 守护程序是用来控制虚拟机的。
如果要配置网络,请在 /etc/conf.d/net 里边进行,但是不要把你的 net.eth0 加入 runlevel default。/etc/init.d/xend 会在启动的时候驱动以及配置你的网络的。(当要远程测试第一次编译的内核的时候,最好还是把 net.eth0 开启而把 xend 暂时禁止。)
如果有一个 Gentoo 专用的 Xen 网络脚本将会非常好。如果你有兴趣,请访问bug #111684。
[编辑] 建立内核
有两个方法建立内核。要么手动编译,要么用 genkernel 来帮你搞定。Genkernel 会帮你建立一个 initrd 文件,你可以用她来激活 LVM,EVMS 和 DMRAID 卷。(注意:(Ed W) 我发现 genkernel 不会帮你建立一个完整的内核,在最后一步拷贝文件的时候会失败,因为 Xen 在另外一个地方建立了 vmlinuz,而不是原来的 bzImage 所在的地方。不过这个不是什么严重问题。)
[编辑] 手动编译内核
| 代码: Fetch the kernel source |
emerge --ask --verbose sys-kernel/xen-sources |
在 /usr/src/linux-2.6.x.y-xen 目录中你可以找到为 Xen 域准备的内核源码。
推荐编译两套内核。一个给 dom0,一个给 domU。你可以用模块,但是所有启动需要的模块都要编译进去内核里边。
Xen 的内核配置有点麻烦 - 涉及很多选项,有的会造成 dom0 或者 domU 启动失败 (比如打开根设备的时候)。如果你遇到了问题,试试默认的 Xen 配置 /usr/src/linux/arch/xen/configs。
| 代码: Xen kernel config (Domain 0) |
XEN --->
[*] Privileged Guest (domain 0)
[*] Block-device backend driver
[*] Network-device backend driver
[ ] Block-device frontend driver
[ ] Network-device frontend driver
[ ] Piplined transmitter (DANGEROUS)
[*] Scrub memory before freeing it to Xen
Processor Type (X86) --->
Networking --->
Networking options --->
[*] 802.1d Ethernet Bridging
|
0域的内核应该包括为 Xen 用到的后端驱动,以及你的所有硬件。Ethernet Bridging 在0域是必须的,因为要用网桥来连接各个U域的 /dev/eth 设备。新开的域的默认的设置是/etc/xen/scripts/vif-bridge。另外一个选择则是在0域使用IP路由,这样就把U域和外部网络隔开了,这个是由/etc/xen/scripts/vif-route设置的。
| 代码: Xen kernel config (Domain U) |
XEN --->
[ ] Privileged Guest (domain 0)
[ ] Block-device backend driver
[ ] Network-device backend driver
[*] Block-device frontend driver
[*] Network-device frontend driver
[ ] Piplined transmitter (DANGEROUS)
[*] Scrub memory before freeing it to Xen
Processor Type (X86) --->
|
U域的内核应该只包括 Xen 的前端驱动,因为U域并不包括任何真实的硬件。
| 代码: Compile and install vmlinuz (Domain 0) |
make all cp vmlinuz /boot/vmlinuz-2.6.x.y-xen0 |
注意: make bzImage 是执行不了的。Xen 管理程序会处理好0域的vmlinuz并且用其启动。这个只会影响到 genkernel 脚本因为这个脚本期待的是一个 bzImage 文件。
你需要选择一个地方来放置你的U域的内核。现在 Xen 还不能从虚拟机器中提取内核来启动,所以你需要把内核放到你的0域。我就把他们放到了 /boot。但是0域的启动并不需要他们,随便放到哪里都可以的。
[编辑] 使用 genkernel
There are a few kinks to look out for if you wish to use genkernel to generate your kernel and initrd images.
| 代码: Fetching the genkernel tool |
emerge genkernel |
The latest genkernel should produce a fully working initramfs. You need the correct boot params, and *do not* add the old ARCH=xen line to genkernel.
Modify the genkernel configuration file /etc/genkernel.conf:
- Set MRPROPER=no. Running a 'mrproper' clean ruins Xen-specific twists to the kernel configuration files.
- Set ARCH_OVERRIDE to 'xen0' or 'xenU'. (Generate xen0 first, since that's our domain 0 kernel).
- Set SUPERARCH to 'i386' if you're on x86 or 'x86_64' for AMD64/EM64T.
- Set MENUCONFIG=yes.
- Set BOOTSPLASH=no. (bootsplash added an image to the end of the initrd which will stop it booting under xen)
Now build your kernel with 'genkernel all'. You might need extra options to genkernel if you're using EVMS, LVM, DMRAID or similar - use 'genkernel --help' to see all options.
When the menu configuration pops up, you'll want to:
- Choose the proper architecture (x86 or x86_64).
- Enable support for your particular hardware.
- Disable support for hardware you don't own.
- Choose to build backend (xen0) or frontend (xenU) drivers or both (multi-dom kernel).
- Enable 802.1d Ethernet Bridging in your xen0 kernel if you wish to bridge the virtual interfaces from your domU kernel to your external network interface (this is the default).
- Otherwise, you probably want to make sure that IP routing is enabled.
你需要选择一个地方来放置你的U域的内核。现在 Xen 还不能从虚拟机器中提取内核来启动,所以你需要把内核放到你的0域。我就把他们放到了 /boot。但是0域的启动并不需要他们,随便放到哪里都可以的。
[编辑] Updating your boot loader
[编辑] GRUB
The hypervisor is installed into /boot/xen.gz. It is booted in the same way as a kernel bzImage. Edit your GRUB config:
| 文件: /boot/grub/grub.conf |
title Xen 3.0 / Linux 2.6.x.y root (hd0,0) kernel /xen.gz dom0_mem=98304 module /vmlinuz-2.6.x.y-xen0 root=/dev/md2 |
The dom0_mem hypervisor option sets the amount of memory to be allocated to domain0 (in this case 96MB). In Xen 3.x the parameter may be specified with a B, K, M or G suffix, representing bytes, kilobytes, megabytes and gigabytes respectively; if no suffix is specified, the parameter defaults to kilobytes.
The module line is used to select the domain 0 kernel image you want the hypervisor to run, and to pass in options to the kernel command line.
If your domain 0 uses an initrd, you can load that by adding another module line (xen wont work with genkernel initrd images. You literally need to gunzip then gzip the initrd file again to get it to boot. It's not obvious why such a simple fix is necessary). For example to boot a non-enforcing SELinux system with EVMS on the root disk then try:
| 文件: /boot/grub/grub.conf |
title Xen 3.0.0 / Linux 2.6.12.6 root (hd0,0) kernel /xen.gz dom0_mem=98304 module /vmlinuz-x86-2.6.12.6-xen0 root=/dev/ram0 real_root=/dev/evms/sda5 udev doevms2 selinux=1 enabled=0 module /initramfs-genkernel-x86-2.6.12.6-xen.gz |
[编辑] Alternative: PXELinux
Network booting (possibly with nfsroot file system) can ease setup and maintenance in some environments, such as a homogeneous cluster.
A domain 0 kernel can be booted using PXE. The syslinux package contains the PXELinux boot program; unfortunately, the current version in Gentoo won't boot a Xen kernel, but a ported binary is available.
Follow the instructions in HOWTO Gentoo Diskless Install and Diskless Nodes with Gentoo to set up a boot server running dhcp and tftp.
You need to serve the following via tftp:
- The Xen PXELinux binary, (Which will be installed to /usr/lib/syslinux/mboot.c32, if you emerge syslinux).
- Xen hypervisor (xen.gz)
- Your Xen domain 0 vmlinuz
- initrd if you need to load modules
In your pxelinux config file add a single line like:
| 文件: /diskless/pxelinux.cfg/default |
DEFAULT mboot.c32 xen.gz dom0_mem=258048 --- vmlinuz-2.6.12.5-xen0 ro console=ttyS0 root=/dev/nfs --- initrd-2.6.12.5-xen0 |
You can omit the --- initrd- bit if you aren't using a ram disk for modules. Also you can use a hard disk rather than nfsroot by changing the root= to point to a block device (eg. root=/dev/hda, or root=/dev/md2 for raid).
Configure the BIOS of your Xen host to boot from the network via PXE (this can be well hidden - on a Dell PowerEdge server I had to enable Onboard Devices -> NIC w/PXE and reboot before Network Controller appeared in the Boot Sequence menu).
On booting you should see the BIOS screen, followed by the PXE loader doing DHCP and fetching PXELinux, then PXELinux booting and fetching the hypervisor and kernel, then the hypervisor booting, and finally the kernel booting and mounting the nfsroot fs from the server. Phew!
[编辑] Running Xen
At this point the PC can be rebooted. Select your Xen option in grub. You should see the Xen hypervisor booting
| 代码: Xen hypervisor boot |
\ \/ /___ _ __ |___ / / _ \ __| | _____ _____| | \ // _ \ '_ \ |_ \| | | |__ / _` |/ _ \ \ / / _ \ | / \ __/ | | | ___) | |_| |__| (_| | __/\ V / __/ | /_/\_\___|_| |_| |____(_)___/ \__,_|\___| \_/ \___|_| http://www.cl.cam.ac.uk/netos/xen University of Cambridge Computer Laboratory Xen version 3.0-devel (portage@edesix.com) (gcc version 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)) Tue Sep 6 17:30:34 BST 2005 Latest ChangeSet: (XEN) Physical RAM map: (XEN) 0000000000000000 - 00000000000a0000 (usable) (XEN) 00000000000f0000 - 0000000000100000 (reserved) (XEN) 0000000000100000 - 000000003fe8cc00 (usable) (XEN) 000000003fe8cc00 - 000000003fe8ec00 (ACPI NVS) (XEN) 000000003fe8ec00 - 000000003fe90c00 (ACPI data) (XEN) 000000003fe90c00 - 0000000040000000 (reserved) (XEN) 00000000f0000000 - 00000000f4000000 (reserved) (XEN) 00000000fec00000 - 00000000fed00400 (reserved) (XEN) 00000000fed20000 - 00000000feda0000 (reserved) (XEN) 00000000fee00000 - 00000000fef00000 (reserved) (XEN) 00000000ffb00000 - 0000000100000000 (reserved) ...... |
Once the Hypervisor has loaded, it will boot your kernel. You should see something like:
| 代码: Xen hypervisor boot |
(XEN) Scrubbing Free RAM: ...........done. (XEN) *** Serial input -> DOM0 (type 'CTRL-a' three times to switch input to Xen). Linux version 2.6.12.5-xen (root@funky.edesix.com) (gcc version 3.3.6 (Gentoo 3.3.6, ssp-3.3.6-1.0, pie-8.7.8)) #2 Tue Sep 6 18:30:28 BST 2005 BIOS-provided physical RAM map: Xen: 0000000000000000 - 0000000006000000 (usable) 96MB LOWMEM available. On node 0 totalpages: 2 |
Followed by the usual kernel boot messages. Log in as normal. Congratulations, you now have a domain 0 Xen kernel up and running!
[编辑] Example: Create a new domainU running ttylinux
Download and unpack ttylinux
Note: The default root login once the domain is booted is user: root/password: root
| 文件: Add /etc/xen/ttylinux |
kernel = "/boot/vmlinuz-2.6-xenU" memory = 64 name = "ttylinux" nics = 1 ip = "1.2.3.4" disk = ['file:/path/to/ttylinux/rootfs,sda1,w'] root = "/dev/sda1 ro" |
| 代码: Create domain |
xm create /etc/xen/ttylinux -c |
[编辑] Creating domainUs from other Linux distributions
The "domi" domain builder from Suse can be used to build Suse, Fedora, Debian, and Gentoo domains.
| 代码: Install domi and build a domU |
emerge app-emulation/domi |
[编辑] Debugging problems
It can be difficult to see what's going on as the system boots. The easiest way is to use another computer connected via the serial port.
Compile support for a serial console into your Xen kernel (Device drivers -> Character devices -> Serial drivers -> Console on 8250/16550 and compatible serial port).
Add something like the following to your grub.conf:
| 文件: /boot/grub/grub.conf |
title Xen 2.0.8_pre20050826 / XenLinux 2.6.12.5 root (hd0,0) kernel /xen.gz dom0_mem=98304 noreboot com1=9600,8n1 module /vmlinuz-2.6.12.5-xen root=/dev/md2 noreboot console=ttyS0 debug |
The noreboot option tells Xen not to allow the kernel to reboot, even if you do shutdown -r. The hypervisor option com1=9600,8n1 and the kernel option console=ttyS0 tell both to output boot messages to the serial port.
[编辑] Resources
[编辑] Where to get help
- Xen Source Wiki
- Xen-Users mailing list - Subscribe or Browse the archives
- Xen-Devel mailing list - Subscribe or Browse the archives
- Xen @ Cambridge University Where it all started.
- Xen Source the company founded by the authors of Xen
