ubuntu用kvm启动虚拟机搭建调试环境
- 查看系统是否支持硬件加速
1
2sudo apt install -y cpu-checker
$(kvm-ok)
一般物理机在物理机上执行都是支持硬件加速
下载kvm组件1
sudo apt install -y qemu qemu-kvm libvirt-daemon-system libvirt-clients virt-manager virtinst bridge-utils
下载系统iso镜像文件
- 国内下载源
1 | ### 清华源 ubuntu-release 中存放的为系统镜像 |
使用virsh 虚拟机管理工具启动虚拟机
1 |
|
如果启动虚拟机报错
1 |
|
changing /etc/libvirt/qemu.conf file1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23# user = "100" # A user named "100" or a user with uid=100
#
###
# cancel this "#"
###
# user = "root"
# The group for QEMU processes run by the system instance. It can be
# specified in a similar way to user.
###
# cancel this "#"
###
# group = "root"
# Whether libvirt should dynamically change file ownership
# to match the configured user/group above. Defaults to 1.
# Set to 0 to disable file ownership changes.
#dynamic_ownership = 1
启动后通过vnc连接目标虚拟机,然后安装目标操作系统,安装完成后会自动关机,然后进入宿主机执行命令启动目标虚拟机
1 | ### 列出所有虚拟机(包括未启动的虚拟机) |
进入虚拟机后安装ssh服务
1 | sudo apt update |
编辑网络创建网桥 vim /etc/netplan/01-netcfg.yaml
1 | network: |
在host中设置端口转发规则,因为是NAT模式,所以其他机器如果想要远程连接虚拟机,则需要在host中设置端口转发:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
set -o errexit
function enable_forward {
echo setup_forward_port
# execute once
KVM_ADAPTER_NAME="virbr0"
KVM_SUBNET="192.168.122.0/24"
WAN_ADAPTER_NAME="eno2"
# allow virtual adapter to accept packets from outside the host
iptables -I FORWARD -i $WAN_ADAPTER_NAME -o $KVM_ADAPTER_NAME -d $KVM_SUBNET -j ACCEPT
iptables -I FORWARD -i $KVM_ADAPTER_NAME -o $WAN_ADAPTER_NAME -s $KVM_SUBNET -j ACCEPT
#WAN_ADAPTER_NAME="cni0"
## allow virtual adapter to accept packets from host k8s container
## forward change destination, but not change source interface
#iptables -I FORWARD -i $WAN_ADAPTER_NAME -o $KVM_ADAPTER_NAME -d $KVM_SUBNET -j ACCEPT
#iptables -I FORWARD -i $KVM_ADAPTER_NAME -o $WAN_ADAPTER_NAME -s $KVM_SUBNET -j ACCEPT
## iptables -I FORWARD -s 10.244.0.0/16 -d $KVM_SUBNET -j ACCEPT
#WAN_ADAPTER_NAME="docker0"
## allow virtual adapter to accept packets from host docker container
## forward change destination, but not change source interface
#iptables -I FORWARD -i $WAN_ADAPTER_NAME -o $KVM_ADAPTER_NAME -d $KVM_SUBNET -j ACCEPT
#iptables -I FORWARD -i $KVM_ADAPTER_NAME -o $WAN_ADAPTER_NAME -s $KVM_SUBNET -j ACCEPT
}
function setup_forward_port {
KVM_ADAPTER_HOST=$1
WAN_PORT=$2
KVM_PORT=$3
echo setup_forward_port $KVM_ADAPTER_HOST
adapter_hosts=("10.1.36.45")
for WAN_ADAPTER_HOST in "${adapter_hosts[@]}"
do
¦ echo "Forwarding wan_adapter_hosts: $WAN_ADAPTER_HOST"
¦ # forward ports from outer-host to guest
¦ iptables -t nat -I PREROUTING -d $WAN_ADAPTER_HOST -p tcp --dport $WAN_PORT -j DNAT --to-destination $KVM_ADAPTER_HOST:$KVM_PORT
¦ # forward ports from inner-host to guest
¦ iptables -t nat -I OUTPUT -d $WAN_ADAPTER_HOST -p tcp --dport $WAN_PORT -j DNAT --to-destination $KVM_ADAPTER_HOST:$KVM_PORT
done
}
function list_forward {
KVM_ADAPTER_HOST=$1
echo list_forward $KVM_ADAPTER_HOST
echo "Processing chain: PREROUTING"
iptables --line-numbers --list PREROUTING -t nat | awk -F: '$3=="'$KVM_ADAPTER_HOST'"''{print}'
echo "Processing chain: OUTPUT"
iptables --line-numbers --list OUTPUT -t nat | awk -F: '$3=="'$KVM_ADAPTER_HOST'"''{print}'
}
function list_forward_port {
KVM_ADAPTER_HOST=$1
KVM_PORT=$2
echo list_forward_port $KVM_ADAPTER_HOST
iptables --line-numbers --list PREROUTING -t nat | awk '$9=="to:'$KVM_ADAPTER_HOST':'$KVM_PORT'" {print}'
iptables --line-numbers --list OUTPUT -t nat | awk '$9=="to:'$KVM_ADAPTER_HOST':'$KVM_PORT'" {print}'
# iptables -t nat -nvL OUTPUT
}
function clear_forward_port {
KVM_ADAPTER_HOST=$1
KVM_PORT=$2
echo clear_forward_port $KVM_ADAPTER_HOST
iptables_chains=("PREROUTING" "OUTPUT")
for chain in "${iptables_chains[@]}"
do
¦ echo "Processing chain: $chain"
¦ for line_num in $(iptables --line-numbers --list $chain -t nat | awk '$9=="to:'$KVM_ADAPTER_HOST':'$KVM_PORT'" {print $1}')
¦ do
¦ ¦ # You can't just delete lines here because the line numbers get reordered
¦ ¦ # after deletion, which would mean after the first one you're deleting the
¦ ¦ # wrong line. Instead put them in a reverse ordered list.
¦ ¦ LINES="$line_num $LINES"
¦ done
¦ # Delete the lines, last to first.
¦ for line in $LINES
¦ do
¦ ¦ # echo $line
¦ ¦ iptables -t nat -D $chain $line
¦ done
¦ unset LINES
done
}
enable_forward
# setup iptables
# ubuntu desktop
KVM_ADAPTER_HOST="192.168.122.128"
setup_forward_port $KVM_ADAPTER_HOST 22622 22 # ssh
# ubuntu server
# KVM_ADAPTER_HOST="192.168.122.121"
# setup_forward_port $KVM_ADAPTER_HOST 22122 22 # ssh
list_forward $KVM_ADAPTER_HOST
#KVM_ADAPTER_HOST="192.168.122.79"
#clear_forward_port $KVM_ADAPTER_HOST 22 # ssh