【OpenWrt】运行在ESXi中的OpenWrt增加硬盘容量

Posted by 西维蜀黍 on 2022-03-20, Last Modified on 2024-08-28

Knowledge

Partition layout

The x86 image is using the following partition layout (as seen from inside of the device):

  1. /dev/sda1 is a 16MB ext4 /boot partition where GRUB and the kernel are stored.
  2. /dev/sda2 is a 256MB partition containing the squashfs root filesystem and a read-write f2fs filesystem OR the ext4 root filesystem (depending on what image you have chosen).

Any additional space in the device is unallocated.

swopenwrt3 - squashfs

扩容原理:让 overlay 指向一个更大的分区,即 sda3

opkg update; opkg install losetup luci-app-hd-idle block-mount kmod-fs-ext4 kmod-usb-storage resize2fs cfdisk vim e2fsprogs fdisk

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

vim /etc/config/fstab
config 'mount'
  option target '/overlay'
  option uuid '08d710c7-b875-4274-8664-3cabeded7728'
  option fstype 'ext4'
  option options 'rw,noatime'
  option enabled '1'
  
root@OpenWrt:~# lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 99.8M  0 loop /overlay
sda        8:0    0   10G  0 disk
├─sda1     8:1    0   16M  0 part /boot
│                                 /boot
├─sda2     8:2    0  104M  0 part /rom
├─sda3     8:3    0  9.9G  0 part
└─sda128 259:0    0  239K  0 part

root@OpenWrt:~# fdisk -l
Disk /dev/loop0: 99.81 MiB, 104660992 bytes, 204416 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
GPT PMBR size mismatch (246303 != 20971519) will be corrected by write.

Disk /dev/sda: 10 GiB, 10737418240 bytes, 20971520 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: gpt
Disk identifier: 95A30341-41EB-C0DE-91D3-8344528D4200

Device      Start    End Sectors  Size Type
/dev/sda1     512  33279   32768   16M Linux filesystem
/dev/sda2   33280 246271  212992  104M Linux filesystem
/dev/sda128    34    511     478  239K BIOS boot

Partition table entries are not in disk order.

root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 4.3M      4.3M         0 100% /rom
tmpfs                   492.9M      1.0M    491.9M   0% /tmp
/dev/loop0               88.0M      1.0M     80.0M   1% /overlay
overlayfs:/overlay       88.0M      1.0M     80.0M   1% /
/dev/sda1                16.0M      6.1M      9.8M  38% /boot
/dev/sda1                16.0M      6.1M      9.8M  38% /boot
tmpfs                   512.0K         0    512.0K   0% /dev

root@OpenWrt:/overlay# block info
/dev/loop0: UUID="6ea141e9-395b-46e8-94c7-ee69b9135ec7" LABEL="rootfs_data" VERSION="1.0" MOUNT="/overlay" TYPE="ext4"
/dev/sda1: UUID="1234-ABCD" LABEL="kernel" VERSION="FAT16" MOUNT="/boot" TYPE="vfat"
/dev/sda2: UUID="0b0dcabc-c53831f2-b9eff295-dfb947a3" VERSION="4.0" MOUNT="/rom" TYPE="squashfs"

在 OpenWrt 使用 SquashFS 文件系统的情况下,扩容是比较复杂的,因为 SquashFS 是一种只读的压缩文件系统,无法直接修改其大小。通常,你会通过添加额外的存储或将现有的 SquashFS 文件系统与一个可写的文件系统(如 OverlayFS)组合来扩展存储空间。

OverlayFS 是 OpenWrt 默认使用的技术,用于将只读的 SquashFS 文件系统与一个可写的文件系统(通常是 JFFS2 或 ext4)结合,以允许写入操作。通过添加额外的存储(如 USB 闪存盘或 SD 卡),你可以扩展 /overlay 部分,从而间接扩展系统的存储容量。

ext4

opkg update; opkg install losetup luci-app-hd-idle block-mount kmod-fs-ext4 kmod-usb-storage resize2fs cfdisk vim e2fsprogs fdisk kmod-usb-core block-mount kmod-fs-ext4 kmod-usb-storage-extras e2fsprogs blkid parted losetup

root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                98.3M     16.7M     79.6M  17% /
tmpfs                   492.7M     92.0K    492.6M   0% /tmp
/dev/sdb1                16.0M      6.1M      9.8M  38% /boot
/dev/sdb1                16.0M      6.1M      9.8M  38% /boot
tmpfs                   512.0K         0    512.0K   0% /dev

root@OpenWrt:~# block info
/dev/sda1: UUID="1234-ABCD" LABEL="kernel" VERSION="FAT16" MOUNT="/boot" TYPE="vfat"
/dev/sda2: UUID="ff313567-e9f1-5a5d-9895-3ba130b4a864" LABEL="rootfs" VERSION="1.0" MOUNT="/" TYPE="ext4"

root@OpenWrt:~# fdisk -l
GPT PMBR size mismatch (246303 != 10485759) will be corrected by write.
Disk /dev/sda: 5 GiB, 5368709120 bytes, 10485760 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: gpt
Disk identifier: 95A30341-41EB-C0DE-91D3-8344528D4200

Device      Start    End Sectors  Size Type
/dev/sda1     512  33279   32768   16M Linux filesystem
/dev/sda2   33280 246271  212992  104M Linux filesystem
/dev/sda128    34    511     478  239K BIOS boot

Partition table entries are not in disk order.

root@OpenWrt:~# lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sda        8:0    0    5G  0 disk
├─sda1     8:1    0   16M  0 part /boot
│                                 /boot
├─sda2     8:2    0    5G  0 part /
└─sda128 259:0    0  239K  0 part

Solution

[Verified] Approach 1 - One-run Script

# verified on ext4
# Expand root partition/filesystem
sh /etc/uci-defaults/70-rootpt-resize

Approach 2

Rezising Partitions

# 查看当前容量
root@OpenWrt:~# df -h

# 查看虚拟机中物理硬盘(对于ESXI而言,是虚拟硬盘)的容量
root@OpenWrt:~# fdisk -l /dev/sdb

# resizing
root@OpenWrt:~# fdisk /dev/sda 

Be sure to resize the image before resizing partitions when installing in a VM.

  1. Install fdisk.
  2. Use fdisk to show the partitions.
  3. Write down the starting sector address of /dev/sda2 (which is the root partition).
  4. Use fdisk to delete the partition 2 (which is sda2), don’t write the changes to disk yet.
  5. Use fdisk to create a new partition 2, choose/type the starting sector address you wrote down earlier (as by default it will try to place it somewhere else), and leave the default end sector address (this will mean the partition will now use all available space).
  6. Write the partition table changes to disk. It may complain about partition signatures already present, write n to NOT remove the partition signature to proceed.

Manually

swopenwrt3 - via cfdisk
# 格式化这个新建的空间
mkfs.ext4 /dev/sda3
mkdir -p /mnt/sda3;mount /dev/sda3 /mnt/sda3
cp -r /overlay/* /mnt/sda3/
umount /mnt/sda3
swopenwrt3 - via fdisk

An example fdisk operation on a 10GB drive:

root@OpenWrt:~# fdisk /dev/sda 
Welcome 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/sda: 30 GiB, 32212254720 bytes, 62914560 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: 0x073036a8

Device     Boot Start     End Sectors    Size Id Type
/dev/sda1  *      512   33279   32768     16M 83 Linux
/dev/sda2       33792 2097151 2063360 1007.5M 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-62914559, default 34816): 33792
Last sector, +/-sectors or +/-size{K,M,G,T,P} (33792-62914559, default 62914559):

Created a new partition 2 of type 'Linux' and of size 30 GiB.
Partition #2 contains a ext4 signature.

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

Command (m for help): p

Disk /dev/sda: 30 GiB, 32212254720 bytes, 62914560 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: 0x073036a8

Device     Boot Start      End  Sectors Size Id Type
/dev/sda1  *      512    33279    32768  16M 83 Linux
/dev/sda2       33792 62914559 62880768  30G 83 Linux

Command (m for help): w
The partition table has been altered.
Syncing disks.

Script

automated method:

opkg update
opkg install fdisk
BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)"
DISK="${BOOT%%[0-9]*}"
PART="$((${BOOT##*[^0-9]}+1))"
ROOT="${DISK}${PART}"
OFFS="$(fdisk ${DISK} -l -o device,start | sed -n -e "\|^${ROOT}\s*|s///p")"
echo -e "p\nd\n${PART}\nn\np\n${PART}\n${OFFS}\n\nn\np\nw" | fdisk ${DISK}

Be sure to update the GPT partition UUID in the GRUB configuration when using efi.img.gz:

opkg update
opkg install lsblk
BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)"
DISK="${BOOT%%[0-9]*}"
PART="$((${BOOT##*[^0-9]}+1))"
ROOT="${DISK}${PART}"
UUID="$(lsblk -n -o PARTUUID ${ROOT})"
sed -i -r -e "s|(PARTUUID=)\S+|\1${UUID}|g" /boot/grub/grub.cfg

Resizing filesystem

Be sure to resize partitions before resizing filesystem. Note that online resizing should work for both F2FS and Ext4 on OpenWrt 19.07, although F2FS requires rebooting to apply changes.

Resizing F2FS overlay

Resize F2FS overlay for squashfs-combined.img.gz:

opkg update
opkg install losetup f2fs-tools
LOOP="$(losetup -n -O NAME | sort | sed -n -e "1p")"
ROOT="$(losetup -n -O BACK-FILE ${LOOP} | sed -e "s|^|/dev|")"
OFFS="$(losetup -n -O OFFSET ${LOOP})"
LOOP="$(losetup -f)"
losetup -o ${OFFS} ${LOOP} ${ROOT}
fsck.f2fs -f ${LOOP}
mount ${LOOP} /mnt
umount ${LOOP}
resize.f2fs ${LOOP}
reboot

Resizing Ext4 rootfs

Resize Ext4 rootfs for ext4-combined.img.gz:

root@OpenWrt:~/go/bin# opkg update; opkg install losetup resize2fs; BOOT="$(sed -n -e "/\s\/boot\s.*$/{s///p;q}" /etc/mtab)"; DISK="${BOOT%%[0-9]*}"; PART="$((${BOOT##*[^0-9]}+1))"; ROOT="${DISK}${PART}"; LOOP="$(losetup -f)"; losetup ${LOOP} ${ROOT}; fsck.ext4 -y ${LOOP}; resize2fs ${LOOP}; reboot
...
e2fsck 1.45.6 (20-Mar-2020)
rootfs was not cleanly unmounted, check forced.
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
Free blocks count wrong (22339, counted=22056).
Fix? yes

Free inodes count wrong (5279, counted=5205).
Fix? yes

Padding at end of inode bitmap is not set. Fix? yes


rootfs: ***** FILE SYSTEM WAS MODIFIED *****
rootfs: 1451/6656 files (0.1% non-contiguous), 4568/26624 blocks
resize2fs 1.45.6 (20-Mar-2020)
Resizing the filesystem on /dev/loop0 to 2617216 (4k) blocks.
The filesystem on /dev/loop0 is now 2617216 (4k) blocks long.

root@OpenWrt:~# Shared connection to 192.168.18.192 closed.

# 重启后,验证一下
root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 9.9G     16.6M      9.8G   0% /
tmpfs                   500.5M     56.0K    500.4M   0% /tmp
/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% /dev

Reference