11.2. Creating a virtual machine from a KVM guest image
Create a new VM from a KVM Guest Image by using the .qcow2 image that you created by using RHEL image builder. The customized image already has cloud-init installed and enabled.
Prerequisites
-
You created a
.qcow2image by using RHEL image builder. -
You have the
qemu-kvmpackage installed on your system. You can check if the/dev/kvmdevice is available on your system, and if virtualization features are enabled in the firmware. -
You have the
libvirtandvirt-installpackages installed on your system. -
You have the
genisoimageutility, which is provided by thexorrisopackage, installed on your system.
Procedure
-
Move the
.qcow2image that you created by using RHEL image builder to the/var/lib/libvirt/images/directory. Create a directory, for example,
cloudinitiso, and navigate to this newly created directory:$ mkdir cloudinitiso $ cd cloudinitisoCreate a file named
meta-data. Add the following information to this file:instance-id: citest local-hostname: vmnameCreate a file named
user-data. Add the following information to the file:# cloud-config user: admin password: password chpasswd: {expire: False} ssh_pwauth: True ssh_authorized_keys: - ssh-rsa AAA...fhHQ== your.email@example.comssh_authorized_keysis your SSH public key. You can find your SSH public key in~/.ssh/id_rsa.pub\.Use the
genisoimageutility to create an ISO image that includes theuser-dataandmeta-datafiles.# genisoimage -output cloud-init.iso -volid cidata -joliet -rock user-data meta-data I: -input-charset not specified, using utf-8 (detected in locale settings) Total translation table size: 0 Total rockridge attributes bytes: 331 Total directory bytes: 0 Path table size(bytes): 10 Max brk space used 0 183 extents written (0 MB)Create a new VM from the KVM Guest Image by using the
virt-installcommand. Include the ISO image you created in step 4 as an attachment to the VM image.# virt-install \ --memory 4096 \ --vcpus 4 \ --name myvm \ --disk rhel-10-x86_64-kvm.qcow2,device=disk,bus=virtio,format=qcow2 \ --disk cloud-init.iso,device=cdrom \ --os-variant rhel 10 \ --virt-type kvm \ --graphics none \ --import-
--graphicsnone means it is a headless RHEL 10 VM. -
--vcpus 4means that it uses 4 virtual CPUs. -
--memory 4096means it uses 4096 MB RAM.
-
The VM installation starts:
Starting install... Connected to domain mytestcivm ... [ OK ] Started Execute cloud user/final scripts. [ OK ] Reached target Cloud-init target. Red Hat Enterprise Linux 10 (Ootpa) Kernel 4.18.0-221.el8.x86_64 on an x86_64
Verification
After the boot is complete, the VM shows a text login interface. To log in to the local console of the VM, you can use user the details from the user-data file:
-
Enter
adminas a username and press . Enter
passwordas password and press .After the login authentication is complete, you have access to the VM by using the CLI.