Managing KVM using the RHEL 9 virsh Command-Line Tool

In previous chapters, we have covered the installation and configuration of KVM-based guest operating systems on RHEL 9. This chapter explores 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 issued at the command prompt with sets of arguments appropriate to the task.

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

# virsh <option>

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:

 

You are reading a sample chapter from Red Hat Enterprise Linux 9 Essentials. Buy the full book now in eBook or Print format.

Includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook Buy Print

 

# virsh
Welcome to virsh, the virtualization interactive terminal.
 
Type:  'help' for help with commands
       'quit' to quit
 
virsh #

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:

# virsh
Welcome to virsh, the virtualization interactive terminal.
 
Type:  'help' for help with commands
       'quit' to quit
 
virsh # list
 Id    Name                           State
----------------------------------------------------
 8     RHEL9VM                       running
 9     CentOS9VM                     running

virsh # start FedoraVM
Domain FedoraVM started
 
virsh # list
 Id    Name                           State
----------------------------------------------------
 8     RHEL9VM                       running
 9     CentOS9VM                     running
10     FedoraVM                      running
 
virsh#

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

# virsh help

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

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

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 9 virtualization host may be viewed at any time using the list option of the virsh tool. For example:

 

You are reading a sample chapter from Red Hat Enterprise Linux 9 Essentials. Buy the full book now in eBook or Print format.

Includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook Buy Print

 

# virsh list

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

irsh # list
 Id    Name                           State
----------------------------------------------------
 8     RHEL9VM                       running
 9     CentOS9VM                     running
10     FedoraVM                      running

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:

# virsh start myGuestOS

Shutting Down a Guest System

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

# virsh shutdown guestName

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

# virsh destroy guestName

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:

 

You are reading a sample chapter from Red Hat Enterprise Linux 9 Essentials. Buy the full book now in eBook or Print format.

Includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook Buy Print

 

# virsh suspend guestName

Similarly, to resume the paused system:

# virsh resume guestName

A suspended session will be lost if the host system is rebooted. Also, be aware that a suspended system continues to reside in memory. Therefore, 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:

# virsh save guestName path_to_save_file

To restore a saved guest operating system session:

 

You are reading a sample chapter from Red Hat Enterprise Linux 9 Essentials. Buy the full book now in eBook or Print format.

Includes 34 chapters and 290 pages. Learn more.

Preview  Buy eBook Buy Print

 

# virsh restore path_to_save_file

Rebooting a Guest System

To reboot a guest operating system:

# virsh reboot guestName

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:

# virsh setmem guestName 256

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 various 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.


Categories