smart-os/03_run_qemu.sh
2022-08-03 12:52:40 +08:00

106 lines
2.9 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/sh
#---------------------
# 初始化公共环境
#---------------------
. ./common.sh
#---------------------
# 停掉 NAT
#---------------------
stop_nat() {
# 停掉 tap0
ip link set tap0 down
# 断开与网桥链接
brctl delif br0 tap0
# 停掉网桥
ip link set br0 down
# 删除设备 tap0
ip tuntap del mode tap tap0
# 删除网桥
brctl delbr br0
# 删除规则链
iptables -D FORWARD -s 192.168.100.0/24 -j ACCEPT
iptables -D FORWARD -d 192.168.100.0/24 -j ACCEPT
iptables -t nat -D POSTROUTING -s 192.168.100.0/24 ! -d 192.168.100.0/24 -j MASQUERADE
}
#---------------------
# 启动 NAT
#---------------------
start_nat() {
# 创建虚拟网桥
brctl addbr br0
# 创建虚拟tap设备
ip tuntap add dev tap0 mode tap
# 将tap设备介入网桥
brctl addif br0 tap0
# 配置网桥ip
ip addr add 192.168.100.1/24 dev br0
ip addr add 192.168.100.2/24 dev tap0
# 启动桥设备和虚拟网卡设备
ip link set br0 up
ip link set tap0 up
# 开启物理网卡的转发功能:
sysctl -w net.ipv4.ip_forward=1
# 配置iptables forward转发规则
# 在基本环境搭建这一节中设置了一个本地网络虚机只能访问host无法访问外网如果需要访问外网需要设置SNAT
iptables -t nat -A POSTROUTING -s 192.168.100.0/24 ! -d 192.168.100.0/24 -j MASQUERADE
# 如果有防火墙的特别是centos系统中记得放开防火墙
iptables -A FORWARD -s 192.168.100.0/24 -j ACCEPT
iptables -A FORWARD -d 192.168.100.0/24 -j ACCEPT
}
#----------------------
# DNS 服务
#----------------------
start_dns() {
# 准备dnsmasq配置文件,启动dnsmasq服务这样就能为虚拟机自动分配IP了
cat<<EOF>dnsmasq.conf
strict-order
pid-file=/var/run/dnsmasq.pid
except-interface=lo
bind-interfaces
listen-address=192.168.0.1
dhcp-range=192.168.100.2,192.168.100.254
dhcp-no-override
dhcp-leasefile=/var/lib/libvirt/dnsmasq/default.leases
dhcp-lease-max=253
dhcp-hostsfile=/var/lib/libvirt/dnsmasq/default.hostsfile
addn-hosts=/var/lib/libvirt/dnsmasq/default.addnhosts
EOF
/usr/sbin/dnsmasq --conf-file=./dnsmasq.conf
}
stop_dns() {
# 杀掉 dhcp 服务
killall dnsmasq
}
#----------------------------------------------
# 运行 smart-os 前,先启动这个,这样就能上网了
#----------------------------------------------
start_nat
#----------------------------------------------------
#
# 多硬盘测试 -hdb extra.img
#
#----------------------------------------------------
if [ "${with_sdb}" = false ]; then
sdb_img=""
else
./mk_sdb.sh
sdb_img="-hdb extra.img"
fi
rm -rf ./qemu.log
# 启动镜像 网络对应 run_nat.sh 里面的配置
qemu-system-x86_64 -drive format=raw,file=disk.img ${sdb_img} -netdev tap,id=nd0,ifname=tap0 -device e1000,netdev=nd0
#qemu-system-x86_64 -serial file:./qemu.log -drive format=raw,file=disk.img -netdev tap,id=nd0,ifname=tap0 -device e1000,netdev=nd0
# stop nat
stop_nat