How to Use KVM to Create Virtual Machine

Configure Firewall

  1. systemctl stop firewalld.service
  2. systemctl disable firewalld.service
  3. firewall-cmd --state
  4.  
  5. sed -i '/^SELINUX=.*/c SELINUX=disabled' /etc/selinux/config
  6. sed -i 's/^SELINUXTYPE=.*/SELINUXTYPE=disabled/g' /etc/selinux/config
  7. grep --color=auto '^SELINUX' /etc/selinux/config
  8. setenforce 0

Install KVM & Virt

  1. yum install qemu-kvm libvirt -y
  2. yum install virt-install -y

Start Virt Service

  1. systemctl start libvirtd && systemctl enable libvirtd
  2.  
  3. [root@localhost ~]# ifconfig
  4. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  5.         inet 192.168.220.202  netmask 255.255.255.0  broadcast 192.168.220.255
  6.         inet6 fe80::c269:7c04:a06b:dce7  prefixlen 64  scopeid 0x20<link>
  7.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  8.         RX packets 3394211  bytes 4731781088 (4.4 GiB)
  9.         RX errors 0  dropped 0  overruns 0  frame 0
  10.         TX packets 264816  bytes 35363147 (33.7 MiB)
  11.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  12.  
  13. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  14.         inet 127.0.0.1  netmask 255.0.0.0
  15.         inet6 ::1  prefixlen 128  scopeid 0x10<host>
  16.         loop  txqueuelen 1  (Local Loopback)
  17.         RX packets 68  bytes 5920 (5.7 KiB)
  18.         RX errors 0  dropped 0  overruns 0  frame 0
  19.         TX packets 68  bytes 5920 (5.7 KiB)
  20.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  21.  
  22. virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
  23.         inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  24.         ether 52:54:00:a5:ea:48  txqueuelen 1000  (Ethernet)
  25.         RX packets 0  bytes 0 (0.0 B)
  26.         RX errors 0  dropped 0  overruns 0  frame 0
  27.         TX packets 0  bytes 0 (0.0 B)
  28.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Create Disk

  1. [root@localhost ~]# qemu-img create -f raw /opt/CentOS-7-x86_64.raw 10G
  2. Formatting '/opt/CentOS-7-x86_64.raw', fmt=raw size=10737418240

Upload System ISO

  1. [root@localhost ~]# ls /ISO
  2. CentOS-7-x86_64-DVD-1708.iso

Begin Install OS

  1. [root@localhost ~]# virt-install --virt-type kvm --name CentOS-7-x86_64 --ram 1024 --cdrom=/ISO/CentOS-7-x86_64-DVD-1708.iso --disk path=/opt/CentOS-7-x86_64.raw --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole
  2.  
  3. Starting install...
  4. Domain installation still in progress. You can reconnect to 
  5. the console to complete the installation process.

Connect With VNC






List Virtual Machine

  1. [root@localhost ~]# virsh list --all
  2.  Id    Name                           State
  3. ----------------------------------------------------
  4.  -     CentOS-7-x86_64                shut off

Start Virtual Machine

  1. [root@localhost ~]# virsh start CentOS-7-x86_64
  2. Domain CentOS-7-x86_64 started
  3.  
  4. [root@localhost ~]# virsh list --all
  5.  Id    Name                           State
  6. ----------------------------------------------------
  7.  3     CentOS-7-x86_64                running

Test Virtual Machine


List Virbr

  1. [root@localhost ~]# brctl show
  2. bridge name     bridge id               STP enabled     interfaces
  3. virbr0          8000.525400a5ea48       yes             virbr0-nic
  4.                                                         vnet0

List br0 Status

  1. [root@localhost ~]# vi create-br0.sh
  2.  
  3. [root@localhost ~]# cat create-br0.sh
  4. brctl addbr br0
  5. brctl addif br0 ens33
  6. ip addr del dev ens33 192.168.220.202/24
  7. ifconfig br0 192.168.220.202/24 up
  8. route add default gw 192.168.220.2
  9.  
  10. [root@localhost ~]# chmod +x create-br0.sh 
  11. [root@localhost ~]# ./create-br0.sh

Create br0

  1. [root@localhost network-scripts]# ifconfig
  2. br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  3.         inet 192.168.220.202  netmask 255.255.255.0  broadcast 192.168.220.255
  4.         inet6 fe80::20c:29ff:fe4e:322a  prefixlen 64  scopeid 0x20<link>
  5.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  6.         RX packets 144  bytes 12890 (12.5 KiB)
  7.         RX errors 0  dropped 0  overruns 0  frame 0
  8.         TX packets 96  bytes 17020 (16.6 KiB)
  9.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  10.  
  11. ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  12.         inet6 fe80::c269:7c04:a06b:dce7  prefixlen 64  scopeid 0x20<link>
  13.         ether 00:0c:29:4e:32:2a  txqueuelen 1000  (Ethernet)
  14.         RX packets 3432987  bytes 4745607852 (4.4 GiB)
  15.         RX errors 0  dropped 0  overruns 0  frame 0
  16.         TX packets 297978  bytes 44267836 (42.2 MiB)
  17.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  18.  
  19. lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
  20.         inet 127.0.0.1  netmask 255.0.0.0
  21.         inet6 ::1  prefixlen 128  scopeid 0x10<host>
  22.         loop  txqueuelen 1  (Local Loopback)
  23.         RX packets 68  bytes 5920 (5.7 KiB)
  24.         RX errors 0  dropped 0  overruns 0  frame 0
  25.         TX packets 68  bytes 5920 (5.7 KiB)
  26.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  27.  
  28. virbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  29.         inet 192.168.122.1  netmask 255.255.255.0  broadcast 192.168.122.255
  30.         ether 52:54:00:a5:ea:48  txqueuelen 1000  (Ethernet)
  31.         RX packets 3970  bytes 167470 (163.5 KiB)
  32.         RX errors 0  dropped 0  overruns 0  frame 0
  33.         TX packets 4261  bytes 11695250 (11.1 MiB)
  34.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  35.  
  36. vnet0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
  37.         inet6 fe80::fc54:ff:feaf:a499  prefixlen 64  scopeid 0x20<link>
  38.         ether fe:54:00:af:a4:99  txqueuelen 1000  (Ethernet)
  39.         RX packets 3970  bytes 223050 (217.8 KiB)
  40.         RX errors 0  dropped 0  overruns 0  frame 0
  41.         TX packets 5218  bytes 11745246 (11.2 MiB)
  42.         TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Vir Edit to Bridge Network

  1. [root@localhost ~]# virsh edit CentOS-7-x86_64
  2. Domain CentOS-7-x86_64 XML configuration edited.
Before Edit

After Edit

Restart Virtual Machine

  1. [root@localhost ~]# virsh shutdown CentOS-7-x86_64 
  2. Domain CentOS-7-x86_64 is being shutdown
  3.  
  4. [root@localhost ~]# virsh start CentOS-7-x86_64
  5. Domain CentOS-7-x86_64 started

Look at the Virtual Machine IP