Environment Preparation
Prerequisites
To encapsulate a Rocky Linux 9 template, obtain an ISO image in advance and add it to ZStack Cloud. Notice the following points when you perform this operation:- Obtain a Rocky Linux 9 installation image from the official website. Ensure compliance with relevant authorizations and permissions.
- This tutorial references Rocky-9.8-x86_64-minimal.iso.
Procedure
-
Add an installation image.
On the main menu of ZStack Cloud, choose . On the Image page, click Add Image. Then, the Add Image page is displayed.
On the displayed page, set the following parameters:- Name: Enter a name for the image.
- Description: Enter a description for the image.
- Image Type: Select System Image.
- Image Format: Select iso.
- CPU Architecture: Select x86_64.
- Platform: Select Linux.
- OS: Select Rocky Linux 9.
- VirtIO: Enable VirtIO.
- Image Storage: Select an image storage added before.
- Image Path: Specify an image URL or upload a local file. Here, upload the local ISO file.
Note: Your local browser serves as a transit point to upload the image. Do not refresh or close the browser or stop the management node service. Otherwise, the operation may fail. - BIOS Mode: Select Legacy.
Encapsulate a Template
Install the Operating System
About this task
To install the operating system, follow these steps:- Create a VM instance.
- Install Rocky Linux 9.
Procedure
-
Create a VM instance.
Create a VM instance in ZStack Cloud and boot it from the installation image. For details, see the Create a VM Instance section in the User Guide. This example uses 2 vCPUs, 4 GiB memory, and a 40 GiB root volume. Use the same boot mode as the installation image.
-
Install Rocky Linux 9.
Configure the Template System
About this task
To configure the template system, follow these steps:- Specify the hostname.
- Configure the network.
- (Optional) Install GuestTools.
- (Optional) Install cloud-init.
Procedure
-
Specify the hostname.
Run the following command to specify the hostname:
sudo hostnamectl set-hostname localhost.localdomain -
Configure the network.
This tutorial uses one Ethernet NIC with IPv4 DHCP. Select its connection UUID and clear source-specific static addresses, DNS settings, and NIC bindings. Changes are persistent by default.
nmcli -f NAME,UUID,TYPE,DEVICE connection show --active PROFILE="REPLACE_WITH_CONNECTION_UUID"sudo nmcli connection modify "$PROFILE" \ connection.autoconnect yes connection.interface-name "" \ ipv4.method auto ipv4.addresses "" ipv4.gateway "" \ ipv4.routes "" ipv4.dns "" ipv4.dns-search "" \ ipv4.ignore-auto-dns no ipv4.ignore-auto-routes no \ ipv4.dhcp-client-id mac \ 802-3-ethernet.mac-address "" \ 802-3-ethernet.cloned-mac-address permanentsudo nmcli connection up "$PROFILE" nmcli device show
Note: Run these commands through the console because reactivating networking can interrupt SSH. Confirm that the DHCP-provided DNS works and disable obsolete autoconnect profiles. -
(Optional) Install GuestTools.
sudo dnf install -y tarAttach the GuestTools ISO from the source VM instance details page. Replace DEVICE with the actual optical device, such as /dev/sr0, and run the following commands. Resolve installation errors before continuing. If the media remains mounted, confirm that it is unused before unmounting it.
( set -e DEVICE="REPLACE_WITH_OPTICAL_DEVICE" test -b "$DEVICE" sudo mkdir -p /mnt/guesttools if mountpoint -q /mnt/guesttools; then printf '%s\n' 'Mount point already in use; inspect it before continuing.' exit 1 fi sudo mount -o ro "$DEVICE" /mnt/guesttools test -f /mnt/guesttools/zs-tools-install.sh cd /mnt/guesttools sudo bash ./zs-tools-install.sh cd / sudo umount /mnt/guesttools )Figure 7. GuestTools Installation Complete 
If prompted to link python3 as python, check the current Python environment before accepting the change.
-
(Optional) Install cloud-init.
Install cloud-init if you need User Data or SSH public key initialization. Use root or an account with sudo privileges to install the following packages from the official repositories.
sudo dnf install -y cloud-init openssh-server && sudo systemctl enable --now sshdRetain SELinux and firewall policies. Add the following settings to /etc/cloud/cloud.cfg.d/99-template.cfg so that NetworkManager manages networking and cloud-init handles initialization. Merge changes if the file already exists.
network: config: disabled preserve_hostname: false ssh_deletekeys: true ssh_pwauth: false
Note: SSH password authentication is disabled. Check the cloud-init default login user and provide an SSH public key for that user when creating VM instances from the image.Save the changes and reboot the source VM instance.
sudo systemctl reboot
Create a System Template
About this task
To create a system template, follow these steps:- Create an image.
- Export the image.
Procedure
-
Create an image.
Remove temporary files, credentials in installation answer files, temporary accounts, and command history. Keep the login accounts and authentication settings required to use the image. cloud-init cleanup does not remove local account passwords.
If cloud-init is installed, confirm that it supports these cleanup options and is configured to generate SSH host keys on first boot as described above. Without cloud-init, the following commands only reset the machine ID, preserve the existing network configuration and SSH host keys, and shut down the VM instance. For images without cloud-init, arrange SSH host-key regeneration separately for each VM Instance.
sudo bash <<'CLEANUP' set -e if command -v cloud-init >/dev/null 2>&1; then cloud-init clean --logs --machine-id --seed rm -f /etc/ssh/ssh_host_*_key /etc/ssh/ssh_host_*_key.pub else truncate -s 0 /etc/machine-id fi if [ -d /var/lib/dbus ]; then rm -f /var/lib/dbus/machine-id ln -s /etc/machine-id /var/lib/dbus/machine-id fi sync systemctl poweroff CLEANUP
Note: Run only on the template source VM instance. Resolve cleanup errors before proceeding. Do not restart the source after cleanup; if restarted, repeat cleanup before creating the image.Confirm that the VM instance is stopped and detach the installation ISO and GuestTools ISO.
On the details page of the VM instance created, choose . Then, the Create Image page is displayed.
On the displayed page, set the following parameters:- Name: Enter a name for the image.
- Description: Optional. Enter a description for the image.
- Image Type: Select System Image here.
- Image Storage: Select an image storage created before.
Figure 8. Enter the System Image Name 
-
Export the image.
You can use the image exported to create VM instances on other cloud platforms directly. The method to export images varies according to image storage types.
- ImageStore image storage.
On the Image page, choose . This button helps export the image.
After generating the image, you can copy the URL of the image exported on the Overview page to download the image. Or, you can copy the URL of the exported image on the Exported page. You can also click Download on the page to download the image directly.
- Ceph image storage. If you use a Ceph image storage, you can export
the image on the UI or from the image storage.
- Export on the UI:
On the Image page, choose . This button helps export the image.
After generating the image, you can copy the URL of the image exported on the Overview page to download the image. Or, you can copy the URL of the exported image on the Exported page. You can also click Download on the page to download the image directly.
- Export from the image storage:
- On the Overview page of the created image, copy Installation Path.
- Login to the terminal of the Ceph image storage, run
rbd export to export the image.
For example: If you obtain the installation path ceph://bak-t-cac5eee23a204c1a914d1743f1584644/7232237c0059409babcd1c7a69452b98, you can run the following commands to export the image:
[root@ceph-node1 ~]# rbd export bak-t-cac5eee23a204c1a914d1743f1584644/7232237c0059409babcd1c7a69452b98 /root/export-test.qcow2 [root@ceph-node1 ~]# # bak-t indicates the installation path of the image [root@ceph-node1 ~]# # /root/export-test.qcow2 indicates the target path and the file name of the exported image
Note: The
command rbd export does not
include such a prefix as ceph://.
- Export on the UI:
- ImageStore image storage.






