Managing KVM on RHEL 6 using the virsh Command-line Tool

From Techotopia
Jump to: navigation, search
PreviousTable of ContentsNext
Configuring a New RHEL 6 KVM Virtual NetworkManaging and Monitoring RHEL 6 based KVM Guest Systems


Purchase and download the full PDF and ePub versions of this RHEL 6 eBook for only $9.99
PDF/ePub edition contains 40 chapters and over 250 pages.

Buy eBook

In previous chapters we have covered the steps necessary to install and configure KVM based guest operating systems on Red Hat Enterprise Linux 6. This chapter is dedicated to explaining the virsh tool, 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 a shell environment. When used in the command line mode, the command is simply issued at the command prompt with sets of arguments appropriate 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:

virsh list

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:

# 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, for example the following virsh session lists the current virtual machines, starts a virtual machine named MyNewVM 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
----------------------------------
  2 Windows7             running
  3 myWin7               running

virsh # start MyNewVM
Domain MyNewVM started

virsh # list
 Id Name                 State
----------------------------------
  2 Windows7             running
  3 myWin7               running
  4 MyNewVM              running

virsh#

The virsh tool supports a wide range of commands, a full 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>

  DESCRIPTION
    Restore a domain.

  OPTIONS
    [--file] <string>  the state to restore

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

su -
virsh list

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

Id Name                 State
----------------------------------
  2 Windows7             running
  3 myWin7               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

Connecting to a Running Guest System

Once the guest operating system has started, a connection to the guest may be established using either the vncviewer tool or the virt-manager console. To use virt-manager, select Applications -> System Tools -> Virtual Machine Manager, select the desired system and click Open.

To connect using vncviewer to the default virtual machine, enter the following command in Terminal window:

vncviewer

When prompted for a server enter localhost:5900. A VNC window will subsequently appear containing the running guest system. If you have multiple virtual machines running, the VNC port of the virtual machine may be identified using the virsh command:

virsh vncdisplay WindowsVM
:4

In order to connect to the console for the WindowsVM, therefore, the following command could be used:

vncviewer :4

Shutting Down a Guest System

The shutdown option of the xm tool is used to shutdown a guest operating system:

virsh shutdown guestName

where guestName is the name of the guest system, to be shutdown.

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 filesystem damage and data loss):

virsh destroy myGuestOS

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 named myXenGuest:

virsh suspend myGuest

Similarly, to resume the paused system:

virsh resume myGuest

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:

virsh save myGuestOS path_to_save_file

To restore a saved guest operating system session:

virsh restore path_to_save_file

Rebooting a Guest System

To reboot a guest operating system:

virsh reboot myGuestOS

Configuring the Memory Assigned to 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 named myGuestOS to 256Mb:

virsh setmem myGuestOS 256

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


Purchase and download the full PDF and ePub versions of this RHEL 6 eBook for only $9.99
PDF/ePub edition contains 40 chapters and over 250 pages.

Buy eBook



PreviousTable of ContentsNext
Configuring a New RHEL 6 KVM Virtual NetworkManaging and Monitoring RHEL 6 based KVM Guest Systems