Install KVM Hypervisor on CentOS 7

Install KVM Hypervisor pada CentOS 7

  • Cek virtualisasi support, pastikan virtualization technology pada bios sudah enable
[root@kvm-centos sys-ops.id]# lscpu | grep Virtualization
Virtualization:        VT-x
Virtualization type:   full
  • Install KVM Hypervisor pada CentOS
yum install qemu-kvm libvirt libvirt-python libguestfs-tools virt-install -y
  • Jika menggunakan CentOS 7 mode Desktop gunakan perintah berikut untuk install KVM
yum install -y qemu-kvm qemu-img virt-manager libvirt libvirt-python libvirt-client virt-install virt-viewer libguestfs-tools

  • Jalankan service libvirtd 
systemctl enable libvirtd
systemctl start libvirtd
systemctl status libvirtd
  • Verifikasi installasi KVM, pastikan modul kvm berhasil di load
[root@kvm-centos sys-ops.id]# lsmod | grep -i kvm
kvm_intel             188740  0
kvm                   637515  1 kvm_intel
irqbypass              13503  1 kvm
  • Verifikasi Network Bridge
[root@kvm-centos sys-ops.id]# brctl show
bridge name     bridge id               STP enabled     interfaces
virbr0          8000.5254007d1a16       yes             virbr0-nic

[root@kvm-centos sys-ops.id]# virsh net-list
 Name                 State      Autostart     Persistent
----------------------------------------------------------
 default              active     yes           yes
  • Verifikasi Private Network 192.168.122.0/24, secara default sudah di tambahkan saat installasi.
[root@kvm-centos sys-ops.id]# virsh net-dumpxml default

<network>
  <name>default</name>
  <uuid>061a0c47-4d34-48fa-82e3-c9239df97d9a</uuid>
  <forward mode='nat'>
    <nat>
      <port start='1024' end='65535'/>
    </nat>
  </forward>
  <bridge name='virbr0' stp='on' delay='0'/>
  <mac address='52:54:00:7d:1a:16'/>
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.2' end='192.168.122.254'/>
    </dhcp>
  </ip>
</network>
  • Edit network ifcfg-enp3s3 agar VM guest dapat terhubung langsung ke jaringan LAN
  • Tambahkan baris berikut paling bawah:
  • BRIDGE=br0
nano /etc/sysconfig/network-scripts/ifcfg-enp0s3
  • Edit file ifcfg-br0
nano /etc/sysconfig/network-scripts/ifcfg-br0
DEVICE="br0"
# I am getting ip from DHCP server #
BOOTPROTO="dhcp"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
ONBOOT="yes"
TYPE="Bridge"
DELAY="0"
  • Restart service NetworkManager
systemctl restart NetworkManager
systemctl restart network
reboot
  • Verifikasi Network Bridge
[root@kvm-centos sys-ops.id]# brctl show
bridge name     bridge id               STP enabled     interfaces
br0             8000.000000000000       no
virbr0          8000.5254007d1a16       yes             virbr0-nic

  • Tampilan KVM Manager jika menggunakan CentOS mode Desktop

Menambahkan VM dengan ISO Image

  • Tambahkan ISO image, upload atau download iso pada directory: /var/lib/libvirt/boot/
[root@kvm-centos boot]# cd /var/lib/libvirt/boot/

[root@kvm-centos boot]# ls -l
total 996352
-rw-r--r--. 1 root root 1020264448 Nov 26 01:19 CentOS-7-x86_64-Minimal-2009.iso
  • Tambahkan VM CentOS 7 dengan spek: CPU 1 Core, RAM 512 MB, Disk 20 GB
[root@kvm-centos sys-ops.id]# virt-install \
--virt-type=kvm \
--name centos-7 \
--ram 512 \
--vcpus=1 \
--os-variant=centos7.0 \
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-Minimal-2009.iso \
--network=bridge=br0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos-7.qcow2,size=20,bus=virtio,format=qcow2
  • Konfigurasi Login VNC,
  • Buka terminal baru via ssh, pastikan sudah allow port 5901 pada firewalld atau iptables centos host kvm
[root@kvm-centos boot]# virsh dumpxml centos-7 | grep vnc
    <graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>
  • Jalankan reverse ssh untuk port vnc 5901 pada terminal baru via ssh
ssh [email protected] -L 5901:127.0.0.1:5901
[email protected]'s password:
Last login: Sat Feb 26 07:57:53 2022 from 192.168.88.1
[root@kvm-centos ~]#
  • Akses vm centos-7 lewat vnc client

Menambahkan VM dengan Cloud Image

  • Tambahkan Cloud image, upload atau download cloud image pada directory: /var/lib/libvirt/boot/
cd /var/lib/libvirt/boot
wget http://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2
  • Buat require directory
D=/var/lib/libvirt/images
VM=centos7-vm
mkdir -vp $D/$VM
  • Buat file mete-data
cd $D/$VM
nano meta-data
  • Masukan beris berikut pada file
instance-id: centos7-vm
local-hostname: centos7-vm
  • Generate ssh key untuk login ke dalam vm
ssh-keygen -t ed25519 -C "VM Login ssh key"
---- output
Generating public/private ed25519 key pair.
Enter file in which to save the key (/root/.ssh/id_ed25519):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_ed25519.
Your public key has been saved in /root/.ssh/id_ed25519.pub.
The key fingerprint is:
SHA256:tacqdm4/SDL+KZn596Dcxhmm3YHr59tsyUlanT0KYdk VM Login ssh key
The key's randomart image is:
+--[ED25519 256]--+
|                 |
|                 |
|          . o    |
|         . = E   |
|        S o.o  .o|
|      o . ++. ooo|
|     . B *o=.*.o.|
|      O.==O.++=  |
|     . OO=+=+oo  |
+----[SHA256]-----+
  • Tampilkan public key
cat /root/.ssh/id_ed25519.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOhN2tRFZEb7s9IrJujKV4gVAekVmzRr6pb/9827dw/S VM Login ssh key
  • Buat file user-data
  • user yang digunakan adalah admin
cd $D/$VM
nano user-data
#cloud-config

# Hostname management
preserve_hostname: False
hostname: centos7-vm
fqdn: centos7-vm.sys-ops.id

# Users
users:
    - default
    - name: admin
      groups: ['wheel']
      shell: /bin/bash
      sudo: ALL=(ALL) NOPASSWD:ALL
      ssh-authorized-keys:
        - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOhN2tRFZEb7s9IrJujKV4gVAekVmzRr6pb/9827dw/S VM Login ssh key

# Configure where output will go
output:
  all: ">> /var/log/cloud-init.log"

# configure interaction with ssh server
ssh_genkeytypes: ['ed25519', 'rsa']

# Install my public ssh key to the first user-defined user configured
# in cloud.cfg in the template (which is centos for CentOS cloud images)
ssh_authorized_keys:
  - ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOhN2tRFZEb7s9IrJujKV4gVAekVmzRr6pb/9827dw/S VM Login ssh key

# set timezone for VM
timezone: Asia/Jakarta

# Remove cloud-init
runcmd:
  - systemctl stop network && systemctl start network
  - yum -y remove cloud-init
  • Copy Cloud Image
cd $D/$VM
cp /var/lib/libvirt/boot/CentOS-7-x86_64-GenericCloud.qcow2 $VM.qcow2
  • Buat disk image sebesar 20GB
cd $D/$VM
export LIBGUESTFS_BACKEND=direct
qemu-img create -f qcow2 -o preallocation=metadata $VM.new.image 20G
virt-resize --quiet --expand /dev/sda1 $VM.qcow2 $VM.new.image
[root@kvm-centos centos7-vm]# ls -l
total 1677984
-rw-r--r--. 1 root root 21478375424 Feb 27 05:05 centos7-vm.new.image
-rw-r--r--. 1 root root   858783744 Feb 27 05:00 centos7-vm.qcow2
-rw-r--r--. 1 root root          51 Feb 27 04:48 meta-data
-rw-r--r--. 1 root root         966 Feb 27 04:56 user-data
  • Overwrite file cloud image
cd $D/$VM
mv $VM.new.image $VM.qcow2
  • Buat cloud-init ISO
mkisofs -o $VM-cidata.iso -V cidata -J -r user-data meta-data
---- output
I: -input-charset not specified, using utf-8 (detected in locale settings)
Total translation table size: 0
Total rockridge attributes bytes: 331
Total directory bytes: 0
Path table size(bytes): 10
Max brk space used 0
183 extents written (0 MB)
  • Buat pool
virsh pool-create-as --name $VM --type dir --target $D/$VM
  • Install CentOS 7 VM
[root@kvm-centos centos7-vm]# virt-install --import --name $VM \
 --memory 512 --vcpus 1 --cpu host \
 --disk $VM.qcow2,format=qcow2,bus=virtio \
 --disk $VM-cidata.iso,device=cdrom \
 --network bridge=virbr0,model=virtio \
 --os-type=linux \
 --os-variant=centos7.0 \
 --graphics spice \
 --noautoconsole
  • Hapus file yang tidak diperlukan
cd $D/$VM\
virsh change-media $VM hda --eject --config
rm meta-data user-data centos7-vm-cidata.iso
  • Cari IP address dari kvm centos7-vm
[root@kvm-centos centos7-vm]# virsh net-dhcp-leases default
 Expiry Time          MAC address        Protocol  IP address                Hostname        Client ID or DUID
-------------------------------------------------------------------------------------------------------------------
 2022-02-27 06:14:52  52:54:00:d0:4c:8a  ipv4      192.168.122.159/24        centos7-vm      -
  • Login ke dalam kvm centos7-vm dengan user admin

Management VM pada KVM

  • Cek list vm
[root@kvm-centos ~]#  virsh list --all
---- output
 Id    Name                           State
----------------------------------------------------
 4     centos7-vm                     running
  • Cek vm info
[root@kvm-centos ~]# virsh dominfo centos7-vm
---- output
Id:             4
Name:           centos7-vm
UUID:           21360893-fc78-4e74-912c-ddca70daaaba
OS Type:        hvm
State:          running
CPU(s):         1
CPU time:       532.6s
Max memory:     524288 KiB
Used memory:    524288 KiB
Persistent:     yes
Autostart:      disable
Managed save:   no
Security model: selinux
Security DOI:   0
Security label: system_u:system_r:svirt_t:s0:c486,c893 (enforcing)
  • Shutdown/stop vm
[root@kvm-centos ~]# virsh shutdown centos7-vm
---- output
Domain centos7-vm is being shutdown
  • Start vm
[root@kvm-centos ~]# virsh start centos7-vm
---- output
Domain centos7-vm started
  • Soft reboot vm
[root@kvm-centos ~]# virsh reboot centos7-vm
---- output
Domain centos7-vm is being rebooted
  • Hard reset vm
[root@kvm-centos ~]# virsh reset centos7-vm
---- output
Domain centos7-vm was reset
  • Force stop vm
[root@kvm-centos ~]# virsh destroy centos7-vm
---- output
Domain centos7-vm destroyed
  • Delete vm
[root@kvm-centos ~]# virsh destroy centos7-vm
Domain centos7-vm destroyed

[root@kvm-centos ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     centos7-vm                     shut off

[root@kvm-centos ~]# virsh undefine centos7-vm
Domain centos7-vm has been undefined

[root@kvm-centos ~]# virsh pool-destroy centos7-vm
Pool centos7-vm destroyed

[root@kvm-centos ~]# D=/var/lib/libvirt/images
[root@kvm-centos ~]# VM=centos7-vm
[root@kvm-centos ~]# rm -ri $D/$VM
rm: descend into directory ‘/var/lib/libvirt/images/centos7-vm’? yes
rm: remove regular file ‘/var/lib/libvirt/images/centos7-vm/meta-data’? yes
rm: remove regular file ‘/var/lib/libvirt/images/centos7-vm/user-data’? yes
rm: remove regular file ‘/var/lib/libvirt/images/centos7-vm/centos7-vm.qcow2’? yes
rm: remove regular file ‘/var/lib/libvirt/images/centos7-vm/centos7-vm-cidata.iso’? yes
rm: remove directory ‘/var/lib/libvirt/images/centos7-vm’? yes

  • Menambahkan VM dengan existing disk file
[root@kvm-centos ~]# virt-install \
--virt-type=kvm \
--name centos-7 \
--ram 512 \
--vcpus=1 \
--os-variant=centos7.0 \
--network=bridge=br0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos7.qcow2 \
--import

herdiana3389

A system administrator with skills in system administration, virtualization, linux, windows, networking, cloud computing, container, etc.