Alibaba Cloud Linux provides VHD and QCOW2 images with built-in cloud-init for on-premises kernel-based virtual machines (KVMs). This topic covers how to use Alibaba Cloud Linux 2 or 3 images on on-premises KVMs.
Background information
This topic uses Anolis OS as the host operating system and the NoCloud data source to generate cloud-init configuration files. These files are attached to the KVM as virtual disks to initialize system settings on boot. Download Anolis OS from the Anolis OS download page. Familiarity with KVM management is assumed.
Step 1: Download an Alibaba Cloud Linux image
Download a VHD or QCOW2 image from one of the following URLs:
-
Alibaba Cloud Linux 3: Alibaba Cloud Linux 3 On-premise Image
-
Alibaba Cloud Linux 2: Alibaba Cloud Linux 2 On-premise Image
Step 2: Obtain the seed.img boot image
Alibaba Cloud Linux images require a seed.img boot image that contains network, account, and YUM repository configurations for cloud-init.
-
The boot image is typically named seed.img. You can rename it, but we recommend keeping the default name.
-
The seed.img boot image contains only cloud-init configuration files, not Alibaba Cloud Linux system files.
Use one of the following methods to obtain the seed.img boot image:
-
Method 1: Download the seed.img boot image
Download the seed.img boot image from one of the following URLs:
-
Alibaba Cloud Linux 3: Alibaba Cloud Linux 3 On-premise Image
-
Alibaba Cloud Linux 2: Alibaba Cloud Linux 2 On-premise Image
NoteThe pre-built image has fixed configurations that cannot be modified. Verify that its settings meet your requirements before use.
-
-
Method 2: Use the NoCloud data source to manually generate the seed.img boot image
NoteNoCloud is a cloud-init data source that provides configuration files in on-premises environments for virtual machines to read at boot.
-
Create two configuration files named
meta-dataanduser-datain a local directory.These files are part of the
seed.imgboot image. On startup, cloud-init reads these files and initializes the virtual machine accordingly.-
Create a directory named
seedand go to the directory.sudo mkdir seed sudo cd seed/ -
Create a configuration file named
meta-data.Example content. The virtual machine name is
alinux-host. Change it as needed.#cloud-config #vim:syntax=yaml local-hostname: alinux-host -
Create a configuration file named
user-data.Example content. Modify as needed. If you use this example directly, ensure you understand the configuration.
-
Creates a user account
alinuxwith passwordaliyunandsudoprivileges. -
Adds the Alibaba Cloud Linux YUM repository.
Alibaba Cloud Linux 3
#cloud-config #vim:syntax=yaml # Create an account named alinux and grant the account the permissions to run sudo commands. users: - default - name: alinux sudo: ['ALL=(ALL) ALL'] plain_text_passwd: aliyun lock_passwd: false # Create a YUM repository for Alibaba Cloud Linux 3. yum_repos: alinux3-module: name: alinux3-module baseurl: https://mirrors.aliyun.com/alinux/$releasever/module/$basearch/ enabled: 1 gpgcheck: 1 gpgkey: https://mirrors.aliyun.com/alinux/$releasever/RPM-GPG-KEY-ALINUX-3 alinux3-updates: name: alinux3-updates baseurl: https://mirrors.aliyun.com/alinux/$releasever/updates/$basearch/ enabled: 1 gpgcheck: 1 gpgkey: https://mirrors.aliyun.com/alinux/$releasever/RPM-GPG-KEY-ALINUX-3 alinux3-plus: name: alinux3-plus baseurl: https://mirrors.aliyun.com/alinux/$releasever/plus/$basearch/ enabled: 1 gpgcheck: 1 gpgkey: https://mirrors.aliyun.com/alinux/$releasever/RPM-GPG-KEY-ALINUX-3 alinux3-powertools: name: alinux3-powertools baseurl: https://mirrors.aliyun.com/alinux/$releasever/powertools/$basearch/ gpgcheck: 1 enabled: 1 gpgkey: https://mirrors.aliyun.com/alinux/$releasever/RPM-GPG-KEY-ALINUX-3 alinux3-os: name: alinux3-os baseurl: https://mirrors.aliyun.com/alinux/$releasever/os/$basearch/ gpgcheck: 1 enabled: 1 gpgkey: https://mirrors.aliyun.com/alinux/$releasever/RPM-GPG-KEY-ALINUX-3Alibaba Cloud Linux 2
#cloud-config #vim:syntax=yaml # Create an account named alinux and grant the account the permissions to run sudo commands. users: - default - name: alinux sudo: ['ALL=(ALL) ALL'] plain_text_passwd: aliyun lock_passwd: false # Create a YUM repository for Alibaba Cloud Linux 2. yum_repos: base: baseurl: https://mirrors.aliyun.com/alinux/$releasever/os/$basearch/ enabled: true gpgcheck: true gpgkey: https://mirrors.aliyun.com/alinux/RPM-GPG-KEY-ALIYUN name: Aliyun Linux - $releasever - Base - mirrors.aliyun.com updates: baseurl: https://mirrors.aliyun.com/alinux/$releasever/updates/$basearch/ enabled: true gpgcheck: true gpgkey: https://mirrors.aliyun.com/alinux/RPM-GPG-KEY-ALIYUN name: Aliyun Linux - $releasever - Updates - mirrors.aliyun.com extras: baseurl: https://mirrors.aliyun.com/alinux/$releasever/extras/$basearch/ enabled: true gpgcheck: true gpgkey: https://mirrors.aliyun.com/alinux/RPM-GPG-KEY-ALIYUN name: Aliyun Linux - $releasever - Extras - mirrors.aliyun.com plus: baseurl: https://mirrors.aliyun.com/alinux/$releasever/plus/$basearch/ enabled: true gpgcheck: true gpgkey: https://mirrors.aliyun.com/alinux/RPM-GPG-KEY-ALIYUN name: Aliyun Linux - $releasever - Plus - mirrors.aliyun.com
-
-
-
(Optional) If you use Alibaba Cloud Linux 3, enable the epel-testing repository.
-
Open the
epel-testing.repofile.sudo vim /etc/yum.repos.d/epel-testing.repo -
Press i to enter edit mode and replace
enabled=0withenabled=1.[epel-testing] name=Extra Packages for Enterprise Linux 8 - Testing - $basearch baseurl=http://mirrors.cloud.aliyuncs.com/epel/testing/8/Everything/$basearch enabled=0 gpgcheck=1 countme=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8 -
Press
Escand enter :wq to save and exit.
-
-
Install the
cloud-utilspackage:sudo yum install -y cloud-utils -
Generate the
seed.imgboot image:sudo cloud-localds seed.img user-data meta-data
-
Step 3: Start your on-premises virtual machine
Start a KVM using one of the following methods:
Use libvirt to start the KVM
-
Create an XML configuration file on your on-premises computer.
Example file:
alinux3.xmlfor Alibaba Cloud Linux 3 oralinux2.xmlfor Alibaba Cloud Linux 2. Modify as needed.Alibaba Cloud Linux 3
<domain type='kvm'> <name>alinux3</name> <memory>1048576</memory><!-- Set the memory size to 1 GB. --> <vcpu>1</vcpu> cpu mode='host-passthrough'><!-- Start the KVM on the server of the corresponding architecture. --> </cpu> <os> <type arch='x86_64'>hvm</type><!-- If the architecture of the server is ARM 64-bit, set arch to aarch64. --> <boot dev='hd'/> </os> <clock sync="localtime"/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/bin/qemu-kvm</emulator><!-- Configure a KVM path based on the operating system. For example, the KVM path for Ubuntu is /usr/bin/kvm. --> <disk type='file' device='disk'><!-- Specify the type parameter based on the image format. Set type to qcow2 if the image is in the QCOW2 format, and set type to vpc if the image is in the VHD format. --> <driver name='qemu' type='qcow2' cache='none' dataplane='on' io='native'/> <!-- If you want to create a snapshot in the QCOW2 format, disable dataplane. --> <source file='path'/> <!-- Enter the absolute path of the Alibaba Cloud Linux 3 image. --> <target dev='vda' bus='virtio'/> </disk> <!-- Add information about the seed.img boot image. --> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/path/to/your/seed.img'/> <!-- Enter the absolute path of the seed.img boot image. --> <target dev='vdb' bus='virtio'/> </disk> <interface type='network'> <source network='default'/> <model type='virtio'/> </interface> <console type='pty'> <target type='virtio' port='0'/> </console> <video> <model type='cirrus' vram='9216' heads='1'/> <alias name='video0'/> </video> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport='yes'/> </devices> </domain>Alibaba Cloud Linux 2
<domain type='kvm'> <name>alinux2</name> <memory>1048576</memory> <!-- Set the memory size to 1 GB. --> <vcpu>1</vcpu> <os> <type arch='x86_64'>hvm</type> <boot dev='hd'/> </os> <clock sync="localtime"/> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart</on_crash> <devices> <emulator>/usr/bin/qemu-kvm</emulator><!-- Configure a KVM path based on the operating system. For example, the KVM path for Ubuntu is /usr/bin/kvm. --> <disk type='file' device='disk'><!-- Specify the type parameter based on the image format. Set type to qcow2 if the image is in the QCOW2 format, and set type to vpc if the image is in the VHD format. --> <driver name='qemu' type='qcow2' cache='none' dataplane='on' io='native'/> <!-- If you want to create a snapshot in the QCOW2 format, disable dataplane. --> <source file='path'/> <!-- Enter the absolute path of the Alibaba Cloud Linux 2 image. --> <target dev='vda' bus='virtio'/> </disk> <!-- Add information about the seed.img boot image. --> <disk type='file' device='disk'> <driver name='qemu' type='raw'/> <source file='/path/to/your/seed.img'/> <!-- Enter the absolute path of the seed.img boot image. --> <target dev='vdb' bus='virtio'/> </disk> <interface type='network'> <source network='default'/> <model type='virtio'/> </interface> <console type='pty'> <target type='virtio' port='0'/> </console> <video> <model type='cirrus' vram='9216' heads='1'/> <alias name='video0'/> </video> <input type='tablet' bus='usb'/> <input type='mouse' bus='ps2'/> <graphics type='vnc' port='-1' autoport='yes'/> </devices> </domain> -
Run the
virshcommand to start the KVM.NoteBy default, libvirt runs as a non-root user. Ensure that this user has permissions to manage the image files and their parent directories.
Alibaba Cloud Linux 3
sudo virsh define alinux3.xml virsh start <KVMName> # Enter the name of the KVM. You can run the sudo virsh list --name command to query the name of the KVM.Alibaba Cloud Linux 2
sudo virsh define alinux2.xml sudo virsh start <KVMName> # Enter the name of the KVM. You can run the sudo virsh list --name command to query the name of the KVM.
Run the qemu-kvm command to start the KVM
Append the following parameter to the qemu-kvm command:
-drive file=/path/to/your/seed.img,if=virtio,format=raw
-
/path/to/your/seed.img: Replace with the actual path of your seed.img file. -
if=virtio: Uses the virtio interface for the virtual disk. -
format=raw: Specifies the RAW disk image format.
Example:
sudo /usr/libexec/qemu-kvm -drive file=/home/ecs-user/seed/seed.img,if=virtio,format=raw
Use the virt-manager graphical interface to start the KVM
Add the absolute path of your seed.img boot image to the KVM configuration file before starting the KVM.
What to do next
After the KVM starts, log on with the username and password specified in the user-data configuration file.
To log on with an SSH key pair, add the public key to the cloud-init configuration file.
References
-
cloud-init automatically initializes Linux virtual machines at boot — configuring the hostname, network, and software packages.
-
libvirt, qemu-kvm, and virt-manager are commonly used KVM management tools. Red Hat documentation.