Mar 172021
 

制作镜像的过程,其实可以说简单,对着文档做就可以,不过整个过程,其实很容易出错。而且现在镜像是需要经常进行修改。所以必须有一套环境,可以自动化生成你需要的镜像才行。思路就是利用官方提供的镜像,自动化处理镜像,满足需求。

需求

  • 基于官方镜像:centos
  • 关闭selinux
  • 更改repo 为阿里云,启用epel
  • cloud init
  • growpart,根分区可以自动扩大
  • root,密码登陆,ssh root登陆
  • 调整时区
  • vim alias vi
  • qemu guest agent
  • 必备工具
  • 脚本优化

准备

一台干净的linux,我用的是centos 7,感觉应该都是可以的。

yum install libguestfs-tools-c

下载官方镜像,定制centos 7.8

curl -O https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/CentOS-7-x86_64-GenericCloud-2003.qcow2

后续就可以开始定制这个镜像,当然最好还是复制一份。

定制化

通过virt-customize 实现镜像的定制化

需要提前运行下面的命令

export LIBGUESTFS_BACKEND=direct

下面就是修改的内容

# root 密码
virt-customize --root-password password:chenshake -a CentOS-7-x86_64-GenericCloud-2003.qcow2

# 设置时区

virt-customize --timezone "Asia/Shanghai" -a CentOS-7-x86_64-GenericCloud-2003.qcow2

#安装工具

virt-customize  --install [net-tools,wget,unzip,qemu-guest-agent] -a CentOS-7-x86_64-GenericCloud-2003.qcow2

#启动服务
virt-customize  --run-command 'systemctl start qemu-guest-agent' -a CentOS-7-x86_64-GenericCloud-2003.qcow2
virt-customize  --run-command 'systemctl enable qemu-guest-agent' -a CentOS-7-x86_64-GenericCloud-2003.qcow2

#上传优化脚本和运行

virt-customize  --upload /root/centos.sh:/root/centos.sh -a CentOS-7-x86_64-GenericCloud-2003.qcow2
virt-customize  --chmod 755:/root/centos.sh -a CentOS-7-x86_64-GenericCloud-2003.qcow2
virt-customize  --run '/root/centos.sh' -a CentOS-7-x86_64-GenericCloud-2003.qcow2

基本的内容,都在优化脚本上,centos.sh , 这个脚本慢慢整理。

Resize qcow2

默认的磁盘其实很小。

# qemu-img info CentOS-7-x86_64-GenericCloud-2003.qcow2 
image: CentOS-7-x86_64-GenericCloud-2003.qcow2
file format: qcow2
virtual size: 8.0G (8589934592 bytes)
disk size: 820M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

查看分区大小

 virt-filesystems --long --parts --blkdevs -h -a CentOS-7-x86_64-GenericCloud-2003.qcow2 
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   8.0G  /dev/sda
/dev/sda   device     -    8.0G  -

扩大分区

# qemu-img resize CentOS-7-x86_64-GenericCloud-2003.qcow2 +30G
Image resized.

这时候你查看相关信息

# qemu-img info CentOS-7-x86_64-GenericCloud-2003.qcow2 
image: CentOS-7-x86_64-GenericCloud-2003.qcow2
file format: qcow2
virtual size: 38G (40802189312 bytes)
disk size: 820M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
# virt-filesystems --long --parts --blkdevs -h -a CentOS-7-x86_64-GenericCloud-2003.qcow2
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   8.0G  /dev/sda
/dev/sda   device     -    38G   -

需要把 /dev/sda1 扩大才行。

cp CentOS-7-x86_64-GenericCloud-2003.qcow2 CentOS-7-x86_64-GenericCloud-2003-orig.qcow2

关键时刻到了

# virt-resize -expand /dev/sda1 CentOS-7-x86_64-GenericCloud-2003-orig.qcow2 CentOS-7-x86_64-GenericCloud-2003.qcow2 
[   0.0] Examining CentOS-7-x86_64-GenericCloud-2003-orig.qcow2
**********

Summary of changes:

virt-resize: warning: unknown/unavailable method for expanding the xfs 
filesystem on /dev/sda1
/dev/sda1: This partition will be resized from 8.0G to 38.0G.

**********
[   5.1] Setting up initial partition table on CentOS-7-x86_64-GenericCloud-2003.qcow2
[   5.4] Copying /dev/sda1
 100% ⟦▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒⟧ 00:00

Resize operation completed with no errors.  Before deleting the old disk, 
carefully check that the resized disk boots and works correctly.

验证一下变化

# virt-filesystems --long --parts --blkdevs -h -a CentOS-7-x86_64-GenericCloud-2003.qcow2
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   38G   /dev/sda
/dev/sda   device     -    38G   -

完成全过程。

验证

验证镜像,当然可以上传到云平台,不过很多对文件的修改。是可以直接看到。

#验证所有修改

guestmount -a CentOS-7-x86_64-GenericCloud-2003.qcow2 -i /mnt

这样就可以到mnt目录下,看看我们对文件的修改,是否生效。

参考文章

  • https://fatmin.com/2016/12/20/how-to-resize-a-qcow2-image-and-filesystem-with-virt-resize/
  • https://blog.oneiroi.co.uk/linux/kvm/virt-resize/RHEL/LVM/kvm-linux-expanding-a-lvm-guest-file-system-using-virt-resize/
  • https://maunium.net/blog/resizing-qcow2-images/

脚本,很简陋

!/bin/bash

#yum seting
cd /etc/yum.repos.d/
mkdir backup
mv *.repo ./backup
curl -O http://mirrors.aliyun.com/repo/Centos-7.repo
curl -O http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all


cd -

#ssh
sed -i 's/#UseDNS yes/UseDNS no/' /etc/ssh/sshd_config
sed -i 's/^GSSAPIAuthentication yes$/GSSAPIAuthentication no/' /etc/ssh/sshd_config
systemctl  restart sshd.service

#vim
echo 'alias vi=vim' >> /etc/profile

 Leave a Reply

(required)

(required)

This site uses Akismet to reduce spam. Learn how your comment data is processed.