Managing virtual machines in KVM using CLI

In our earlier tutorial, we discussed ‘Virtualization in Linux using KVM‘ & created a virtual machine using graphical user interface. In this tutorial, we will learn to create & manage virtual machines using Command line interface (CLI). The CLI tool to create new machine is called ‘virt-install’ & for managing KVM based Virtual machines in CLI using command line tool is ‘virsh’.

Creating new virtual machine

To create a new virtual machine in CLI using ‘virt-install’, run the following command

$ virt-install –name=Centos7 –disk path=/mnt/Centos7/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 ( i.e. 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.

Once the above command has been executed, a popup window (virt-viewer window) will open. The window is used to communicate with our Virtual machine & we will use it to complete the installation of operating system on KVM.

Managing Virtual machines

After a new virtual machine has been created, next thing that we need to do is to manage. & to manage a virtual machine in CLI ‘virsh’ command is used. To view all the commands that can be executed with virsh, run

$ virsh help

& we will have list of all the commands that can be used with virsh. Some the examples of important virsh commands are listed below.

List all VMs

To list all VMs that have been installed under KVM, run

$ virsh list –all

Id Name State

—————————————————-

1 Centos 7 running

2 Win_mach running

Starting a VM

To start a VM, run the following command

$ virsh start Centos7

here Centos7 is the name of the virtual machine that we want to run.

Shutting a VM

For shutting down a VM, run the following

$ virsh shutdown Centos7

This will shutdown VM in orderly manner. But if we want to instantly shutdown a VM, run

$ virsh destroy Centos7

It will stop VM instantly but it might cause damage to VM.

Rebooting a VM

For rebooting VM, run

$ virsh reboot Centos7

Suspending a VM

For suspending a VM, command is

$ virsh destroy Centos7

Resuming a VM

For resuming a VM after it has been suspended, run

$ virsh resume Centos7

Note :- Once the host has been rebooted, suspended state of all the VMs will be lost.

Saving a system State

To save the current state of a VM, run the following

$ virsh save Centos7 /VM/new

Where, /VM/new 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 /VM/new

A saved state can be restored even after a host system reboot.

Connecting to VM console

$ virsh console Centos7

To exit out of console. Press ’ctrl+j’.

Displaying VM information

To display complete VM information, run

$ virsh console Centos7

Displaying VM resources i.e. memory & cpu usage

To see the memory & cpu being used by VM, run

$ virt-top

 

These were some the important regularly used commands for managing VM using ‘virsh’. If you command you are looking for is not among the mentioned examples use ‘virsh help’.

Shujat Husain

A continuous learner with several years of experience in linux environment. I love to read, learn & write about Linux & new technologies. And I also write at linuxtechlab.com

What is in your mind, about this post ? Leave a Reply

Close
  Our next learning article is ready, subscribe it in your email

What is your Learning Goal for Next Six Months ? Talk to us