Virtual machines are a great way to run multiple operating systems on one computer without having to deal with the hassle of dual-booting your computer and managing multiple bootloaders. Not to mention it also protects your hardware against unwanted failure and changes made by different operating systems.
That said, different types of virtual machines are managed differently and sometimes can be a bit too much for new users. To help you out, here’s a KVM cheatsheet with every Virsh command you need to know to manage KVM guest virtual machines.
Also read: ADB Commands Cheatsheet
Virsh Commands for KVM machines
Virsh is a management user interface for Virsh guest domains and can be used to create, pause, restart and shut down domains. The basic syntax for most Virsh commands is as follows.
virsh [option] [command] [domain] [arguements]
Command | Description |
---|---|
virsh nodeinfo | Displays the host node information and the machines that support virtualisation. |
virsh list –all | Lists all (inactive and active) domains. |
virsh list | Lists only active domains. |
virsh domrename (current name) (new name) | Renames the specified domain with the given new name. |
virsh edit domain | Edits the source file for the specific domain. |
virsh start (machine name) | Starts the specified virtual machine. |
virsh autostart (machine name) | Automatically starts the specified virtual machine on system boot. |
virsh autostart –disable (machine name) | Disables the autostart option for the specified virtual machine. |
virsh stop (machine name) virsh shutdown (machine name) | Shuts down the specified virtual machine. |
virsh destroy (machine name) | Forcefully shuts down the specified virtual machine. |
for i in `sudo virsh list | grep running | awk ‘{print $2}’` do sudo virsh shutdown $i | Stops all running virtual machines. |
virsh reboot (machine name) | Restarts the specified virtual machine. |
virsh undefine (machine name) –remove-all-storage | Removes the specified virtual machine along with its storage columns. |
virsh install (machine name) | Installs a new virtual machine. The command also accepts additional arguments to specify the name, OS, hardware and so on. |
virsh console (machine name) | Connects to the specified machine’s console. Use –force flag if an active session for the machine’s console already exists. |
export EDITOR=nano/vim virsh edit (machine name) | The first command specifies the editor and the virsh command opens the specified machine’s XML file for edits. |
virsh suspend (machine name) | Suspends the specified virtual machine. |
virsh resume (machine name) | Resumes the specified virtual machine. |
virsh save (machine name) filename.saved | Saves the current state of the specified machine to the specified file. |
virsh restore filename.save | Restores a machine from the saved file state. |
virsh vol-create-as (pool name) (volume name).qcow2 (size) | Creates a volume of the specified size in the default storage pool. |
virsh vol-list –pool default virsh vol-list –pool (pool name) | Lists all volumes according to the specified pool. |
virsh attach-disk –domain (machine name) \ –source (enter source) \ –persistent \ –target (target name) | Attaches a volume to the virtual machine. Source determines the volume path. Persistent makes live change persistent. Target specifies the disk device target, |
virsh detach-disk –domain (machine name) \ –persistent \ –target (target name) | Detaches a volume from the specified machine. |
virsh pool-list | Lists storage pools. |
virsh vol-delete (volume name).qcow2 –pool default | Deletes the specified volume from the default pool. |
virsh snapshot-create-as \ –domain (machine name)\ –name “Snapshot name” \ –description “Enter description” | Creates a snapshot of the specified machine. |
virsh snapshot-revert –domain (machine name) –snapshotname (Snapshot name) –running | Reverts the currently running snapshot. |
virsh snapshot-revert –domain (machine name) –snapshotname (Snapshot name) | Deletes the specified snapshot. |
virt-clone –connect qemu:///system \ –original (machine name)\ –name (clone machine name) \ –file path/to/filename.qcow2 | Clones the specified machine. |
virsh setvcpus –domain (machine name) –count (number of CPUs) –config | Adds additional CPUs to the specified machine. |
virsh setmaxmem (machine name) (memory size) –config | Sets the maximum memory of the specified machine. |
virt-ls -l -d (machine name) (drive directory) | Moves to the specified directory in the machine. |
virt-cat -d (machine name) (drive directory) | Uses Cat to open the specified file in the machine. |
virt-edit -d (machine name) (drive directory) | Edits the specified file in the machine. |
virt-df -d (machine name) | Displays the specified machine’s disk usage. |
sudo virt-filesystems -l -h -d (machine name) | Shows the file systems on the specified machine. Add –partition to see the partitions as well. |
virt-top | Shows stats of virtualised machines. |
sudo virt-log -d (machine name) | Shows system logs of the specified machine. |
Also read: Every Crosh command you need to know