系统环境:

  • 系统环境:debian-12.4.0-amd64-netinst.iso
  • 内核版本:debian 6.1.0-15-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.66-1 (2023-12-09) x86_64 GNU/Linux
  • KVM版本:

安装准备:

1.安装网络桥接工具:

1
sudo apt install -y bridge-utils

2.配置网卡桥接:

2.1.编辑网卡主配置文件 /etc/network/interfaces, 将其内容修改为:

1
2
3
4
5
6
7
8
9
10
11
12
13
leazhi@debian:~$ cat /etc/network/interfaces
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eno1
iface eno1 inet dhcp

2.2.在 /etc/network/interfaces.d 目录下创建名为 br0 的网卡配置文件,内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
leazhi@debian:~$ cat /etc/network/interfaces.d/br0 
# Briage network config
auto br0
iface br0 inet static
bridge_ports eno1
bridge_stp off
bridge_waitport 0
bridge_fd 0
bridge_maxwait 5
address 192.168.3.162
netmask 255.255.255.0
gateway 192.168.3.1
broadcast 192.168.3.255
#dns-nameservers 223.5.5.5 114.114.114.114

2.3.重启下网络服务:

1
sudo systemctl restart networking.service

安装KVM:

执行如下命令安装KVM:

1
sudo apt install --no-install-recommends qemu-system libvirt-clients libvirt-daemon-system libvirt-daemon virtinst libguestfs-tools libosinfo-bin -y

配置KVM:

为防止安装虚拟机报如下错误,则可以事先做如下配置:

1.防止安装虚拟机初始化时报:stderr=failed to parse default acl file /etc/qemu/bridge.conf’
Transport endpoint is not connected`
1
2
ERROR    /usr/lib/qemu/qemu-bridge-helper --use-vnet --br=br0 --fd=32: failed to communicate with bridge helper: stderr=failed to parse default acl file `/etc/qemu/bridge.conf'
: Transport endpoint is not connected

解决方法:

在 /etc/ 目录下创建 qemu 目录:

1
sudo mkdir /etc/qemu

然后在创建的目录下新建 bridge.conf 文件,并加入如下内容:

1
2
allow virbr0
allow all

2.防止安装虚拟机初始化时报:stderr=failed to create tun device: Operation not permitted : Transport endpoint is not connected:

1
2
ERROR    /usr/lib/qemu/qemu-bridge-helper --use-vnet --br=br0 --fd=32: failed to communicate with bridge helper: stderr=failed to create tun device: Operation not permitted
: Transport endpoint is not connected

解决方法:

先查看 /usr/lib/qemu/qemu-bridge-helper 权限:

1
2
leazhi@ubuntu2310:~$ sudo ls -al /usr/lib/qemu/qemu-bridge-helper 
-rwxr-xr-x 1 root root 696392 10月 4 06:13 /usr/lib/qemu/qemu-bridge-helper

然后将该文件的权限修改为 4755:

1
2
3
leazhi@ubuntu2310:~$ sudo chmod 4755 /usr/lib/qemu/qemu-bridge-helper 
leazhi@ubuntu2310:~$ sudo ls -al /usr/lib/qemu/qemu-bridge-helper
-rwsr-xr-x 1 root root 696392 10月 4 06:13 /usr/lib/qemu/qemu-bridge-helper

安装GUEST:

Debian

本地安装:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
virt-install --name vm001-debian12 --ram 4096 --disk path=/data/kvm/storage/vm001-debian12.4.qcow2,size=50 --vcpus 4 --os-variant debian11 --network bridge=br0 --graphics none  --console pty,target_type=serial --extra-args 'console=ttyS0,115200n8' --arch x86_64 --autostart  --location /data/kvm/images/debian-12.4.0-amd64-netinst.iso

# OR
leazhi@debian:/media/cdrom$ virt-install --name vm002-debian12 --ram 4096 --disk path=/data/kvm/storage/vm002-debian12.4.qcow2,size=50 --vcpus 4 --os-variant debian11 --network bridge=br0 --graphics none --console pty,target_type=serial --extra-args 'console=ttyS0,115200n8' --arch x86_64 --autostart --location /data/kvm/images/debian-12.4.0-amd64-netinst.iso,kernel=install.amd/vmlinuz,initrd=install.amd/initrd.gz
WARNING KVM acceleration not available, using 'qemu'

Starting install...
Retrieving 'vmlinuz' | 0 B 00:00:00 ...
Retrieving 'initrd.gz' | 0 B 00:00:00 ...
Allocating 'vm002-debian12.4.qcow2' | 0 B 00:00:00 ...
Creating domain... | 0 B 00:00:00
Running text console command: virsh --connect qemu:///session console vm002-debian12
Connected to domain 'vm002-debian12'
Escape character is ^] (Ctrl + ])
[ 0.000000] Linux version 6.1.0-15-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.66-1 (2023-12-09)
[ 0.000000] Command line: console=ttyS0,115200n8

从网络安装:

1
virt-install  --name vm002-debian12 --ram 4096 --disk path=/data/kvm/storage/vm002-debian12.qcow2,size=50 --vcpus 4 --os-variant debian11 --network bridge=br0 --graphics none  --console pty,target_type=serial --extra-args 'console=ttyS0,115200n8 serial' --autostart  --location 'https://mirrors.tuna.tsinghua.edu.cn/debian/dists/bookworm/main/installer-amd64/'

注意:如果虚拟机安装完成后无法上网,且无法 ping 局域网其它在线主机,那么请查看宿主机是否有安装 docker(如果有安装 docker ,那么请确认 iptables 是否有以下 2 条规则)

1
2
iptables -I FORWARD -i br0 -j ACCEPT
iptables -I FORWARD -O br0 -j ACCEPT