xen系统剖析
xen系统是redhat最早支持的虚拟化技术,后来redhat收入了KVM并将KVM集合到内核中,但这不表明xen过时了,这只是redhat的对市场的策略问题,这里我不详细说明。之前我已经写过了关于redhat下如何安装xen,本文档详细对xen的工作原理进行分析。
1、xen虚拟机的生成
name = “boottest”
uuid = “134acb0b-ebad-9620-98ef-a86e12bb94ba”
maxmem = 512
memory = 512
vcpus = 1
builder = “hvm”
kernel = “/usr/lib/xen/boot/hvmloader”
boot = “n”
pae = 1
acpi = 1
apic = 1
localtime = 0
on_poweroff = “destroy”
on_reboot = “restart”
on_crash = “restart”
device_model = “/usr/lib64/xen/bin/qemu-dm”
sdl = 0
vnc = 1
vncunused = 1
keymap = “en-us”
disk = [ "file:/opt/boottest.img,hda,w" ]
vif = [ "mac=00:16:36:53:d5:6c,bridge=xenbr0,script=vif-bridge" ]
parallel = “none”
serial = “pty”
以上为一个简单的xm完全虚拟化配置选项。细细分析可以发现其实这个配置文件中有许多参数的定义就像写一个bios的选项。简单的就不详细说明了,我来说一下配置文件中的重要项目,对于初次使用xen的用户来说见意使用 virt-manager 图型工具.
xen配置文件其实就是相当于一台主机的bios配置文件:
kernel = “/usr/lib/xen/boot/hvmloader”
kernel参数用来指定虚拟机引导程序,在使用完全虚拟化时指定为以上值,hvmloader是一个二进制程序,他的内部封装了一个引导虚拟机的内核.但是在准虚拟化时使用virt-manager创建虚拟机后我们会看到kernel的参数不存在了,多了一个bootloader = ‘/usr/bin/pygrub’.因为准虚拟化使用了IO桥接,它需要使用本地的kernel,而pygrub并不是一个像hvmloader一样的带有内核引导程序,它是一个使用python编写的grub引导脚本.之后我会提到这个程序.
boot = “n”
boot参数用来指定虚拟机的启动设备常用的选项为[cdn],c代表使用虚拟磁盘引导也就是后面file:/opt/boottest.img文件,d代表使用虚拟光驱引导,n代表使用pxe引导,当然有用户在机器启动时按下f12键启动pxe,但在实际应用中我们发现虚拟的引导过程是很快的F12的操作机会很短.
pae =1
acpi =1
apic =1
usb =1
这四项和bios中控制非常相似,上面的配置文件中并没有usb项,因为上面的虚拟机不支持usb设备。pae,apic是重要的参数,这两个参数不打开直接影响你的虚拟机不能安装64x系统,这里不详述原因了,大家可以google一下这两个cpu参数的作用。
详细的xenconfig的配置项大家可以参考xen官方文档。
http://www.xen.org/files/Support/XenConfigurationDetails.pdf
xen的配置与你将来要装的系统没有关系,你可以完全把它看成你组状的一台物理机。但有一点需要注意准虚拟化只支持linux.而windows的io桥接是通过加入到windows的xen驱动实现的。
2、虚拟机的磁盘设备
在虚拟机中磁盘是一个文件,它是如何产生的呢?
testboot1.img: data
它是有virt-image工具生成的, virt-image –name example –vnc -i image.xml,那image.xml是什么呢?它是一个有libvirt定义的虚拟磁盘配置文件.
在这里我要说明一下virt-manager,libvirt,xen的关系
virt-manager其实是由redhat支持维护的一个开源项目它提供了图型化和shell工具管理xen和kvm,在使用redhat安装虚拟化时redhat建议安装yum install virt-manager libvirt libvirt-python python-virtinstyum,在安装完成后我们发现了两种管理虚拟机的方法,xm和virt*,那么libvirt是什么呢?它是虚拟化管理接口.
virt-manager ->用户cgi程序
libvirt ->接口程序
xen ->虚拟化核
这样我们就很清楚了。
详细的内容大家可以查阅以下网站
xen -> xen.org
libvirt ->libvirt.org
virt-manager ->virt-manager.org
xen正常生成这个文件后如果我们发现虚拟磁盘不能正常工作,我们如何检查这个磁盘的情况呢?
如果是一个新的磁盘文件,我们可以试着格式化一下。
[root@localhost opt]# mkfs.ext3 -jF testboot1.img
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
512000 inodes, 1024000 blocks
51200 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1048576000
32 block groups
32768 blocks per group, 32768 fragments per group
16000 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information:
done
This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost opt]# file testboot1.img
testboot1.img: Linux rev 1.0 ext3 filesystem data (large files)
它是一个好的数据文件,如果是一个已经安装好系统的虚拟磁盘,
[root@localhost vm_disks]# file wv33.disk.xm
wv33.disk.xm: x86 boot sector, Microsoft Windows XP MBR, Serial 0x4b5f4b5f; partition 1: ID=0×7, active, starthead 1, startsector 63, 20450682 sectors
[root@localhost vm_disks]# file pvend999.disk.xm
pvend999.disk.xm: x86 boot sector; partition 1: ID=0×83, active, starthead 1, startsector 63, 208782 sectors; partition 2: ID=0x8e, starthead 0, startsector 208845, 10024560 sectors, code offset 0×48
我们通过file命令可以看到磁盘信息,看它是不是完整,如果完整正明分区信息正常,我们可以在linux下对磁盘文件进行fsck。
在虚拟化后的磁盘文件不要把它看成文件,可以把它看成一设备块,所有的磁盘命令对它都适用。
3、pygrub分析
准虚拟化技术使虚拟机实现了真正的上线应用,其主要的技术在io桥接,io桥接在之前已经与大家分享了。这次主要是说明一pygrub,在刚刚接初虚拟化时主要为实现我们的需求,并没有太过细的去分析这个文件,导至后来测试集中管理xen软件convirture2.0时出现了许多问题,pygrub是一个python编的一个grub软件,grub的功能不在详述,它只是为了读取虚拟磁盘中的boot信息,它与hvmloader完全不同,当我们创建手工创建一个准虚拟化系统时,我们手工生成了一个磁盘文件,这个磁盘文件并没有安装任何linux系统,当我们使用xm create testvm时会发生错误:
[root@localhost opt]# pygrub testboot1.img
Traceback (most recent call last):
File “/usr/bin/pygrub”, line 682, in ?
chosencfg = run_grub(file, entry, fs)
File “/usr/bin/pygrub”, line 536, in run_grub
g = Grub(file, fs)
File “/usr/bin/pygrub”, line 203, in __init__
self.read_config(file, fs)
File “/usr/bin/pygrub”, line 397, in read_config
raise RuntimeError, “couldn’t find bootloader config file in the image provided.”
RuntimeError: couldn’t find bootloader config file in the image provided.
pygrub无法正常读取到boot config,这是因为这个磁盘还是一个空磁盘。为什么我们使用virt-manager创建准虚拟化时不会出现这样的问题呢?
因为virt-manager替我们多做一个操作,我们使用virt-manager创建准虚拟化虚机时virt-manager只能选择从http/nfs/ftp来安装,这个目录都被指定到了http://xxx/pub/os/Linux/RedHat/enterprise/x86_64/5ASU4/install/ 但其实virt-manager又替我们做了一步,它把kernerl和ramdisk参数帮我们设置install/images/xen/vmlinux 与install/images/xen/initrd.img。由vmlinux引导虚拟机启动进行安装,完成后virt-manager重新生成了一个虚拟机配置文件放在/etc/xen目录,并把它的kernerl和ramdisk参数取消,加入了bootloader = ‘/usr/bin/pygrub’。所以我们在virt0manager创建准虚拟化时是看不到config文件的。
[root@localhost opt]# pygrub testboot1.img
pyGRUB version 0.6
谀哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪目
硾[10m Red Hat Enterprise Linux Server (2.6.18-164.el5) 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
硾[10m 硾[10m
滥哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪哪馁
Use the ?and ?keys to select which entry is highlighted.
Press enter to boot the selected OS, ‘e’ to edit the
commands before booting, ‘a’ to modify the kernel arguments
before booting, or ‘c’ for a command line.
Will boot selected entry in 1 seconds
dd if=/dev/zero of=bigfile bs=1024k count=1 seek=10000
/sbin/mkfs.ext3 bigfile
sudo mount -o loop /home/admin/dayu-postcheck/postcheck/bigfile /home/admin/dayu-postcheck/postcheck/yangtmp/