20 Virsh Commands for Managing VMs | Virsh CLI Guide

Category : Linux/ Unix

Virsh Useful Commands for Managing Virtual Machines

virsh

Introduction

Virsh is a powerful command-line utility for managing virtual machines (VMs) that are controlled by libvirt, the virtualization management library. It provides a wide range of commands to create, edit, monitor, and control VMs running on hypervisors like KVM, Xen, and more. In this blog post, we’ll explore some of the most useful virsh commands that every system administrator should know.

Listing and Monitoring VMs

To get started, let’s look at some basic commands for listing and monitoring VMs:

  1. virsh list – Lists all running VMs. Add the --all option to include inactive VMs as well.
Example:
virsh list --all
  1. virsh dominfo VM_NAME – Displays detailed information about a specific VM, including its state, CPU/memory usage, and more.
Example:
virsh dominfo
  1. virsh domstate VM_NAME – Shows the current state of a VM (running, paused, shut off, etc.).
Example:
virsh domstate
  1. virsh domstats VM_NAME – Provides statistics about a VM’s CPU, memory, disk, and network usage.
Example:
virsh domstats

Controlling VM State

Virsh allows you to easily control the state of your VMs with the following commands:

  1. virsh start VM_NAME – Starts an inactive VM.
Example: 
virsh start
  1. virsh shutdown VM_NAME – Gracefully shuts down a running VM.
Example:
  1. virsh destroy VM_NAME – Forcefully stops a VM, similar to pulling the power plug. Use with caution!
Example:
virsh destroy
  1. virsh reboot VM_NAME – Reboots a running VM.
Example:
virsh reboot
  1. virsh suspend VM_NAME – Suspends a running VM, saving its state to disk.
Example:
virsh suspend
  1. virsh resume VM_NAME – Resumes a suspended VM.
Example:
virsh resume

Editing VM Configuration

To modify the configuration of a VM, you can use the following commands:

  1. virsh edit VM_NAME – Opens the XML configuration file of a VM in your default text editor. Make sure to only edit VMs that are powered off.
Example:
virsh edit

This will open the XML configuration in a text editor.

  1. virsh setvcpus VM_NAME COUNT – Changes the number of virtual CPUs assigned to a VM. The VM needs to be powered off for this to take effect.
Example:
virsh setvcpus
  1. virsh setmem VM_NAME SIZE – Modifies the memory allocated to a VM in kilobytes. The new size cannot exceed the initial memory defined for the VM.
Example:
virsh setmem

Managing Storage and Networking

Virsh provides commands to manage storage and networking for your VMs:

  1. virsh attach-disk VM_NAME SOURCE_PATH TARGET_DEV – Attaches a new disk device to a VM.
Example:
virsh attach disk
  1. virsh detach-disk VM_NAME TARGET_DEV – Detaches a disk device from a VM.
Example:
virsh detach disk
  1. virsh attach-interface VM_NAME NETWORK – Attaches a new network interface to a VM.
Example:
virsh attach interface
  1. virsh detach-interface VM_NAME INTERFACE_TYPE – Detaches a network interface from a VM.
Example:
virsh detach interface

Additional Useful Commands in Virsh

Here are some more virsh commands that can be handy for managing your VMs:

  1. virsh qemu-monitor-command VM_NAME --hmp "block_resize drive-virtio-disk0 SIZE_IN_GB G" – Modifies the disk space allocated to a VM.
Example:
virsh qemu monitor
  1. virsh set-user-password VM_NAME USER_NAME NEW_PASSWORD – Changes the password of a user inside a VM, including the root user.
Example:
virsh set user password
  1. virsh set-guest-hostname VM_NAME NEW_HOSTNAME – Changes the hostname of a VM.
Example:
virsh set guest hostname
  1. virsh autostart --disable VM_NAME – Disables autostart for a VM, preventing it from automatically starting when the host boots.
Example:
virsh autostart disable
  1. virsh domexec VM_NAME COMMAND – Runs a command inside a VM.
Example:
virsh domexec
  1. The location of the log file for a specific VM: tail -f /var/log/libvirt/qemu/vm1.log
vm log file

Conclusion

Virsh is an essential tool for any system administrator managing virtual machines with libvirt. With the commands covered in this blog post, you can efficiently list, monitor, control, and modify your VMs from the command line. Mastering virsh will greatly simplify your virtualization management tasks and boost your productivity. Experiment with these commands in a test environment and refer to the virsh manual pages for more advanced usage and options.

Related Content: OpenVZ Useful Commands