制作腾讯云CentOS系统镜像

Summary: Author: 张亚飞 | 阅读时间: 4 minute read | Published: 2016-05-17
Filed under Categories: LinuxTags: Note,

制作腾讯云 CentOS 系统镜像

Linux 系统安装 cloud-init 制作 Linux 镜像

由于腾讯云系统镜像更新过慢,目前还没有新的 CentOS-8 资源可用,于是参照以下方案从已有系统打包制作自定义镜像

制作腾讯云 CentOS-8 系统镜像

首先在阿里云启动一条新的CentOS-8机器

注意: 由于是要编译整个根目录,固建议编译系统需要再挂载单独的分区拿来导出系统镜像

登录目标机器

ssh root@8.210.170.106

查看并挂载磁盘分区

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  472K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1        20G  2.4G   18G  12% /
tmpfs           782M     0  782M   0% /run/user/0
# fdisk -l
Disk /dev/vda: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xd2e22281

Device     Boot Start      End  Sectors Size Id Type
/dev/vda1  *     2048 41943039 41940992  20G 83 Linux


Disk /dev/vdb: 20 GiB, 21474836480 bytes, 41943040 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

可以看到 /dev/vdb 还没挂载,于是执行挂载命令并重启服务器

fdisk -l && mkfs.ext4 /dev/vdb && echo '/dev/vdb /data ext4 defaults 0 0' >> /etc/fstab && reboot

重启系统后再次查看发现 /dev/vdb 已挂载到目录 /data

# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  472K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
/dev/vda1        20G  2.4G   18G  12% /
/dev/vdb         20G   45M   19G   1% /data
tmpfs           782M     0  782M   0% /run/user/0

更新编译系统

首先检查默认系统可以发现阿里云已安装有 cloud-init 系统服务

# ls -al /lib/systemd/system/cloud-
cloud-config.service      cloud-config.target       cloud-final.service       cloud-init-local.service  cloud-init.service        cloud-init.target
# cloud-init --version
/usr/local/bin/cloud-init 19.1.3

如果没有则需要手动安装

  • 我们更新并安装系统依赖
yum update -y

CentOS 需替换为 cloud.cfg

/etc/cloud/cloud.cfg

users:
 - default

disable_root: 0
ssh_pwauth:   1

datasource_list: [ ConfigDrive, TencentCloud ]
datasource:
  ConfigDrive:
    dsmode: local
  TencentCloud:
    metadata_urls: ['http://metadata.tencentyun.com']

cloud_init_modules:
 - migrator
 - bootcmd
 - write-files
 - growpart
 - resizefs
 - set_hostname
 - update_hostname
 - ['update_etc_hosts', 'once-per-instance']
 - rsyslog
 - users-groups
 - ssh

cloud_config_modules:
 - mounts
 - locale
 - set-passwords
 - rh_subscription
 - yum-add-repo
 - package-update-upgrade-install
 - ntp
 - timezone
 - resolv_conf
 - puppet
 - chef
 - salt-minion
 - mcollective
 - disable-ec2-metadata
 - runcmd

unverified_modules: ['resolv_conf']

cloud_final_modules:
 - rightscale_userdata
 - scripts-per-once
 - scripts-per-boot
 - scripts-per-instance
 - scripts-user
 - ssh-authkey-fingerprints
 - keys-to-console
 - phone-home
 - final-message
 - power-state-change

system_info:
  default_user:
    name: root
    lock_passwd: false
    gecos: Cloud User
    groups: [wheel, adm, systemd-journal]
    sudo: ["ALL=(ALL) NOPASSWD:ALL"]
    shell: /bin/bash
  distro: rhel
  paths:
    cloud_dir: /var/lib/cloud
    templates_dir: /etc/cloud/templates
  ssh_svcname: sshd

执行以下命令添加用户 useradd syslog

useradd syslog

检查如果没有 /usr/bin/cloud-init 程序则需手动建立软连接

ln -s /usr/local/bin/cloud-init /usr/bin/cloud-init 

针对 CentOSRedhat 操作系统,需替换以下配置文件

/lib/systemd/system/cloud-init-local.service

[Unit]
Description=Initial cloud-init job (pre-networking)
Wants=network-pre.target
After=systemd-remount-fs.service
Before=NetworkManager.service
Before=network-pre.target
Before=shutdown.target
Conflicts=shutdown.target
RequiresMountsFor=/var/lib/cloud
[Service]
Type=oneshot
ExecStart=/usr/bin/cloud-init init --local
ExecStart=/bin/touch /run/cloud-init/network-config-ready
RemainAfterExit=yes
TimeoutSec=0
# Output needs to appear in instance console output
StandardOutput=journal+console
[Install]
WantedBy=cloud-init.target

/lib/systemd/system/cloud-init.service

[Unit]
Description=Initial cloud-init job (metadata service crawler)
Wants=cloud-init-local.service
Wants=sshd-keygen.service
Wants=sshd.service
After=cloud-init-local.service
After=systemd-networkd-wait-online.service
After=networking.service
After=systemd-hostnamed.service
Before=network-online.target
Before=sshd-keygen.service
Before=sshd.service
Before=systemd-user-sessions.service
Conflicts=shutdown.target
[Service]
Type=oneshot
ExecStart=/usr/bin/cloud-init init
RemainAfterExit=yes
TimeoutSec=0
# Output needs to appear in instance console output
StandardOutput=journal+console
[Install]
WantedBy=cloud-init.target

执行以下命令:

sudo systemctl daemon-reload
sudo systemctl enable cloud-init-local.service 
sudo systemctl restart cloud-init-local.service
sudo systemctl enable cloud-init.service
sudo systemctl restart cloud-init.service
sudo systemctl enable cloud-config.service
sudo systemctl restart cloud-config.service
sudo systemctl enable cloud-final.service
sudo systemctl restart cloud-final.service
sudo systemctl status cloud-init-local.service
sudo systemctl status cloud-init.service
sudo systemctl status cloud-config.service
sudo systemctl status cloud-final.service

以下操作执行完成后,请勿重启服务器,否则需重新执行下以下操作。 执行以下命令,检查 cloud-init 相关配置是否成功。

cloud-init init --local
rm -rf /var/lib/cloud

安装系统基础软件

yum install -y git && mkdir -p /opt/data && git clone https://zyf%40iirii.com:ZYF_bin312@e.coding.net:443/coam/Run.run_s.git && mv Run.run_s /opt/data/run_s
wget http://download.redis.io/redis-stable.tar.gz -O /tmp/redis-stable.tar.gz && tar xvzf /tmp/redis-stable.tar.gz -C /tmp && cd /tmp/redis-stable && make MALLOC=libc && cp /tmp/redis-stable/src/redis-cli /usr/bin/ && chmod 755 /usr/bin/redis-cli

CentOS-7

gcc -v
yum -y install centos-release-scl
yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
gcc -v

如果报 cc: command not found 则执行 yum install -y gcc && rpm -qa|grep gcc 安装 gcc

CentOS 7.7系统安装Redis 6.0.3

编译系统镜像

注意: 需要手动注释掉以上步骤配置的 /etc/fstab 挂载磁盘: /dev/vdb /data ext4 defaults 0 0 因为有的系统默认没有挂载云硬盘

再以整个根分区目录编译最终的系统镜像文件

# yum install -y qemu-kvm
# qemu-img convert -f raw -O qcow2 /dev/vda /data/centos-8.0.qcow2

编译完好有 3.2 G的系统镜像文件

/data

# du -hs *
3.2G	centos-8.0.qcow2
16K	lost+found

/data

# qemu-img info /data/centos-8.0.qcow2
image: /data/centos-8.0.qcow2
file format: qcow2
virtual size: 50G (53687091200 bytes)
disk size: 3.5G
cluster_size: 65536
Format specific information:
    compat: 1.1
    lazy refcounts: false
    refcount bits: 16
    corrupt: false
  • 使用 qemu-img resize 减少

/data

qemu-img resize --shrink /data/centos-8.0.qcow2 -10G && qemu-img info /data/centos-8.0.qcow2

其它参考

qemu-img create -f qcow2 -o preallocation=metadata newimage.qcow2 NEW_SIZE

Shrinking qcow2 images Resizing KVM qcow2 images 缩减镜像 virtual size 记 [](http://libguestfs.org/)

fdisk -l && mkfs.ext4 /dev/vdb && echo '/dev/vdb /data ext4 defaults 0 0' >> /etc/fstab && reboot
yum update -y
yum install -y libguestfs-tools
yum install -y qemu-kvm
systemctl start libvirtd
qemu-img convert -f raw -O qcow2 /dev/vda /data/centos-8.0.qcow2
qemu-img info /data/centos-8.0.qcow2
qemu-img create -f qcow2 -o preallocation=metadata /data/centos-8.00.qcow2 10G
virt-resize /data/centos-8.0.qcow2 /data/centos-8.00.qcow2
qemu-img convert -c -f raw -O qcow2 /dev/vda /data/centos-8.0.0.qcow2
qemu-img convert -f raw -O raw /dev/vda /data/centos-8.0.raw

qcow2raw 格式

qemu-img convert -f qcow2 -O raw centos7.qcow2 centos7.raw

KVM虚拟机导出最小化体积的qcow2镜像文件


最后使用 COSCMD 上传到腾讯云存储桶

COSCMD 命令行工具

安装 coscmd

CentOS-8

dnf install python3-pip
pip3 install coscmd

CentOS-8

yum install -y python3-pip
pip3 install coscmd

配置

# coscmd config -a AKIDiJhYGncxKVQ5koyjfWOE0b7Uip11AW8e -s bzEyUdjgIm0x4xaw1yhBPM4Y1PamuyoO -b mirror-1251154801 -r ap-shanghai

或者直接配置以下文件

~/.cos.conf

[common]
secret_id = AKIDiJhYGncxKVQ5koyjfWOE0b7Uip11AW8e
secret_key = bzEyUdjgIm0x4xaw1yhBPM4Y1PamuyoO
bucket = mirror-1251154801
region = ap-shanghai
max_thread = 5
part_size = 1
retry = 5
timeout = 60
schema = https
verify = md5
anonymous = False

使用以下命令上传镜像

将本地的 /data/centos-8.qcow2 文件上传到 cosdata/centos-8.qcow2 路径下

# coscmd upload /data/centos-8.qcow2 data/centos-8.qcow2

Comments

Cor-Ethan, the beverage → www.iirii.com