Creating Linux Images Based on ISO Using KubeVirt
This document describes a virtual machine solution implemented based on the open-source component KubeVirt. It utilizes KubeVirt virtualization technology to create a Linux operating system image from an ISO image file. The ISO is uploaded to the cluster via CDI DataVolume, eliminating the need to build and push container images to a registry.
Prerequisites
-
All components in the cluster are functioning properly.
-
A Linux image should be prepared in advance. This document uses the Ubuntu operating system as an example.
-
The
kubectlcommand-line tool is installed and configured to access the cluster. -
A StorageClass that supports
ReadWriteOnce(RWO) access mode is available in the cluster.
Constraints and Limitations
-
When starting KubeVirt, the file system size of the custom image will affect the speed of writing the image to the PVC disk. If the file system is too large, it may result in a prolonged creation time.
-
It is recommended to keep the Linux root partition size below 100G to minimize the initial size. After configuring cloud-init, allocate larger storage for the root partition when creating the virtual machine, and the system will automatically expand it.
Procedure
Upload the Linux ISO to a DataVolume
Upload the ISO file directly to the cluster storage using the CDI upload mechanism, without building a container image.
-
Create an upload-type DataVolume by saving the following YAML to a file named
dv-iso-upload.yaml. Adjust thestoragesize based on the actual ISO file size. -
Execute the following command to create the DataVolume.
-
Wait for the DataVolume to enter the
UploadReadyphase. -
Set up port-forwarding to the CDI upload proxy.
-
Obtain an authentication token.
-
Upload the ISO file using curl. Replace the file path with the actual path to your ISO.
Note: The
--insecureflag is used to skip self-signed certificate verification. In a production environment, configure proper certificates. Large file uploads may take a long time; ensure network stability. -
Verify that the DataVolume status changes to
Succeeded.
Create Virtual Machine
-
Enter the Container Platform.
-
Click Virtualization > Virtual Machines in the left navigation bar.
-
Click Create Virtual Machine.
-
Fill in the parameters on the form page as follows. For specific parameters and configurations, please refer to Create Virtual Machine.
-
Switch to YAML.
-
Replace the configuration under the spec.template.spec.domain.devices.disks field with the following content. The ISO DataVolume is mounted as a CDROM with the highest boot priority, and the rootfs disk is used as the installation target.
-
Replace the spec.template.spec.volumes field with the following content. The ISO is referenced as a DataVolume instead of a container image.
-
Review the YAML file; the complete YAML configuration after completion is as follows.
-
Click Create.
-
Click Actions > VNC Login.
-
When prompted with press any key boot from CD or DVD, press any key to enter the installation program; if you do not see the prompt, click Send Remote Command in the upper left corner of the page, and then click Ctrl-Alt-Delete from the dropdown menu to reboot the server.
Note: If a message appears at the top of the virtual machine detail page stating Current virtual machine has configuration changes that require a restart to take effect. Please restart., you can ignore this message; a restart is not necessary.
Install Linux Operating System
-
After entering the installation page, follow the installation guide to proceed. This document gives an example of installing the Ubuntu operating system; the configuration items during the installation process of different operating systems are generally similar, and thus will not be elaborated further. Some configuration items are explained below.
-
Wait for the installation to complete.
Modify YAML File
-
Enter the Container Platform.
-
In the left navigation bar, click Virtualization > Virtual Machines.
-
Click on the Virtual Machine Name in the list to enter the details page.
-
Click Stop.
-
Click Actions > Update in the upper right corner.
-
Switch to YAML.
-
Confirm that the disk named rootfs under spec.template.spec.domain.devices.disks has a bootOrder of 1. If it is not 1, modify it to 1.
-
Delete the relevant content for the ISO disk named iso-disk under spec.template.spec.domain.devices.disks; the specific content to delete is as follows.
-
Delete the relevant content for the volume named iso-disk under spec.template.spec.volumes; the specific content to delete is as follows.
-
Click Update.
-
Click Start.
Install Required Software and Modify Configuration
Note: The following commands and configuration files may vary slightly between different operating systems; please adjust according to your actual environment.
-
Enter your username and password to log in to the operating system.
-
Switch to root user privileges.
-
Install the software packages.
-
For CentOS series, execute the command:
-
For Debian series, execute the command:
-
-
Edit the SSHD configuration file.
-
Execute the following command to edit the sshd_config file.
-
Add the following configurations.
-
Save the modified configuration.
-
-
Execute the following command to delete the default password for the root user.
-
Modify the source address file.
-
Execute the following command to modify the system's source address file and change the address to a suitable mirror site address.
-
Save the configuration after modifications.
-
-
Modify the cloud-init configuration to automatically expand the root directory.
-
Execute the following command to edit the cloud.cfg configuration file.
-
Add the following configuration content.
-
Save the configuration after modifications.
-
-
After completing the configuration, shut down the operating system.
Export and Use the Custom Linux Image
For specific operations, please refer to Export Virtual Machine Image.