在平时使用时,为了减少存储空间和加快网络传输速度,我们总希望能把基础镜像做的尽量小,本文介绍如何制作清爽干净的kvm基础镜像。
-
根据自身需求,对系统进行配置:
- 这一步是为了确保系统符合你的需求,比如安装必要的软件,配置网络等。
-
卸载不需要的软件:
- 使用
yum remove
或dnf remove
命令卸载不需要的软件包。例如:bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>yum remove [package_name]
- 确保不要卸载系统运行所必需的软件。
- 使用
-
对系统缓存进行清理:
- 清理临时文件和日志可以减少镜像大小。以下命令可以清理缓存:
bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>
rm -rf /tmp/* rm -f /etc/udev/rules.d/70-persistent-net.rules rm -f /var/log/wtmp /var/log/btmp /var/log/anaconda/* cd /var/log for i in cron dmesg dmesg.old lastlog maillog messages pm-powersave.log secure Xorg.0.log Xorg.0.log.old Xorg.9.log Xorg.9.log.old do true > $i done dmesg -c echo >/root/.bash_history yum clean all cloud-init clean rm -rf /var/lib/cloud history -c
- 清理临时文件和日志可以减少镜像大小。以下命令可以清理缓存:
-
使用 dd 命令将客户机未使用的磁盘空间用0填满:
- 这一步确保未使用的磁盘空间被标记为0,从而在转换为 qcow2 镜像时不会被错误地认为是已使用空间。例如:
bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>
dd if=/dev/zero of=~/junk rm -f ~/junk
- 这一步确保未使用的磁盘空间被标记为0,从而在转换为 qcow2 镜像时不会被错误地认为是已使用空间。例如:
-
关机:
- 使用
halt -p
命令安全地关闭系统:bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>halt -p
- 使用
-
在宿主机上,执行镜像整理:
- 使用
virt-sysprep
命令清理镜像中的临时文件和日志,确保镜像是干净的。如果没有安装libguestfs-tools
,可以通过以下命令安装:bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>yum install libguestfs-tools -y
- 然后执行:
bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>
virt-sysprep -a kvm-base.qcow2
- 使用
-
压缩并制作新镜像:
- 使用
qemu-img convert
命令将镜像转换为压缩的 qcow2 格式:bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>qemu-img convert -p -c -O qcow2 kvm-base.qcow2 new.qcow2
- 使用
-
检查镜像是否有错误:
- 使用
qemu-img check
命令检查新镜像的完整性:bash<button class="MuiButtonBase-root MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary MuiButton-root MuiButton-text MuiButton-textPrimary MuiButton-sizeSmall MuiButton-textSizeSmall MuiButton-colorPrimary copyBtn___l3xJQ css-xox1ca" tabindex="0" type="button"></button>qemu-img check new.qcow2
- 使用