在一文中,我们已经成功制作出了 CentOS7 磁盘镜像。下面我们说明下如何通过它来生成目标虚拟机,同时测试下之前制作好的虚拟机磁盘镜像模版是否有问题。
1、创建目标虚拟机的系统盘:
[root@localhost ~]# lvcreate -L 15G -n centos7_vm vg0 Logical volume "centos7_vm" created
2、查看成功创建的虚拟机系统盘:
[root@localhost ~]# lvs LV VG Attr LSize Origin Snap% Move Log Copy% Convert centos7_vm vg0 -wi-a- 15.00G
3、将之前导出的centos7_mbr 重新dd到目标虚拟机系统盘里:
[root@localhost ~]# dd if=/root/centos7_mbr of=/dev/vg0/centos7_vm 2048+0 records in2048+0 records out1048576 bytes (1.0 MB) copied, 0.101847 seconds, 10.3 MB/s[root@localhost ~]#
4、修改系统盘分区大小(以扇区数为单位):
[root@localhost ~]# echo "000001CA:00F8DF01" | xxd -r - /dev/vg0/centos7_vm [root@localhost ~]#
因为前面我们创建的目标虚拟机系统盘大小是15G,因此这里我们需要将磁盘分区1所占用的扇区数大小设置为00F8DF01(具体计算方式后续再整理),而000001CA则表示保存磁盘分区1占用扇区数的开始字节位置,具体说明请参考。
5、建立系统盘的磁盘分区映射,将系统盘的所有分区映射到/dev/mapper下:
[root@localhost ~]# kpartx -av /dev/vg0/centos7_vm add map centos7_vm1 : 0 31455232 linear /dev/vg0/centos7_vm 2048[root@localhost ~]# ll /dev/mapper/total 0brw-rw---- 1 root disk 253, 1 Jun 30 10:47 centos7_vm1crw------- 1 root root 10, 62 May 5 17:18 controlbrw-rw---- 1 root disk 253, 0 Jun 30 10:46 vg0-centos7_vm[root@localhost ~]#
红色的部分即为虚拟机磁盘的根分区。
6、格式化目标虚拟机根分区文件系统为ext3(或者ext4等等):
[root@localhost ~]# mkfs.ext3 /dev/mapper/centos7_vm1 mke2fs 1.39 (29-May-2006)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)1966080 inodes, 3931904 blocks196595 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4026531840120 block groups32768 blocks per group, 32768 fragments per group16384 inodes per groupSuperblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208Writing inode tables: done Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 34 mounts or180 days, whichever comes first. Use tune2fs -c or -i to override.[root@localhost ~]#
7、将目标虚拟机根分区mount到 /mnt(或者其他目录)下:
[root@localhost ~]# mount /dev/mapper/centos7_vm1 /mnt/[root@localhost ~]# ll /mnt/total 16drwx------ 2 root root 16384 Jun 30 10:58 lost+found[root@localhost ~]#
8、解压磁盘镜像模板系统文件 centos7_img 到 /mnt下,并 umout :
[root@localhost ~]# tar zxf /root/centos7_img -C /mnt[root@localhost ~]# ll /mnt/total 76lrwxrwxrwx 1 root root 7 Jun 30 11:02 bin -> usr/bindr-xr-xr-x 4 root root 4096 Jun 19 22:36 bootdrwxr-xr-x 2 root root 4096 Jun 19 22:14 devdrwxr-xr-x 74 root root 4096 Jun 26 10:26 etcdrwxr-xr-x 2 root root 4096 Jun 10 2014 homelrwxrwxrwx 1 root root 7 Jun 30 11:02 lib -> usr/liblrwxrwxrwx 1 root root 9 Jun 30 11:02 lib64 -> usr/lib64drwx------ 2 root root 16384 Jun 19 22:14 lost+founddrwxr-xr-x 2 root root 4096 Jun 10 2014 mediadrwxr-xr-x 2 root root 4096 Jun 10 2014 mntdrwxr-xr-x 2 root root 4096 Jun 10 2014 optdrwxr-xr-x 2 root root 4096 Jun 19 22:14 procdr-xr-x--- 2 root root 4096 Jun 19 17:35 rootdrwxr-xr-x 2 root root 4096 Jun 19 22:14 runlrwxrwxrwx 1 root root 8 Jun 30 11:02 sbin -> usr/sbindrwxr-xr-x 2 root root 4096 Jun 10 2014 srvdrwxr-xr-x 2 root root 4096 Jun 19 22:14 sysdrwxrwxrwt 7 root root 4096 Jun 23 19:17 tmpdrwxr-xr-x 13 root root 4096 Jun 19 22:15 usrdrwxr-xr-x 20 root root 4096 Jun 23 19:17 var[root@localhost ~]# umount /mnt/
9、删除磁盘分区映射:
[root@localhost ~]# kpartx -d /dev/vg0/centos7_vm [root@localhost ~]# ll /dev/mapper/total 0crw------- 1 root root 10, 62 May 5 17:18 controlbrw-rw---- 1 root disk 253, 0 Jun 30 10:46 vg0-centos7_vm[root@localhost ~]#
10、至此,虚拟机系统盘已经准备好,下面就是需要准备虚拟机配置文件:
kernel = "/usr/lib/xen/boot/hvmloader"builder='hvm'memory = 1024name = "centos7_vm"vif = [ 'bridge=eth0' ]disk = [ 'phy:/dev/vg0/centos7_vm,hda,w' ]vcpus = 1on_reboot = 'restart'on_crash = 'restart'boot = 'c'sdl = 0vnc = 1vnclisten = '0.0.0.0'vncdisplay = 6stdvga = 0serial = 'pty'usbdevice = 'tablet'xen_platform_pci=1
11、启动虚拟机,并通过vnc登陆该虚拟机:
至此,通过前面制作的 CentOS7 磁盘镜像模板成功生成了一个全新的虚拟机。