如何在KVM上创建CentOS/Fedora/Rhel VM模板

时间:2020-02-23 14:45:10  来源:igfitidea点击:

如何为KVM上的CentOS/Fedora/Rhel Linux发行版创建VM模板?
我喜欢KVM,我将它用于所有家庭虚拟化实验室。
KVM是X86硬件包含虚拟化扩展(Intel VT或者AMD-V)的Linux的开源完整虚拟化解决方案。
VM模板是我们可以从中克隆,转换或者部署更多虚拟机的VM实例的主副本。

VM模板在部署需要跨部署中需要一致性的高数量的类似VM时更有用。
如果在从模板创建的实例中出现问题,则可以以最小的努力从模板克隆一个新鲜的VM。

第1步:安装KVM

在Linux系统中安装KVM。

KVM服务(LibVirtd)应运行并启用以在启动时启动。

sudo systemctl start libvirtd
sudo systemctl enable libvirtd

使能够 vhost-netUbuntu/Debian上的内核模块。

sudo modprobe vhost_net
echo vhost_net | sudo tee -a /etc/modules

第2步:创建CentOS/Fedora/Rhel VM

在我们创建基线模板之前,我们需要安装CentOS/Fedora或者Rhel Linux服务器。
我建议我们将磁盘大小保留小用于基础OS安装。
本教程将使用Virt-Install命令行工具展示在KVM上安装CentOS 7 VM。 1.使用QEMU-IMG创建10 GB VM图像。

$sudo qemu-img create -o preallocation=metadata -f qcow2 /var/lib/libvirt/images/centos.qcow2 10G
Formatting '/var/lib/libvirt/images/centos.qcow2', fmt=qcow2 size=10737418240 cluster_size=65536 preallocation=metadata lazy_refcounts=off refcount_bits=16

2.开始安装基线操作系统。

sudo virt-install --virt-type kvm --name centos7 --ram 1024 \
   --disk /var/lib/libvirt/images/centos.qcow2,format=qcow2 \
   --network network=default \
   --graphics vnc,listen=0.0.0.0 --noautoconsole \
   --os-type=linux --os-variant=rhel7.0 \
   --location=/home/jmutai/iso/CentOS-7-x86_64-Minimal-1810.iso

替换:CentOS7与基础实例名称为基础实例./var/lib/libvirt/images/centos.qcow2与磁盘镜像/home/jmutai/iso/centos-7-x86_64-minimal-1810.iso与路径ISO FileDefault以KVM网络的名称使用。

在开始安装时,我们应该获取

Starting install...
Setting input-charset to 'UTF-8' from locale.
Retrieving file vmlinuz...                                                                                                      | 6.3 MB  00:00:00     
Setting input-charset to 'UTF-8' from locale.
Retrieving file initrd.img...                                                                                                   |  50 MB  00:00:00     
Domain installation still in progress. You can reconnect to 
the console to complete the installation process.

打开Verv Manager完成安装。

对于控制台安装,请使用以下行。

sudo virt-install --virt-type kvm --name centos7 --ram 1024 \
   --disk /var/lib/libvirt/images/centos.qcow2,format=qcow2 \
   --network network=default \
   --os-type=linux --os-variant=rhel7.0 \
   --location=/home/jmutai/iso/CentOS-7-x86_64-Minimal-1810.iso \
   --graphics none \
   --console pty,target_type=serial \
   --extra-args 'console=ttyS0,115200n8 serial'

检查并设置控制台安装参数。

第3步:准备CentOS/Fedora/Rhel VM模板 1.更新系统

完成VM安装后,登录实例并将所有系统包更新为最新版本。

sudo yum -y update

2.安装缺少的标准基本包:

sudo yum install -y epel-release vim bash-completion wget curl telnet net-tools unzip lvm2

3.安装ACPID和Cloud-init包。

sudo yum -y install acpid cloud-init cloud-utils-growpart
sudo sudo systemctl enable --now acpid

4.禁用Zeroconf路由

echo "NOZEROCONF=yes" | sudo tee -a /etc/sysconfig/network

5.配置GRUB_CMDLINE_LINUX - 用于OpenStack使用情况。

如果我们计划将模板导出到OpenStack浏览镜像服务,请编辑 /etc/default/grub文件并配置GRUB_CMDLINE_LINUX选项。
线路应如下所示 - 删除RHGB REQUE和ADD CONSOLE = TTY0 CONSOLE = TTYS0,115200N8.

GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=cl/root rd.lvm.lv=cl/swap console=tty0 console=ttyS0,115200n8"

生成GRUB配置。

sudo grub2-mkconfig -o /boot/grub2/grub.cfg

6.在基准模板上安装我们需要的其他包。 7.完成后,关闭虚拟机。

sudo poweroff

第4步:清洁VM模板

你需要 virt-sysprep用于清洁实例的工具。

sudo virt-sysprep -d centos7

清理VM后,LibVirt域缩写。

$sudo virsh undefine centos7
Domain centos7 has been undefined

KVM VM模板已准备好使用。

第5步:使用KVM模板

我们可以从模板手动配置VM或者使用Terraform自动配置VMS上的VM。