Bookmark

How to Resize the OpenWrt RootFS for EXT4 and SquashFS

When OpenWrt is flashed to storage such as a microSD card or USB drive on a Raspberry Pi, NanoPi, or Orange Pi, it may initially use only a small part of the available capacity.

Even if the card or drive is 32 GB or 64 GB, OpenWrt may use only a few hundred megabytes. This prevents installing larger packages or applications such as Docker and PassWall.

This guide explains how to expand OpenWrt's root filesystem to use more of the available SD-card or USB capacity.

  • There are two approaches.
    • Method 1 uses the OpenWrt expansion script and supports EXT4 and SquashFS.
    • Method 2 performs the EXT4 steps manually for cases where the script fails or every step needs to be visible.

The manual example is based on work by jeremienguyen on VOZ.VN, using a NanoPi R2S, a 32 GB SD card, and EXT4.

  • Use PuTTY or another SSH client to log in to OpenWrt.
  • Connect the router to the Internet before starting.

The basic difference between EXT4 and SquashFS

  • SquashFS is read-only, while EXT4 supports read and write operations.
  • With SquashFS, the root filesystem is normally mounted through OverlayFS. Changes are stored in the writable upper layer; resetting the device removes that layer and returns to the read-only base.
  • With EXT4, the system writes directly to the filesystem. Changes remain after a reset unless the complete system is flashed again.
  • SquashFS and OverlayFS can add some boot time, but they are useful for systems with small or slower storage such as NAND and SPI flash.

Method 1 — automatic script

The script:

  • Supports EXT4 and SquashFS.
  • Detects the root partition and filesystem.
  • Expands the root partition into available space.
  • Is preserved across firmware upgrades.
  • Runs automatically after an upgrade when needed.

Create the scripts over SSH:

 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
cat << "EOF" > /etc/uci-defaults/70-rootpt-resize
if [ ! -e /etc/rootpt-resize ] \
&& type parted > /dev/null \
&& lock -n /var/lock/root-resize
then
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e \
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DISK="/dev/$(basename "${ROOT_BLK%/*}")"
ROOT_PART="${ROOT_BLK##*[^0-9]}"
parted -f -s "${ROOT_DISK}" \
resizepart "${ROOT_PART}" 100%
mount_root done
touch /etc/rootpt-resize
reboot
fi
exit 1
EOF
cat << "EOF" > /etc/uci-defaults/80-rootfs-resize
if [ ! -e /etc/rootfs-resize ] \
&& [ -e /etc/rootpt-resize ] \
&& type losetup > /dev/null \
&& type resize2fs > /dev/null \
&& lock -n /var/lock/root-resize
then
ROOT_BLK="$(readlink -f /sys/dev/block/"$(awk -e \
'$9=="/dev/root"{print $3}' /proc/self/mountinfo)")"
ROOT_DEV="/dev/${ROOT_BLK##*/}"
LOOP_DEV="$(awk -e '$5=="/overlay"{print $9}' \
/proc/self/mountinfo)"
if [ -z "${LOOP_DEV}" ]
then
LOOP_DEV="$(losetup -f)"
losetup "${LOOP_DEV}" "${ROOT_DEV}"
fi
resize2fs -f "${LOOP_DEV}"
mount_root done
touch /etc/rootfs-resize
reboot
fi
exit 1
EOF
cat << "EOF" >> /etc/sysupgrade.conf
/etc/uci-defaults/70-rootpt-resize
/etc/uci-defaults/80-rootfs-resize
EOF

The first script creates /etc/uci-defaults/70-rootpt-resize.

Install the required packages and run the script:
opkg update opkg install parted losetup resize2fs sh /etc/uci-defaults/70-rootpt-resize

Or download and run the OpenWrt script:

<div class="codeBlock CMD">
wget -U "" -O expand-root.sh "https://openwrt.org/_export/code/docs/guide-user/advanced/expand_root?codeblock=0"
. ./expand-root.sh
</div>

If the automatic script fails, use the manual EXT4 procedure below.

Method 2 — manually resize an EXT4 root filesystem

This method applies to EXT4 partitions only.
Update package lists and install the required tools:
opkg update && opkg install parted tune2fs resize2fs
Check the current filesystem:
df -h # Example output # Filesystem Size Used Available Use% Mounted on # /dev/root 102.4M 14.5M 85.8M 14% / # tmpfs 495.8M 1.1M 494.7M 0% /tmp
Open Parted:
parted # GNU Parted 3.6 # Using /dev/mmcblk0 # (parted)
Run these commands:
print # Show the card partitions. resizepart 2 100% # Resize partition 2. # Answer yes if prompted. print quit

A typical session looks like this:

 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
root@OpenWrt:~# parted
GNU Parted 3.4
Using /dev/mmcblk0
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print
Model: SD LX32G (sd/mmc)
Disk /dev/mmcblk0: 31.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number  Start   End     Size    Type     File system  Flags
 1      33.6MB  50.3MB  16.8MB  primary  ext2         boot
 2      67.1MB  176MB   109MB   primary  ext2
(parted) resizepart 2 100%
Warning: Shrinking a partition can cause data loss, are you sure you want to continue? Yes/No? yes
(parted) print
Model: SD LX32G (sd/mmc)
Disk /dev/mmcblk0: 31.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number  Start    End     Size    Type     File system  Flags
 1      133.6MB  50.3MB  16.8MB  primary  ext2         boot
 2      67.1MB   30.0GB  29.9GB  primary  ext2
(parted) quit
Information: You may need to update /etc/fstab.
root@OpenWrt:~#
If resizepart 2 100% reports that /dev/mmcblk0p2 is in use, answer n, quit Parted, run umount /dev/mmcblk0p2, and repeat the operation.
After Parted finishes:
mount -o remount,ro / tune2fs -O^resize_inode /dev/mmcblk0p2 fsck.ext4 /dev/mmcblk0p2 reboot
After the router reboots, run:
resize2fs /dev/mmcblk0p2

Check the result:

1
2
3
4
5
root@OpenWrt:~# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root                 7.3G     14.5M     27.5G   0% /
tmpfs                   495.8M     68.0K    495.7M   0% /tmp
tmpfs                   512.0K         0    512.0K   0% /dev

Good luck!


0 Bình luận

Góp Ý / Bình Luận / Đánh giá