In previous chapters, we have covered the installation and configuration of KVM-based guest operating systems on CentOS Stream 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>
Code language: plaintext (plaintext)
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 CentOS Stream 9 Essentials. Buy the full book now in eBook or Print format.
Full book includes 34 chapters and 290 pages. Learn more. |
# virsh
Welcome to virsh, the virtualization interactive terminal.
Type: 'help' for help with commands
'quit' to quit
virsh #
Code language: plaintext (plaintext)
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#
Code language: plaintext (plaintext)
The virsh tool supports a wide range of commands, a complete listing of which may be obtained using the help option:
# virsh help
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
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 CentOS 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 CentOS Stream 9 Essentials. Buy the full book now in eBook or Print format.
Full book includes 34 chapters and 290 pages. Learn more. |
# virsh list
Code language: plaintext (plaintext)
The above command will display output containing a line for each guest similar to the following:
virsh # list
Id Name State
----------------------------------------------------
8 RHEL9VM running
9 CentOS9VM running
10 FedoraVM running
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
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 CentOS Stream 9 Essentials. Buy the full book now in eBook or Print format.
Full book includes 34 chapters and 290 pages. Learn more. |
# virsh suspend guestName
Code language: plaintext (plaintext)
Similarly, to resume the paused system:
# virsh resume guestName
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
To restore a saved guest operating system session:
You are reading a sample chapter from CentOS Stream 9 Essentials. Buy the full book now in eBook or Print format.
Full book includes 34 chapters and 290 pages. Learn more. |
# virsh restore path_to_save_file
Code language: plaintext (plaintext)
Rebooting a Guest System
To reboot a guest operating system:
# virsh reboot guestName
Code language: plaintext (plaintext)
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
Code language: plaintext (plaintext)
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.