本文出自 “贾芸斐” 博客,请务必保留此出处http://jiayf.blog.51cto.com/1659430/351894
Xen 是剑桥大学计算机实验室开发的一个开放源代码的虚拟化软件,功能与 VMware 类似。
1. 安装 Xen
[root@xen ~]# yum install kernel-xen xen virt-manager
Loaded plugins: rhnplugin, security
This system is not registered with RHN.
RHN support will be disabled.
Cluster | 1.3 kB 00:00
Cluster/primary | 6.5 kB 00:00
Cluster 32/32
ClusterStorage | 1.3 kB 00:00
ClusterStorage/primary | 8.8 kB 00:00
ClusterStorage 39/39
VT | 1.3 kB 00:00
VT/primary | 9.0 kB 00:00
VT 36/36
Setting up Install Process
Package kernel-xen-2.6.18-164.el5.i686 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package virt-manager.i386 0:0.6.1-8.el5 set to be updated
--> Processing Dependency: gnome-python2-gnomekeyring >= 2.15.4 for package: virt-manager
--> Processing Dependency: gtk-vnc-python >= 0.3.8 for package: virt-manager
--> Processing Dependency: libvirt-python >= 0.3.3 for package: virt-manager
--> Processing Dependency: python-virtinst >= 0.400.3 for package: virt-manager
---> Package xen.i386 0:3.0.3-94.el5 set to be updated
--> Processing Dependency: xen-libs = 3.0.3-94.el5 for package: xen
--> Processing Dependency: libxenctrl.so.3.0 for package: xen
--> Processing Dependency: libfsimage.so.1.0 for package: xen
--> Processing Dependency: libxenguest.so.3.0 for package: xen
--> Processing Dependency: libxenstore.so.3.0 for package: xen
--> Processing Dependency: bridge-utils for package: xen
--> Processing Dependency: libblktap.so.3.0 for package: xen
--> Processing Dependency: libfsimage.so.1.0(libfsimage.so.1.0) for package: xen
--> Running transaction check
---> Package bridge-utils.i386 0:1.1-2 set to be updated
---> Package gnome-python2-gnomekeyring.i386 0:2.16.0-3.el5 set to be updated
---> Package gtk-vnc-python.i386 0:0.3.8-3.el5 set to be updated
--> Processing Dependency: gtk-vnc = 0.3.8-3.el5 for package: gtk-vnc-python
--> Processing Dependency: libgtk-vnc-1.0.so.0 for package: gtk-vnc-python
---> Package libvirt-python.i386 0:0.6.3-20.el5 set to be updated
--> Processing Dependency: libvirt = 0.6.3-20.el5 for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.6.1) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.2.3) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.1.1) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.1.9) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.2.1) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.4.5) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.2.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0 for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.1.5) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.4.1) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.1.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.3.3) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.6.3) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.6.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.4.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.5.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.1.4) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.3.0) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.3.2) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.0.3) for package: libvirt-python
--> Processing Dependency: libvirt.so.0(LIBVIRT_0.0.5) for package: libvirt-python
---> Package python-virtinst.noarch 0:0.400.3-5.el5 set to be updated
---> Package xen-libs.i386 0:3.0.3-94.el5 set to be updated
--> Running transaction check
---> Package gtk-vnc.i386 0:0.3.8-3.el5 set to be updated
---> Package libvirt.i386 0:0.6.3-20.el5 set to be updated
--> Processing Dependency: cyrus-sasl-md5 for package: libvirt
--> Processing Dependency: iscsi-initiator-utils for package: libvirt
--> Running transaction check
---> Package cyrus-sasl-md5.i386 0:2.1.22-5.el5 set to be updated
---> Package iscsi-initiator-utils.i386 0:6.2.0.871-0.10.el5 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository
Size
================================================================================
Installing:
virt-manager i386 0.6.1-8.el5 VT 1.5 M
xen i386 3.0.3-94.el5 VT 1.9 M
Installing for dependencies:
bridge-utils i386 1.1-2 Server 28 k
cyrus-sasl-md5 i386 2.1.22-5.el5 Server 45 k
gnome-python2-gnomekeyring i386 2.16.0-3.el5 Server 16 k
gtk-vnc i386 0.3.8-3.el5 Server 80 k
gtk-vnc-python i386 0.3.8-3.el5 Server 12 k
iscsi-initiator-utils i386 6.2.0.871-0.10.el5 Server 773 k
libvirt i386 0.6.3-20.el5 VT 1.9 M
libvirt-python i386 0.6.3-20.el5 VT 132 k
python-virtinst noarch 0.400.3-5.el5 VT 378 k
xen-libs i386 3.0.3-94.el5 Server 154 k
Transaction Summary
================================================================================
Install 12 Package(s)
Update 0 Package(s)
Remove 0 Package(s)
Total download size: 6.9 M
Is this ok [y/N]: y
Downloading Packages:
(1/12): iscsi-initiator-utils-6.2.0.871-0.10.el5.i386.rp | 773 kB 00:00
(2/12): bridge-utils-1.1-2.i386.rpm | 28 kB 00:00
(3/12): gtk-vnc-python-0.3.8-3.el5.i386.rpm | 12 kB 00:00
(4/12): xen-libs-3.0.3-94.el5.i386.rpm | 154 kB 00:00
(5/12): cyrus-sasl-md5-2.1.22-5.el5.i386.rpm | 45 kB 00:00
(6/12): gtk-vnc-0.3.8-3.el5.i386.rpm | 80 kB 00:00
(7/12): xen-3.0.3-94.el5.i386.rpm | 1.9 MB 00:00
(8/12): gnome-python2-gnomekeyring-2.16.0-3.el5.i386.rpm | 16 kB 00:00
(9/12): libvirt-python-0.6.3-20.el5.i386.rpm | 132 kB 00:00
(10/12): virt-manager-0.6.1-8.el5.i386.rpm | 1.5 MB 00:00
(11/12): libvirt-0.6.3-20.el5.i386.rpm | 1.9 MB 00:00
(12/12): python-virtinst-0.400.3-5.el5.noarch.rpm | 378 kB 00:00
--------------------------------------------------------------------------------
Total 2.3 MB/s | 6.9 MB 00:02
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : xen-libs 1/12
Installing : bridge-utils 2/12
Installing : gtk-vnc 3/12
Installing : gtk-vnc-python 4/12
Installing : cyrus-sasl-md5 5/12
Installing : iscsi-initiator-utils 6/12
Installing : libvirt 7/12
Installing : libvirt-python 8/12
Installing : gnome-python2-gnomekeyring 9/12
Installing : python-virtinst 10/12
Installing : xen 11/12
Installing : virt-manager 12/12
Installed:
virt-manager.i386 0:0.6.1-8.el5 xen.i386 0:3.0.3-94.el5
Dependency Installed:
bridge-utils.i386 0:1.1-2 cyrus-sasl-md5.i386 0:2.1.22-5.el5
gnome-python2-gnomekeyring.i386 0:2.16.0-3.el5 gtk-vnc.i386 0:0.3.8-3.el5
gtk-vnc-python.i386 0:0.3.8-3.el5 iscsi-initiator-utils.i386 0:6.2.0.871-0.10.el5
libvirt.i386 0:0.6.3-20.el5 libvirt-python.i386 0:0.6.3-20.el5
python-virtinst.noarch 0:0.400.3-5.el5 xen-libs.i386 0:3.0.3-94.el5
Complete!
|
[root@xen ~]# init 6
|
[root@xen ~]# /etc/init.d/xend start
启动 xend: [确定]
|
[root@xen ~]# /etc/init.d/xend stop
停止 xend: [确定]
|
[root@xen ~]# /etc/init.d/xend restart
restart xend: [确定]
|
[root@xen ~]# chkconfig --add xend
[root@xen ~]# chkconfig --add xendomains
[root@xen ~]# chkconfig --level 345 xend on
[root@xen ~]# chkconfig --level 345 xendomains on
|
1. 创建 Xen 虚拟系统安装树
与 EMC VMware 和微软 Virtual Server 一样,Xen 在完全虚拟化的环境中可以支持从 ISO 光盘镜像文件或物理光驱作为安装源来引导安装系统,但在半虚拟化环境中必须使用安装树进行安装。安装树是一个包含所有系统安装文件及相关程序的目录,这个目录通过 HTTP、FTP、NFS 进行发布。
这里我们将要在 Xen 半虚拟化环境下安装一个 RHEL5 系统,以 RHEL5 为例介绍制作 HTTP安装树的方法:
A. 安装并启动 Apache 服务
Apache 的安装过程省略,这里假定 Apache 网页存放目录为/var/www/html
B. 将 RHEL5 安装映像文件复制到 Linux 系统中
将 RHEL5.iso 文件复制到/tmp/iso/下
C. 将 RHEL5 安装映像文件挂载到 Apache 根目下
[root@xen ~]# mv /tmp/iso/rhel-server-5.4-i386-dvd.iso /tmp/iso/RHEL5.iso [root@xen ~]# mount -o loop /tmp/iso/RHEL5.iso /var/www/html |
2. 使用字符工具 virt-install 创建 Xen 虚拟系统
A. 创建块设备文件存储目录
[root@xen ~]# mkdir /vmdisk |
B. 执行 virt-install 安装程序
[root@xen ~]# virt-install --prompt |
3. 配置虚拟系统环境
运行 virt-install 程序后将进入 Xen 的字符引导安装界面,按提示信息输入虚拟系统配置
[root@xen ~]# virt-install --prompt What is the name of your virtual machine? vmrhel5 How much RAM should be allocated (in megabytes)? 200 What would you like to use as the disk (file path)? /vmdisk/vmrhel5 How large would you like the disk (/vmdisk/vmrhel5) to be (in gigabytes)? 4 What is the install URL? http://192.168.0.1 开始安装...... 搜索文件 .treeinfo...... | 437 B 00:00 搜索文件 .treeinfo...... | 437 B 00:00 搜索文件 .treeinfo...... | 437 B 00:00 搜索文件 vmlinuz...... | 2.1 MB 00:00 搜索文件 initrd.img...... | 6.6 MB 00:00 创建存储文件...... | 4.0 GB 00:00 创建域...... | 0 B 00:03 连接到域 vmrhel5 换码符为 ^] Linux version 2.6.18-164.el5xen (mockbuild@x86-002.build.bos.redhat.com) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Tue Aug 18 16:06:30 EDT 2009 . . . |
输入内容可参照:
1) 输入 Xen 虚拟系统的名称
What is the name of your virtual machine? vmrhel5
2) 输入需要分配给虚拟系统的内存
How much RAM should be allocated (in megabytes)? 300
3) 输入虚拟块设备的路径
What would you like to use as the disk (path)? /vmdisk/vmrhel5
4) 输入块设备的大小
How large would you like the disk (/vmdisk/vmrhel5) to be (in gigabytes)? 6
5) 是否使用图形安装界面
Would you like to enable graphics support? (yes or no) no
6) 输入安装树路径
What is the install location? http://192.168.0.1
4. 安装 RHEL5 系统
A. 选择安装向导语言
字符界面下只支持英文,所以保持默认选项“English”,直接回车
B. 设定 TCP/IP 选项
手动设定设定 IPv4 网络设定,这个步骤非常关键,如果 IP 设置不正确,将无法连接到安装树,导致无法安装系统
C. 进入 Linux 字符安装向导
根据安装向导提示,设置 RHEL5 安装选项,安装过程与在真实硬件环境的安装过程一致
5. 使用图形工具 virt-manager 创建 Xen 虚拟系统
A. 打开 virt-manager 管理工具
B. 连接 Xen 服务器(上面已经连接)
C. virt-manage 图形管理界面
D. 启动创建新虚拟向导
E. 命名虚拟系统
F. 选择虚拟化方式
G. 选择要安装的操作系统类型
H. 指定安装树路径
I. 分配存储空间
J. 分配内存和 CPU
K. 选择网络连接类型
L. Xen 虚拟系统设置完成
M. 启动 RHEl5 安装引导程序
1. 使用 xm 命令管理 Xen 虚拟机
A. 显示运行状态
在命令行下使用“xm list”命令显示 Xen 虚拟系统当前运行的状况
[root@xen ~]# xm list Name ID Mem(MiB) VCPUs State Time(s) Domain-0 0 366 1 r----- 258.7 vmrhel5 2 127 1 ------ 31.0 |
B. 连接虚拟系统
使用“xm console”命令通过字符界面连接到运行中的 vmrhel5 虚拟系统
[root@xen ~]# xm create /etc/xen/vmrhel5 |
C. 关闭虚拟系统
使用“xm shutdown”命令启动 vmrhel5 虚拟系统
[root@xen ~]# xm shutdown vmrhel5 |
D. 重启动虚拟系统
使用“xm reboot”命令重启动 vmrhel5 虚拟系统
[root@xen ~]# xm reboot vmrhel5 |
2. 使用 virt-manager 图形工具管理 Xen 虚拟机
在 Linux 图形桌面 X-Windows 中打开“应用程序”->“系统工具”->“Virtual Machine Manager”,选择“本地 Xen 宿主”,点击“连接”,启动图形管理工具
在 virt-manager 中右键单击要管理的虚拟系统,在弹出菜单中选择“Details”将打开“虚拟系统状态窗口”,如图 5.16 所示,在此窗口中可以查看虚拟系统的名称、CPU 和内存占用情况,还可对虚拟系统进行“暂停”和“关闭”的操作。
在“虚拟系统状态窗口”,点击“硬件”选项卡可查看并修改虚拟系统的硬件配置参数
在 virt-manager 中右键单击要管理的虚拟系统,在弹出菜单中选择“打开”,将打开虚拟系统的终端,并且还可对虚拟系统进行“运行”、“关闭”、“暂停”和“保存”的操作。
3. 配置虚拟系统随服务器启动
当物理服务器重启时,所有的虚拟系统会自动停止。但当物理服务器重启完成时,这些虚拟系统并不会自动启动,而需要手动的方式进行启动。
要设置 Xen 的虚拟系统随服务器启动,首先要了解 Xen 上已安装的虚拟系统的配置文件存放位置。通过 virt-install 命令或者使用 virt-manager 图形管理工具在 Xen 服务器上创建的虚拟系统,其配置文件默认保存在/etc/xen 目录下。
[root@xen ~]# cat /etc/xen/vmrhel5 name = "vmrhel5" uuid = "d164e9eb-b574-583c-9a6e-7a2fb435fc28" maxmem = 128 memory = 128 vcpus = 1 bootloader = "/usr/bin/pygrub" on_poweroff = "destroy" on_reboot = "restart" on_crash = "restart" vfb = [ "type=vnc,vncunused=1,keymap=en-us" ] disk = [ "tap:aio:/vmdisk/vmrhel5,xvda,w" ] vif = [ "mac=00:16:36:75:23:a8,bridge=virbr0,script=vif-bridge" ] |
要使虚拟系统能够随服务器启动,其操作很简单,只需要将需要随服务器启动的虚拟系统的配置文件放到/etc/xen/auto 目录中,Xen 会在启动时将 auto 目录下的虚拟系统启动
[root@xen ~]# mv /etc/xen/vmrhel5 /etc/xen/auto/