searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

制作清爽干净的kvm基础镜像

2024-07-22 08:51:21
24
0

    在平时使用时,为了减少存储空间和加快网络传输速度,我们总希望能把基础镜像做的尽量小,本文介绍如何制作清爽干净的kvm基础镜像。

  1. 根据自身需求,对系统进行配置

    • 这一步是为了确保系统符合你的需求,比如安装必要的软件,配置网络等。
  2. 卸载不需要的软件

    • 使用 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]
    • 确保不要卸载系统运行所必需的软件。
  3. 对系统缓存进行清理

    • 清理临时文件和日志可以减少镜像大小。以下命令可以清理缓存:
      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
  4. 使用 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
  5. 关机

    • 使用 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
  6. 在宿主机上,执行镜像整理

    • 使用 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
  7. 压缩并制作新镜像

    • 使用 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
  8. 检查镜像是否有错误

    • 使用 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
0条评论
0 / 1000
孔****云
6文章数
0粉丝数
孔****云
6 文章 | 0 粉丝
原创

制作清爽干净的kvm基础镜像

2024-07-22 08:51:21
24
0

    在平时使用时,为了减少存储空间和加快网络传输速度,我们总希望能把基础镜像做的尽量小,本文介绍如何制作清爽干净的kvm基础镜像。

  1. 根据自身需求,对系统进行配置

    • 这一步是为了确保系统符合你的需求,比如安装必要的软件,配置网络等。
  2. 卸载不需要的软件

    • 使用 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]
    • 确保不要卸载系统运行所必需的软件。
  3. 对系统缓存进行清理

    • 清理临时文件和日志可以减少镜像大小。以下命令可以清理缓存:
      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
  4. 使用 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
  5. 关机

    • 使用 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
  6. 在宿主机上,执行镜像整理

    • 使用 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
  7. 压缩并制作新镜像

    • 使用 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
  8. 检查镜像是否有错误

    • 使用 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
文章来自个人专栏
ibox
6 文章 | 2 订阅
0条评论
0 / 1000
请输入你的评论
0
0