大蠢驴博客

好好学习,天天向上。

0%

一种有效的OpenWrt扩容方式

介绍

  • 本文所描述的OpenWrt扩容方法,适用于ext4-combined-efi.img.gz固件,将OpenWrt运行在KVM虚拟机中
  • 扩容后的镜像可以正确挂载boot分区,OpenWrt的部分代码基于分区ID实现,本文所描述的方法不会修改分区ID
  • 运行于其他平台时,扩容思路也是类似的

Host操作

调整虚拟磁盘容量

1
2
gzip  -d openwrt-21.02.2-x86-64-generic-ext4-combined-efi.img.gz
qemu-img resize openwrt-21.02.2-x86-64-generic-ext4-combined-efi.img 1G

第三方系统操作

以下步骤需要将镜像文件挂载到某个第三方的操作系统上,这里我用的RockyLinux

记录目标分区的各ID信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@RockyLinux8 ~]# blkid
...
/dev/vdb1: SEC_TYPE="msdos" LABEL="kernel" UUID="1266-E1D4" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="EFI System Partition" PARTUUID="2785922e-ff64-67db-d7c1-d777e6133601"
/dev/vdb2: LABEL="rootfs" UUID="ff313567-e9f1-5a5d-9895-3ba130b4a864" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="2785922e-ff64-67db-d7c1-d777e6133602"
/dev/vdb128: PARTUUID="2785922e-ff64-67db-d7c1-d777e6133680"
...

[root@RockyLinux8 ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
...
vdb 252:16 0 1G 0 disk
├─vdb1 252:17 0 16M 0 part
├─vdb2 252:18 0 104M 0 part
└─vdb128 259:0 0 239K 0 part
...

调整分区大小

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
[root@RockyLinux8 ~]# fdisk /dev/vdb

Welcome to fdisk (util-linux 2.32.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

GPT PMBR size mismatch (246846 != 2097151) will be corrected by write.
The backup GPT table is corrupt, but the primary appears OK, so that will be used.
The backup GPT table is not on the end of the device. This problem will be corrected by write.

Command (m for help): p

Disk /dev/vdb: 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: gpt
Disk identifier: 2785922E-FF64-67DB-D7C1-D777E6133600

Device Start End Sectors Size Type
/dev/vdb1 512 33279 32768 16M EFI System
/dev/vdb2 33792 246783 212992 104M Microsoft basic data
/dev/vdb128 34 511 478 239K BIOS boot

Partition table entries are not in disk order.

Command (m for help): d
Partition number (1,2,128, default 128): 2

Partition 2 has been deleted.

Command (m for help): n
Partition number (2-127, default 2):
First sector (33280-2097118, default 34816): 33792
Last sector, +sectors or +size{K,M,G,T,P} (33792-2097118, default 2097118):

Created a new partition 2 of type 'Linux filesystem' and of size 1007.5 MiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N

Command (m for help): t
Partition number (1,2,128, default 128): 2
Partition type (type L to list all types): 11

Changed type of partition 'Linux filesystem' to 'Microsoft basic data'.

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

还原分区的ID信息

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
[root@RockyLinux8 ~]# gdisk /dev/vdb
GPT fdisk (gdisk) version 1.0.3

Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): x

Expert command (? for help): ?
a set attributes
c change partition GUID
d display the sector alignment value
e relocate backup data structures to the end of the disk
f randomize disk and partition unique GUIDs
g change disk GUID
h recompute CHS values in protective/hybrid MBR
i show detailed information on a partition
j move the main partition table
l set the sector alignment value
m return to main menu
n create a new protective MBR
o print protective MBR data
p print the partition table
q quit without saving changes
r recovery and transformation options (experts only)
s resize partition table
t transpose two partition table entries
u replicate partition table on new device
v verify disk
w write table to disk and exit
z zap (destroy) GPT data structures and exit
? print this menu

Expert command (? for help): c
Partition number (1-128): 2
Enter the partition's new unique GUID ('R' to randomize): 2785922e-ff64-67db-d7c1-d777e6133602
New GUID is 2785922E-FF64-67DB-D7C1-D777E6133602

Expert command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/vdb.
The operation has completed successfully.

如果需要修改UUID,用tune2fs

1
[root@RockyLinux8 ~]# tune2fs /dev/vdb2 -U ff313567-e9f1-5a5d-9895-3ba130b4a864

更新文件系统

1
[root@RockyLinux8 ~]# resize2fs /dev/vdb2