Managing KVM based Virtual machines : Using CLI
Hello Linux-fanatics, 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. In this tutorial, 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 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
Here virt-install is the command to create 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 installation of OS on 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
& 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,
Id Name State
1 ltechlab running
2 win7 running
Starting a VM
To start a VM, run the following commands,
Where VM name , in our case, can be ltechlab or win7.
Shutting a VM
For shutting down a VM, run the following
This will shutdown VM in orderly manner. To instantly shutdown a VM, run the following
It will stop VM instantly & might cause damage to VM.
Rebooting a VM
For rebooting VM, run
Suspending a VM
For suspending a VM, command is
Resuming a VM
For resuming a VM, command is
Note :- If the host is rebooted, suspended state of the VM will be lost.
Saving a system State
To save the current state of a VM, run the following
Where, /data/vm is the destination where the state of the VM will be saved.
Restoring state of a VM
A saved state of the VM can be restored using the following command
A saved state can be restored even after a host system reboot.
Connecting to VM console
To exit out of console. Press ’ctrl+j’.
Displaying VM information
To display complete VM information, run the following command
Displaying VM memory & cpu usage
To see the memory & cpu being used by VM, run
Guys these were some commands that are used for managing a VM through CLi. If having any doubts or queries, please use comment box down below & we will surely get back to you.