一、使用DomU自有kernel来启动运行DomU
上篇博文中创建的pv格式的vm,内核和引导文件都是放在DomU中,
1、准备磁盘映像文件
losetup
losetup -a: 显示所有已用的loop设备相关信息
losetup -f: 显示第一个空闲的loop设备文件
losetup -d:拆除loop设备
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
| [iyunv@BAIYU_175 xen]# qemu-img create -f raw -o size=5G busybox3.img
Formatting 'busybox3.img', fmt=raw size=5368709120
[iyunv@BAIYU_175 xen]# mount -o loop /images/xen/busybox.img /mnt
#这个命令会自动调用losetup,把磁盘镜像文件和回环设备建立关联关系
[iyunv@BAIYU_175 xen]# losetup -a
/dev/loop0: [0802]:538231 (/images/xen/busybox.img)
[iyunv@BAIYU_175 xen]# losetup -f
/dev/loop1
[iyunv@BAIYU_175 xen]# umount /mnt
[iyunv@BAIYU_175 xen]# losetup /dev/loop0 /images/xen/busybox3.img #挂载后可以进行分区
[iyunv@BAIYU_175 xen]# losetup -a
/dev/loop0: [0802]:524756 (/images/xen/busybox3.img)
[iyunv@BAIYU_175 xen]# losetup -f
/dev/loop1
[iyunv@BAIYU_175 xen]# fdisk /dev/loop1
Unable to read /dev/loop1
[iyunv@BAIYU_175 xen]# fdisk /dev/loop0
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x4778379d.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): p
Disk /dev/loop0: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4778379d
Device Boot Start End Blocks Id System
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4):
Value out of range.
Partition number (1-4): 2
First cylinder (67-652, default 67):
Using default value 67
Last cylinder, +cylinders or +size{K,M,G} (67-652, default 652): +2G
Command (m for help): p
Disk /dev/loop0: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xbb6d135d
Device Boot Start End Blocks Id System
/dev/loop0p1 1 66 530113+ 83 Linux
/dev/loop0p2 67 328 2104515 83 Linux
Command (m for help): w
[iyunv@BAIYU_175 xen]# kpartx /dev/loop0
loop0p1 : 0 1060227 /dev/loop0 63
loop0p2 : 0 4209030 /dev/loop0 1060290
[iyunv@BAIYU_175 xen]# kpartx -av /dev/loop0
add map loop0p1 (253:0): 0 1060227 linear /dev/loop0 63
add map loop0p2 (253:1): 0 4209030 linear /dev/loop0 1060290
[iyunv@BAIYU_175 xen]# ls /dev/mapper/
control loop0p1 loop0p2
[iyunv@BAIYU_175 xen]# mke2fs -t ext4 /dev/mapper/loop0p1
[iyunv@BAIYU_175 xen]# mke2fs -t ext4 /dev/mapper/loop0p2
[iyunv@BAIYU_175 xen]# mkdir /mnt/{boot,sysroot}
[iyunv@BAIYU_175 xen]# mount /dev/mapper/loop0p1 /mnt/boot/
[iyunv@BAIYU_175 xen]# mount /dev/mapper/loop0p2 /mnt/sysroot/
[iyunv@BAIYU_175 xen]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz
[iyunv@BAIYU_175 xen]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img
[iyunv@BAIYU_175 xen]# grub-install --root-directory=/mnt /dev/loop0
#创建grub文件,
Probing devices to guess BIOS drives. This may take a long time.
/dev/loop0 does not have any corresponding BIOS drive.
[iyunv@BAIYU_175 xen]# ls /mnt/boot/grub/
device.map ffs_stage1_5 minix_stage1_5 stage2 xfs_stage1_5
e2fs_stage1_5 iso9660_stage1_5 reiserfs_stage1_5 ufs2_stage1_5
fat_stage1_5 jfs_stage1_5 stage1 vstafs_stage1_5
[iyunv@BAIYU_175 xen]# vi /mnt/boot/grub/grub.conf
[iyunv@BAIYU_175 xen]# cat /mnt/boot/grub/grub.conf
default=0
timeout=5
title BusyBox(kernel-2.6.32)
root (hd0,0)
kernel /vmlinuz root=/dev/xvda1 ro selinux=0 init=/bin/sh
initrd /initramfs.img
[iyunv@BAIYU_175 ~]# cp -a busybox-1.22.1/_install/* /mnt/sysroot
[iyunv@BAIYU_175 ~]# cd /mnt/sysroot/
[iyunv@BAIYU_175 sysroot]# ls
bin linuxrc lost+found sbin usr
[iyunv@BAIYU_175 sysroot]# mkdir lib/modules dev proc sysc etc var tmp -pv
mkdir: 已创建目录 "lib"
mkdir: 已创建目录 "lib/modules"
mkdir: 已创建目录 "dev"
mkdir: 已创建目录 "proc"
mkdir: 已创建目录 "sysc"
mkdir: 已创建目录 "etc"
mkdir: 已创建目录 "var"
mkdir: 已创建目录 "tmp"
[iyunv@BAIYU_175 sysroot]# cp /lib/modules/2.6.32-573.el6.x86_64/kernel/drivers/net/xen-netfront.ko lib/modules
[iyunv@BAIYU_175 ~]# losetup -a
/dev/loop0: [0802]:524756 (/images/xen/busybox3.img)
[iyunv@BAIYU_175 ~]# kpartx -d /dev/loop0
[iyunv@BAIYU_175 ~]# losetup -a
[iyunv@BAIYU_175 ~]# cd /etc/xen/
[iyunv@BAIYU_175 xen]# ls
auto busybox2 busybox.orig scripts vm2.orig xlexample.hvm
busybox busybox2.orig cpupool vm2 xl.conf xlexample.pvlinux
[iyunv@BAIYU_175 xen]# cp busybox busybox3
[iyunv@BAIYU_175 xen]# vi busybox3
[iyunv@BAIYU_175 xen]# grep -v '^#\|^$' busybox3
name = "anyfish-003"
memory = 256
vcpus = 2
vif = [ 'bridge=xenbr0' ]
disk = [ '/images/xen/busybox3.img,raw,xvda,rw' ]
bootloader='/usr/bin/pygrub'
[iyunv@BAIYU_175 xen]# xl create busybox3 -c
|
二、创建虚拟机并完成Centos6.5系统安装
1、创建磁盘映像文件;
2、获取安装指定版本的系统所需要kernel及initrd文件;
3、创建DomU配置文件,示例如下:
1
2
3
4
5
6
7
8
9
| name = "centos-001"
kernel = "/images/kernel/vmlinuz"
ramdisk = "/images/kernel/initrd.img"
extra = "ks=http://172.16.0.1/centos6.x86_64.cfg"
memory = 512
vcpus = 2
vif = [ 'bridge=xenbr0' ]
disk = [ '/images/xen/centos6.6.img,qcow2,xvda,rw' ]
on_reboot = "shutdown"
|
4、安装完成后,创建虚拟机的配置文件需要做出修改;
1
2
3
4
5
6
| name = "centos-001"
bootloader = "pygrub"
memory = 512
vcpus = 2
vif = [ 'bridge=xenbr0' ]
disk = [ '/images/xen/centos6.6.img,qcow2,xvda,rw' ]
|
启动图形窗口:
在创建虚拟机的配置文件中定义vfb
(1) vfb = [ 'sdl=1' ]
(2) vnc
(a) yum install tigervnc
(b) vfb = [ 'vnc=1' ]
vnc监听的端口为5900, 相应的DISPLAYNUM为0
使用Dom0中物理磁盘分区为DomU提供存储空间:
三、使用libvirt实现xen虚拟机管理
yum install libvirt libvirt-deamon-xen virt-manager python-virtinst libvirt-client
service libvirtd start
virt-manager, virsh, virt-install
四、总结一下前面学的内容
1、xen:是Type-I的虚拟化技术解决方案
2、三个组件
Xen Hypervisor:分配cpu、内存、interrupt(中断)
Dom0:特权域,驱动I/O,并为DomU提供模拟的IO设备,
Xen管理工具栈
xm:
xl:
libvirt/virsh:
DomU:非特权域
Guest OS
3、工作模式
PV
FV(HVM)
PV on HVM:I/O半虚拟化但cpu完全虚拟化
4、安装Xen Hyperversionz的过程
安装CentOS-->安装Xen及能运行于Dom0中的内核-->修改/etc/grub.conf以xen.gz为内核,而原来的内核及ramdisk做为xen的模块使用,
5、创建虚拟机的过程
两种方式:
由Dom0提供内核:
kernel= 路径
ramdisk= 路径
根文件系统,
由DomU提供内核:
在配置文件中添加bootloader=“pygrub”
五、xen虚拟机实时迁移
前言: xen虚拟机提供了一种类似于heartbeat高可用方案,在保证也不中断的业务情况下实现虚拟机迁移技术。在保证虚拟机上的服务正常的情况下将运行中的Domain迁移到其他机器上,实现xen虚拟机的高可用。 1、实验准备
(1)各个测试机之间时间要同步 (2)node3提供iscsi网络共享存储 (4)node4与node5做为xen虚拟机 (5)本人使用的都是CentOS6.5操作系统 (6)关闭所有节点的iptables和SELinux
对busybox小系统进行实施迁移
修改node4节点与node5节点的xen配置文件,开启以下选项 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| [iyunv@node4 ~]# vim /etc/xen/xend-config.sxp
(xend-relocation-server yes)
(xend-relocation-port 8002)
(xend-address '172.16.2.16')
(xend-relocation-hosts-allow '')
[iyunv@node5 ~]# vim /etc/xen/xend-config.sxp
(xend-relocation-server yes)
(xend-relocation-port 8002)
(xend-address '172.16.2.15')
(xend-relocation-hosts-allow '')
[iyunv@node4 ~]# service xend restart;ssh node5 'service xend restart'
Stopping xend daemon: [ OK ]
Starting xend daemon: [ OK ]
Stopping xend daemon: [ OK ]
Starting xend daemon: [ OK ]
|
将运行在node5节点的busybox小系统迁移到node4节点
1
2
3
4
5
6
7
8
| [iyunv@node5 ~]# xm migrate -l busybox 172.16.2.15 \\将busybox系统迁移到node4节点
[iyunv@node4 ~]# xm list \\在node4节点查看,busybox已经成功移植到node4节点
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 1 r----- 427.8
busybox 3 512 2 -b---- 0.1
[iyunv@node5 ~]# xm list \\node5节点已经没由DomU在运行
Name ID Mem VCPUs State Time(s)
Domain-0 0 1024 1 r----- 64.7
|
实际生产环境中可对Xen平台做高可用,当物理机故障可移动检测并进行实时迁移,保证业务不受影响。
|