在《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- .00G
3、将之前导出的磁盘MBR主引导扇区centos7_mbr 重新dd到目标虚拟机系统盘里:
[root@localhost ~]# dd if=/root/centos7_mbr of=/dev/vg0/centos7_vm
+ records in
+ records out
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 : linear /dev/vg0/centos7_vm
[root@localhost ~]# ll /dev/mapper/
total
brw-rw---- root disk , Jun : centos7_vm1
crw------- root root , May : control
brw-rw---- root disk , Jun : vg0-centos7_vm
[root@localhost ~]#
红色的部分即为虚拟机磁盘的根分区。
6、格式化目标虚拟机根分区文件系统为ext3(或者ext4等等):
[root@localhost ~]# mkfs.ext3 /dev/mapper/centos7_vm1
mke2fs 1.39 (-May-)
Filesystem label=
OS type: Linux
Block size= (log=)
Fragment size= (log=)
inodes, blocks
blocks (5.00%) reserved for the super user
First data block=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , , , , , Writing inode tables: done
Creating journal ( blocks): done
Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every mounts or
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
drwx------ root root Jun : lost+found
[root@localhost ~]#
8、解压磁盘镜像模板系统文件 centos7_img 到 /mnt下,并 umout :
[root@localhost ~]# tar zxf /root/centos7_img -C /mnt
[root@localhost ~]# ll /mnt/
total
lrwxrwxrwx root root Jun : bin -> usr/bin
dr-xr-xr-x root root Jun : boot
drwxr-xr-x root root Jun : dev
drwxr-xr-x root root Jun : etc
drwxr-xr-x root root Jun home
lrwxrwxrwx root root Jun : lib -> usr/lib
lrwxrwxrwx root root Jun : lib64 -> usr/lib64
drwx------ root root Jun : lost+found
drwxr-xr-x root root Jun media
drwxr-xr-x root root Jun mnt
drwxr-xr-x root root Jun opt
drwxr-xr-x root root Jun : proc
dr-xr-x--- root root Jun : root
drwxr-xr-x root root Jun : run
lrwxrwxrwx root root Jun : sbin -> usr/sbin
drwxr-xr-x root root Jun srv
drwxr-xr-x root root Jun : sys
drwxrwxrwt root root Jun : tmp
drwxr-xr-x root root Jun : usr
drwxr-xr-x root root Jun : var
[root@localhost ~]# umount /mnt/
9、删除磁盘分区映射:
[root@localhost ~]# kpartx -d /dev/vg0/centos7_vm
[root@localhost ~]# ll /dev/mapper/
total
crw------- root root , May : control
brw-rw---- root disk , Jun : vg0-centos7_vm
[root@localhost ~]#
10、至此,虚拟机系统盘已经准备好,下面就是需要准备虚拟机配置文件:
kernel = "/usr/lib/xen/boot/hvmloader"
builder='hvm'
memory =
name = "centos7_vm"
vif = [ 'bridge=eth0' ]
disk = [ 'phy:/dev/vg0/centos7_vm,hda,w' ]
vcpus =
on_reboot = 'restart'
on_crash = 'restart'
boot = 'c'
sdl =
vnc =
vnclisten = '0.0.0.0'
vncdisplay =
stdvga =
serial = 'pty'
usbdevice = 'tablet'
xen_platform_pci=
11、启动虚拟机,并通过vnc登陆该虚拟机:
至此,通过前面制作的 CentOS7 磁盘镜像模板成功生成了一个全新的虚拟机。