Nas使用VirtualBox安装OpenWrt当软路由

想法

之前家里是光纤通过运营商的光猫桥接,然后一个路由器拨号,再连一个交换机。因为之前已经搞了台NAS,之前一直想整台HP Gen8的,有iLO管理口,再加上eSXi虚拟化,搞个软路由和NAS,这样比较完美。后面没买到便宜的Gen8,现在考虑功耗比较大,软Raid导致噪音比较大,就算了没整Gen8了。看看后面能不能换个大House,整个小的机柜,再搞个服务器玩玩吧。

现在路由器、交换机和NAS在一个小小的工具箱里面,夏天温度有点高,想想看把软路由整到NAS里面去吧。NAS用的OS是OMV5,基于Debian,比较熟悉,整个虚拟机安装OpenWrt应该是一个可行的方案。KVM一直不是太熟悉,直接用VirtualBox,之前VirtualBox没用过CLI,正好供机会学习下CLI控制。

这样的好处就是不用再单独开一个硬件路由器了,减少了一点点电费和热量,然后性能会强一些;缺点就是Host OS挂掉的话,就全屋不能上网了。

主要参考三篇官方文章OpenWrt on VirtualBox HowTo,VirtualBox Advanced,VirtualBox Manual

构架

NAS主板有一个千兆电口,然后自己备了一个PCI双网卡,但是因为主板的原因,PCI网卡的最高速率好像只有400M,所以想的就是一个PCI的口连ISP,板载千兆电口连交换机,多一个口备用。

Host OS Debian里面将板载网卡(enp1s0)设置成静态IP(192.168.1.2/32),PCI网卡(enp6s0/enp7s0)设置成启用(否则网卡为Down状态,无法使用)。

OpenWrt三网口,管理口(eth0),Hostonly,用于Host访问;WAN口(eth1),桥接物理网口,连接ISP网口;LAN口(eth2),桥接物理网口,连接交换机。大致示意图可参照OpenWrt说明

Networking Example

安装Virtual Box

官方文档安装VirtualBox和Extension Pack,另外需要安装编译工具和对应头文件,dkms,build-essential, linux-headers-$(uname -r),将用户加入用户组usermod -a -G vboxusers user

有一点就是如果内核有升级的话,VirtualBox就需要重新设置下/usr/sbin/vboxconfig,所以非必要情况下不升级内核,apt-mark hold linux-image-amd64 linux-headers-$(uname -r)

安装Extension Pack,VBoxManage extpack install ext.vbox-extpack

检查服务vboxautostart-service是否自动启动,这样Host启动时,会自动启动对应的vm。

安装OpenWrt

下载对应OpenWrt镜像,参考官方地址

创建vm

1
2
3
4
5
6
7
mkdir -p ~/vbox
cd ~/vbox
VBoxManage createvm openwrt --ostype Linux26_64 --registervm --default --basepath `pwd`
cd ~/vbox/openwrt
wget -c https://downloads.openwrt.org/releases/19.07.8/targets/x86/64/openwrt-19.07.8-x86-64-combined-ext4.img.gz
gzip -d openwrt-*x86-64-combined*.img.gz
VBoxManage convertfromraw --format VDI openwrt-*x86-64-combined*.img openwrt.vdi

VirtualBox创建一个Linux vm,设置基础目录为~/vbox/openwrt,下载对应镜像,并转换为VirtualBox可用的文件。

修改vm配置

1
2
3
4
5
6
VBoxManage hostonlyif create
VBoxManage hostonlyif ipconfig vboxnet0
VBoxManage hostonlyif ipconfig vboxnet0 --ip 192.168.56.1 --netmask 255.255.255.0
VBoxManage dhcpserver add --network=vboxnet0 --server-ip=192.168.56.1 --lower-ip=192.168.56.101 --upper-ip=192.168.56.254 --etmask=255.255.255.0 --enable

VBoxManage modifyvm openwrt --memory 128 --boot1 disk --boot2 none --boot3 none --nic1 hostonly --nictype1 virtio --hostonlyadapter1 vboxnet0 --nic2 bridged --nictype2 virtio --bridgeadapter2 enp6s0 --nic3 bridged --nictype3 virtio --bridgeadapter3 enp1s0 --mouse ps2 --keyboard ps2 --audio none --usb off --usbehci off --usbxhci off --vrde on --vrdeport 10001 --audostart-enabled of --autostart-delay 120

这里主要是创建一个hostonly网络,并设置好IP,DHCP等,Host的IP是192.168.56.1,等下Openwrt里面再设置管理口的IP。然后修改openwrt vm的内存为128,只从硬盘启动,网卡1为hostonly管理网卡,网卡2为bridged的WAN(enp6s0)口,网卡3为bridged的LAN(enp1s0)口。然后关闭声卡,USB。

VBoxManage Manual

挂载OpenWrt镜像

1
2
VBoxManage showvminfo openwrt | grep 'Storage Controller'
VBoxManage storageatt openwrt --storagectl 'SATA' --device 0 --port 0 --type hdd --medium ~/vbox/openwrt/openwrt.vdi

正常默认情况下有个IDE和SATA,IDE用于光驱,SATA用于硬盘使用。然后将openwrt的镜像挂载为硬盘。

虚拟机随host自动启动

参考Manual,AutoStart VirtualBox VMs on System Boot on Linux

1
VBoxManage modifyvm openwrt --autostart-enabled on --autostart-delay 10

远程控制vrde

参考Manual

1
VBoxManage modifyvm openwrt --vrde on --vrde-port 10001

这样就可以通过Host OS的10001端口进行远程操作,直接使用windows的mstsc可以连接,linux的rdesktop。

OpenWrt网络配置

前面配置好OpenWrt后,先将网卡设置成断开,然后启动OpenWrt,防止网络冲突啥的。

1
2
3
VBoxManage controlvm openwrt poweroff
VBoxManage modifyvm openwrt --cableconnected1 off --cableconnected2 off --cableconnected3 off
VBoxManage startvm --type headless

然后远程连上10001端口进行网络设置,将三张网卡分别设置好。

/etc/config/network
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
config interface 'loopback'
option ifname 'lo'
option proto 'static'
option ipaddr '127.0.0.1'
option netmask '255.0.0.0'

config globals 'globals'

config interface 'lan'
option ifname 'eth2'
option proto 'static'
option netmask '255.255.255.0'
option ip6assign '60'
option ipaddr '192.168.1.1'

config interface 'wan'
option ifname 'eth1'
option proto 'pppoe'
option password 'password'
option ipv6 'auto'
option username 'username'

config interface 'mng'
option proto 'static'
option netmask '255.255.255.0'
option ifname 'eth0'
option ipaddr '192.168.56.2'

配置DHCP,主要是为了使用IPv6,见lan配置处。

/etc/config/dhcp
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
config dnsmasq
option domainneeded '1'
option localise_queries '1'
option rebind_protection '1'
option rebind_localhost '1'
option local '/lan/'
option domain 'lan'
option expandhosts '1'
option authoritative '1'
option readethers '1'
option leasefile '/tmp/dhcp.leases'
option resolvfile '/tmp/resolv.conf.auto'
option localservice '1'
option confdir '/tmp/dnsmasq.d'

config dhcp 'lan'
option interface 'lan'
option start '100'
option limit '150'
option leasetime '12h'
option ra_management '1'
option ra 'server'
option dhcpv6 'server'
option ndp 'relay'

config dhcp 'wan'
option interface 'wan'
option ignore '1'

取消ipv6防火墙,禁止LAN口上网,需要授权才能使用。见OpenWrt限制设备连接Internet。如没有此需求,将forwarding段的enabled选项设置为1即可。

/etc/config/firewall
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
config defaults
option syn_flood '1'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'REJECT'
option disable_ipv6 '1'
option drop_invalid '1'

config zone
option name 'lan'
option input 'ACCEPT'
option output 'ACCEPT'
option forward 'ACCEPT'
list network 'lan'

config zone
option name 'wan'
option input 'REJECT'
option output 'ACCEPT'
option forward 'REJECT'
option masq '1'
option mtu_fix '1'
list network 'wan'

config forwarding
option src 'lan'
option dest 'wan'
option enabled '0'

一切就绪后就可以将网卡连接上使用了。

1
2
3
VBoxManage controlvm openwrt poweroff
VBoxManage modifyvm openwrt --cableconnected1 on --cableconnected2 on --cableconnected3 on
VBoxManage startvm --type headless