OpenWrt的安装和初步配置
使用纯净的 [[OpenWrt]] 镜像进行操作,并安装在[[群晖]]的 [[VMM]] 内。
下载镜像
- 官方网址是:OpenWrt
- 某个版本的全部镜像地址:releases/24.10.5
- 选择适合的镜像,这里选择的是 x86/64/generic-ext4-combined.img.gz
不同版本的快速选择
| 关键词 | 区别与建议 |
|---|---|
| ext4 vs squashfs | squashfs 是只读格式,支持“恢复出厂设置”,但调整分区很麻烦;ext4 是可读写格式,适合需要灵活调整空间容量的虚拟机。 |
| efi vs (空白) | efi 适用于开启了 UEFI 引导的虚拟机;群晖 VMM 默认是 Legacy BIOS,所以选不带 efi 的即可。 |
| rootfs | 仅包含文件系统,不含引导。这种镜像通常用于升级现有的系统,不适合第一次安装时作为启动盘。 |
安装镜像
- 解压文件为 img 后缀的镜像文件
- VMM 映像 硬盘映像里导入上述镜像文件
- VMM 虚拟机,导入,配置1C1G,分配10G存储,配置网卡为e1000提高兼容性
- 启动该虚拟机
修改网络
vi /etc/config/network
...
config interface 'lan'
...
option ipaddr '192.168.1.254'
option gateway '192.168.1.1'
list dns '114.114.114.114'
...
/etc/init.d/network restart
ping 192.168.1.1
ping google.com
安装 Web 界面(LuCI)
opkg update
opkg install luci
opkg install luci-i18n-base-zh-cn
浏览器直接访问OpenWrt即可。
扩容
不同文件格式的扩容方式不同,这里针对的是[[ext4]]格式。
- 输入命令进入分区工具:fdisk /dev/sda
- 查看当前分区: 输入
p并回车。你会看到/dev/sda1(引导)和/dev/sda2(根分区)。请记录下/dev/sda2的 Start(起始扇区)数值,通常是34816。 - 删除旧分区记录(别担心,数据还在内存里): 输入
d,然后输入2(选择删除第二个分区)。 - 新建分区: 输入
n,然后输入p(主分区),输入2。- First sector: 输入你刚才记录的 Start 数值(必须完全一致!)
- Last sector: 直接按 回车,默认会占用所有剩余空间。
- 处理签名: 如果系统询问
Do you want to remove the signature?,一定要输入N(保留签名),否则系统可能无法引导。 - 保存并退出: 输入
w保存更改。 - 重启 OpenWrt: 输入
reboot。只有重启后,内核才会加载新的分区表。
opkg update
opkg install fdisk e2fsprogs resize2fs
root@OpenWrt:~# fdisk /dev/sda
Welcome to fdisk (util-linux 2.40.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
This disk is currently in use - repartitioning is probably a bad idea.
It's recommended to umount all file systems, and swapoff all swap
partitions on this disk.
Command (m for help): p
Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xe73bfb4e
Device Boot Start End Sectors Size Id Type
/dev/sda1 * 512 33279 32768 16M 83 Linux
/dev/sda2 33792 246783 212992 104M 83 Linux
Command (m for help): d
Partition number (1,2, default 2): 2
Partition 2 has been deleted.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (33280-20971519, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-20971519, default 20971519): +5G
Created a new partition 2 of type 'Linux' and of size 5 GiB.
Partition #2 contains a ext4 signature.
Do you want to remove the signature? [Y]es/[N]o: n
Command (m for help): w
The partition table has been altered.
Syncing disks.
reboot
重启后重新配置文件系统这块,因为 OpenWrt 过于精简,所以相对操作比较复杂。
# 重启后的操作
opkg update
opkg install losetup
losetup /dev/loop0 /dev/sda2
resize2fs -f /dev/loop0
reboot
旁路由的通病
21.02 版本开始,底层网络架构有了变化,需要进行一些设置。
这是最关键的一步,解决了流量“只出不进”的问题:
- 进入 网络 (Network) -> 防火墙 (Firewall)。
- 找到 lan 区域(通常是绿色),点击 修改 (Edit)。
- 勾选 流量伪装 (IP masquerading)。
- 点击 保存并应用。
此时,本地的设备手动配置好ip和网关后,数据就会先从OpenWrt走。