当前位置: 首页 > news >正文

虚拟机下的OpenWrt磁盘Overlay扩容

方式一:使用新磁盘扩容(推荐)
这种方式不仅适用于虚拟机,也适用于裸机安装的OpenWrt系统。

给新磁盘分区并格式化

对于裸机,可使用USB、SATA等接口添加新硬盘;对于虚拟机,可在虚拟机配置项中添加新虚拟磁盘。

以虚拟机为例,我这里添加一个1GB的新虚拟磁盘。

添加完成后进入OpenWrt的Shell。(通过ssh或者直接使用虚拟化平台提供的控制台)

安装分区需要用到的工具:

opkg update
opkg install fdisk kmod-fs-ext4

查看系统现有的磁盘和分区信息,我使用的是原版的OpenWrt,默认只有120M大小的磁盘,安装几个插件,空间就已经捉襟见肘了。/dev/sda是原有的系统盘,/dev/sdb是我刚添加的1G虚拟磁盘。

root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 4.0M      4.0M         0 100% /rom
tmpfs                    54.1M      2.3M     51.8M   4% /tmp
/dev/loop0               92.8M     73.7M     12.1M  86% /overlay
overlayfs:/overlay       92.8M     73.7M     12.1M  86% /
/dev/sda1                15.7M      4.8M     10.6M  31% /boot
/dev/sda1                15.7M      4.8M     10.6M  31% /boot
tmpfs                   512.0K         0    512.0K   0% /devroot@OpenWrt:~# fdisk -l
Disk /dev/loop0: 100 MiB, 104857600 bytes, 204800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytesDisk /dev/sda: 120.5 MiB, 126353408 bytes, 246784 sectors
Disk model: Virtual disk
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: 0x529e51f2Device     Boot Start    End Sectors  Size Id Type
/dev/sda1  *      512  33279   32768   16M 83 Linux
/dev/sda2       33792 246783  212992  104M 83 LinuxDisk /dev/sdb: 1 GiB, 1073741824 bytes, 2097152 sectors
Disk model: Virtual disk
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

使用fdisk给新磁盘分区,全部使用默认值就可以了。最后输入w保存分区表并退出。

root@OpenWrt:~# fdisk /dev/sdbWelcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x31c553f5.Command (m for help): n
Partition typep   primary (0 primary, 0 extended, 4 free)e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097151, default 2097151):Created a new partition 1 of type 'Linux' and of size 1023 MiB.Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

使用ext4文件系统格式化分区。

root@OpenWrt:~# mkfs.ext4 /dev/sdb1
mke2fs 1.45.6 (20-Mar-2020)
Creating filesystem with 261888 4k blocks and 65536 inodes
Filesystem UUID: 01002f49-6f61-46d4-9ab1-c2d38324bc45
Superblock backups stored on blocks:32768, 98304, 163840, 229376Allocating group tables: done
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

复制原Overlay分区到新分区

首先创建一个临时的挂载点,用于挂载上一步创建的新分区。

mkdir /new_overlay
mount /dev/sdb1 /new_overlay

将原/rom/overlay下的文件,全部拷贝到/new_overlay。

cp -a /rom/overlay /new_overlay

重新挂载Overlay分区

安装磁盘挂载使用的工具blkid。因为系统的磁盘名称生成没有规律,所以一般使用磁盘的分区UUID来挂载。

opkg update
opkg install blkid

查看新磁盘的UUID并记录下来备用。

root@OpenWrt:~# blkid
/dev/loop0: LABEL="rootfs_data" UUID="ce382a4e-ad60-41dc-80da-f6851a25de1e" BLOCK_SIZE="1024" TYPE="ext4"
/dev/sdb1: UUID="01002f49-6f61-46d4-9ab1-c2d38324bc45" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="31c553f5-01"
/dev/sda1: LABEL="kernel" UUID="84173db5-fa99-e35a-95c6-28613cc79ea9" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="529e51f2-01"
/dev/sda2: TYPE="squashfs" PARTUUID="529e51f2-02"

修改系统挂载配置,替换原来的Overlay分区。用新磁盘的UUID替换掉原来/overlay挂载点的UUID。并且enabled改为1。

vim /etc/config/fstabconfig globaloption anon_swap '0'option anon_mount '0'option auto_swap '1'option auto_mount '1'option delay_root '5'option check_fs '0'config mountoption target '/rom/overlay'# 替换掉这里的UUID# option uuid 'ce382a4e-ad60-41dc-80da-f6851a25de1e'option uuid '01002f49-6f61-46d4-9ab1-c2d38324bc45'option enabled '1'

最后重启系统,重新进入Shell查看分区信息。已经使用新的分区作为Overlay了。

LuCI界面也提供挂载点的配置,原版OpenWrt需要安装block-mount包,并且重启系统后方能在System菜单里看到。使用LuCI界面配置会更加直观友好。此外官方提示non-MTD设备启动时不读取/etc/config/fstab (https://openwrt.org/docs/guide-user/additional-software/extroot_configuration)[https://openwrt.org/docs/guide-user/additional-software/extroot_configuration),需要打补丁才能解决。
一些第三方的固件是有这个补丁的,可以直接使用。但是原版没有,如果你不会打补丁,那么就用下面的这种方式吧。

方式二:在原有磁盘上扩容
这种方式适用于虚拟机,可以直接修改虚拟磁盘大小达到扩容的目的。

更改虚拟磁盘大小

关闭虚拟机,修改磁盘大小。

扩容磁盘大小

修改磁盘大小需要在另一个系统上进行操作,已经挂载的磁盘无法进行分区大小调整。

使用fdisk进行分区操作,然后重新创建新的分区,新的分区起始扇区和原来的分区保持一致,但是结束扇区比原来的大,以此达到扩容的目的。(操作有风险,做好备份)

最简单的方式,关闭需要调整分区的虚拟机,创建一个新的虚拟机,然后将需要调整分区的磁盘挂载给新的虚拟机。这样在调整分区完成后,删除新的虚拟机,打开原来的虚拟机即可。

在新的虚拟机上安装需要用到的工具。

opkg update
opkg install lsblk blkid resize2fs losetup

查看分区信息,/dev/sdb就是我们需要操作的硬盘。

root@OpenWrt:~# lsblk
NAME   MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 1003.5M  0 loop /overlay
sda      8:0    0      1G  0 disk
├─sda1   8:1    0     16M  0 part /boot
└─sda2   8:2    0 1007.5M  0 part /rom
sdb      8:16   0      1G  0 disk
├─sdb1   8:17   0     16M  0 part
└─sdb2   8:18   0    104M  0 part
sr0     11:0    1   1024M  0 rom

载入/dev/sdb到loop设备。

root@OpenWrt:~# losetup -f /dev/sdb
root@OpenWrt:~# losetup
NAME       SIZELIMIT  OFFSET AUTOCLEAR RO BACK-FILE DIO LOG-SEC
/dev/loop1         0       0         0  0 /dev/sdb    0     512
/dev/loop0         0 4194304         1  0 /sda2       0     512

读取loop设备的分区信息,加载到系统。

root@OpenWrt:~# partx -a /dev/loop1
root@OpenWrt:~# lsblk
NAME      MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
loop0       7:0    0 1003.5M  0 loop /overlay
loop1       7:1    0      1G  0 loop
├─loop1p1 259:0    0     16M  0 part
└─loop1p2 259:1    0    104M  0 part
sda         8:0    0      1G  0 disk
├─sda1      8:1    0     16M  0 part /boot
└─sda2      8:2    0 1007.5M  0 part /rom
sdb         8:16   0      1G  0 disk
├─sdb1      8:17   0     16M  0 part
└─sdb2      8:18   0    104M  0 part
sr0        11:0    1   1024M  0 rom

从上面的结果可以看到,loop1p2就是我们需要扩容的分区,虽然已经扩展到1G,但是识别到的仍然是原来的大小。我们使用fdisk调整此分区的大小。

root@OpenWrt:~# fdisk /dev/loop1Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.Command (m for help): p
Disk /dev/loop1: 1 GiB, 1073741824 bytes, 2097152 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: 0x529e51f2Device       Boot Start    End Sectors  Size Id Type
/dev/loop1p1 *      512  33279   32768   16M 83 Linux
/dev/loop1p2      33792 246783  212992  104M 83 LinuxCommand (m for help): d
Partition number (1,2, default 2): 2Partition 2 has been deleted.Command (m for help): n
Partition typep   primary (1 primary, 0 extended, 3 free)e   extended (container for logical partitions)
Select (default p):Using default response p.
Partition number (2-4, default 2):
First sector (33280-2097151, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-2097151, default 2097151):Created a new partition 2 of type 'Linux' and of size 1007.5 MiB.
Partition #2 contains a squashfs signature.Do you want to remove the signature? [Y]es/[N]o: nCommand (m for help): wThe partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argumentThe kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or partx(8).

根据提示说明,分区表修改之后,内核仍然会使用旧的分区表,所以需要更新分区信息。再次查看,loop1p2分区已经是扩容后的大小了。

root@OpenWrt:~# partx -u /dev/loop1
root@OpenWrt:~# lsblk
NAME      MAJ:MIN RM    SIZE RO TYPE MOUNTPOINT
loop0       7:0    0 1003.5M  0 loop /overlay
loop1       7:1    0      1G  0 loop
├─loop1p1 259:2    0     16M  0 part
└─loop1p2 259:3    0 1007.5M  0 part
sda         8:0    0      1G  0 disk
├─sda1      8:1    0     16M  0 part /boot
└─sda2      8:2    0 1007.5M  0 part /rom
sdb         8:16   0      1G  0 disk
├─sdb1      8:17   0     16M  0 part
└─sdb2      8:18   0    104M  0 part
sr0        11:0    1   1024M  0 rom

最后卸载磁盘,关闭这个用于扩容的虚拟机,开启原来的虚拟机。

root@OpenWrt:~# partx -d /dev/loop1
root@OpenWrt:~# losetup -d /dev/loop1

在原来的虚拟机上执行:

resize2fs /dev/loop0
http://www.xdnf.cn/news/568891.html

相关文章:

  • genicamtl_lmi_gocator_objectmodel3d
  • 掌握HTTPX:从基础到高并发工程实践
  • 自由开发者计划 001:创建一个用于查看 Jupyter Notebook 的谷歌浏览器插件
  • FPGA降低功耗研究
  • 【76. 最小覆盖子串】
  • 操作系统 第四章 -2
  • Docker安装MinIO对象存储中间件
  • 图片识别(TransFormerCNNMLP)
  • 分享一些多模态文档解析思路
  • 蓝桥杯3503 更小的数
  • polarctf-web-[某函数的复仇]
  • 基于python的机器学习(七)—— 数据特征选择
  • PostgreSQL中通过查询数据插入到表的几种方法( SELECT INTO和INSERT INTO ... SELECT)
  • uniapp+vue3 随机、换一批
  • anaconda创建环境出错HTTPS
  • Captiks动捕与步态分析步态分析系统MOVIT:16μs 无线同步 + 快速校准,破解传统光电困局
  • 集成 OpenTelemetry + Grafana:实现 ABP VNext 的全链路可观测性
  • API面临哪些风险,如何做好API安全?
  • vue+threeJs 创建千色三角形宇宙
  • spring-retry
  • Pandoc3.7新特性:存在合并单元格的 HTML 表格会被保留为嵌入的 HTML 表格
  • WordPress Elementor零基础教程
  • 【Redis8】最新安装版与手动运行版
  • 电商项目-商品微服务-规格参数管理,分类与品牌管理需求分析
  • FFmpeg中使用Android Content协议打开文件设备
  • 免费的DDOS防护对网站业务有用吗?
  • 电子电路:什么是寄生电容?
  • 迅为RK3568开发板Linux_NVR_SDK系统开发编译 rootfs 提示分区小
  • 以太坊ETH创建代币完整教程
  • 2025.05.21华为暑期实习机考真题解析第一题