🌐 VI | EN

How to Resize the OpenWrt RootFS for EXT4 and SquashFS

Nghia Phan
Nghia Phan
🌐 Bản tiếng Việt Technical Guide Views

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.

{{< notice “tip” >}}

  • 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. {{< /notice >}}

  • 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:

cat << "EOF" > /etc/uci-defaults/70-rootpt-resizeif [ ! -e /etc/rootpt-resize ] \&& type parted > /dev/null \&& lock -n /var/lock/root-resizethenROOT_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 donetouch /etc/rootpt-resizerebootfiexit 1EOFcat << "EOF" > /etc/uci-defaults/80-rootfs-resizeif [ ! -e /etc/rootfs-resize ] \&& [ -e /etc/rootpt-resize ] \&& type losetup > /dev/null \&& type resize2fs > /dev/null \&& lock -n /var/lock/root-resizethenROOT_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}" ]thenLOOP_DEV="$(losetup -f)"losetup "${LOOP_DEV}" "${ROOT_DEV}"firesize2fs -f "${LOOP_DEV}"mount_root donetouch /etc/rootfs-resizerebootfiexit 1EOFcat << "EOF" >> /etc/sysupgrade.conf/etc/uci-defaults/70-rootpt-resize/etc/uci-defaults/80-rootfs-resizeEOF

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

{{< steps >}} Install the required packages and run the script:

opkg update opkg install parted losetup resize2fs sh /etc/uci-defaults/70-rootpt-resize
{{< /steps >}}

Or download and run the OpenWrt script:

{{< steps >}}

wget -U "" -O expand-root.sh “https://openwrt.org/_export/code/docs/guide-user/advanced/expand_root?codeblock=0” . ./expand-root.sh
{{< /steps >}}

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

Method 2 — manually resize an EXT4 root filesystem

{{< notice “warning” >}} This method applies to EXT4 partitions only. {{< /notice >}}

{{< steps >}} 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
{{< /steps >}}

A typical session looks like this:

root@OpenWrt:~# partedGNU Parted 3.4Using /dev/mmcblk0Welcome to GNU Parted! Type 'help' to view a list of commands.(parted) printModel: SD LX32G (sd/mmc)Disk /dev/mmcblk0: 31.7GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk 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) printModel: SD LX32G (sd/mmc)Disk /dev/mmcblk0: 31.7GBSector size (logical/physical): 512B/512BPartition Table: msdosDisk 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) quitInformation: You may need to update /etc/fstab.root@OpenWrt:~#

{{< notice “warning” >}} If resizepart 2 100% reports that /dev/mmcblk0p2 is in use, answer n, quit Parted, run umount /dev/mmcblk0p2, and repeat the operation. {{< /notice >}}

{{< steps >}} 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
{{< /steps >}}

Check the result:

root@OpenWrt:~# df -hFilesystem                Size      Used Available Use% Mounted on/dev/root                 7.3G     14.5M     27.5G   0% /tmpfs                   495.8M     68.0K    495.7M   0% /tmptmpfs                   512.0K         0    512.0K   0% /dev

Good luck!

Comments & Discussion

Share your thoughts, ask questions and feedback

Markdown & QQ Emoji
Loading comments...