一、Xen的安装配置详解:
1、配置yum源
http://mirrors.163.com/centos/6.5/xen4/x86_64/ 163或其他yum源
1
2
3
4
5
6
7
|
# cat /etc/yum.repos.d/xen4.repo [Xen4] name = Xen4 project
#baseurl=http://mirror.centos.org/centos/6/xen4/x86_64/ baseurl = ftp: / / 172.16 . 0.1 / pub / Sources / 6.x86_64 / xen4 / x86_64 #这里使用的是本地yum源
gpgcheck = 0
cost = 500
|
2、配置好查看:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# yum list all | grep xen libvirt - daemon - driver - xen.x86_64 0.10 . 2.8 - 7.el6 .centos.alt @Xen4
xen.x86_64 4.2 . 4 - 30.el6 .centos.alt @Xen4
xen - hypervisor.x86_64 4.2 . 4 - 30.el6 .centos.alt @Xen4
xen - libs.x86_64 4.2 . 4 - 30.el6 .centos.alt @Xen4
xen - licenses.x86_64 4.2 . 4 - 30.el6 .centos.alt @Xen4
xen - runtime.x86_64 4.2 . 4 - 30.el6 .centos.alt @Xen4
libvirt - daemon - xen.x86_64 0.10 . 2.8 - 7.el6 .centos.alt Xen4
xen - devel.x86_64 4.2 . 4 - 30.el6 .centos.alt Xen4
xen - doc.x86_64 4.2 . 4 - 30.el6 .centos.alt Xen4
xen - ocaml.x86_64 4.2 . 4 - 30.el6 .centos.alt Xen4
xen - ocaml - devel.x86_64 4.2 . 4 - 30.el6 .centos.alt Xen4
xenserverjava.noarch 5.6 . 100.1 - 1.el6 epel
xenserverjava - javadoc.noarch 5.6 . 100.1 - 1.el6 epel
|
注意查看kernel的版本;3.10测试是无法启动的;需要低于3.10版本。建议安装时直接指定内核。
3、安装完成后修改grub文件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# yum -y install xen kernel-3.4.61 kernel-firmware-3.4.61 # vim /boot/grub/grub.conf #注意按照以下修改 #vmlinuz和initramfs都作为xen内核的模块 default = 0
timeout = 5
splashimage = (hd0, 0 ) / grub / splash.xpm.gz
hiddenmenu title CentOS ( 3.4 . 61 - 9.el6 .centos.alt.x86_64)
root (hd0, 0 )
kernel / xen.gz dom0_mem = 1024M cpufreq = xen dom0_max_vcpus = 2 dom0_vcpus_pin
module / vmlinuz - 3.4 . 61 - 9.el6 .centos.alt.x86_64 ro root = / dev / mapper / vg0 - root rd_NO_LUKS rd_NO_DM LANG = en_US.UTF - 8 rd_LVM_LV = vg0 / swap rd_NO_MD SYSFONT = latarcyrheb - sun16 crashkernel = auto rd_LVM_LV = vg0 / root KEYBOARDTYPE = pc KEYTABLE = us rhgb crashkernel = auto
module / initramfs - 3.4 . 61 - 9.el6 .centos.alt.x86_64.img
.... |
4、重启进入新内核即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# uname -r 3.4 . 61 - 9.el6 .centos.alt.x86_64
# rpm -q xen xen - 4.2 . 4 - 30.el6 .centos.alt.x86_64
# xm info host : node4.soul.com release : 3.4 . 61 - 9.el6 .centos.alt.x86_64
version : #1 SMP Wed Sep 11 15:34:17 UTC 2013
machine : x86_64 nr_cpus : 1
nr_nodes : 1
cores_per_socket : 1
threads_per_core : 1
cpu_mhz : 2399
hw_caps : 0fabfbff : 28100800 : 00000000 : 00003f40 : 80b82221 : 00000000 : 00000001 : 00000000
virt_caps : hvm ...... |
5、设置桥接网络;注意需要必须关闭NetworkManager和开机自启。
1
2
3
4
5
6
7
8
9
10
11
|
# chkconfig NetworkManager off # service NetworkManager stop # vim bridge.sh #提供脚本自动配置桥设备 #!/bin/bash ip = `ifconfig eth0 | awk - F : '/inet addr:/{print $2}' | awk '{print $1}' `
brctl addbr br0 ifconfig eth0 0 up
brctl addif br0 eth0 ifconfig br0 ${ip} / 16 up
brctl stp br0 on |
如需机器重启后还生效的需要更改配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# cd /etc/sysconfig/network-scripts/ # cp ifcfg-eth0 ifcfg-br0 # vim ifcfg-br0 DEVICE = br0
BOOTPROTO = none
NM_CONTROLLED = no #需要更改no
ONBOOT = yes
TYPE = Bridge #首字符需大写
IPADDR = 192.168 . 0.114
NETMASK = 255.255 . 0.0
GATEWAY = 192.168 . 0.254
DNS1 = 172.16 . 0.1
IPV6INIT = no
USERCTL = no
# vim ifcfg-eth0 DEVICE = eth0
BOOTPROTO = none
HWADDR = 00 : 0c : 29 :f9:b3:df
NM_CONTROLLED = yes
ONBOOT = yes
BRIDGE = br0
TYPE = Ethernet
UUID = "12058363-37d9-4857-8c22-0a0da4b52c88"
IPV6INIT = no
USERCTL = no
#重启网卡测试即可。 |
二、下面创建一个虚拟机并安装系统测试
提供initrd.img vmlinuz这两个文件;repo镜像文件的isolinux文件夹里
本次直接下载到root家目录下。
1、提供配置文件
1
2
3
4
5
6
7
8
9
|
# cd /etc/xen/ # vim centos6 kernel = "/root/vmlinuz" ramdisk = "/root/initrd.img" memory = 512
name = "centos6"
vif = [ 'bridge=br0' ] #指定网卡为桥接br0上
disk = [ 'file:/xen/vm1/centos6.img,xvda,w' ] #指定disk在后端和前端名称和权限
on_reboot = "destroy" #执行xm reboot命令或在当前域内部执行重启操作时由Xen执行的动作;destroy为关机。
|
创建上述disk文件
1
2
|
# cd /xen/vm1/ # dd if=/dev/zero of=centos6.img oflag=direct seek=102399 bs=1M count=1 |
2、启动测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# cd /etc/xen/ # xm create centos6 # xm console centos6 ┌────────┤ Choose a Language ├────────┐
│ │
│ What language would you like to use │
│ during the installation process? │ │ │
│ Catalan ↑ │
│ Chinese(Simplified) ? │
│ Chinese(Traditional) ? │
│ Croatian ? │
│ Czech ? │
│ Danish ? │
│ Dutch ? │
│ English ↓ │
│ │
│ ┌────┐ │
│ │ OK │ │ │
│ └────┘ │
测试启动成功。可以测试安装了... ┌───────────────┤ Formatting ├─——───────────┐ │ │
│ Creating ext4 filesystem on / dev / mapper / VolGroup - lv_root │ │ │
│ │ └──────────────────────────────────———┘
|
3、安装完成后需要先关机;然后更改配置文件
1
2
3
4
5
6
7
8
9
10
|
# cd /etc/xen/ # vim centos6 #kernel = "/root/vmlinuz" #ramdisk = "/root/initrd.img" bootloader = "/usr/bin/pygrub" #添加这行;注释上述两行;保存启动虚拟机
memory = 512
name = "centos6"
vif = [ 'bridge=br0' ]
disk = [ 'file:/xen/vm1/centos6.img,xvda,w' ]
on_reboot = "destroy"
|
重启后xm console centos6连接测试或者ssh远程连接测试即可。
配置好网关测试连接即可。
1
2
3
4
|
localhost.localdomain login: #测试可以正常启动登陆
CentOS release 6.5 (Final)
Kernel 2.6 . 32 - 431.el6 .x86_64 on an x86_64
# ifconfig eth0 192.168.0.11/16 up |
-------------------------------------------------------------------------------------------
三、基础工具使用
1、vnc使用说明
-
安装vnc;远程桌面连接:
1
2
3
4
5
6
|
# yum -y install tigervnc-server # vncserver :1 tcp LISTEN 0 5 * : 5901 * : * users:(( "Xvnc" , 9358 , 6 )) #监听端口:5901
#可以在windows客户端使用vnc客户端连接。 #同时也可以启动多个vnc;且相互独立。 # vncserver :2 ... |
-
创建vnc安装格式:
创建配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# vim rhel6 kernel = "/root/vnc/vmlinuz"
ramdisk = "/root/vnc/initrd.img" vcpus = 2
memory = 512
name = "rhel6"
disk = [ 'file:/xen/vm3/rhel6.qcow2,xvda,w' ]
vif = [ 'bridge=br0' ]
vfb = [ 'vnc=1,sdl=1' ] #vnc选项;只能Dom0下使用vnc连接后启动当前窗口下打开
on_reboot = "destroy"
# pwd / xen / vm3 #该目录下创建文件
# qemu-img create -f qcow2 -o size=120G,preallocation=metadata rhel6.qcow #创建完成后在连接Dom0的vnc下启动虚拟机安装即可。 |
2、libvirt使用方法:
-
安装:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
# yum -y install libvirt # service libvirtd start # brctl show bridge name bridge id STP enabled interfaces
br0 8000.000c29f9b3df no eth0
vif1. 0
virbr0 8000.000000000000 yes
# yum -y install virt-manager #安装上管理工具 # yum -y install virt-viewer #安装virt-viewer工具 # virt-manager & #启动即可安装;需要Xmanager支持 # virt-install -n "centos-pxe" -r 512 --vcpus=1 --pxe --disk path=/xen/vm2/centos-pxe.img --network bridge=br0 --force -v #基于pxe引导安装;需要在启动虚拟机是开启CPU的虚拟化Intel VT-x/EPT...和虚拟化CPU性能... 具体用法:virt - install - - help
|
-------------------------------------------------------------------------------------------
四、xm 工具详解
1、动态添加磁盘
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# xm block-list centos6 #显示块设备 Vdev BE handle state evt - ch ring - ref BE - path
51712 0 0 4 8 8 / local / domain / 0 / backend / vbd / 1 / 51712 # xm block-attach Error: 'xm block-attach' requires between 4 and 5 arguments.
Usage: xm block - attach <Domain> <BackDev> <FrontDev> <Mode> [BackDomain]
Create a new virtual block device. #先创建一个磁盘文件: # cd /xen/vm1/ # ls -lh total 3.3G
- rw - r - - r - - 1 root root 100G May 22 10 : 30 centos6.img
# qemu-img create -f qcow -o size=10G test.qcow Formatting 'test.qcow' , fmt = qcow size = 10737418240 encryption = off
[root@node4 vm1] # ls -lh
total 3.3G
- rw - r - - r - - 1 root root 100G May 22 10 : 32 centos6.img
- rw - r - - r - - 1 root root 41K May 22 10 : 32 test.qcow
# xm block-attach centos6 file:/xen/vm1/test.qcow xvdb w #添加 # xm block-list centos6 Vdev BE handle state evt - ch ring - ref BE - path
51712 0 0 4 8 8 / local / domain / 0 / backend / vbd / 1 / 51712 51728 0 0 4 10 868 / local / domain / 0 / backend / vbd / 1 / 51728
# fdisk -l /dev/xvd[a-z] Disk / dev / xvda: 107.4 GB, 107374182400 bytes
.... Disk / dev / xvdb: 0 MB, 40960 bytes #查看已显示;动态添加成功
.... |
2、删除磁盘;删除前请确保被删除磁盘未被挂载使用;或者先卸载挂载使用的分区:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
# xm block-detach Error: 'xm block-detach' requires between 2 and 3 arguments.
Usage: xm block - detach <Domain> <DevId> [ - f| - - force]
Destroy a domain's virtual block device. # xm block-list centos6 Vdev BE handle state evt - ch ring - ref BE - path
51712 0 0 4 8 8 / local / domain / 0 / backend / vbd / 1 / 51712 51728 0 0 4 10 868 / local / domain / 0 / backend / vbd / 1 / 51728 # xm block-detach centos6 51728 #移除 # xm block-list centos6 Vdev BE handle state evt - ch ring - ref BE - path
51712 0 0 4 8 8 / local / domain / 0 / backend / vbd / 1 / 51712 #fdisk 查看下 相关命令操作都是一样; xm - - help
|
-------------------------------------------------------------------------------------------
五、其他命令说明
save/restore:保存恢复虚拟机
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# xm save Usage: xm save [ - c] <Domain> <CheckpointFile>
Save a domain state to restore later. - c, - - checkpoint Leave domain running after creating snapshot
# xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 985 2 r - - - - - 1298.5
centos6 1 512 1 - b - - - - 25.0
# xm save centos6 /tmp/centos.save #把内存中的数据保存起来 - rwxr - xr - x 1 root root 514M May 22 10 : 56 centos.save
# xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 985 2 r - - - - - 1342.6
# xm restore /tmp/centos.save #恢复 # xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 985 2 r - - - - - 1401.7
centos6 8 512 1 - b - - - - 0.0
|
pause/unpause:暂停启动
1
2
3
4
5
6
7
8
9
|
# xm pause centos6 #暂停 # xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 985 2 r - - - - - 1404.7
centos6 8 512 1 - - p - - - 0.5
# xm unpause centos6 具体还需要xm - - help 查看详细。
|
-------------------------------------------------------------------------------------------
六、虚拟机的实时迁移
1、配置两台xen主机;并修改其配置文件:
1
2
3
4
5
6
7
8
9
10
|
#启动以下选项 # vim /etc/xen/xend-config.sxp (xend - relocation - server yes)
(xend - relocation - port 8002 )
(xend - relocation - address '')
(xend - relocation - hosts - allow '')
# service xend restart # ss -tunl | grep 8002 tcp LISTEN 0 5 * : 8002 * : *
|
2、使用共享存储(NFS/ISCSI)或者分布式文件系统
这里使用的是NFS:
1
2
3
4
5
|
# vim /etc/exports / xen / vm1 192.168 . 0.0 / 16 (rw,no_root_squash)
另台Xen挂载 # mount -t nfs 192.168.0.114:/xen/vm1/ /xen/vm1/ |
3、启动测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# xm create -c centos6 #给虚拟机配置地址长ping测试 # xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 512 2 r - - - - - 273.1
centos6 2 512 1 - b - - - - 12.1
# xm migrate #可以迁移到别的节点 # xm migrate -l centos6 192.168.0.114 迁移到别的节点 # xm list Name ID Mem VCPUs State Time(s)
Domain - 0 0 512 2 r - - - - - 284.1
#测试迁移正常;ping值也是正常未中断 # xm migrate -l centos6 192.168.0.113 可以再迁移回来 |
到此配置完成。