标签:
在《Xen虚拟机磁盘镜像模板制作(三)—CentOS 7》一文中,我们已经成功制作出了 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、将之前导出的磁盘MBR主引导扇区 centos7_mbr 重新dd到目标虚拟机系统盘里:
[root@localhost ~]# dd if=/root/centos7_mbr of=/dev/vg0/centos7_vm 2048+0 records in 2048+0 records out 1048576 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占用扇区数的开始字节位置,具体说明请参考《磁盘MBR主引导扇区》。
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 0 brw-rw---- 1 root disk 253, 1 Jun 30 10:47 centos7_vm1 crw------- 1 root root 10, 62 May 5 17:18 control brw-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: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) 1966080 inodes, 3931904 blocks 196595 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=4026531840 120 block groups 32768 blocks per group, 32768 fragments per group 16384 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 34 mounts or 180 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 16 drwx------ 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 76 lrwxrwxrwx 1 root root 7 Jun 30 11:02 bin -> usr/bin dr-xr-xr-x 4 root root 4096 Jun 19 22:36 boot drwxr-xr-x 2 root root 4096 Jun 19 22:14 dev drwxr-xr-x 74 root root 4096 Jun 26 10:26 etc drwxr-xr-x 2 root root 4096 Jun 10 2014 home lrwxrwxrwx 1 root root 7 Jun 30 11:02 lib -> usr/lib lrwxrwxrwx 1 root root 9 Jun 30 11:02 lib64 -> usr/lib64 drwx------ 2 root root 16384 Jun 19 22:14 lost+found drwxr-xr-x 2 root root 4096 Jun 10 2014 media drwxr-xr-x 2 root root 4096 Jun 10 2014 mnt drwxr-xr-x 2 root root 4096 Jun 10 2014 opt drwxr-xr-x 2 root root 4096 Jun 19 22:14 proc dr-xr-x--- 2 root root 4096 Jun 19 17:35 root drwxr-xr-x 2 root root 4096 Jun 19 22:14 run lrwxrwxrwx 1 root root 8 Jun 30 11:02 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Jun 10 2014 srv drwxr-xr-x 2 root root 4096 Jun 19 22:14 sys drwxrwxrwt 7 root root 4096 Jun 23 19:17 tmp drwxr-xr-x 13 root root 4096 Jun 19 22:15 usr drwxr-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 0 crw------- 1 root root 10, 62 May 5 17:18 control brw-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 = 1024 name = "centos7_vm" vif = [ ‘bridge=eth0‘ ] disk = [ ‘phy:/dev/vg0/centos7_vm,hda,w‘ ] vcpus = 1 on_reboot = ‘restart‘ on_crash = ‘restart‘ boot = ‘c‘ sdl = 0 vnc = 1 vnclisten = ‘0.0.0.0‘ vncdisplay = 6 stdvga = 0 serial = ‘pty‘ usbdevice = ‘tablet‘ xen_platform_pci=1
11、启动虚拟机,并通过vnc登陆该虚拟机:
至此,通过前面制作的 CentOS7 磁盘镜像模板成功生成了一个全新的虚拟机。
标签:
原文地址:http://www.cnblogs.com/CasonChan/p/4602037.html