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.
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.
-
A repository for storing images should be prepared in advance. This document uses the build-harbor.example.cn repository as an example; please replace it according to your actual environment.
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
Convert Linux ISO Image into Docker Image
-
Navigate to the directory where the ISO image is stored and execute the following command in the terminal to rename the ISO image to ubuntu.iso.
-
Create a Dockerfile by executing the following command.
-
Edit the Dockerfile, add the following content, and save it.
-
Build the Docker image by executing the following command.
-
Push the image to the repository by executing the following command.
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.
-
Add the following content under the spec.template.spec.volumes field.
-
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 Windows 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 disk named containerdisk under spec.template.spec.domain.devices.disks; the specific content to delete is as follows.
-
Delete the relevant content for the disk named containerdisk 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.