Changes

Jump to: navigation, search

Managing KVM on RHEL using the virsh Command-Line Tool

6,740 bytes added, 19:59, 10 June 2019
Created page with "{{#pagetitle: Managing KVM on RHEL 8 using the virsh Command-Line Tool }} <table border="0" cellspacing="0" width="100%"><tr> <td width="20%">[[|Previous]]<td align="center">[..."
{{#pagetitle: Managing KVM on RHEL 8 using the virsh Command-Line Tool }}
<table border="0" cellspacing="0" width="100%"><tr>
<td width="20%">[[Previous]]<td align="center">[[Red Hat Enterprise Linux Essentials|Table of Contents]]<td width="20%" align="right">[[Next]]</td>
<tr>
<td width="20%"><td align="center"><td width="20%" align="right"></td>
</table>
<hr>


<htmlet>rhel8</htmlet>


In previous chapters we have covered the steps necessary to install and configure KVM-based guest operating systems on RHEL 8. This chapter is dedicated to exploring some additional areas of the ''virsh'' tool that have not been covered in previous chapters, and how it may be used to manage KVM-based guest operating systems from the command-line.

== The virsh Shell and Command-Line ==

The ''virsh'' tool is both a command-line tool and an interactive shell environment. When used in the command-line mode, the command is simply issued at the command prompt with sets of arguments appropriate to the task to be performed.

To use the options as command-line arguments, use them at a terminal command prompt as shown in the following example:

<pre>
# virsh &lt;option&gt;
</pre>

The ''virsh'' tool, when used in shell mode, provides an interactive environment from which to issue sequences of commands.

To run commands in the ''virsh'' shell, run the following command:

<pre>
# virsh
Welcome to virsh, the virtualization interactive terminal.
&#160;
Type: ‘help’ for help with commands
‘quit’ to quit
&#160;
virsh #
</pre>

At the ''virsh #'' prompt enter the options you wish to run. The following ''virsh'' session, for example, lists the current virtual machines, starts a virtual machine named FedoraVM and then obtains another listing to verify the VM is running:

<pre>
# virsh
Welcome to virsh, the virtualization interactive terminal.
&#160;
Type: ‘help’ for help with commands
‘quit’ to quit
&#160;
virsh # list
Id Name State
----------------------------------------------------
8 RHEL8VM running
9 CentOS7VM running

virsh # start FedoraVM
Domain FedoraVM started
&#160;
virsh # list
Id Name State
----------------------------------------------------
8 RHEL8VM running
9 CentOS7VM running
10 FedoraVM running
&#160;
virsh#
</pre>

The ''virsh'' tool supports a wide range of commands, a full listing of which may be obtained using the help option:

<pre>
# virsh help
</pre>

Additional details on the syntax for each command may be obtained by specifying the command after the help directive:

<pre>
# virsh help restore
NAME
restore - restore a domain from a saved state in a file
&#160;
SYNOPSIS
restore &lt;file&gt; [--bypass-cache] [--xml &lt;string&gt;] [--running] [--paused]
&#160;
DESCRIPTION
Restore a domain.
&#160;
OPTIONS
[--file] &lt;string&gt; the state to restore
--bypass-cache avoid file system cache when restoring
--xml &lt;string&gt; filename containing updated XML for the target
--running restore domain into running state
--paused restore domain into paused state
</pre>

In the remainder of this chapter we will look at some of these commands in more detail.

== Listing Guest System Status ==

The status of the guest systems on a RHEL 8 virtualization host may be viewed at any time using the ''list'' option of the ''virsh'' tool. For example:

<pre>
# virsh list
</pre>

The above command will display output containing a line for each guest similar to the following:

<pre>
virsh # list
Id Name State
----------------------------------------------------
8 RHEL8VM running
9 CentOS7VM running
10 FedoraVM running
</pre>

== Starting a Guest System ==

A guest operating system can be started using the ''virsh'' tool combined with the start option followed by the name of the guest operating system to be launched. For example:

<pre>
# virsh start myGuestOS
</pre>

== Shutting Down a Guest System ==

The ''shutdown'' option of the ''virsh'' tool, as the name suggests, is used to shutdown a guest operating system:

<pre>
# virsh shutdown guestName
</pre>

Note that the shutdown option allows the guest operating system to perform an orderly shutdown when it receives the shutdown instruction. To instantly stop a guest operating system the ''destroy'' option may be used (with all the attendant risks of file system damage and data loss):

<pre>
# virsh destroy guestName
</pre>

== Suspending and Resuming a Guest System ==

A guest system can be suspended and resumed using the ''virsh'' tool’s suspend and resume options. For example, to suspend a specific system:

<pre>
# virsh suspend guestName
</pre>

Similarly, to resume the paused system:

<pre>
# virsh resume guestName
</pre>

Note that a suspended session will be lost if the host system is rebooted. Also, be aware that a suspended system continues to reside in memory. To save a session such that it no longer takes up memory and can be restored to its exact state (even after a reboot), it is necessary to save and restore the guest.

== Saving and Restoring Guest Systems ==

A running guest operating system can be saved and restored using the ''virsh'' utility. When saved, the current status of the guest operating system is written to disk and removed from system memory. A saved system may subsequently be restored at any time (including after a host system reboot):

To save a guest:

<pre>
# virsh save guestName path_to_save_file
</pre>

To restore a saved guest operating system session:

<pre>
# virsh restore path_to_save_file
</pre>

== Rebooting a Guest System ==

To reboot a guest operating system:

<pre>
# virsh reboot guestName
</pre>

== Configuring the Memory Assigned to a Guest OS ==

To configure the memory assigned to a guest OS, use the ''setmem'' option of the ''virsh'' command. For example, the following command reduces the memory allocated to a guest system to 256Mb:

<pre>
# virsh setmem guestName 256
</pre>

Note that acceptable memory settings must fall within the memory available to the current Domain. This may be increased using the ''setmaxmem'' option.

== Summary ==

The ''virsh'' tool provides a wide range of options for creating, monitoring and managing guest virtual machines. As outlined in this chapter, the tool can be used in either command-line or interactive modes.

Navigation menu