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

Posted by 西维蜀黍 on 2022-03-20, Last Modified on 2023-05-02

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.

Solution

Refer to https://openwrt.org/docs/guide-user/installation/openwrt_x86#resizing_partitions

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

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

utomated 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