What is KVM ?
Kernel based Virtual Machine (KVM) is a virtual infrastructure for Linux kernel that turn it into hypervisor. It was merged into the Linux kernel mainline in kernel version 2.6.20, which was released on February 5, 2007. KVM requires a processor with hardware virtualization extensions
Pro and Cons of KVM
Benefits:
[root@vm01 ~]# grep -E 'svm|vmx' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm svm extapic abm sse4a misalignsse 3dnowprefetch osvw xop fma4 retpoline_amd ssbd vmmcall arat npt svm_lock nrip_save vmcb_clean flushbyasid decodeassists
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm svm extapic abm sse4a misalignsse 3dnowprefetch osvw xop fma4 retpoline_amd ssbd vmmcall arat npt svm_lock nrip_save vmcb_clean flushbyasid decodeassists
[root@vm01 ~]#
2. Install kvm packages and enable the required services.
[root@vm01 ~]#yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install -y
qemu-kvm : QEMU is a machine emulator and virtualizer
libvirt : Library providing a simple virtualization API
libvirt-python : The libvirt virtualization API python2 binding
libguestfs-tools : System administration tools for virtual machines
virt-install :Utilities for installing virtual machines
[root@vm01 ~]# systemctl enable libvirtd
[root@vm01 ~]# systemctl start libvirtd
[root@vm01 ~]# systemctl status libvirtd
3. Configure the Networking for KVM.
Update the Base interface file. In this example ens33 is the interface connected to internet and will used as bridge interface for the VMs
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
HWADDR=00:0c:29:02:07:ef
BRIDGE=virbr0
[root@vm01 ~]#
Create a new Interface config file for virbr0. Please note virbr0 is the virtual bridge interface to be used in the VMs
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-virbr0
TYPE="BRIDGE"
BOOTPROTO="none"
NAME="virbr0
DEVICE="virbr0"
ONBOOT="yes"
IPADDR="192.168.2.115"
PREFIX="24"
GATEWAY="192.168.2.1"
DNS1="192.168.2.1"
[root@vm01 ~]#
Enable the IPv4 forwarding.
Add "net.ipv4.ip_forward = 1" entry in /usr/lib/sysctl.d/60-libvirtd.conf and load the file
[root@vm01 ~]#echo "net.ipv4.ip_forward = 1" >> /usr/lib/sysctl.d/60-libvirtd.conf
[root@vm01 ~]#sysctl -w /usr/lib/sysctl.d/60-libvirtd.conf
[root@vm01 ~]#sysctl -p /usr/lib/sysctl.d/60-libvirtd.conf
Update the Network Configuration/DHCP range of the virbr0 interface
[root@vm01 ~]#virsh net-edit default
<network>
<name>default</name>
<uuid>df65966e-31da-4b15-a710-8082c2151b9b</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:64:d3:2c'/>
<ip address='192.168.2.115' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.2.200' end='192.168.2.220'/>
</dhcp>
</ip>
</network>
NAT : This means any guests connected through it, use the host IP address for communication to the outside world. Computers external to the host can't initiate communications to the guests inside, when the virtual network switch is operating in NAT mode.
Route : With routed mode, the virtual switch is connected to the physical host LAN, passing guest network traffic back and forth without using NAT.
Isolate : In this mode, guests connected to the virtual switch can communicate with each other, and with the host. However, their traffic will not pass outside of the host, nor can they receive traffic from outside the host.
Add a network routing rule for Guests VMs connectivity
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/route-virbr0
192.168.2.0/24 via 192.168.2.115 dev virbr0
4. Restart the KVM host.
5. Creating a virtual Machine from Command Line
Update the base interface file. In this example ens33 is the interface connected to internet and will be used as bridge interface for the VMs.
[root@vm01 ~]#virt-install --network bridge:virbr0 --name testvm1 --os-variant=centos7.0 --ram=1024 --vcpu=1 --disk path=/kvmstore/testvm1.img,size=4 --graphics none --location=/tmp/CentOS-7-x86_64-DVD-1810.iso --extra-args="console=tty0 console=ttyS0,115200"
Please Note:
6. Common KVM Management Tasks
List all VMs on a host, in all state.
#virsh list --all
To display the VM information/Configuration
# virsh dominfo <vm name>
Stop VM
#virsh shutdown <vm name>
Start VM
#virsh start <VM name>
Enabling AutoStart of the VM
#virsh autostart <vm name>
Taking console of the VM, from the KVM host.
# virsh console <vm id>
To exit console of the VM, from the KVM host.
keyboard keys : Ctrl + ^]
Deleting Guest VM
# virsh shutdown <vm name>
Kernel based Virtual Machine (KVM) is a virtual infrastructure for Linux kernel that turn it into hypervisor. It was merged into the Linux kernel mainline in kernel version 2.6.20, which was released on February 5, 2007. KVM requires a processor with hardware virtualization extensions
Pro and Cons of KVM
Benefits:
- Easy Scalable
- Lower cost
- Secure support live / offline VM Migration
- Full Virtualization
- Single physical server is down, it may take all local hosted kvm down
- Complex networking
- System with VT
- Linux OS with minimum of above kernel version 2.6.20.
- RAM must be more the 8GB
- Disk space more 20 GB.
[root@vm01 ~]# grep -E 'svm|vmx' /proc/cpuinfo
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm svm extapic abm sse4a misalignsse 3dnowprefetch osvw xop fma4 retpoline_amd ssbd vmmcall arat npt svm_lock nrip_save vmcb_clean flushbyasid decodeassists
flags : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt rdtscp lm constant_tsc art rep_good nopl tsc_reliable nonstop_tsc aperfmperf pni pclmulqdq ssse3 fma cx16 sse4_1 sse4_2 popcnt aes xsave avx hypervisor lahf_lm svm extapic abm sse4a misalignsse 3dnowprefetch osvw xop fma4 retpoline_amd ssbd vmmcall arat npt svm_lock nrip_save vmcb_clean flushbyasid decodeassists
[root@vm01 ~]#
[root@vm01 ~]#yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install -y
qemu-kvm : QEMU is a machine emulator and virtualizer
libvirt : Library providing a simple virtualization API
libvirt-python : The libvirt virtualization API python2 binding
libguestfs-tools : System administration tools for virtual machines
virt-install :Utilities for installing virtual machines
[root@vm01 ~]# systemctl enable libvirtd
[root@vm01 ~]# systemctl start libvirtd
[root@vm01 ~]# systemctl status libvirtd
3. Configure the Networking for KVM.
Update the Base interface file. In this example ens33 is the interface connected to internet and will used as bridge interface for the VMs
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
HWADDR=00:0c:29:02:07:ef
BRIDGE=virbr0
[root@vm01 ~]#
Create a new Interface config file for virbr0. Please note virbr0 is the virtual bridge interface to be used in the VMs
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-virbr0
TYPE="BRIDGE"
BOOTPROTO="none"
NAME="virbr0
DEVICE="virbr0"
ONBOOT="yes"
IPADDR="192.168.2.115"
PREFIX="24"
GATEWAY="192.168.2.1"
DNS1="192.168.2.1"
[root@vm01 ~]#
Enable the IPv4 forwarding.
Add "net.ipv4.ip_forward = 1" entry in /usr/lib/sysctl.d/60-libvirtd.conf and load the file
[root@vm01 ~]#echo "net.ipv4.ip_forward = 1" >> /usr/lib/sysctl.d/60-libvirtd.conf
[root@vm01 ~]#sysctl -w /usr/lib/sysctl.d/60-libvirtd.conf
[root@vm01 ~]#sysctl -p /usr/lib/sysctl.d/60-libvirtd.conf
Update the Network Configuration/DHCP range of the virbr0 interface
[root@vm01 ~]#virsh net-edit default
<network>
<name>default</name>
<uuid>df65966e-31da-4b15-a710-8082c2151b9b</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:64:d3:2c'/>
<ip address='192.168.2.115' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.2.200' end='192.168.2.220'/>
</dhcp>
</ip>
</network>
NAT : This means any guests connected through it, use the host IP address for communication to the outside world. Computers external to the host can't initiate communications to the guests inside, when the virtual network switch is operating in NAT mode.
Route : With routed mode, the virtual switch is connected to the physical host LAN, passing guest network traffic back and forth without using NAT.
Isolate : In this mode, guests connected to the virtual switch can communicate with each other, and with the host. However, their traffic will not pass outside of the host, nor can they receive traffic from outside the host.
Add a network routing rule for Guests VMs connectivity
[root@vm01 ~]# cat /etc/sysconfig/network-scripts/route-virbr0
192.168.2.0/24 via 192.168.2.115 dev virbr0
4. Restart the KVM host.
5. Creating a virtual Machine from Command Line
Update the base interface file. In this example ens33 is the interface connected to internet and will be used as bridge interface for the VMs.
[root@vm01 ~]#virt-install --network bridge:virbr0 --name testvm1 --os-variant=centos7.0 --ram=1024 --vcpu=1 --disk path=/kvmstore/testvm1.img,size=4 --graphics none --location=/tmp/CentOS-7-x86_64-DVD-1810.iso --extra-args="console=tty0 console=ttyS0,115200"
Please Note:
--network bridge:virbr0
|
Is the underplaying network interface
for guest VMs NIC
|
--name testvm1
|
Is the name of the host VM/Container
|
--os-variant=rhel7.7
|
In the case centos7 is the guest VM
type (# osinfo-query os | grep -i centos)
|
--ram=1024
|
This will allocate 01 GB RAM to the
guest VM
|
--vcpu=1
|
This will allocate 01 vCPU to guest
VM
|
--disk path= /kvmstore/testvm1.img,
size=4
|
This create /kvmstore/testvm1.image
as OS store file the guest VM with
04GB size.
|
--graphics none
|
This set no graphical interface,
installation will go command line.
|
--location=/tmp/rhel-server-7.7-x86_64-dvd.iso
|
Location of OS Media to be used for
the OS installation.
|
--extra-args="console=tty0
console=ttyS0,115200"
|
Console Connectivity options
|
6. Common KVM Management Tasks
List all VMs on a host, in all state.
#virsh list --all
To display the VM information/Configuration
# virsh dominfo <vm name>
Stop VM
#virsh shutdown <vm name>
Start VM
#virsh start <VM name>
Enabling AutoStart of the VM
#virsh autostart <vm name>
Taking console of the VM, from the KVM host.
# virsh console <vm id>
To exit console of the VM, from the KVM host.
keyboard keys : Ctrl + ^]
Deleting Guest VM
# virsh shutdown <vm name>
Deleting Guest VM and root disk (vda).
# virsh undefine <vm name> --storage vda
Deleting Guest VM and del all disk and iso image attached to vm.
# virsh undefine <vm name> --remove-all-storage
Immediately shuts down the virtual machine, probably because it is unresponsive
#virsh destroy <vm name>
Cloning VM (Suspend the source VM first, Once cloing finisher resume it.)
# virsh suspend <source vm>
# virt-clone --connect qemu:///system --original <soruce_vm> --name <clone_vm> -f <disk path>
#virsh resume <source_vm>
Example:
Cloning VM (Suspend the source VM first, Once cloing finisher resume it.)
# virsh suspend <source vm>
# virt-clone --connect qemu:///system --original <soruce_vm> --name <clone_vm> -f <disk path>
#virsh resume <source_vm>
Example:
[root@vvm01 ~]# virsh suspend vvm06
Domain vvm06 suspended
[root@vvm01 ~]# virt-clone --connect qemu:///system --original vvm06 --name vvm08 -f /kvmstore/testvm3.img
Allocating 'testvm3.img' | 4.0 GB 00:38
Clone 'vvm08' created successfully.
[root@vvm01 ~]#
[root@vvm01 ~]# virsh resume vvm06
Domain vvm06 resumed
To snapshot management:
# virsh snapshot-create <vm name>
# virsh snapshot-list <vm name>
# virsh snapshot-revert <vm name>
<snapshot id>
# virsh snapshot-delete <vm name>
<snapshot id>
Example:
[root@vvm01 ~]# virsh snapshot-create vvm06
[root@vvm01 ~]# virsh snapshot-list vvm06
Name Creation Time State
------------------------------------------------------------
1631078353 2021-09-08 01:19:13 -0400 shutoff
[root@vvm01 ~]# virsh snapshot-revert vvm06 1631078353
Attaching and detaching disk to guest:
The below command will create a disk on 2 GB with pre-allocation of disk.
The below command will create a disk on 2 GB with pre-allocation of disk.
# qemu-img create -f raw
testvm1-2G 2G
Below command will create a disk with no allocation of disk.
# qemu-img create -f qcow2 testvm1-2G 2G
# virsh attach-disk <vm name> --source /var/lib/libvirt/images/testvm1-2G --target vdb --persistent
# qemu-img create -f qcow2 testvm1-2G 2G
# virsh attach-disk <vm name> --source /var/lib/libvirt/images/testvm1-2G --target vdb --persistent
# virsh domfsinfo <vm name>
# virsh detach-disk <vm name> --target vdbExample:
Rename host name:
virsh domrename hostname newhostname.
Note: host rename required vm shutdown.
To change memory and CPU:
[root@vvm01 ~]# qemu-img create -f qcow2 vvm06-disk2 10G
[root@vvm01 ~]# qemu-img create -f raw vvm06-disk3 10G
[root@vvm01 ~]# virsh attach-disk vvm06 --source /kvmstore/vvm06-disk2 --target vdb --persistent
Rename host name:
virsh domrename hostname newhostname.
Note: host rename required vm shutdown.
To change memory and CPU:
[root@vvm01 ~]# virsh edit rhel-vm3
<domain type='kvm'>
<name>rhel-vm3</name>
<uuid>6999879c-0035-47c2-9353-cc4c3eb7f97b</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type>
<boot dev='hd'/>
</os>
How to attach ISO image to VM:
Check the disk attached to vm.
[root@vvm01 ~]# virsh domblklist centos-vm1
Target Source
------------------------------------------------
vda /kvmstore/centos-vm1.img
hda -
Attached the ISO image to VM.
[root@vvm01 ~]# virsh change-media centos-vm1 hda /kvmstore/iso-home/CentOS-7-x86_64-DVD-1908.iso --insert
Successfully inserted media.
Check the ISO image is attached or not.
[root@vvm01 iso-home]# virsh domblklist centos-vm1
Target Source
------------------------------------------------
vda /kvmstore/centos-vm1.img
hda /kvmstore/iso-home/CentOS-7-x86_64-DVD-1908.iso
Mount the ISO image on /mnt folder in VM.
[root@centos-vm1 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 is write-protected, mounting read-only
[root@centos-vm1 ~]#
Detach iso image from the VM.
[root@vvm01 ~]# virsh change-media centos-vm1 hda /kvmstore/iso-home/CentOS-7-x86_64-DVD-1908.iso --eject
Successfully ejected media.
[root@vvm01 ~]#
Check the status again.
[root@vvm01 ~]# virsh domblklist centos-vm1
Target Source
------------------------------------------------
vda /kvmstore/centos-vm1.img
hda -
Log location:
#tail /var/log/libvirt/qemu/<vm name>.log
How to Enable GUI for KVM:
1. Install below package. (xclock package contain all the GUI packages)
[root@node01 ~]#yum install xclock xterm xauth
2. Updated the below sshd configuration.
[root@node01 ~]# cat /etc/ssh/sshd_config | egrep 'AddressFamily|X11Forwarding'
AddressFamily any
X11Forwarding yes
[root@node01 ~]#
3. After updating the configuration kindly restart sshd services.
[root@node01 ~]# systemctl restart sshd
4. Open new terminal in mobaxterm
open MobaXterm_Personal_12.4
5. Execute the below command to check if GUI is working or not.
[root@node01 ~]# xclock
6. Install virt-manager
[root@node01 ~]# yum install virt-manager
7. Open virt-manager and you can see all the VMs available.
[root@node01 ~]# virt-manager
#tail /var/log/libvirt/qemu/<vm name>.log
How to Enable GUI for KVM:
2. Updated the below sshd configuration.
[root@node01 ~]# cat /etc/ssh/sshd_config | egrep 'AddressFamily|X11Forwarding'
AddressFamily any
X11Forwarding yes
[root@node01 ~]#
3. After updating the configuration kindly restart sshd services.
[root@node01 ~]# systemctl restart sshd
4. Open new terminal in mobaxterm
open MobaXterm_Personal_12.4
5. Execute the below command to check if GUI is working or not.
[root@node01 ~]# xclock
6. Install virt-manager
[root@node01 ~]# yum install virt-manager
7. Open virt-manager and you can see all the VMs available.
[root@node01 ~]# virt-manager
Note: If you install OS using virt-manager then you will face console issue. To resolve the console issue kindly refer to below link.
https://ostechnix.com/how-to-enable-virsh-console-access-for-kvm-guests/
Procedure to configure 2 network on KVM host
1. Configured the new ethernet card (ifcfg-ens35 and ifcfg-virbr1)
2. Create dumpfile using the below command
[root@kvm01 ~]# virsh net-dumpxml default > /root/vnet1.xml
3. Modify the dump file as per new network requirement
[root@kvm01 ~]# cat /root/vnet1.xml
<network>
<name>vnet1</name>
<forward mode='nat'/>
<bridge name='virbr1' stp='on' delay='0'/>
<mac address='00:0c:29:0d:4b:32'/>
<ip address='172.16.1.220' netmask='255.255.255.0'>
<dhcp>
<range start='172.16.1.221' end='172.16.1.229'/>
</dhcp>
</ip>
</network>
[root@kvm01 ~]#
4. Execute the below command to create vnet1
[root@kvm01 ~]# virsh net-define vnet1.xml
5. To modify it use below command.
[root@kvm01 ~]# virsh net-edit vnet1
6. Check if vnet is created or not.
[root@kvm02 ~]# virsh net-list --all
Name State Autostart Persistent
----------------------------------------------------------
default inactive yes yes
vnet1 inactive no yes
[root@kvm02 ~]#
6. Location of vnet configuration file.
[root@kvm02 ~]# ll /etc/libvirt/qemu/networks
total 8
drwx------ 2 root root 25 Jan 4 09:23 autostart
-rw------- 1 root root 574 Jan 6 07:45 default.xml
-rw------- 1 root root 567 Jan 6 09:03 vnet1.xml
[root@kvm02 ~]#
7. Build new OS with 2 ethernets.
[root@kvm01 ~]# virt-install --network bridge:virbr0 --network bridge:virbr1 --name testvm1 --os-variant=centos7.0 --ram=1024 --vcpu=1 --disk path=/kvmstore/testvm1.img,size=4 --graphics none --location=/tmp/CentOS-7-x86_64-DVD-1511.iso --extra-args="console=tty0 console=ttyS0,115200"
No comments:
Post a Comment