Water's Home

Just another Life Style

0%

How to Use KVM to Create Virtual Machine

Configure Firewall

systemctl stop firewalld.service
systemctl disable firewalld.service
firewall-cmd –state

sed -i ‘/^SELINUX=.*/c SELINUX=disabled’ /etc/selinux/config
sed -i ‘s/^SELINUXTYPE=.*/SELINUXTYPE=disabled/g’ /etc/selinux/config
grep –color=auto ‘^SELINUX’ /etc/selinux/config
setenforce 0

Install KVM & Virt

yum install qemu-kvm libvirt -y
yum install virt-install -y

Start Virt Service

systemctl start libvirtd && systemctl enable libvirtd

[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.220.202 netmask 255.255.255.0 broadcast 192.168.220.255
inet6 fe80::c269:7c04:a06b:dce7 prefixlen 64 scopeid 0x20
ether 00:0c:29:4e:32:2a txqueuelen 1000 (Ethernet)
RX packets 3394211 bytes 4731781088 (4.4 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 264816 bytes 35363147 (33.7 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1 (Local Loopback)
RX packets 68 bytes 5920 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5920 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:a5:ea:48 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Create Disk

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

Upload System ISO

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

Begin Install OS

[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

Starting install…
Domain installation still in progress. You can reconnect to
the console to complete the installation process.

Connect With VNC

List Virtual Machine

[root@localhost ~]# virsh list –all
Id Name State


  • CentOS-7-x86\_64                shut off
    

Start Virtual Machine

[root@localhost ~]# virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started

[root@localhost ~]# virsh list –all
Id Name State


3 CentOS-7-x86_64 running

Test Virtual Machine

List Virbr

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

List br0 Status

[root@localhost ~]# vi create-br0.sh

[root@localhost ~]# cat create-br0.sh
brctl addbr br0
brctl addif br0 ens33
ip addr del dev ens33 192.168.220.202/24
ifconfig br0 192.168.220.202/24 up
route add default gw 192.168.220.2

[root@localhost ~]# chmod +x create-br0.sh
[root@localhost ~]# ./create-br0.sh

Create br0

[root@localhost network-scripts]# ifconfig
br0: flags=4163 mtu 1500
inet 192.168.220.202 netmask 255.255.255.0 broadcast 192.168.220.255
inet6 fe80::20c:29ff:fe4e:322a prefixlen 64 scopeid 0x20
ether 00:0c:29:4e:32:2a txqueuelen 1000 (Ethernet)
RX packets 144 bytes 12890 (12.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 96 bytes 17020 (16.6 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

ens33: flags=4163 mtu 1500
inet6 fe80::c269:7c04:a06b:dce7 prefixlen 64 scopeid 0x20
ether 00:0c:29:4e:32:2a txqueuelen 1000 (Ethernet)
RX packets 3432987 bytes 4745607852 (4.4 GiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 297978 bytes 44267836 (42.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1 (Local Loopback)
RX packets 68 bytes 5920 (5.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 68 bytes 5920 (5.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

virbr0: flags=4163 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:a5:ea:48 txqueuelen 1000 (Ethernet)
RX packets 3970 bytes 167470 (163.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 4261 bytes 11695250 (11.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

vnet0: flags=4163 mtu 1500
inet6 fe80::fc54:ff:feaf:a499 prefixlen 64 scopeid 0x20
ether fe:54:00:af:a4:99 txqueuelen 1000 (Ethernet)
RX packets 3970 bytes 223050 (217.8 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5218 bytes 11745246 (11.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Vir Edit to Bridge Network

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

Before Edit

After Edit

Restart Virtual Machine

[root@localhost ~]# virsh shutdown CentOS-7-x86_64
Domain CentOS-7-x86_64 is being shutdown

[root@localhost ~]# virsh start CentOS-7-x86_64
Domain CentOS-7-x86_64 started

Look at the Virtual Machine IP