How to use the command 'virsh' (with examples)
virsh
is a command-line interface tool for managing virtual guests and hypervisors. It provides a comprehensive suite of commands for controlling both the state and configuration of virtual machines. Operating over Libvirt, an open-source API for managing platform virtualization, virsh
is widely used by system administrators to handle tasks such as life cycle management, storage management, and network configuration for VMs efficiently.
Connect to a hypervisor session
Code:
virsh connect qemu:///system
Motivation:
Connecting to a hypervisor session is a critical first step when managing virtual machines with virsh
. The connect
command allows users to open a session with a specified hypervisor, giving them the ability to manage and manipulate virtual machines hosted on that hypervisor. This is especially important in environments where multiple hypervisors may be in use, as it enables targeted management and control.
Explanation:
qemu:///system
: This URI specifies the hypervisor to connect to.qemu
is a popular hypervisor, andsystem
denotes a system-wide session. The triple forward slashes (:///
) are part of the URI syntax for specifying system-level connections.
Example Output:
Connecting to uri: qemu:///system
List all domains
Code:
virsh list --all
Motivation:
Listing all domains provides a snapshot of both active and inactive virtual machines under management. This overview is essential when performing administrative tasks, as it allows users to quickly ascertain the current state of all virtual machines and plan next steps, such as starting or stopping guests as required.
Explanation:
--all
: This flag instructsvirsh
to display both running and halted virtual machines, offering a complete list as opposed to only those that are currently active.
Example Output:
Id Name State
----------------------------------
1 vm1 running
- vm2 shut off
- vm3 paused
Dump guest configuration file
Code:
virsh dumpxml guest_id > path/to/guest.xml
Motivation:
Dumping the configuration file of a guest domain is a crucial task for backup and replication purposes. By obtaining the XML configuration file, administrators can inspect, archive, or duplicate the settings of a virtual machine to restore or recreate it later with accuracy.
Explanation:
guest_id
: This represents the identifier for the virtual machine, which can be its ID, name, or UUID.>
: This redirection operator sends the output of the command to a specified file.path/to/guest.xml
: The file path where the configuration will be saved, allowing for later reference or use.
Example Output:
Saved guest configuration to path/to/guest.xml
Create a guest from a configuration file
Code:
virsh create path/to/config_file.xml
Motivation:
Creating a guest from a configuration file is useful when replicating standardized setups across multiple environments. It allows administrators to rapidly instantiate new virtual machines using predefined settings, aiding in workflows like setting up development or testing environments consistently.
Explanation:
path/to/config_file.xml
: This is the path to the XML configuration file that contains the necessary settings for creating the virtual machine. It providesvirsh
with all required specifications to spin up the VM.
Example Output:
Domain created from path/to/config_file.xml
Edit a guest’s configuration file
Code:
virsh edit guest_id
Motivation:
Editing the configuration of a virtual machine allows administrators to adjust its settings without recreating it from scratch. This might include changing system resources, modifying network settings, or updating security configurations. The edit
command provides a straightforward interface for making such modifications directly.
Explanation:
guest_id
: This indicates the virtual machine to be edited. The ID can be the machine’s ID number, name, or UUID.- The command will open the VM’s configuration in a text editor, defaulting to the one set in the
$EDITOR
environmental variable.
Example Output:
Editing configuration for guest: guest_id
Start/reboot/shutdown/suspend/resume a guest
Code:
virsh command guest_id
Motivation:
Managing the lifecycle of a virtual machine is a frequent administrative task. Whether starting, rebooting, shutting down, suspending, or resuming a guest, these actions control VM operation and availability. They are critical for maintaining system performance and scheduling maintenance windows.
Explanation:
command
: This placeholder can be replaced with specific lifecycle actions, such asstart
,reboot
,shutdown
,suspend
, orresume
.guest_id
: As with other commands, this specifies the target virtual machine for the given action, identified by its ID, name, or UUID.
Example Output (for shutdown
):
Guest guest_id is shutting down.
Save the current state of a guest to a file
Code:
virsh save guest_id filename
Motivation:
Saving the state of a virtual machine to a file is essential for snapshotting its current condition. This process is useful for later restoration, allowing the VM to resume from the exact point it was saved. Such capability is invaluable during system updates or before making significant configuration changes.
Explanation:
guest_id
: Identifies the virtual machine whose state will be saved.filename
: Represents the file path where the current VM state will be saved. This allows for the VM state to be resumed later.
Example Output:
Domain guest_id state saved to filename
Delete a running guest
Code:
virsh destroy guest_id && virsh undefine guest_id
Motivation:
Deleting a running guest is a two-step process involving both stopping the virtual machine and removing its associated configuration. This might be necessary when decommissioning a VM permanently to free up resources or comply with new organizational policies.
Explanation:
destroy
: This command halts the virtual machine forcefully, akin to pulling the power plug on a physical machine.undefine
: This subsequent command removes the VM’s configuration from the hypervisor.guest_id
: Indicates the VM to be deleted, using its ID, name, or UUID.
Example Output:
Domain guest_id destroyed
Domain guest_id undefined
Conclusion:
The virsh
command offers a powerful suite for direct interaction with virtual machine environments via the command line. With its ability to manage the entire lifecycle of VMs, virsh
remains a vital tool for administrators in dynamic and diverse virtualized environments. Each use case underscores the flexibility and capability of virsh
in managing various tasks efficiently, simplifying the complex processes involved in virtualization management.