OpenWrt ext4系统下的根分区扩容指南
以下为本篇文章测试环境,因此仅对部分环境有借鉴意义
型号 | Raspberry Pi 4 Model B Rev 1.4 |
---|---|
架构 | ARMv8 Processor rev 3 (v8l) x 4 (1800Mhz, 48.2°C) |
目标平台 | bcm27xx/bcm2711 |
固件版本 | ImmortalWrt 24.10.0 r32824-6a73dae98c9c / LuCI openwrt-24.10 branch 25.017.24510~d42ec55 |
使用的固件为openwrt-24.10.0-bcm27xx-bcm2711-rpi-4-ext4-factory。非overlay系统
原理
首先扩展根分区,然后扩展文件系统 Expanding root partition(扩展根分区)
- 这指的是在物理或虚拟存储设备上增加分区的大小
- 涉及到修改分区表,调整分区的物理边界
- 使用类似 fdisk、parted 等工具来完成
- 这是底层的存储空间调整
Expanding root filesystem(扩展根文件系统)
- 这指的是调整文件系统本身的大小以使用更大的分区空间
- 在分区已经扩展后,需要执行此操作才能实际使用新增的空间
- 使用类似 resize2fs(ext4)、xfs_growfs(XFS)等工具来完成
- 这是文件系统层面的操作
简单来说:
- 扩展根分区是在存储设备层面增加可用空间
- 扩展根文件系统是让文件系统知道并能够使用这些新增的空间
在 OpenWRT 中,如果要完整地扩展根目录的可用空间,通常需要依次执行这两个步骤。先扩展分区,再扩展文件系统,这样才能真正利用到新增的存储空间。
准备
安装对应工具
首先,需要安装parted
resize2fs
losetup
opkg update
opkg install parted resize2fs lostup
检查OpenWrt系统固件
由于我的OpenWrt系统是ext4,因此并没有使用overlay,从命令输出也可以看出来
root@ImmortalWrt:~# block info
/dev/mmcblk0p1: UUID="67A3-2229" LABEL="boot" VERSION="FAT16" MOUNT="/boot" TYPE="vfat"
/dev/mmcblk0p2: UUID="ff313567-e9f1-5a5d-9895-3ba130b4a864" LABEL="rootfs" VERSION="1.0" MOUNT="/" TYPE="ext4"
root@ImmortalWrt:~# mount
/dev/root on / type ext4 (rw,noatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
cgroup2 on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime)
/dev/mmcblk0p1 on /boot type vfat (rw,noatime,fmask=0022,dmask=0022,codepage=437,iocharset=iso8859-1,shortname=mixed,errors=remount-ro)
tmpfs on /dev type tmpfs (rw,nosuid,noexec,noatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,noatime,mode=600,ptmxmode=000)
debugfs on /sys/kernel/debug type debugfs (rw,nosuid,nodev,noexec,noatime)
bpffs on /sys/fs/bpf type bpf (rw,nosuid,nodev,noexec,noatime,mode=700)
这确实不是 overlay 文件系统。从输出可以看到: 当前有两个分区:
- /dev/mmcblk0p1:FAT16格式,挂载在 /boot
- /dev/mmcblk0p2:ext4格式,挂载在根目录 /
目前的系统是直接运行在 ext4 分区上,而不是 overlay 文件系统
调整分区表
使用parted进行调整
得知统是直接运行在 ext4 分区上,因此我们使用parted检查分区并扩容
parted -l -s # 查看磁盘名和序号
parted /dev/mmcblk0 resizepart 2 100% #
此时运行命令,发现我们的分区表已成功调整
root@ImmortalWrt:~# parted /dev/mmcblk0 print
Model: SD ED2S5 (sd/mmc)
Disk /dev/mmcblk0: 128GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system Flags
1 4194kB 71.3MB 67.1MB primary fat16 boot, lba
2 75.5MB 128GB 128GB primary ext4
使用fdisk进行调整
如果你无法使用parted进行调整,请尝试使用fdisk调整。若上一步调整成功,则无需理会此步骤。
opkg install parted
首先查看分区表:
fdisk -l
Disk /dev/mmcblk0: 119.38 GiB, 128177930240 bytes, 250347520 sectors
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: 0x3c92ff95
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 8192 139263 131072 64M 6 FAT16
/dev/mmcblk0p2 147456 250347519 250200064 119.3G 83 Linux
发现我们需要修改的磁盘名为/dev/mmcblk0
,因此执行:
fdisk /dev/mmcblk0
按照图示内容进行修改即可,建议mmcblk0p2
按照原本的start
位置开始分区,避免其他错误。
root@ZhangOpenWrt:~# fdisk /dev/mmcblk0
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.
Command (m for help): p
Disk /dev/mmcblk0: 119.38 GiB, 128177930240 bytes, 250347520 sectors
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: 0x3c92ff95
Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 8192 139263 131072 64M 6 FAT16
/dev/mmcblk0p2 147456 761855 614400 300M 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):
First sector (2048-250347519, default 2048): 147456
Last sector, +/-sectors or +/-size{K,M,G,T,P} (147456-250347519, default 250347519):
Created a new partition 2 of type 'Linux' and of size 119.3 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.
最后使用fdisk -l
可以查看分区表已经成功修改。
拓展文件系统
Loop Device 是一个虚拟块设备接口,它可以将文件或块设备映射为一个新的块设备,这种映射提供了一个中间层,允许以更安全的方式操作底层设备
物理设备(如/dev/mmcblk0p2) -> Loop Device(/dev/loop0) -> resize2fs操作
方法:
# 1. 建立映射
losetup /dev/loop0 /dev/mmcblk0p2
# 此时创建了一个从 loop0 到实际分区的映射
# 2. 文件系统操作
resize2fs -f /dev/loop0
# resize2fs 通过 loop device 接口修改文件系统
# 3. 变更生效
reboot
# 重启使所有缓存的更改写入磁盘
一般来说,在进行第二步之后,可以使用
df -h
的方法查看硬盘分区大小是否改变,若改变,可重启保存更改。若未改变,请先排查错误原因,避免配置错误后重启导致的系统奔溃。