This tutorial discusses how to manage KVM using CLI with the help of a utility called 'virsh'. In our last tutorial, we learned to install hardware based virtualization software named KVM and also created a Virtual machine in KVM using its graphical tool called virt-manager. We will learn to create new VM with ‘virt-install’ & managing KVM based Virtual machines in CLI using command-line tool named ‘virsh’.
Firstly, we will create a new Virtual machine using ‘virt-install’,
Creating a new virtual machine
Virt-install is a command-line tool used to create KVM based virtual machines. To create a new virtual machine, run the following command
$ virt-install --name=Win7 --disk path=/mnt/win7/Vol1.img --graphics spice --vcpu=2 --ram=2048 --location=/home/dan/Windows_7_pro.iso --network bridge=br0
Here virt-install is the command to create a new VM,
--name is the name of VM,
--disk path , is the storage path for VM,
--graphics, is the way to connect to VM (usually its spice),
--vcpu, number of CPUs for the VM,
--ram, is amount of memory for VM,
--location, the source path of installation ISO,
--network, is the bridge adapter to be used by VM>
After successful execution of this command, a new window (virt-viewer popup window) will launch where you can communicate with your VM & continue the installation of OS on KVM.
Managing KVM
Now that the machine has been created, we will learn managing KVM based VMs via CLI using ‘virsh’. Virsh supports a large number of commands, to view all of them use
$ virsh help
& you will have all the commands that can be used with visrsh. Some the important commands are listed here in tutorial as well.
Listing all VMs
To list all VMs that have been installed under KVM, use the following command,
$ virsh list --all
Id Name State
----------------------------------------------------
1 ltechlab running
2 win7 running
Starting a VM
To start a VM, run the following commands,
$ virsh start VM_name
Where VM name , in our case, can be ltechlab or win7.
Shutting a VM
For shutting down a VM, run the following
$ virsh shutdown win7
This will shutdown VM in an orderly manner. To instantly shut down a VM, run the following
$ virsh destroy win7
It will stop VM instantly & might cause damage to VM.
Rebooting a VM
For rebooting VM, run
$ virsh reboot win7
Suspending a VM
For suspending a VM, the command is
$ virsh suspend win7
Resuming a VM
For resuming a VM, the command is
$ virsh resume win7
Note:- If the host is rebooted, the suspended state of the VM will be lost.
Saving a System State
To save the current state of a VM, run the following
$ virsh save win7 /data/vm
Where/data/vm is the destination where the state of the VM will be saved.
Restoring the state of a VM
A saved state of the VM can be restored using the following command
$ virsh restore /data/vm
A saved state can be restored even after a host system reboot.
Connecting to VM console
$ virsh console win7
To exit out of the console. Press ’ctrl+j’.
Displaying VM information
To display complete VM information, run the following command
$ virsh dominfo win7
Displaying VM memory & CPU usage
To see the memory & CPU being used by VM, run
$ virt-top
Guys these were some commands that are used to manage KVM using CLI in CentOS. If having any doubts or queries, please use the comment box down below & we will surely get back to you.
In the Managing KVM heading, virsh is misspelled.
Thanks, have corrected it.