How to Create and Manage KVM Virtual Machine through Command Line in Linux

To Create and Manage KVM Virtual Machine through Command Line in Linux

In this article we will discuss about creating and Managing virtual machine through command line.

To manage virtual guest machines we use “ virsh” command.

[root@linuxhelp ~]# man virsh

virt-install” command used to install the virtual machine.

[root@linuxhelp ~]# man virt-install

qemu-img” command is used for creating disk image.

[root@linuxhelp ~]# man qemu-img

Configure the Storage Pool before creating virtual machines, here we are going to store the image file for the virtual machines.
Now run the following command to define storage pool.

[root@linuxhelp ~]# virsh pool-define-as centosvm dir - - - - " /var/lib/libvirt/images/centosvm/" 
Pool centosvm defined

To view the list of created pool storage.

[root@linuxhelp ~]# virsh pool-list --all
Name State Autostart
-----------------------------------------
centosvm inactive no
default active yes

Now run the following command to build the created storage pool.

[root@linuxhelp ~]# virsh pool-build centosvm
Pool centosvm built

Activate the storage pool.

[root@linuxhelp ~]# virsh pool-start centosvm 
Pool centosvm started
[root@linuxhelp ~]# virsh pool-list --all
Name State Autostart
-----------------------------------------
centosvm active no
default active yes

To start the storage pool service automatically, run the autostart command.

[root@linuxhelp ~]# virsh pool-autostart centosvm
Pool centosvm marked as autostarted
[root@linuxhelp ~]# virsh pool-list --all
Name State Autostart
-----------------------------------------
centosvm active yes
default active yes

Now the storage pool is configured. Run the following command to view the information about the storage pool.

[root@linuxhelp ~]# virsh pool-info centosvm 
Name: centosvm 
UUID: 737f134e-ee87-489e-8326-a2c7eaea2526 
State: running 
Persistent: yes 
Autostart: yes 
Capacity: 17.55 GiB 
Allocation: 11.29 GiB 
Available: 6.26 GiB

Create Disk image for the virtual machine.

[root@linuxhelp ~]# qemu-img create -f raw /var/lib/libvirt/images/centosvm.img 10G
Formatting ' /var/lib/libvirt/images/centosvm.img' , fmt=raw size=10737418240

Create a new virtual machine through a command line using virt-install command.
Mention the below options to install the virtual machine.

--name : Name for the virtual machine.
--disk path : Location of disk image.
--graphics spice : To connect to graphical console “ spice”
--vpcu : Number of virtual cpu’ s.
--ram : Amount of ram to allocate for guest.
--location : source path of installation packages.
--network :For virtual network.


Choose the requirement for the installation and complete the process.

After the virtual machine is created run the following command to view the status of all virtual machines.

[root@linuxhelp ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     centos7                        running

Run the below command to edit the configuration file located in /etc/libvirt/qemu/centos7.xml

[root@linuxhelp ~]# virsh edit centos7

View the Virtual Machine’ s information, by running the following command.

[root@linuxhelp ~]# virsh dominfo centos7
Id:             2
Name:           centos7
UUID:           23af0e98-cc4b-4e27-a594-301137883bb8
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       177.5s
Max memory:     1048576 KiB
Used memory:    1048576 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c19,c176 (enforcing)

To display the CPU and Memory Usage for virtual machines.

[root@linuxhelp ~]# virt-top

Shutdown the virtual machine, by using the following command.

[root@linuxhelp ~]# virsh shutdown (or) turn off centos7
Domain centos7 is being shutdown

To reboot the virtual machine.

[root@linuxhelp ~]# virsh reboot centos7
Domain centos7 is being rebooted

Finally start the virtual machine.

[root@linuxhelp ~]# virsh start centos7
Domain centos7 started
FAQ
Q
How to convert VMware machines to virt-manager?
A
Enable IDE in the registry. Start the machine and execute mergeide.reg. See the ProxMox wiki for very detailed instructions.
Remove VMware tools via the Control Panel.
Please run the followi
Q
Whether the KVM can install on windows?
A
KVM is only for the linux based os
Q
I wanna install KVM on Ubuntu?
A
refer the link as follow for install KVM "https://www.linuxhelp.com/install-kvm-virtualization-extension-ubuntu/"
Q
Why does Qubes use virtualization?
A
We believe that this is currently the only practically viable approach to implementing strong isolation while simultaneously providing compatibility with existing applications and drivers.
Q
How to set Virtual NAT and Route based networking?
A
Any host running the libvirt daemon can manage and create virtual networks. Libvirt virtual networks use firewall rules to act as a router, providing VMs transparent access to the host machin