Wondering how to manage KVM guest virtual machines using virsh commands? We can help you.
Virsh is a management user interface for virsh guest domains. It can be used to create, pause, restart, and shutdown domains. In addition, it can be used to list current domains available in our Virtualization hypervisor platform.
Virsh interacts with Libvirt which is a library aimed at providing a long-term stable API. It currently supports Xen, QEMU, KVM, LXC, OpenVZ, VirtualBox, and VMware ESX.
Here at Bobcares we often use virsh commands to manage KVM guest virtual machines of our customers.
Today let’s see how our Support Engineers use Virsh commands as a part of our Server Management Services.
Various commands to Manage KVM guest virtual machines using Virsh
The basic structure of most virsh usage is given below:
virsh [OPTION]... <command> <domain> [ARG]...
Display node information
To display the host node information and the machines that support the virtualization process, we can use the following command:
$ sudo virsh nodeinfo
List all domains
To list both inactive and active domains, we can use the following command:
$ sudo virsh list --all
List only active domains
For listing only active domains we can use the following command:
$ sudo virsh list
To start a VM
For starting a VM we can use the following command by replacing ‘domain_name‘ with the required domain name:
$ sudo virsh start domain_name
$ sudo virsh list
Autostart VM
To set a VM to start automatically on system startup, we can use the following command:
$ sudo virsh autostart domain_name
$ sudo virsh dominfo domain_name
Autostart disable
To disable autostart feature for a VM, we can use the following command:
$ virsh autostart --disable domain_name
$ virsh dominfo domain_name
Stop/Shutdown VM
To shut down a running VM gracefully, we can use the following command:
$ sudo virsh shutdown domain_name
$ sudo virsh list
Force shutdown VM
To perform a forceful shutdown of the active domain we can use the following command:
$ sudo virsh destroy domain_name
Stop all running VMs
To shut down all running domains, we can use the following command:
$ for i in `sudo virsh list | grep running | awk '{print $2}'` do
sudo virsh shutdown $i
done
Reboot VM
To restart a VM we can use the following command:
$ sudo virsh reboot domain_name
Remove VM
To cleanly remove a VM including its storage columns, we can use the following commands:
$ sudo virsh destroy domain_name 2> /dev/null
$ sudo virsh undefine domain_name
$ sudo virsh pool-refresh default
$ sudo virsh vol-delete --pool default doamin_name.qcow2
Create a VM
To create a new virtual machine with virsh, we can use the following command:
$ sudo virt-install
Connect to VM console
To connect to the guest console, use the command:
$ sudo virsh console domain_name
Edit VM XML file
To edit a VM XML file, we can use the following command:
# To use vim text editor
$ sudo EDITOR=vim virsh edit domain_name
# To use nano text editor
$ sudo EDITOR=nano virsh edit domain_name
Suspend/Resume VM
To suspend a guest we can use the following command:
$ sudo virsh suspend domain_name
Resuming a guest VM
To restore a suspended guest VM, we can use the following command:
$ sudo virsh resume domain_name
Save VM
To save the current state of a VM to a file, we can use the following command:
$ sudo virsh save domain_name domain_name.save
$ ls -l domain_name.saved
Restoring a saved VM
To restore the saved VM from the file, we can use the following command:
$ virsh restore domain_name.save
$ sudo virsh list
Managing Volumes using virsh
Now let us see how to create a storage volume, attach it to a VM, detach it from a VM and how to delete a volume.
Creating volume
To create a 2GB volume named domain_name_vol2 on the default storage pool, we can use the following command:
$ sudo virsh vol-create-as default domain_name_vol2.qcow2 2G
$ sudo du -sh /var/lib/libvirt/images/domain_name_vol2.qcow2
Attach a volume to VM
To attach created volume above to VM domain_name, we can use the following command:
# virsh attach-disk --domain domain_name \
--source /var/lib/libvirt/images/domain_name_vol2.qcow2 \
--persistent --target vdb
Detach volume on VM
To detach the above volume domain_name_vol2 from the VM domain_name, we can use the following command:
$ sudo virsh detach-disk --domain domain_name --persistent --live --target vdb
Delete volume
To delete volume, we can use the following commands:
$ sudo virsh vol-delete domain_name_vol2.qcow2 --pool default
$ sudo virsh pool-refresh default
$ sudo virsh vol-list default
[Need any further assistance with Virsh commands? – We can help you]
Conclusion
In short, we saw various commands that our Support Techs use to manage KVM guest virtual machines using virsh commands.
0 Comments