Skip to main content
Previously, a minimal virtual machine was created without an attached virtual disk, meaning it couldn’t host an operating system. That setup was useful for demonstrating basic management commands using bash. In this guide, we will build a complete virtual machine—with an operating system installed—to simulate real-world scenarios and introduce additional commands.

Downloading a Disk Image

The first step is to download a disk image. Ubuntu provides pre-configured cloud images that are ideally suited for virtual machines. Although similar images power services like Google Cloud, we will download the minimal cloud image using wget. Before you proceed, note that you may have encountered alternative commands similar to:
For our purposes, we use wget:

Verifying the Integrity of the Image

To ensure that the downloaded image is authentic and uncorrupted, verify its checksum using the SHA-256 checksum provided on the Ubuntu page. Two files are released: SHA256SUMS (which contains checksums for all images) and SHA256SUMS.gpg (which is used to verify the checksum file itself). First, download the checksum file:
Now verify the checksum:
Since the output confirms “OK,” the image is intact and ready for use.

Inspecting and Resizing the Disk Image

Next, inspect the image using QEMU’s tool to examine file format, virtual size, and allocated disk space:
Although the virtual disk is set to 3.5 GiB, only 227 MiB is currently allocated. To install additional software, a larger disk is necessary. Expand the virtual disk to approximately 10 GiB with:
Even though the virtual size is now 10 GiB, physical disk usage only increases as the virtual machine utilizes the extra space.

Creating a Storage Pool and Copying the Disk Image

Virtualization tools typically use storage pools to store virtual disk images, snapshots, and other data. By default, the storage pool is located at /var/lib/libvirt. To confirm, check the directory contents:
Then, copy the disk image to the images subdirectory:

Creating a Virtual Machine with virt‑install

Now comes the more advanced step: creating a virtual machine and specifying the image file as the virtual disk. The virt-install utility makes this straightforward. If parameters are missing, an error will appear indicating that required options (such as the operating system name) must be provided. For example, running:
This error reminds you to specify the OS name. To list available operating system variants, use:
Scroll through the output to locate the variant you plan to use (in this case, Ubuntu 24.04) and note its identifier (for example, ubuntu24.04).

Examining the Manual

For additional command-line options, consult the manual:
Below is an excerpt from the manual detailing the creation of a VM from an existing disk image:
The image shows a manual page for the "virt-install" command, which is used to provision new virtual machines. It includes sections like NAME, SYNOPSIS, and DESCRIPTION, explaining its functionality and options.
Remember to include the --import option to bypass a new OS installation because the disk image already contains an operating system:
  • With --import, the first device specified via --disk or --filesystem serves as the boot device.
  • By default, virt-install would perform a full installation.

Reviewing Available Options

Run the following command to view all available options:
Key options include:
  • Name, Memory, and vCPUs: Assign a name to the VM, allocate memory (in megabytes), and set the number of virtual CPUs.
  • Disk: Specify the path to the existing disk image. For example:
  • Graphics: Use --graphics none to disable a graphical interface and run the VM solely via a text console.

Building and Running the Virtual Machine

Use the following complete command to create a VM running Ubuntu 24.04 with 1024 MiB memory, 1 vCPU, and the pre-copied cloud image while disabling graphical output:
When you run the command, you may observe output similar to this:
This output indicates that you are connected directly to the VM’s text console—similar to having a monitor and keyboard attached. To exit the console, press Control + the closing square bracket (Ctrl + ]). To list all virtual machines, run:
Shut down the VM by running:
If necessary, force a shutdown using:
Once shut down, you can remove the VM’s definition and all associated storage (including the copied image):

Recreating the Virtual Machine Using Cloud-Init

The Ubuntu cloud image includes cloud-init automation scripts that allow you to set a root password automatically. To recreate the VM with cloud-init that generates a random root password, append the --cloud-init root-password-generate=on option.
Once the installation begins and the VM boots, you will eventually see a login prompt on the console. The boot output (including kernel messages and cloud-init logs) will scroll until you reach:
Log in using “root” as the username, and paste the generated password (note that nothing appears as you paste). On first login, you will be required to change the password:
After updating the password, you’ll be logged into the Ubuntu operating system running on your virtual machine. Verify the virtual disk size is approximately 10 GiB:
Then update the package lists to ensure network connectivity:
To exit the VM’s console, press Control + ]. You can reconnect at any time with:
If no login prompt appears, simply press Enter.

Handling OS Variants with virt‑install

Sometimes the operating system inside a virtual disk image may be a newer or unlisted variant in the virt-install OS database. Here are some options to handle such cases:
  1. Use --osinfo detect=on to let virt-install automatically determine the OS type
Use --osinfo detect=on to let virt-install automatically determine the OS type.Example:
  1. Specify a Generic OS Variant:
    If you are sure the distribution is modern, you might use a generic identifier:
  2. Disable Strict OS Requirement:
    Use --osinfo detect=on,require=off or set the environment variable:
If you see an error similar to:
refer to the provided options.

Wrapping Up

In this guide, we covered the following steps:
  • Downloading and verifying an Ubuntu cloud image
  • Inspecting and resizing the virtual disk using QEMU
  • Setting up a storage pool by copying the image to /var/lib/libvirt/images/
  • Creating a virtual machine with virt-install, including essential options such as name, memory, vCPUs, disk, and graphics
  • Utilizing the --import option to deploy a guest from an existing disk image
  • Incorporating cloud-init to automatically generate a root password
  • Connecting to and managing the VM console using virsh
With these steps, you now have a fully functional virtual machine running Ubuntu 24.04. In the next lesson, we’ll explore additional configuration and management options.
The image shows a command-line interface with options and examples for creating a new virtual machine using specified install media. It includes parameters for configuring memory, vCPUs, CPU features, metadata, and XML options.

Watch Video