ubuntu20.04 uses kvm to build a virtual machine + bridge bridge, which is easy to test and effective

Posted by westen on Fri, 15 Oct 2021 18:52:07 +0200

premise

The host configuration is as follows:

  • The computer is a notebook

  • Processor intel i5-6300 HQ 2.30GHz * 4

  • Memory 16G

  • Hard Disk 500G Solid State

  • System Ubuntu 20.04.3 LTS 64-bit

inspect

Check if the CPU supports virtualization

$ grep -Eoc '(vmx|svm)' /proc/cpuinfo

# Output greater than 0
8

Output greater than 0 indicates support for virtualization

Check whether the motherboard turns on Virtualization

$ sudo apt update && sudo apt install cpu-checker -y
$ kvm-ok

# The following output results are available
INFO: /dev/kvm exists
KVM acceleration can be used

Install KVM Suite

Install related packages

sudo apt install qemu-kvm libvirt-daemon-system libvirt-clients bridge-utils virtinst virt-manager -y

qemu-kvm: Provides hardware underlying virtualization.
libvirt-daemon-system: Runs libvirt as a daemon for system services.
libvirt-clients: Provides long-term stable C API s for different virtual machines
bridge-utils: Provides network bridging capabilities
virtinst: Provides a series of command line work for creating a virtual machine for libvirt
virt-manager: KVM Virtual Machine Management GUI, if the server does not have GUI installed, it is not necessary to install it.

Check libvirtd

$ sudo systemctl is-active libvirtd

# Output the following results to indicate availability
active

Add current user to libvirt and kvm user groups

USER is the current user

sudo usermod -aG libvirt USER
sudo usermod -aG kvm USER

Configure Bridge

  1. sudo vim /etc/sysctl.d/bridge.conf

The contents are as follows:

net.bridge.bridge-nf-call-ip6tables=0
net.bridge.bridge-nf-call-iptables=0
net.bridge.bridge-nf-call-arptables=0
  1. sudo vim /etc/udev/rules.d/99-bridge.rules

The contents are as follows:

ACTION=="add", SUBSYSTEM=="module", KERNEL=="br_netfilter", \           RUN+="/sbin/sysctl -p /etc/sysctl.d/bridge.conf"
  1. ip link

After KVM is installed, there will be interfaces created by default and need to be deleted

View Interface

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether d4:be:d9:f3:1e:5f brd ff:ff:ff:ff:ff:ff
6: virbr0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:1d:5b:25 brd ff:ff:ff:ff:ff:ff
7: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN mode DEFAULT group default qlen 1000
    link/ether 52:54:00:1d:5b:25 brd ff:ff:ff:ff:ff:ff

Delete Default Port

$ sudo virsh net-destroy default
$ sudo virsh net-undefine default

View again

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
    link/ether d4:be:d9:f3:1e:5f brd ff:ff:ff:ff:ff:ff

If so, you need to delete it manually

$ ip link delete virbr0 type brigde 
$ ip link delete virbr0-nic
  1. sudo vim /etc/netplan/00-installer-config.yaml

The contents are as follows:

network:
  ethernets:
    eno1:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      interfaces: [ eno1 ]
      addresses: [192.168.0.100/24]
      gateway4: 192.168.0.1
      mtu: 1500
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]
      parameters:
        stp: true
        forward-delay: 4
      dhcp4: no
      dhcp6: no
  version: 2

Be careful to replace by yourself:

eno1: cannot use a wireless network card because it is the name of the network card of the notebook wired network card

addresses: the ip inside is the real ip address and mask in the LAN

gateway4: Gateway is a gateway in a local area network

  1. sudo netplan apply

Once executed, the br0 bridge will be created successfully

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eno1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br0 state UP mode DEFAULT group default qlen 1000
    link/ether c8:d3:ff:dd:74:f1 brd ff:ff:ff:ff:ff:ff
    altname enp9s0
3: wlo1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether c8:21:58:17:5a:bb brd ff:ff:ff:ff:ff:ff
    altname wlp8s0
4: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether c8:d3:ff:dd:74:f1 brd ff:ff:ff:ff:ff:ff
  1. vim host-bridge.xml

Create a kvm bridge profile

<network>
  <name>host-bridge</name>
  <forward mode="bridge"/>
  <bridge name="br0"/>
</network>
  1. Create a kvm Bridge
$ sudo virsh net-define host-bridge.xml
$ sudo virsh net-start host-bridge
$ sudo virsh net-autostart host-bridge
  1. Check to see if the creation was successful
$ virsh net-list --all
 Name          State    Autostart   Persistent
------------------------------------------------
 host-bridge   active   yes         yes

Create Virtual Machine

sudo virt-install --name=centos7_2003 --ram=4096 --vcpus=2 --virt-type=kvm --os-type=linux --os-variant=centos7.0 --network default,model=virtio --graphics=vnc,password=123123,port=5911,listen=0.0.0.0 --noautoconsole --accelerate --cdrom=/var/lib/libvirt/images/CentOS-7-x86_64-Minimal-2003.iso --disk path=/var/lib/libvirt/images/CentOS7.qcow2,device=disk,format=qcow2,bus=virtio,cache=writeback,size=40

You can modify it yourself:

--name=centos7_2003 is the name of the virtual machine

--ram=4096 is virtual machine memory

--vcpus=2 is the number of cpu cores

password=123123 is the password required to access the virtual machine interface (you can delete it)

-cdrom=/var/lib/libvirt/images/CentOS-7-x86_64-Minimal-2003.iso is a mirror path, download it yourself Aliyun centos7

-disk path=/var/lib/libvirt/images/CentOS7.qcow2 disk storage path

size=40 disk size

Check for success

$ virsh list --all
 Id   Name           State
------------------------------
 1    centos7_2003   running

Configure virtual machine static ip

$ vi /etc/sysconfig/network-scripts/ifcfg-eth0

# The contents are as follows
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static # Need to change to static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=c45492fe-bc6f-40f4-a799-e620660c5b61
DEVICE=eth0
ONBOOT=yes # Need to change to yes

IPADDR=192.168.0.100 # ip over the same network as br0
METMASK=255.255.255.0 # Same mask as br0
DNS1=114.114.114.114
GATEWAY=192.168.0.1 # Same gateway as br0
IPV6_PEERNDS=yes
IPV6_PEEROUTES=yes
IPV6_PRIVACY=no
# End of Content

View Connectivity

#Restart Network
$ systemctl restart NetworkManager

# See if the network is connected
$ ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114) 56(84) bytes of data.
64 bytes from 114.114.114.114: icmp_seq=1 ttl=94 time=39.9 ms
64 bytes from 114.114.114.114: icmp_seq=2 ttl=68 time=39.7 ms
$ ping www.baidu.com
PING www.a.shifen.com (220.181.38.149) 56(84) bytes of data.
64 bytes from 220.181.38.149 (220.181.38.149): icmp_seq=1 ttl=52 time=43.6 ms
64 bytes from 220.181.38.149 (220.181.38.149): icmp_seq=2 ttl=52 time=43.7 ms

summary

When the host is restarted, all related networks are automatically created and tested.

kvm build

Bridge building, foreign websites, may not always open

Topics: Linux