一、使用DomU自有kernel来启动运行DomU
上篇博文中创建的pv格式的vm,内核和引导文件都是放在DomU中,
1、准备磁盘映像文件
losetup
losetup -a: 显示所有已用的loop设备相关信息
losetup -f: 显示第一个空闲的loop设备文件
losetup -d:拆除loop设备
[root@BAIYU_175 xen]# qemu-img create -f raw -o size=5G busybox3.img Formatting 'busybox3.img', fmt=raw size=5368709120 [root@BAIYU_175 xen]# mount -o loop /p_w_picpaths/xen/busybox.img /mnt #这个命令会自动调用losetup,把磁盘镜像文件和回环设备建立关联关系 [root@BAIYU_175 xen]# losetup -a /dev/loop0: [0802]:538231 (/p_w_picpaths/xen/busybox.img) [root@BAIYU_175 xen]# losetup -f /dev/loop1 [root@BAIYU_175 xen]# umount /mnt [root@BAIYU_175 xen]# losetup /dev/loop0 /p_w_picpaths/xen/busybox3.img #挂载后可以进行分区 [root@BAIYU_175 xen]# losetup -a /dev/loop0: [0802]:524756 (/p_w_picpaths/xen/busybox3.img) [root@BAIYU_175 xen]# losetup -f /dev/loop1 [root@BAIYU_175 xen]# fdisk /dev/loop1 Unable to read /dev/loop1 [root@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 [root@BAIYU_175 xen]# kpartx /dev/loop0 loop0p1 : 0 1060227 /dev/loop0 63 loop0p2 : 0 4209030 /dev/loop0 1060290 [root@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 [root@BAIYU_175 xen]# ls /dev/mapper/ control loop0p1 loop0p2 [root@BAIYU_175 xen]# mke2fs -t ext4 /dev/mapper/loop0p1 [root@BAIYU_175 xen]# mke2fs -t ext4 /dev/mapper/loop0p2 [root@BAIYU_175 xen]# mkdir /mnt/{boot,sysroot} [root@BAIYU_175 xen]# mount /dev/mapper/loop0p1 /mnt/boot/ [root@BAIYU_175 xen]# mount /dev/mapper/loop0p2 /mnt/sysroot/ [root@BAIYU_175 xen]# cp /boot/vmlinuz-2.6.32-573.el6.x86_64 /mnt/boot/vmlinuz [root@BAIYU_175 xen]# cp /boot/initramfs-2.6.32-573.el6.x86_64.img /mnt/boot/initramfs.img [root@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. [root@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 [root@BAIYU_175 xen]# vi /mnt/boot/grub/grub.conf [root@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 [root@BAIYU_175 ~]# cp -a busybox-1.22.1/_install/* /mnt/sysroot [root@BAIYU_175 ~]# cd /mnt/sysroot/ [root@BAIYU_175 sysroot]# ls bin linuxrc lost+found sbin usr [root@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" [root@BAIYU_175 sysroot]# cp /lib/modules/2.6.32-573.el6.x86_64/kernel/drivers/net/xen-netfront.ko lib/modules [root@BAIYU_175 ~]# losetup -a /dev/loop0: [0802]:524756 (/p_w_picpaths/xen/busybox3.img) [root@BAIYU_175 ~]# kpartx -d /dev/loop0 [root@BAIYU_175 ~]# losetup -a [root@BAIYU_175 ~]# cd /etc/xen/ [root@BAIYU_175 xen]# ls auto busybox2 busybox.orig scripts vm2.orig xlexample.hvm busybox busybox2.orig cpupool vm2 xl.conf xlexample.pvlinux [root@BAIYU_175 xen]# cp busybox busybox3 [root@BAIYU_175 xen]# vi busybox3 [root@BAIYU_175 xen]# grep -v '^#\|^$' busybox3 name = "anyfish-003" memory = 256 vcpus = 2 vif = [ 'bridge=xenbr0' ] disk = [ '/p_w_picpaths/xen/busybox3.img,raw,xvda,rw' ] bootloader='/usr/bin/pygrub' [root@BAIYU_175 xen]# xl create busybox3 -c
二、创建虚拟机并完成Centos6.5系统安装
1、创建磁盘映像文件;
2、获取安装指定版本的系统所需要kernel及initrd文件;
3、创建DomU配置文件,示例如下:
name = "centos-001" kernel = "/p_w_picpaths/kernel/vmlinuz" ramdisk = "/p_w_picpaths/kernel/initrd.img" extra = "ks=http://172.16.0.1/centos6.x86_64.cfg" memory = 512 vcpus = 2 vif = [ 'bridge=xenbr0' ] disk = [ '/p_w_picpaths/xen/centos6.6.img,qcow2,xvda,rw' ] on_reboot = "shutdown"
4、安装完成后,创建虚拟机的配置文件需要做出修改;
name = "centos-001" bootloader = "pygrub" memory = 512 vcpus = 2 vif = [ 'bridge=xenbr0' ] disk = [ '/p_w_picpaths/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配置文件,开启以下选项
[root@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 '') [root@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 '') [root@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节点
[root@node5 ~]# xm migrate -l busybox 172.16.2.15 \\将busybox系统迁移到node4节点 [root@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 [root@node5 ~]# xm list \\node5节点已经没由DomU在运行 Name ID Mem VCPUs State Time(s) Domain-0 0 1024 1 r----- 64.7
实际生产环境中可对Xen平台做高可用,当物理机故障可移动检测并进行实时迁移,保证业务不受影响。