RHEL/CentOS 7: How to Create a Guest in KVM


After you install and configure a KVM on the host system, you can create guest operating systems. Every guest is defined by a set of resources and parameters stored in the XML format. When you want to create a new guest, creating such an XML file is quite cumbersome. There are two ways to create a guest:

  • Using virt-manager
  • Using virt-install

While virt-manager is a GUI and not very well suited to automate things.

In this post, we will cover a generic approach to create a new virtual machine using the bridge-eth0 network bridge and create a virtual disk on the localfs-vm storage pool, which is formatted as QCOW2. The QCOW2 format is a popular virtual disk format as it allows thin provisioning and snapshotting. We will boot the RHEL 7 installation media located on the localfs-iso storage pool (rhel7-install.iso) to start installing a new RHEL 7 system.

Let’s create some guests and delete them.

Create a guest

Let’s first create a disk for the guest and then create the guest on this disk, as follows:

  1. Create a 10 GB QCOW2 format disk in the localfs-vm pool, as follows:
    ~]# virsh vol-create-as --pool localfs-vm --name rhel7_guest-vda.qcows2 --format qcows2 –capacity 10G
  2. Create the virtual machine and start it through the following command using backslash at the end of each line:
    ~]# virt-install \
    --hvm \
    --name rhel7_guest \
    –-memory=2048,maxmemory=4096 \
    --vcpus=2,maxvcpus=4 \
    --os-type linux \
    --os-variant rhel7 \
    --boot hd,cdrom,network,menu=on \
    --controller type=scsi,model=virtio-scsi \
    --disk device=cdrom,vol=localfs-iso/rhel7-install.iso,readonly=on,bus=scsi \
    --disk device=disk,vol=localfs-vm/rhel7_guest-vda.qcow2,cache=none,bus=scsi \
    --network network=bridge-eth0,model=virtio \
    --graphics vnc \
    --graphics spice \
    --noautoconsole \
    --memballoon virtio

Deleting a guest

At some point, you’ll need to remove the guests. You can do this as follows:

  1. First, ensure that the guest is down by running the following:
    ~]# virsh list –all
     Id    Name                           State
    -     rhel7_guest                     shut off

    If the state is not shut off, you can forcefully shut it down:

    ~]# virsh destroy --domain <guest name>
  2. List the storage volumes in use by your guest and copy this somewhere:
    ~]# virsh domblklist <guest name>
    Type       Device     Target     Source
    file       disk       vda        /vm/rhel7_guest-vda.qcow2
    file       cdrom      hda        /iso/rhel7-install.iso
  3. Delete the guest through the following command:
    ~]# virsh undefine --domain <guest name> --storage vda

    Adding --remove-all-storage to the command will wipe off the data on the storage volumes dedicated to this guest prior to deleting the volume from the pool.

The virt-install command supports creating storage volumes (disks) by specifying the pool, size, and format. However, if this storage volume already exists, the application will fail. Depending on the speed of your KVM host disks (local or network) and the size of the guest’s disks, the process of creating a new disk may take some time to be completed. By specifying an existing disk with virt-install, you can reuse the disk should you need to reinstall the guest. It would be possible to only create the disk on the first pass and change your command line appropriately after this. However, the fact remains that using virsh vol-create-as gives you more granular control of what you want to do.

We’re using the QCOW2 format to contain the guest’s disk as it is a popular format when it comes to storing KVM guest disks. This is because it supports thin provisioning and snapshotting.

When creating the guest, we specify both the maxmemory option for memory configuration and the maxvcpus option for vcpus configuration. This will allow us to add CPUs and RAM to the guest while it is running. If we do not assign these, we’ll have to shut down the system before being able to change the XML configuration using the following command:

~# virsh edit <hostname>

As you can see, we’re using the virtio driver for any hardware (network, disks, or balloon) that supports it as it is native to the KVM and is included in the RHEL 7 kernel.


If, for some reason, your guest OS doesn’t support virtio drivers, you should remove the --controller option of the command line and the bus specification from the --disk option.

For more information on virtio support, go to http://wiki.libvirt.org/page/Virtio.

The --memballoon option will ensure that we do not run into problems when we overcommit our memory. When specific guests require more memory, the ballooning driver will ensure that the “idle” guests’ memory can be evenly redistributed.

The graphics option will allow you to connect to the guest through the host using either VNC (which is a popular client to control remote computers) or spice (which is the default client for virt-manager). The configuration for both VNC and spice is insecure, though. You can either set this up by specifying a password—by adding password=<password> to each graphics stanza—or by editing the /etc/libvirt/qemu.conffile on the KVM host, which will be applied to all guests.

In this post, we used “local” install media in the form of an ISO image to install the system. However, it is also possible to install a guest without a CD, DVD, or an ISO image. The --location installation method option allows you to specify a URI that contains your kernel/initrd pair, which is required to start the installation.

Using --location in combination with --extra-args will allow you to specify kernel command-line arguments to pass to the installer. This can be used, for instance, to pass on the location of an Anaconda kickstart file for automated installs and/or specifying your IP configuration during the installer.


Please enter your comment!
Please enter your name here