这段时间一直在研究如何制作一个适合Xen虚拟化的Windows Server 2008(2012)镜像,中间虽然遇到了一些阻挠,不过最终还是顺利解决,成功制作出了Xen Windows Server 2008(2012) 模板镜像。这里记录下整个制作过程:
1、首先要确保服务器的VT已经打开,可以通过如下命令来查看:
[[email protected] ~]# cat /proc/cpuinfo | egrep ‘(vmx|svm)‘
如果有输出,则说明该服务器的VT已经打开。
2、创建一个将要用来安装Windows Server 2008(2012)系统的LV,命令如下:
[[email protected] ~]# lvcreate -L 15G -n windows vg0 Logical volume "windows" created
参数说明:
1)-L:指定LV的大小。这里设置为15G,是因为Windows Server 2008(2012)系统本身安装完后,占用系统盘较大,大约在9--10G;另外官方推荐系统盘大小要在13G以上
2)vg0:这里是我测试创建的vg
3、查看已经成功创建的系统LV:
[[email protected] ~]# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert windows vg0 -wi-a- 15.00G [[email protected] ~]# ll /dev/vg0/windows lrwxrwxrwx 1 root root 23 Jun 2 13:12 /dev/vg0/windows -> /dev/mapper/vg0-windows
4、系统LV 创建好后,开始准备Xen虚拟机的配置文件:
kernel = "/usr/lib/xen/boot/hvmloader" builder=‘hvm‘ memory = 2048 name = "windows" vif = [ ‘bridge=eth0‘ ] disk = [ ‘phy:/dev/vg0/windows,hda,w‘,‘file:/root/cn_windows_server_2008_r2_standard_enterprise_datacenter_and_web_with_sp1_x64_dvd_617598.iso,hdc:cdrom,r‘ ] vcpus = 1 on_reboot = ‘restart‘ on_crash = ‘restart‘ boot = ‘d‘ sdl = 0 vnc = 1 vnclisten = ‘0.0.0.0‘ vncdisplay = 1 stdvga = 0 serial = ‘pty‘ usbdevice = ‘tablet‘ localtime = 1
注意:
1)这里使用的是全虚拟化方式安装Windows Server虚拟机,后面会安装半虚拟化驱动qplpv driver
2)要设置虚拟机从CDROM启动
5、创建并启动Windows Server虚拟机,命令如下:
[[email protected] ~]# xm create /root/windows.cfg Using config file "/root/windows.cfg". Started domain windows (id=442)
6、此时Windows Server虚拟机已经启动,进入到系统安装界面,下面就是按部就班地安装系统;我们通过VNC客户端连接到虚拟机上,如下所示:
7、系统安装到下面这一步时:
这里我们需要事先手动对整块磁盘进行分区并格式化,因为如果我们采用了系统默认的分区格式化方式,系统会帮我们创建一个100M的隐藏分区,后期我们在导出系统文件时会比较麻烦。
这里同时按住 Shift + F10,弹出命令提示符界面,执行如下命令进行磁盘分区并格式化:
执行命令后,关闭命令提示符,点击"刷新",发现此时磁盘分区已经创建好了,而且没有那100M的隐藏分区,如下图:
8、后面就是系统安装过程了:
9、系统安装完成后,关闭虚拟机,修改Xen虚拟机配置文件:
kernel = "/usr/lib/xen/boot/hvmloader" builder=‘hvm‘ memory = 2048 name = "windows" vif = [ ‘bridge=eth0‘ ] disk = [ ‘phy:/dev/vg0/windows,hda,w‘,‘file:/root/xen-windows-driver.iso,hdc:cdrom,r‘ ] vcpus = 1 on_reboot = ‘restart‘ on_crash = ‘restart‘ boot = ‘c‘ sdl = 0 vnc = 1 vnclisten = ‘0.0.0.0‘ vncdisplay = 1 stdvga = 0 serial = ‘pty‘ usbdevice = ‘tablet‘ localtime = 1
注意:
1)设置系统从硬盘启动
2)修改CDROM为xen-windows-driver.iso
PS:这里的xen-windows-driver.iso是本人通过 mkisofs 命令制作的,里面仅仅包含了qplpv drvier及agent,你同样可以制作你自己需要的iso文件
10、下面就是根据需要修改一些Windows Server系统配置的工作,具体如下:
1)关闭防火墙
2)开启远程桌面
3)设置新磁盘自动联机,具体命令如下:
4)安装半虚拟化驱动qplpv driver
安装完重启后,我们发现"设备管理器"里的驱动变为Xen的了
11、至此,系统配置修改已经完成了,下面关闭虚拟机,备份磁盘MBR启动扇区,命令如下:
[[email protected] ~]# dd if=/dev/vg0/windows of=windows_mbr bs=512 count=1 1+0 records in 1+0 records out 512 bytes (512 B) copied, 0.010009 seconds, 51.2 kB/s[[email protected] ~]# ll windows_mbr -rw-r--r-- 1 root root 512 Jun 2 15:49 windows_mbr
12、创建磁盘分区映射,将所有分区映射到/dev/mapper下:
[[email protected] ~]# kpartx -av /dev/vg0/windows add map windows1 : 0 31453184 linear /dev/vg0/windows 2048 [[email protected] ~]# ll /dev/mapper/ total 0 crw------- 1 root root 10, 62 May 5 17:18 control brw-rw---- 1 root disk 253, 5 Jun 2 15:34 vg0-windows brw-rw---- 1 root disk 253, 4 Jun 2 15:54 windows1 [[email protected] ~]#
因为前面我们安装系统时只创建了一个分区,因此这里就只有一个windows分区,即windows1
13、mount windows分区到mnt下:
[[email protected] ~]# mount -t ntfs-3g /dev/mapper/windows1 /mnt/ [[email protected]alhost ~]# ll /mnt/ total 2097184 drwxrwxrwx 1 root root 4096 Jun 2 14:17 Boot -rwxrwxrwx 1 root root 383786 Nov 21 2010 bootmgr -rwxrwxrwx 1 root root 8192 Jun 2 14:17 BOOTSECT.BAK lrwxrwxrwx 2 root root 60 Jul 14 2009 Documents and Settings -> /mnt//Users -rwxrwxrwx 1 root root 2147082240 Jun 2 15:32 pagefile.sys drwxrwxrwx 1 root root 0 Jul 14 2009 PerfLogs drwxrwxrwx 1 root root 4096 Jun 2 14:20 ProgramData drwxrwxrwx 1 root root 4096 Jun 2 15:31 Program Files drwxrwxrwx 1 root root 4096 Jun 2 15:31 Program Files (x86) drwxrwxrwx 1 root root 0 Jun 2 14:20 Recovery drwxrwxrwx 1 root root 0 Jul 14 2009 $Recycle.Bin drwxrwxrwx 1 root root 4096 Jun 2 14:18 System Volume Information drwxrwxrwx 1 root root 4096 Jun 2 14:41 Users drwxrwxrwx 1 root root 16384 Jun 2 15:29 Windows [[email protected] ~]#
因为windows文件系统是NTFS格式的,所以这里类型要设置为ntfs-3g
14、删除文件 pagefile.sys:
[[email protected] ~]# rm -f /mnt/pagefile.sys [[email protected] ~]# ll /mnt/ total 424 drwxrwxrwx 1 root root 4096 Jun 2 14:17 Boot -rwxrwxrwx 1 root root 383786 Nov 21 2010 bootmgr -rwxrwxrwx 1 root root 8192 Jun 2 14:17 BOOTSECT.BAK lrwxrwxrwx 2 root root 60 Jul 14 2009 Documents and Settings -> /mnt//Users drwxrwxrwx 1 root root 0 Jul 14 2009 PerfLogs drwxrwxrwx 1 root root 4096 Jun 2 14:20 ProgramData drwxrwxrwx 1 root root 4096 Jun 2 15:31 Program Files drwxrwxrwx 1 root root 4096 Jun 2 15:31 Program Files (x86) drwxrwxrwx 1 root root 0 Jun 2 14:20 Recovery drwxrwxrwx 1 root root 0 Jul 14 2009 $Recycle.Bin drwxrwxrwx 1 root root 4096 Jun 2 14:18 System Volume Information drwxrwxrwx 1 root root 4096 Jun 2 14:41 Users drwxrwxrwx 1 root root 16384 Jun 2 15:29 Windows [[email protected] ~]#
15、unmount windows分区:
[[email protected] ~]# umount /mnt/ [[email protected] ~]# ll /mnt/ total 0 [[email protected] ~]#
16、打包并压缩windows1分区里的系统文件:
[[email protected] ~]# ntfsclone --save-image --output - /dev/mapper/windows1 | gzip -c > windows_img ntfsclone v1.13.1 (libntfs 9:0:0) NTFS volume version: 3.1 Cluster size : 4096 bytes Current volume size: 16104026112 bytes (16105 MB) Current device size: 16104030208 bytes (16105 MB) Scanning volume ... 100.00 percent completed Accounting clusters ... Space in use : 7274 MB (45.2%) Saving NTFS to image ... 100.00 percent completed Syncing ... [[email protected] ~]# ll windows_img -rw-r--r-- 1 root root 3073945978 Jun 2 16:14 windows_img
17、删除磁盘分区映射:
[[email protected] ~]# kpartx -d /dev/vg0/windows [[email protected] ~]# ll /dev/mapper/ total 0 crw------- 1 root root 10, 62 May 5 17:18 control brw-rw---- 1 root disk 253, 5 Jun 2 15:34 vg0-windows [[email protected] ~]#
18、至此,我们需要的Xen Windows Server模版镜像就制作好了,下面两个文件就是我们需要的:
[[email protected] ~]# ll windows_* -rw-r--r-- 1 root root 3073945978 Jun 2 16:14 windows_img -rw-r--r-- 1 root root 512 Jun 2 15:49 windows_mbr [[email protected] ~]#
PS:这里虽然是Windows Server 2008示例说明,但2012过程同上。