操作场景
用户上云部署应用前,获取的镜像通常为从操作系统官方下载 *.iso 格式的原始镜像文件,此格式文件无法在天翼云平台上直接使用。需要用户基于ISO制作对应格式的镜像文件后(如 *.qcow2),才可以导入为弹性云主机实例可用的私有镜像。
Packer 是一款开源的虚拟机镜像构建工具,可以通过一个HCL或者JSON格式模板配置可以快速的创建主流的操作系统云主机镜像。Packer包含构建器(Builder)、配置器(Provisioner)、后处理器(Post-Processor)三个组件。
本文提供Packer工具的使用全流程,为您介绍如何构建天翼云可用的镜像文件,帮助您快速上云。
约束与限制
当前仅支持x86架构的私有镜像构建,ARM架构的私有镜像构建方法正在建设中。
对于x86架构中的海光类型是否支持,取决于原始镜像本身是否已可以在海光芯片上正常启动和运行。
操作步骤
使用 Packer 构建镜像文件的过程如下:
需要您准备一台本地的物理机或虚拟机,在其中构建制作私有镜像的虚拟化环境。
安装 Packer 软件与关联插件,根据天翼云的镜像配置要求,设定配置模板。
执行 Packer 构建命令,构建镜像文件。
一、构建服务器
构建服务器并准备ISO镜像文件。
推荐使用Packer软件的服务器配置如下:
CPU:Intel 或 AMD,4核或以上,
内存:8GB或以上,
存储:100GB(存储空间需要包含服务器自身操作系统、源ISO镜像文件、构建的镜像文件的空间,建议您预留的空间大于这些空间总和),
网络:联通公网,需要下载虚拟化、Packer 等软件。
注意
使用Packer构建镜像的过程中需要使用虚拟化软件(包括KVM、Libvirt、QEMU等),因此请您确认:使用Packer软件构建镜像的物理机或虚拟机支持虚拟化。
如果您无法确定本机是否支持虚拟化,您可以采用本示例的方式,基于Windows操作系统,安装VMware Workstation,并勾选支持虚拟化的配置,创建出服务器。
本示例基于 Windows 操作系统的宿主机,使用 VMware Workstation 创建出 CentOS-9 64位 操作系统的虚拟机,作为使用 Packer 构建镜像的服务器。
使用 VMware Workstation 请确认勾选“虚拟化 Intel VT-x/EPT 或 AMD-V/RVI(V)”,如下图所示:
创建完成后,将原始ISO镜像上传至该虚机。
本示例使用的是ctyunos-22.06.iso作为演示,目标为制作ctyunos-22.06.qcow2的镜像文件。
安装虚拟化环境。
加载 KVM 模块。
执行以下命令在虚拟机内加载KVM模块。
modprobe kvm执行下述命令检查虚拟机是否加载KVM模块。
lsmod | grep kvm或者
egrep -o '(vmx|svm)' /proc/cpuinfo如果有返回表示已加载:
vmx:代表 Intel 的虚拟化支持(VT-x)
svm:代表 AMD 的虚拟化支持(AMD-V)
如下图所示:
启动 Libvirt 服务。
在操作系统命令行中执行以下命令:
# 启动libvirtd服务 systemctl enable libvirtd # 查看服务启动状态 systemctl restart libvirtd # 配置用户添加到libvirt组,以便授予该用户访问和管理虚拟化功能的权限 usermod -aG libvirt $(whoami) # 编辑qemu.conf vim /etc/libvirt/qemu.conf # 取消注释 user = "root" 和 group = "root"。 # 修改前:#user = "qemu" # 修改后:user = "root" # 修改前:#group = "qemu" # 修改后:group = "root" # 重启服务器生效 reboot安装 QEMU 软件。
#下载qemu安装包: wget -c https://download.qemu.org/qemu-8.0.4.tar.xz # 解压缩 tar -xvf qemu-8.0.4.tar.xz #编译 cd qemu-8.0.4/ ./configure --target-list=x86_64-softmmu --prefix=/usr make make all至此使用Packer制作私有镜像的服务器构建完成。
二、准备 Packer 软件
安装 Packer 软件。
Packer 下载链接请参考:https://releases.hashicorp.com/packer,本示例中使用1.4.3版本。
下载 Packer 软件。
wget https://releases.hashicorp.com/packer/1.11.2/packer_1.11.2_linux_amd64.zip解压 Packer 软件。
unzip packer_1.11.2_linux_amd64.zip为 Packer 增加执行权限。
chmod +x packer执行下述命令验证 Packer 是否成功安装。
mv packer /usr/local/bin/ #下述3个命令执行1个即可 packer -v #或者 packer --version #或者 packer -machine-readable version设定 Packer。
安装Packer QEMU插件。
Packer QEMU 插件提供了一个构建器(Builder),使得 Packer 能够利用 QEMU 来创建和定制镜像文件。
#安装qemu packer插件 packer plugins install github.com/hashicorp/qemu准备JSON模板文件。
Packer 使用 Template 模板文件来定义构建过程。它指定了构建器(Builders)、配置器(Provisioners)和后处理器(Post-Processors)等组件的配置,这些组件共同工作以创建机器镜像。
Template 文件通常使用 JSON 或 HashiCorp Configuration Language (HCL) 格式编写,详细内容请参考:https://developer.hashicorp.com/packer/docs/templates。
本示例基于ctyunos-22.06.iso镜像,提供的JSON模板示例文件
ctyunos_example.json如下:{ "variables": { "cpu": "2", "ram": "4096", "name": "ctyunos", "disk_size": "20960", "version": "22", "iso_checksum": "4c695e58a753fc8b8496bd3646f4f467b7ead1b63e933bd053d5af855c9973a5", "iso_url": "ctyunos-22.06.iso", "headless": "false", "config_file": "ctyunos_22_06.cfg", "ssh_username": "root", "ssh_password": "Password@123", "destination_server": "download.goffinet.org" }, "builders": [ { "name": "{{user `name`}}{{user `version`}}", "type": "qemu", "format": "qcow2", "accelerator": "kvm", "qemu_binary": "/usr/libexec/qemu-kvm", "net_device": "virtio-net", "disk_interface": "virtio", "disk_cache": "none", "qemuargs": [ ["-m", "{{user `ram`}}M"], ["-smp", "{{user `cpu`}}"], ["-display", "vnc=:2"] ], "ssh_wait_timeout": "1800s", "http_directory": "/var/www/html", "ssh_username": "{{user `ssh_username`}}", "ssh_password": "{{user `ssh_password`}}", "ssh_pty": true, "ssh_agent_auth": false, "iso_url": "{{user `iso_url`}}", "iso_checksum": "{{user `iso_checksum`}}", "boot_wait": "3s", "boot_command": [ "<up><wait><tab><wait> net.ifnames=0 biosdevname=0 text ks=http://{{ .HTTPIP }}:{{ .HTTPPort }}/{{user `config_file`}}<enter><wait>" ], "disk_size": "{{user `disk_size`}}", "disk_discard": "unmap", "disk_compression": true, "headless": "{{user `headless`}}", "shutdown_command": "shutdown -P now", "shutdown_timeout": "5m", "output_directory": "artifacts/qemu/{{user `name`}}{{user `version`}}" } ], "provisioners": [ { "type": "file", "source": "/root/automation.zip", "destination": "/root/automation.zip" }, { "type": "shell", "execute_command": "echo 'packer'|{{.Vars}} sudo -S -E bash '{{.Path}}'", "inline": [ "unzip /root/automation.zip -d /root", "chmod u+x /root/automation/creating_mirror_scripts.sh", "/root/automation/creating_mirror_scripts.sh" ], "expect_disconnect": true, "valid_exit_codes": [0, 1] } ], "post-processors": [ { "type": "shell-local", "inline": [ "mv artifacts/qemu/{{user `name`}}{{user `version`}}/packer-{{user `name`}}{{user `version`}} artifacts/qemu/{{user `name`}}{{user `version`}}/ctyunos-22.06-230117-x86_64-dve-240929-R1.qcow2" ] } ] }准备Kickstart 配置文件。
Kickstart 配置文件(通常命名为ks.cfg),主要用于自动化安装操作系统。该文件详细定义了安装过程的各个方面,包括系统的镜像地址、安装方式、分区设置等。只要系统在获取到这个文件后,就会按照文件中所定义的配置方式进行安装。
本示例中将Kickstart 配置文件命名为
ctyunos_22_06.cfg,在上述模板示例文件ctyunos_example.json的参数"variables"中引用"config_file": "ctyunos_22_06.cfg"。内容如下:#version=DEVEL # Use graphical install graphical %packages @^minimal-environment @standard %end # Keyboard layouts keyboard --vckeymap=us --xlayouts='us' # System language lang en_US.UTF-8 # Network information network --bootproto=dhcp --device=ens3 --onboot=off --ipv6=auto --activate network --hostname=localhost.localdomain # Use hard drive installation media harddrive --dir= --partition=LABEL=ctyunos-22.06-x86_64 # Run the Setup Agent on first boot firstboot --enable # System services services --enabled="chronyd" ignoredisk --only-use=vda # Partition clearing information clearpart --none --initlabel # Disk partitioning information part / --fstype="xfs" --ondisk=vda --size=20959 # System timezone timezone Asia/Shanghai --utc # Root password rootpw --iscrypted $6$cPAfx8xCk0rxNiwG$3eKaNUZUJwsdSzn/2AqXYgtc7MuQs5Z0xXW5DVskb.5kTKwjUPW.fd4X0F3P//1h436THh0GnqAW3LGDu.9lO. %addon com_redhat_kdump --disable --reserve-mb='128' %end reboot %anaconda pwpolicy root --minlen=8 --minquality=1 --strict --nochanges --notempty pwpolicy user --minlen=8 --minquality=1 --strict --nochanges --emptyok pwpolicy luks --minlen=8 --minquality=1 --strict --nochanges --notempty %end验证 Packer 模板有效性。
验证 Packer 模板文件的语法和配置是否正确。这个命令会检查模板文件的语法是否符合 Packer 的要求,并且会验证配置信息是否符合各个构建器(Builders)、配置器(Provisioners)等的要求。如果模板文件未通过验证,Packer 会输出所有的错误消息,帮助用户识别和修正问题。
三、开始构建
开始构建
执行下述命令构建模板:
packer build ctyunos_example.json如果希望看到更详细的日志信息,可以执行:
PACKER_LOG=1 packer build ctyunos_example.json如果有错误无法明确原因,可以调试模式运行:
PACKER_LOG=1 packer build -debug ctyunos_example.json执行效果如下图:
构建完成
待构建完成后,执行下述命令,可看到已构建的qcow2格式镜像。
[root@localhost ~]# ll artifacts/qemu/ctyunos22/ctyunos-22.06-230117-x86_64-dve-240929-R1.qcow2 -rw-r--r-- 1 root root 1735169536 Oct 8 15:43 artifacts/qemu/ctyunos22/ctyunos-22.06-230117-x86_64-dve-240929-R1.qcow