Xen虚拟机磁盘镜像模板制作(三)—CentOS 7

这里整理下制作Xen CentOS 7磁盘镜像模版的流程:

1、创建一个将要用来安装CentOS 7系统的LV,命令如下:

[root@localhost ~]# lvcreate -L 5G -n centos7 vg0
  Logical volume "centos7" created
[root@localhost ~]# lvs
  LV      VG   Attr   LSize Origin Snap%  Move Log Copy%  Convert
  centos7 vg0  -wi-a- 5.00G                                      
[root@localhost ~]# 

这里我们设置的系统盘LV大小为5G,其实可以设置更小,只要能放得下CentOS 7系统文件即可。

2、准备虚拟机配置文件:

kernel = "/usr/lib/xen/boot/hvmloader"
builder='hvm'
memory = 1024
name = "centos7"
vif = [ 'bridge=eth0' ]
disk = [ 'phy:/dev/vg0/centos7,hda,w','file:/root/CentOS-7-x86_64-DVD-1503-01.iso,hdc:cdrom,r' ]
vcpus  = 1
on_reboot = 'restart'
on_crash = 'restart'
boot = 'd'
sdl = 0
vnc = 1
vnclisten = '0.0.0.0'
vncdisplay = 5
stdvga = 0
serial = 'pty'
usbdevice = 'tablet'
xen_platform_pci=1

注意:

1)这里使用的是全虚拟化方式安装

2)设置虚拟机从CDROM启动

3、创建并启动CentOS虚拟机,命令如下:

[root@localhost ~]# xm create /root/centos7.cfg 
Using config file "/root/centos7.cfg".
Started domain centos7 (id=574)
[root@localhost ~]# xm li
Name                                        ID   Mem VCPUs      State   Time(s)
Domain-0                                     0  1856     8     r-----  92575.7
centos7                                    574  1024     1     -b----      1.5

4、此时CentOS虚拟机已经启动,进入到系统安装界面,下面就是按部就班地安装系统;我们通过VNC客户端连接到虚拟机上,如下所示:

5、系统安装到下面这一步,这里我们需要手动对磁盘进行分区,而并不需要系统默认的分区方式:

 

6、点击进入INSTALLATION DESTINATION,这里我们选择 I will configure partitioning:

7、手动系统磁盘进行格式化分区,这里我们仅仅只需要根分区(不需要swap分区):

 

8、后面就是系统安装过程了,等系统安装完成后,我们修改配置文件让虚拟机从硬盘启动,然后通过VNC登陆到虚拟机里,做一些修改以适应需要:

1)确保ssh-server运行

2)关闭虚拟机selinux、firewall等

3)清除一些垃圾数据

4)其他需要的操作

9、关闭虚拟机,然后创建磁盘分区映射,将磁盘所有分区映射到/dev/mapper下:

[root@localhost ~]# kpartx -av /dev/vg0/centos7 
add map centos7p1 : 0 10483712 linear /dev/vg0/centos7 2048
[root@localhost ~]# ll /dev/mapper/
total 0
brw-rw---- 1 root disk 253,  1 Jun 26 17:53 centos7p1
crw------- 1 root root  10, 62 May  5 17:18 control
brw-rw---- 1 root disk 253,  0 Jun 26 14:36 vg0-centos7
[root@localhost ~]#

10、备份下磁盘MBR主引导扇区,具体命令如下:

[root@localhost ~]# dd if=/dev/vg0/centos7 of=centos7_mbr bs=512 count=2000
2000+0 records in
2000+0 records out
1024000 bytes (1.0 MB) copied, 0.123914 seconds, 8.3 MB/s
[root@localhost ~]# 

这里为什么count要是2000?主要是因为仅仅dd出磁盘前面512字节的MBR扇区无法从磁盘上成功引导进入CentOS系统,我们需要将从磁盘开始的第一个扇区到磁盘根分区的开始扇区之间的数据全部dd出来。那么这个count值 2000(bs=512的情况下)如何得到呢?具体说明如下:

1)我们首先通过 hexedit 命令打开整块磁盘,查看下整块磁盘的字节数:

[root@localhost ~]# hexedit -s --color /dev/vg0/centos7

然后在第一个sector下面会看到如下字样,0x140000000 就是整块磁盘的字节数:

2)我们通过 hexedit 查看下磁盘根分区的字节数:

[root@localhost ~]# hexedit -s --color /dev/mapper/centos7p1 

同样在第一个sector下面会看到如下字样,0x13FF00000 就是根分区的字节数:

 

3)从上面两个数字一算,就可以知道我们需要 dd 多少字节的数据:

0x140000000 - 0x13FF00000 = 0x100000,换算成十进制,差不多就是1MB的数据,因此count也就是2000了。

11、将磁盘根分区文件系统mount到/mnt下:

[root@localhost ~]# mount /dev/mapper/centos7p1 /mnt/
[root@localhost ~]# ll /mnt/
total 76
lrwxrwxrwx  1 root root     7 Jun 26 17:13 bin -> usr/bin
dr-xr-xr-x  4 root root  4096 Jun 26 17:33 boot
drwxr-xr-x  2 root root  4096 Jun 26 17:13 dev
drwxr-xr-x 74 root root  4096 Jun 26 17:33 etc
drwxr-xr-x  2 root root  4096 Jun 10  2014 home
lrwxrwxrwx  1 root root     7 Jun 26 17:13 lib -> usr/lib
lrwxrwxrwx  1 root root     9 Jun 26 17:13 lib64 -> usr/lib64
drwx------  2 root root 16384 Jun 26 17:13 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 26 17:13 proc
dr-xr-x---  2 root root  4096 Jun 26 17:17 root
drwxr-xr-x  2 root root  4096 Jun 26 17:13 run
lrwxrwxrwx  1 root root     8 Jun 26 17:13 sbin -> usr/sbin
drwxr-xr-x  2 root root  4096 Jun 10  2014 srv
drwxr-xr-x  2 root root  4096 Jun 26 17:13 sys
drwxrwxrwt  7 root root  4096 Jun 26 17:33 tmp
drwxr-xr-x 13 root root  4096 Jun 26 17:13 usr
drwxr-xr-x 20 root root  4096 Jun 26 17:33 var
[root@localhost ~]# 

12、进入/mnt/目录下,打包整个系统文件:

[root@localhost ~]# cd /mnt/
[root@localhost mnt]# tar zvcf /root/centos7_img *

13、退出/mnt/目录,umount磁盘文件系统,并删除磁盘分区映射:

[root@localhost mnt]# cd /root/
[root@localhost ~]# umount /mnt/
[root@localhost ~]# kpartx -d /dev/vg0/centos7 
[root@localhost ~]#

14、至此,CentOS7磁盘镜像模版就制作完成了,下面就是我们需要的文件了:

[root@localhost ~]# ll centos7*
-rw-r--r-- 1 root root 355956449 Jun 26 18:26 centos7_img
-rw-r--r-- 1 root root   1024000 Jun 26 17:44 centos7_mbr
[root@localhost ~]# 
原文地址:https://www.cnblogs.com/CasonChan/p/4602014.html