Managing KVM based Virtual machines : Using CLI

Last updated on March 12th, 2018 at 09:29 pm

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

$ 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 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.

 

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

$ visrsh 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 orderly manner. To instantly shutdown 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, command is

$ virsh suspend win7

 

Resuming a VM

For resuming a VM, command is

$ virsh resume win7

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

$ virsh save win7 /data/vm

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

$ 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 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 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.

 

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Donate us some of you hard earned money:

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply