Creating an Ubuntu 22.04 VM Using a Cloud Image
This guide will walk you through the steps to create an Ubuntu 22.04 virtual machine (VM) using a cloud image on a Fedora host. We will use the ubuntu-22.04-server-cloudimg-amd64.img that you have already downloaded.
Step 1: Prepare the Cloud Image
First, you should rename the downloaded cloud image to a .qcow2 extension to avoid confusion, as this is the format expected by QEMU/KVM
mv ubuntu-22.04-server-cloudimg-amd64.img ubuntu-22.04-server-cloudimg-amd64.qcow2
Step 2: Install Required Packages
Before proceeding, ensure that your system has the necessary packages installed. On Fedora, you can install cloud-utils and qemu-img which are required for working with cloud images
sudo dnf update
sudo dnf install cloud-utils qemu-img
Step 3: Set Up the VM Configuration
Create a configuration for your VM, including the name, username, and password. Replace the placeholders with your desired values
VM_NAME="ubuntu-22-cloud-image"
USERNAME="your_username"
PASSWORD="your_password"
Step 4: Convert and Resize the Image
Convert the cloud image to a QCOW2 format and resize it to the desired capacity. For example, to resize to 20GB
sudo mkdir /var/lib/libvirt/images/$VM_NAME
sudo qemu-img convert -f qcow2 -O qcow2 ubuntu-22.04-server-cloudimg-amd64.qcow2 /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2
sudo qemu-img resize /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2 20G
Step 5: Create Cloud-Init Configuration Files
Create a cloud-init configuration file to set up the initial settings of the VM, such as default user and password
echo "#cloud-config
system_info:
default_user:
name: $USERNAME
home: /home/$USERNAME
password: $PASSWORD
chpasswd: { expire: False }
hostname: $VM_NAME
ssh_pwauth: True" | sudo tee /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg
Generate an ISO with this configuration using cloud-localds
sudo cloud-localds /var/lib/libvirt/images/$VM_NAME/cloud-init.iso /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg
Step 6: Create the Virtual Machine
Use virt-install to create the VM with the cloud image and cloud-init configuration
sudo virt-install \
--name $VM_NAME \
--memory 1024 \
--disk /var/lib/libvirt/images/$VM_NAME/root-disk.qcow2,device=disk,bus=virtio \
--disk /var/lib/libvirt/images/$VM_NAME/cloud-init.iso,device=cdrom \
--os-type linux \
--os-variant ubuntu19.04 \
--virt-type kvm \
--graphics none \
--network network=default,model=virtio \
--import
Step 7: Clean Up
After the VM is successfully created, you can remove the temporary cloud-init configuration files
sudo rm /var/lib/libvirt/images/$VM_NAME/cloud-init.iso
sudo rm /var/lib/libvirt/images/$VM_NAME/cloud-init.cfg
Step 8: Access the Virtual Machine
Once the VM is up and running, you can access it using SSH. The IP address can be obtained using virsh commands
virsh net-dhcp-leases default
ssh $USERNAME@
Replace with the actual IP address of your VM.
Step 9: Additional Configuration (Optional)
If you need to make further changes to the VM's configuration, you can use virsh edit to modify its XML definition
sudo virsh edit $VM_NAME
Conclusion
You have now successfully created an Ubuntu 22.04 VM using a cloud image on your Fedora host. This VM can be used for development, testing, or any other purpose that requires an isolated Ubuntu environment. Remember to replace placeholders with your actual configuration values throughout the process.