陈沙克

Apr 022021
 

最近做镜像,折腾repo。记录一下,发现各个repo,居然最好用,体贴的是腾讯云。

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos6_base.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-6.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos8_base.repo

这样就基本搞定。不过有时候你会遇到key的问题。所以你可以

rpm --import https://mirrors.cloud.tencent.com/epel/RPM-GPG-KEY-EPEL-6'

这是epel,可以参考。这样基本就没啥问题。

  • https://mirrors.cloud.tencent.com/help/centos.html
Mar 302021
 

目前红帽已经不支持CentOS 6,也就导致网上的repo全部失效。这个时候,要做一个镜像,就变得比较麻烦。

DIB 已经失效,只能使用更早的版本,pip安装,所以就放弃,全部使用virt来进行定制。需要准备CentOS 6.8的repo,后续会提供。我是这CentOS 7.6 虚拟机里,进行下面的操作。

mkdir cloud-images
cd cloud-images
curl -O https://mirrors.ustc.edu.cn/centos-cloud/centos/6/images/CentOS-6-x86_64-GenericCloud-1605.qcow2

Virt

yum install libguestfs-tools-c libguestfs libguestfs-tools

必须运行这个才能正常使用,退出还需要重新运行

export LIBGUESTFS_BACKEND=direct
cp cloud-images/CentOS-6-x86_64-GenericCloud-1605.qcow2 /root/CentOS-6.8-x86.qcow2
export image_name='CentOS-6.8-x86.qcow2' 
virt-customize -a $image_name --timezone "Asia/Shanghai"
virt-customize -a $image_name --edit '/etc/selinux/config:s/SELINUX=enforcing/SELINUX=disabled/' 
virt-customize -a $image_name --delete "/etc/yum.repos.d/CentOS-*"
virt-customize -a $image_name --upload /root/CentOS-Base.repo:/etc/yum.repos.d/CentOS-Base.repo
virt-customize -a $image_name --upload /root/epel.repo:/etc/yum.repos.d/epel.repo
virt-customize -a $image_name --run-command 'yum clean all'
virt-customize -a $image_name --run-command 'yum makecache'
virt-customize -a $image_name --install [net-tools,wget,vim,unzip]
virt-customize -a $image_name --run-command "yum -y install qemu-guest-agent"
virt-customize -a $image_name --run-command "chkconfig qemu-ga on"
virt-customize -a $image_name --run-command 'yum -y install cloud-utils-growpart dracut-modules-growroot'
virt-customize -a $image_name --append-line '/etc/profile:alias vi=vim'

ssh相关

virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/GSS/#GSS/'
virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/#UseDNS yes/UseDNS no/'
virt-customize -a $image_name --edit '/etc/cloud/cloud.cfg:s/ssh_pwauth:   0/ssh_pwauth: 1/'
virt-customize -a $image_name --edit '/etc/cloud/cloud.cfg:s/disable_root: 1/disable_root: 0/'

Cloud init

virt-customize -a $image_name --upload /etc/cloud/cloud.cfg.d/91-dib-cloud-init-datasources.cfg:/etc/cloud/cloud.cfg.d/91-dib-cloud-init-datasources.cfg

virt-customize -a $image_name --upload /etc/cloud/cloud.cfg.d/92-ec2-datasource.cfg:/etc/cloud/cloud.cfg.d/92-ec2-datasource.cfg

grub 修改

virt-customize -a $image_name --edit '/etc/grub.conf:s/serial --unit=0 --speed=115200/#serial --unit=0 --speed=115200/'
virt-customize -a $image_name --edit '/etc/grub.conf:s/terminal --timeout=1 serial console/#terminal --timeout=1 serial console/'
virt-customize -a $image_name --edit '/etc/grub.conf:s/rd_NO_DM/rd_NO_DM edd=off/'
virt-customize -a $image_name --edit '/etc/grub.conf:s/console=ttyS0,115200/ /'
Mar 262021
 

这里面其实就是记录一下做一个指定版本的镜像,所需要做的东西。假设DIB和virt已经装好。下面就是一个实际的操作过程。可以参考我上一篇文章。国内目前好像就中科大提供镜像下载。我这里下载的是7.6

https://mirrors.ustc.edu.cn/centos-cloud/centos/7/images/

我习惯就是

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

安装DIB和Virt

yum install centos-release-openstack-train -y
yum install diskimage-builder -y
yum install libguestfs-tools-c libguestfs-tools libguestfs -y

设置环境变量, 如何你希望再build一个centos 7.9 镜像,那么你只需要把 avoid pacage update 这个去掉就可以。

export LIBGUESTFS_BACKEND=direct
export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-1905.qcow2" 
export DIB_RELEASE=7
export DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="yes"
export DIB_AVOID_PACKAGES_UPDATE=1
export image_name='CentOS-7.6-x86.qcow2'

设置好环境变量,后续就简单了。

openstack平台

DIB_CLOUD_INIT_DATASOURCES="ConfigDrive, OpenStack"  disk-image-create -a amd64 -o $image_name  -x --image-size 40 vm base centos disable-selinux cloud-init cloud-init-datasources dhcp-all-interfaces growroot epel

Zstack

disk-image-create -a amd64 -o  $image_name -x --image-size 40 vm base centos disable-selinux cloud-init dhcp-all-interfaces epel

这样也就几分钟就可以创建出镜像。

# 设置时区

virt-customize -a $image_name --timezone "Asia/Shanghai" 

#安装工具

virt-customize -a $image_name --install epel-release

virt-customize -a $image_name --install net-tools,wget,vim,unzip,qemu-guest-agent,jq,bash-completion

virt-customize -a $image_name --install net-tools,wget,vim,unzip,qemu-guest-agent,jq,bash-completion,yum-utils,device-mapper-persistent-data,lvm2,openssl,socat,conntrack,ebtables,ipset,sysstat,iotop,iftop,nload,bridge-utils,bash-completion,bind-utils,nc,binutils,iscsi-initiator-utils,kmod-xfs,xfsprogs,sg3_utils-devel.x86_64,sg3_utils-libs.x86_64,sg3_utils.x86_64,psmisc

#启动服务
virt-customize -a $image_name --run-command 'systemctl enable qemu-guest-agent' 


#SSH服务
virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/GSS/#GSS/'
virt-customize -a $image_name --edit '/etc/ssh/sshd_config:s/#UseDNS yes/UseDNS no/'

#vim
virt-customize -a $image_name --append-line '/etc/profile:alias vi=vim'
virt-customize -a $image_name --append-line '/etc/profile:alias c="clear"'
virt-customize -a $image_name --append-line '/etc/profile:unset MAILCHECK'


#查看修改
virt-cat -a $image_name /etc/ssh/sshd_config 


virt-customize -a $image_name --run-command 'yum clean all'
virt-customize -a $image_name --run-command 'yum makecache'

最后一步,安装zstack的agent

virt-customize -a $image_name --firstboot-command '/bin/bash -c "$(curl -s -S http://169.254.169.254/vm-tools.sh)"'

由于zstack的agent,使用9100端口,如果你也使用普罗米修斯来监控,也必然导致端口冲突,所以我修改端口为9104。当多个启动脚本要运行,需要设置一下他们的优先级,默认是安装字母顺序来运行,所以我把重启agent的改成02。

virt-customize -a $image_name --firstboot-command "sed -i 's/9100/9104/g' /usr/local/zstack/zwatch-vm-agent/conf.yaml

virt-customize -a $image_name --firstboot-command. "/bin/systemctl restart zwatch-vm-agent.service"

virt-customize -a $image_name --run-command 'mv /usr/lib/virt-sysprep/scripts/0001--bin-systemctl-restart-zwatch-vm-agent-service /usr/lib/virt-sysprep/scripts/0002--bin-systemctl-restart-zwatch-vm-agent-service'

查看修改后的结果

virt-ls  -a $image_name  /usr/lib/virt-sysprep/scripts

上传镜像

python -m SimpleHTTPServer 80

如果希望定制内核版本

virt-customize -a $image_name --run-command 'rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org'
virt-customize -a $image_name --run-command 'rpm -Uvh https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm'
virt-customize -a $image_name --run-command 'yum --enablerepo=elrepo-kernel install kernel-lt -y'
virt-customize -a $image_name --run-command 'grub2-set-default 0'
virt-customize -a $image_name --run-command 'grub2-mkconfig -o /boot/grub2/grub.cfg'

elrepo在国外,速度很慢。可以修改使用国内的源

sed -i 's#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g' /etc/yum.repos.d/elrepo.repo
sed -i 's|^mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/elrepo.repo

安装docker

virt-customize -a $image_name --run-command 'yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo'
virt-customize -a $image_name --run-command 'yum install docker-ce-20.10.7 -y'
virt-customize -a $image_name --run-command 'systemctl enable docker'
virt-customize -a $image_name --append-line '/etc/profile:KKZONE=cn'

设置docker代理

virt-customize -a $image_name --run-command 'mkdir -p /etc/docker'
virt-customize -a $image_name --upload /etc/docker/daemon.json:/etc/docker/daemon.json 
sudo sh -c 'echo {\"registry-mirrors\": [\"https://fz1p31b1.mirror.aliyuncs.com\"]} > /etc/docker/daemon.json'
Mar 232021
 

最近打算搞裸机测试,如果用虚拟机来做裸机测试,最大的一个难点就是如何模拟IPMI。

OpenStack经常都是项目开发过程中,解决了很多现实的问题,专门开发了一个IPMI模拟工具。

yum -y install python3-pip python3-devel gcc libvirt-devel ipmitool
pip3 install --upgrade pip
pip3 install virtualbmc

上面的代码,应该是这物理机器上安装,当然你也可以这虚拟机安装,管理你虚拟机里创建出来的虚拟机。

参考下面的文档来操作。

参考文章

  • https://ikhsanputra.com/posts/2020-03-16-install-virtualbmc-on-centos-7/
  • https://segmentfault.com/a/1190000022482150
  • https://www.cnblogs.com/microman/p/6107879.html
  • https://ironic-book.readthedocs.io/zh_CN/latest/ironic/vbmc.html
Mar 192021
 

这两个工具,都可以对镜像进行定制,有什么区别呢?DIB功能比较强大,灵活,模块化,通过一个一个Element,来实现镜像的定制。Virt-customize,可以让你不需要写代码,搞定基本你想要的定制。

调整镜像的大小,对于DIB来说,就一个参数,对于Virt来说,还是比较折腾的。下面的操作,写成一个脚本。应该都不难了。把镜像的名字改成变量。基本就没问题的。

这次还是基于CentOS官方镜像,进行整个过程的记录。

准备

设置Repo

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
yum list

下载镜像

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

安装DIB和Virt套件

yum install centos-release-openstack-train
yum install diskimage-builder
yum install libguestfs-tools-c

使用libguestfs前,需要运行

export LIBGUESTFS_BACKEND=direct

检查镜像

确认镜像下载的CentOS版本,这点其实挺重要。

virt-inspector -a CentOS-7-x86_64-GenericCloud-1905.qcow2 > c.txt

输出的内容很多,重定向到文本,打开文本,你就基本可以看到下面的信息。你就可以确定你的镜像的版本。

<?xml version="1.0"?>
<operatingsystems>
  <operatingsystem>
    <root>/dev/sda1</root>
    <name>linux</name>
    <arch>x86_64</arch>
    <distro>centos</distro>
    <product_name>CentOS Linux release 7.6.1810 (Core) </product_name>
    <major_version>7</major_version>
    <minor_version>6</minor_version>
    <package_format>rpm</package_format>
    <package_management>yum</package_management>
    <hostname>localhost.localdomain</hostname>
    <osinfo>centos7.0</osinfo>
    <mountpoints>
      <mountpoint dev="/dev/sda1">/</mountpoint>
    </mountpoints>
    <filesystems>
      <filesystem dev="/dev/sda1">
        <type>xfs</type>
        <uuid>60d67439-baf0-4c8b-94a3-3f10a362e8fe</uuid>
      </filesystem>

DIB

先用DIB,对镜像做一下标准对动作。

设置环境

export DIB_LOCAL_IMAGE="/root/cloud-images/CentOS-7-x86_64-GenericCloud-2003.qcow2"
export DIB_DISTRIBUTION_MIRROR="https://mirrors.ustc.edu.cn/centos"
export DIB_RELEASE=7
export DIB_CLOUD_INIT_ALLOW_SSH_PWAUTH="yes"

有一点你需要注意的,不管你下载的是那个CetnOS 7的镜像,DIB都会将他升级到最新的版本的CentOS,当前是CentOS 7.9。

如果你就只需要一个固定版本的镜像,那么你下载这个版本的镜像回来。加上下面的参数。另外镜像里面的base repo,你也是需要小心处理。需要在repo里指定版本,这样才能避免yum update 或 yum upgrade 升级系统。

export DIB_AVOID_PACKAGES_UPDATE=1

这样就可以开始构建镜像

DIB_CLOUD_INIT_DATASOURCES="ConfigDrive, OpenStack"  disk-image-create -a amd64 -o CentOS_Linux_release_7.8.2003.qcow2  -x --image-size 40 vm base centos disable-selinux cloud-init cloud-init-datasources dhcp-all-interfaces growroot

如果不是OpenStack平台的虚拟机使用,那么这个地方需要注意,例如zstack平台,要注意差异,growroot,这个功能,Zstack平台是不用的。growroot,是创建的时候,调整根分区的大小,Flavor,定义的磁盘大小。

DIB_CLOUD_INIT_DATASOURCES="ConfigDrive, Ec2"  disk-image-create -a amd64 -o CentOS_Linux_release_7.8.2003.qcow2  -x --image-size 40 vm base centos disable-selinux cloud-init cloud-init-datasources dhcp-all-interfaces

都启用EC2和OpenStack,会导致虚拟机启动速度慢。

好好看看这篇文章:https://mp.weixin.qq.com/s/hDe7djVI0ZufkrApoMpWjw

默认虚拟机是才有bios启动,如果你希望改成UEFI启动,那么你可以加入下面参数就可以。

block-device-efi 

应该很快,20分钟,就可以完成第一个镜像的构建。后续再做修改,时间就会短很多。把镜像的大小调整成40G。

# qemu-img info CentOS_Linux_release_7.8.2003.qcow2 
image: CentOS_Linux_release_7.8.2003.qcow2
file format: qcow2
virtual size: 40G (42949672960 bytes)
disk size: 619M
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false

# virt-filesystems -a CentOS_Linux_release_7.8.2003.qcow2 --long --parts --blkdevs -h
Name       Type       MBR  Size  Parent
/dev/sda1  partition  83   40G   /dev/sda
/dev/sda   device     -    40G   -

virt-customize

后续使用这个工具来完成我们对镜像的定制。

# root 密码
#virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --root-password password:chenshake 

# 设置时区

virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --timezone "Asia/Shanghai" 

#安装工具

virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --install [net-tools,wget,vim,unzip,qemu-guest-agent] 

#启动服务
virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --run-command 'systemctl enable qemu-guest-agent' 


#SSH服务
virt-sysprep -a CentOS_Linux_release_7.8.2003.qcow2 --edit '/etc/ssh/sshd_config:s/GSS/#GSS/'
virt-sysprep -a CentOS_Linux_release_7.8.2003.qcow2 --edit '/etc/ssh/sshd_config:s/#UseDNS yes/UseDNS no/'


#查看修改
virt-cat -a CentOS_Linux_release_7.8.2003.qcow2 /etc/ssh/sshd_config 


#上传优化脚本和运行

virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --upload /root/centos.sh:/root/centos.sh 
virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 -chmod 755:/root/centos.sh 
virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --run '/root/centos.sh'

安装Zstack的agent

virt-customize -a CentOS_Linux_release_7.8.2003.qcow2 --firstboot-command '/bin/bash -c "$(curl -s -S http://169.254.169.254/vm-tools.sh)"'

脚本,逐步完善,目前已经可以满足我使用

!/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 -
# only for centos 7.6
#sed -i 's/$releasever/7.6.1810/g' /etc/yum.repos.d/Centos-7.repo
#vim
echo 'alias vi=vim' >> /etc/profile

脚本如果需要指定版本的centos镜像,我们需要考虑在脚本加入一行。

sed -i 's/$releasever/7.6.1810/g' /etc/yum.repos.d/Centos-7.repo

查看修改结果

# virt-ls -a CentOS_Linux_release_7.6.1810.qcow2 /etc/yum.repos.d/
Centos-7.repo
backup
epel-7.repo

上传镜像

启用pyton的http server,简单很多。通过ip地址,就可以访问。

python -m SimpleHTTPServer 8080

参考文章

  • https://cloud.tencent.com/developer/article/1452066
  • https://www.cyberciti.biz/faq/check-running-services-in-rhel-redhat-fedora-centoslinux/
  • https://www.tecmint.com/list-all-running-services-under-systemd-in-linux/
  • https://docs.openstack.org/diskimage-builder/latest/elements/base/README.html
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