手把手教你Linux虚拟机封装
注:本次封装是在7.3 版本下的Red Hat系统中进行的。
1. 安装一台新的虚拟机
1)打开虚拟机管理器
2)开始安装虚拟机
2. 虚拟机设置
2.1 按照之前设定的超级用户密码登陆虚拟机
2.2 修改ip地址
ip addr show
可以看到最小化安装是没有eth0,那么应该如何修改呢?
vi /boot/grub2/grub.cfg
在100行后面加上 net.ifnames=0
reboot #重启
这时候就可以修改ip地址了
vi /etc/sysconfig/network-scripts/ifcfg-eth0 #因为最小安装没有vim,所以要用vi
systemctl restart network #重启网络
ip addr show
2.3 关闭并禁用NetworkManager
因为会影响网络配置,DNS等
systemctl stop NetworkManager
systemctl disable NetworkManager
2.4 配置yum源
首先得在真机中配置好yum源,直接让虚拟机访问即可
cd /etc/yum.repos.d/
ls
vi yum.repo
yum clean all
yum repolist
yum install -y vim lftp net-tools bash-*(不能tab补全的原因)
安装一些最基本的东西
2.5 关闭selinux
vim /etc/selinux/config
7 SELINUX=disabled
reboot #重启
2.6 关闭并禁用防火墙
systemctl stop firewalld
systemctl disable firewalld
2.7 修改主机名
hostnamectl set-hostname server1
hostname
2.8 添加解析
vim /etc/hosts
3. 清理虚拟机缓存文件
因为每次开机都会生成一些临时文件,缓存文件等,如果不清除,每次快照都会有这些东西,快照会变大。
1. 首先需要在真机安装 virt-sysprep命令
yum whatprovides */virt-sysprep
yum install libguestfs-tools-c-1.32.7-3.el7.x86_64 -y
2. 清理虚拟机临时及缓存文件命令:
cd /var/lib/libvirt/images/
virt-sysprep -d 虚拟机名
3. 有时它会报错,信息如下:
[ 0.0] Examining the guest ...
virt-sysprep: error: libguestfs error: could not create appliance through
libvirt.
Try running qemu directly without libvirt using this environment variable:
export LIBGUESTFS_BACKEND=direct
Original error from libvirt: \u4e0d\u652f\u6301\u7684\u914d\u7f6e\uff1ashared access for disk
'sdb' requires use of supported storage format [code=67 int1=-1]
If reporting bugs, run virt-sysprep with debugging enabled and include the
complete output:
virt-sysprep -v -x [...]
解决:执行下面命令更新包即可
yum upgrade libguestfs-tools
virt-sysprep -d 虚拟机名
4. 快照虚拟机
qemu-img create -f qcow2 -b rhel7.5-kk.qcow2 kk1
qemu-img create -f qcow2 -b rhel7.5-kk.qcow2 kk2
qemu-img create -f qcow2 -b rhel7.5-kk.qcow2 kk3
这样,一个虚拟机快照就做好了,其他两个同理。