1. rhel7.6虚拟机的封装及压缩

1. 配置宿主主机的环境

现在宿主主机上安装httpd,并且,搭建好网络仓库(redhat7.6)

[root@westos ~]# vim /etc/yum.repos.d/westos.repo		//搭建网络仓库
[root@westos ~]# cat /etc/yum.repos.d/westos.repo
[rhel7.6]
name=rhel7.6
baseurl=http://172.25.23.250/westos
gpgcheck=0
enabled=1
[root@westos ~]# mkdir /var/www/html/westos				//准备好镜像的挂载点
[root@westos ~]# mount /iso/rhel-server-7.6-x86_64-dvd.iso /var/www/html/westos			//挂载镜像
mount: /var/www/html/westos: WARNING: device write-protected, mounted read-only.
[root@westos ~]# df
Filesystem     1K-blocks      Used Available Use% Mounted on
devtmpfs         3904620         0   3904620   0% /dev
tmpfs            3933812     19224   3914588   1% /dev/shm
tmpfs            3933812     18492   3915320   1% /run
tmpfs            3933812         0   3933812   0% /sys/fs/cgroup
/dev/sda6      199582500 150773400  48809100  76% /
/dev/nvme0n1p5   1038336    230232    808104  23% /boot
/dev/nvme0n1p1    262144     35952    226192  14% /boot/efi
tmpfs             786760      1168    785592   1% /run/user/42
tmpfs             786760        28    786732   1% /run/user/1000
/dev/loop0       8238560   8238560         0 100% /var/www/html/westosdir
/dev/loop1       4391278   4391278         0 100% /var/www/html/westos				    //挂载成功
[root@westos ~]# 

之后的虚拟机都使用的是私有网段172.25.23.0
这个私有网段需要在宿主主机上配置

[root@westos ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0 	//网桥的配置文件
[root@westos ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0 
DEVICE=br0
ONBOOT=yes
BOOTPROTO=none
IPADDR0=172.25.254.23
NETMASK0=255.255.255.0
IPADDR1=172.25.23.250											//宿主主机上的私有网段
NETMASK1=255.255.255.0
TYPE=Bridge
[root@westos ~]# cat /etc/sysconfig/network-scripts/ifcfg-enp8s0 
# Generated by dracut initrd
DEVICE=enp8s0
ONBOOT=yes
BRIDGE=br0
BOOTPROTO=none
[root@westos ~]# nmcli connection reload						//重启服务
[root@westos ~]# nmcli connection down Bridge\ br0 				//Bridge br0 是系统设定的网桥名字
Connection 'Bridge br0' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/2)
[root@westos ~]# nmcli connection up Bridge\ br0 
Connection successfully activated (master waiting for slaves) (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)
[root@westos ~]# ip addr show br0								//查看网桥上的IP
9: br0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether e8:6a:64:71:b6:a2 brd ff:ff:ff:ff:ff:ff
    inet 172.25.254.23/24 brd 172.25.254.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
    inet 172.25.23.250/24 brd 172.25.23.255 scope global noprefixroute br0
       valid_lft forever preferred_lft forever
       [root@westos ~]# cat /etc/hosts							//本地解析
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.23.1 server1												//未来会使用的虚拟机的IP
[root@westos ~]# ping -w 3 server1
PING server1 (172.25.23.1) 56(84) bytes of data.
64 bytes from server1 (172.25.23.1): icmp_seq=1 ttl=64 time=0.165 ms
64 bytes from server1 (172.25.23.1): icmp_seq=2 ttl=64 time=0.182 ms
64 bytes from server1 (172.25.23.1): icmp_seq=3 ttl=64 time=0.166 ms
--- server1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 76ms
rtt min/avg/max/mdev = 0.165/0.171/0.182/0.007 ms

2. 虚拟机的封装

  • 第一步:打开虚拟机管理器virt-manager,采用本地的方式安装虚拟机rhel7.6(要修改虚拟机的网络模式)
  • 第二步:安装好,并且reboot之后,需要开启selinux
  • 第三步:修改主机名称,修改网卡配置文件,修改本地解析文件
    注意以下几点:
    企业7的版本可以不使用nmcli;
    如果不使用wifi的话,可以选择关闭NetworkManager
[root@server1 ~]# hostname
server1
[root@server1 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.25.23.1
PREFIX=24
GATEWAY=172.25.23.250
DNS1=114.114.114.114
[root@server1 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.23.250 westos.westos.org
172.25.23.1   server1							//之后的虚拟机
172.25.23.2   server2
172.25.23.3   server3
172.25.23.4   server4
172.25.23.5   server5
172.25.23.6   server6
172.25.23.7   server7
172.25.23.8   server8
172.25.23.9   server9
[root@server1 ~]# systemctl disable --now NetworkManager
[root@server1 ~]# systemctl restart network			//重启服务
[root@server1 ~]# ping -w 3 172.25.23.250			//虚拟机server1可以成功ping到宿主主机,连接成功
PING 172.25.23.250 (172.25.23.250) 56(84) bytes of data.
64 bytes from 172.25.23.250: icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from 172.25.23.250: icmp_seq=2 ttl=64 time=0.137 ms
64 bytes from 172.25.23.250: icmp_seq=3 ttl=64 time=0.199 ms
64 bytes from 172.25.23.250: icmp_seq=4 ttl=64 time=0.142 ms

--- 172.25.23.250 ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 2999ms
rtt min/avg/max/mdev = 0.079/0.139/0.199/0.043 ms
  • 第四步:搭建网络仓库,编写yum源
[root@server1 ~]# cat /etc/yum.repos.d/westos.repo 
[rhel7.6]
name=rhel7.6
baseurl=http://172.25.23.250/westos
gpgcheck=0
enabled=1
[root@server1 ~]# yum repolist
  • 第五步:安装必须的软件
[root@server1 ~]# yum install -y vim bash-* lftp
  • 第六步:编写selinux的配置文件,关闭selinux
[root@server1 ~]# cat /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 
  • 第七步:封装完毕

3. 生成母盘,创建子盘

/var/lib/libvirt/images目录里查看虚拟机rhel7.6的文件过大,大概21G,需要进行压缩生成母镜像

  • 第一步:安装关闭bug的指令
[root@westos images]# yum install -y libguestfs-tools		//安装virt-sysprep
[root@westos images]# virt-sysprep -d rhel7.6					//关闭bug
  • 第二步:压缩镜像并生成子镜像,同时可以在virt-manager删除之前的21G的虚拟机
[root@westos images]# virt-sparsify --compress rhel7.6.qcow2 rhel7.6-base.qcow2		//压缩镜像
[root@westos images]# qemu-img create -f qcow2 -b rhel7.6-base.qcow2 server1		//快照,子镜像
Formatting 'server1', fmt=qcow2 size=21474836480 backing_file=rhel7.6-base.qcow2 cluster_size=65536 lazy_refcounts=off refcount_bits=16
[root@westos images]# du -h rhel7.6.qcow2 				//原先虚拟机的大小
21G	rhel7.6.qcow2
[root@westos images]# du -h rhel7.6-base.qcow2 			//母镜像的大小
575M	rhel7.6-base.qcow2

4. 安装快照虚拟机

记得在安装前要选择网络模式为网桥模式

猜你喜欢

转载自blog.csdn.net/weixin_47133613/article/details/115307800