Changes

Created page with "{{#pagetitle: Creating KVM Virtual Machines on RHEL 8 with virt-install and virsh }} <table border="0" cellspacing="0" width="100%"><tr> <td width="20%">Creating KVM Virtual..."
{{#pagetitle: Creating KVM Virtual Machines on RHEL 8 with virt-install and virsh }}
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Creating KVM Virtual Machines on RHEL using Cockpit and virt-manager|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Creating a RHEL KVM Networked Bridge Interface|Next]]</td>
<tr>
<td width="20%">Creating KVM Virtual Machines on RHEL 8 using Cockpit and virt-manager<td align="center"><td width="20%" align="right">Creating a RHEL 8 KVM Networked Bridge Interface</td>
</table>
<hr>


<htmlet>rhel8</htmlet>


In the previous chapter we explored the creation of KVM guest operating systems on a RHEL 8 host using Cockpit and the ''virt-manager'' graphical tool. In this chapter we will turn our attention to the creation of KVM-based virtual machines using the ''virt-install'' and ''virsh'' command-line tools. These tools provide all the capabilities of the ''virt-manager'' and Cockpit options with the added advantage that they can be used within scripts to automate virtual machine creation. In addition, the ''virsh'' command allows virtual machines to be created based on a specification contained within a configuration file.

The ''virt-install'' tool is supplied to allow new virtual machines to be created by providing a list of command-line options. This chapter assumes that the necessary KVM tools are installed. For details on these requirements read the chapter entitled [[Installing KVM Virtualization on RHEL|Installing KVM Virtualization on RHEL 8]].

== Running virt-install to build a KVM Guest System ==

The ''virt-install'' utility accepts a wide range of command-line arguments that are used to provide configuration information related to the virtual machine being created. Some of these command-line options are mandatory (specifically name, memory and disk storage must be provided) while others are optional.

At a minimum, a ''virt-install ''command will typically need the following arguments:

* '''--name''' - The name to be assigned to the virtual machine.

* '''--memory''' - The amount of memory to be allocated to the virtual machine.

* '''--disk''' - The name and location of an image file to be used as storage for the virtual machine. This file will be created by ''virt-install'' during the virtual machine creation unless the '''--import''' option is specified to indicate an existing image file is to be used.

* '''--cdrom''' or '''--location''' - Specifies the local path or the URL of a remote ISO image containing the installation media for the guest operating system.

A summary of all the arguments available for use when using ''virt-install'' can be found in the man page:

<pre>
$ man virt-install
</pre>

== An Example RHEL 8 virt-install Command ==

With reference to the above command-line argument list, we can now look at an example command-line construct using the ''virt-install'' tool.

Note that in order to be able to display the virtual machine and complete the installation, a ''virt-viewer ''instance will need to be connected to the virtual machine after it is started by the ''virt-install'' utility. By default, ''virt-install ''will attempt to launch ''virt-viewer'' automatically once the virtual machine starts running. If ''virt-viewer ''is not available, ''virt-install'' will wait until a'' virt-viewer'' connection is established. The ''virt-viewer'' session may be running locally on the host system if it has a graphical desktop, or a connection may be established from a remote client as outlined in the chapter entitled [[Creating KVM Virtual Machines on RHEL using Cockpit and virt-manager|Creating KVM Virtual Machines on RHEL 8 using Cockpit and virt-manager]].

The following command creates a new KVM virtual machine configured to run Fedora 29 using KVM para-virtualization. It creates a new 10GB disk image, assigns 1024MB of RAM to the virtual machine and configures a virtual CD device for the installation media ISO image:

<pre>
virt-install --name MyFedora --memory 1024 --disk path=/tmp/myFedora.img,size=10 --network network=default --os-variant fedora29 --cdrom /tmp/Fedora-Server-dvd-x86_64-29-1.2.iso
</pre>

As the creation process runs, the ''virt-install'' command will display status updates of the creation progress:

<pre>
Starting install...
Allocating ‘MyFedora.img’ | 10 GB 00:00:01
Domain installation still in progress. Waiting for installation to complete.
</pre>

Once the guest system has been created, the ''virt-viewer'' screen will appear containing the operating system installer loaded from the specified installation media:


[[File:rhel_8_virt-install_viewer.png]]


Figure 22-1

From this point, follow the standard installation procedure for the guest operating system.

== Starting and Stopping a Virtual Machine from the Command-Line ==

Having created the virtual machine from the command-line it stands to reason that you may also need to start it from the command-line in the future. This can be achieved using the virsh command-line utility, referencing the name assigned to the virtual machine during the creation process. For example:

<pre>
# virsh start MyFedora
</pre>

Similarly, the virtual machine may be sent a shutdown signal as follows:

<pre>
# virsh shutdown MyFedora
</pre>

If the virtual machine fails to respond to the shutdown signal and does not begin a graceful shutdown the virtual machine may be destroyed (with the attendant risks of data loss) using the destroy directive:

<pre>
# virsh destroy MyFedora
</pre>

== Creating a Virtual Machine from a Configuration File ==

The ''virsh create'' command can take as an argument the name of a configuration file on which to base the creation of a new virtual machine. The configuration file uses XML format. Arguably the easiest way to create a configuration file is to dump out the configuration of an existing virtual machine and modify it for the new one. This can be achieved using the ''virsh dumpxml'' command. The following command outputs the configuration data for a virtual machine domain named MyFedora to a file named ''MyFedora.xml'':

<pre>
# virsh dumpxml MyFedora &gt; MyFedora.xml
</pre>

Once the file has been generated, load it into an editor to review and change the settings for the new virtual machine.

At the very least, the &lt;name&gt;, &lt;uuid&gt; and image file path &lt;source file&gt; must be changed in order to avoid conflict with the virtual machine from which the configuration was taken. In the case of the UUID, this line can simply be deleted from the file.

The virtualization type, memory allocation, number of CPUs to name but a few options may also be changed if required. Once the file has been modified, the new virtual machine may be created as follows:

<pre>
# virsh create MyFedora.xml
</pre>

== Summary ==

KVM provides the ''virt-install ''and ''virsh'' command-line tools as a quick and efficient alternative to using the Cockpit and ''virt-manager'' tools to create and manage virtual machine instances. These tools have the advantage that they can be used from within scripts to automate the creation and management of virtual machines. The ''virsh'' command also includes the option to create VM instances from XML-based configuration files.


<htmlet>rhel8</htmlet>


<hr>
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Creating KVM Virtual Machines on RHEL using Cockpit and virt-manager|Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Creating a RHEL KVM Networked Bridge Interface|Next]]</td>
<tr>
<td width="20%">Creating KVM Virtual Machines on RHEL 8 using Cockpit and virt-manager<td align="center"><td width="20%" align="right">Creating a RHEL 8 KVM Networked Bridge Interface</td>
</table>