alphaspirit - Fotolia


The top 20 virsh commands for hypervisor and VM management

There are so many virsh commands that it can be hard to know which ones are best suited to your VM management needs. With this list, you have a great place to start.

Managing your hypervisor and VMs is made much easier with the virsh command-line tool, which is built on the open source libvirt management API. With virsh commands, you can create, edit, migrate and suspend VMs, among a host of other options. There are so many commands, in fact, that they're broken up into categories like domain commands, storage pool commands and snapshot commands, just to name a few. With a variety of commands at your fingertips, how do you know which ones are most important to effective data center management? Here is a list of the heavy hitters to get you started:

Generic virsh commands

Commands in this category aren't specific to a VM, but will help with general management tasks.

help: Get a complete list of the available virsh commands, grouped into different categories. Specify a group to narrow down the list and see short descriptions for each, or specify a specific command to get more detailed information about it, including the name, synopsis, description and options.

list: You can use this command to obtain information about existing VMs and their current states. Depending on what you're looking for, you can specify --inactive or --all. This will return the ID name and state. Possible states include running, paused and crashed.

connect: Connect locally to the hypervisor with this command. You can also gain remote access through uniform resource identifiers. Common values supported include xen:///, which is the default, qemu:///system, qemu:///session and lxc:///. To establish a read-only connection, add --readonly to the command.

Domain commands

Manipulate the specified VM directly with these virsh commands.

desc: Display or change the description and title of a VM. Flags include --live, --config, --edit and --title. Note that if you use both --live and --config, the --config flag will take precedence. Also, it's recommended that you keep the VM title short, but that's not a hard and fast rule.

save: This command stops the VM and saves the data to a file. This frees up the memory previously allocated to the VM, since it's no longer running on the system. To display the progress of the save process, use --verbose. To restore a previously saved VM, use the restore command.

setmem: You can change the memory allocation for your VMs with this command, but be sure the count is in kilobytes. By using setmaxmem, you can change the maximum memory allocation for your VMs. The --config, --live and --current flags are potential specifications for both setmem and setmaxmem.

migrate: Migrate a VM to another host, with the option to live migrate or direct migrate, among others. It's important to note that individual hypervisors might not support all of the migration types. When live-migrating a VM, set a maximum downtime with migrate-setmaxdowntime.

undefine: This command converts a running VM to a transient VM without stopping it. If the VM is inactive, the command will remove its configuration. You can also include various flags, such as --managed-save, --snapshots-metadata, --storage, --remove-all-storage and --wipe-storage.

dump: Create a dump log file for a VM so any errors can be diagnosed. Add --live if you want the VM to run until the dump has been completed, as opposed to it being paused up front. With --crash, the VM is stopped and its status changed to crashed. Add --reset to reset the VM after the dump.

shutdown: Gracefully shut down a VM. This command is safer to use than the destroy command, which is only recommended when the VM is unresponsive, because it can result in corrupted file systems. You can choose a different shutdown method than the default with the --mode parameter.

Storage pool commands

Commands in this category are used to manipulate storage pools.

pool-list: Obtain a list of active pool objects. Filter by specifying --persistent, --transient, --autostart or --no-autostart. In addition, you can list pools with specific types. If you want to pull a list of inactive pools, use --inactive. To get a complete list, use --all.

pool-build: You can build a given pool with this command. Options include --overwrite and --no-overwrite. If you specify --overwrite, existing data on the target device is overwritten, and if you specify --no-overwrite, you'll receive an error if a filesystem exists on the target device.

pool-edit: This command allows you to edit the XML configuration of a storage pool in your default text editor. It's helpful in that it will do some error checking for you.

Volume commands

Manage storage volumes with these virsh commands.

vol-create: Create a volume from an XML file or command-line arguments. More specifically, you can create a volume using another volume as input with vol-create-from, and you can create a volume from a set of arguments with vol-create-as. You can also set the allocation size and volume file format.

vol-resize: This command resizes the given volume in bytes. Capacity will be the new size, but there is the option to add to the existing size by using the --delta command extension. It's important to note that vol-resize is not safe to use for active VMs, but you can use the blockresize command for live resizing, if need be.

vol-wipe: Wipe the volume and ensure data that was previously on it isn't accessible anymore. This is especially helpful when there is confidential information on the VM. With the algorithm switch, you can choose an alternate wiping algorithm, rather than rewriting the volume with zeros.

Snapshot commands

Commands in this category can manipulate VM snapshots.

snapshot-list: You can obtain a list of all the available snapshots for a VM with this command. The list will include the snapshot name, the time it was created, and the state of the VM. There are also many filters you can add to the list, such as --from, --leaves, --metadata, --inactive and --internal.

snapshot-create: Create a VM snapshot with the name, description and disks specified in the XML file. If you would like to create a snapshot without using the properties specified in the XML file, use snapshot-create-as. With the --halt specification, leave the VM inactive after the snapshot is created.

snapshot-revert: This command allows you to revert the VM to a specific snapshot. To revert to the current snapshot, use --current. The state of the VM will be the same as it was at the time the snapshot was taken and any changes since that snapshot was taken will be lost.

snapshot-delete: You can delete a specific snapshot with this command or you can delete the current snapshot by adding --current. Add --children if you want to delete the snapshot and its children or point-in-time copies. By including --children-only, you can delete the snapshot's children, leaving the actual snapshot intact.

This is by no means an all-encompassing list. You're sure to find other extremely useful virsh commands and multiple specifications for them in each of the above categories. Some of the categories not mentioned, but worth looking into, include: device commands, nodedev commands, virtual network commands, interface commands, secret commands, nwfilter commands and qemu-specific commands. When it comes to successfully managing your hypervisor and VMs, it's important to know all of your options, their limitations and their potential outcomes.

Next Steps

Solve VM overprovisioning problems

Manage storage for your VMs

Make VM maintenance a priority

Dig Deeper on Open source virtualization