How to Expand OpenWrt Overlay and Enable Swap

What is Overlay

Squashfs (.sfs) is a GPL open-source read-only compressed file system for Linux kernels. Squashfs can compress files, inodes, and directory structures within the file system.

Squashfs is designed specifically for general use in read-only file systems and can be applied to data backup or on computers with limited system resources.

The file system commonly used by OpenWRT is SquashFS, so it is recommended to download firmware that uses this file system.

The characteristics of this file system are: read-only and compressed.

How does a read-only file system manage to save settings and install software? It uses the /overlay partition. The firmware already includes an /overlay partition, but its pointer can be changed.

How to Expand OpenWrt Overlay and Enable Swap

Although the original files cannot be modified, we place the modifications in the overlay partition and then map them to the original location, so when reading, we can access our modified files. When the system fails or resets, the configuration files in the firmware are copied to the overlay layer, restoring the original settings.

Since the configuration of the /overlay pointer is also in the overlay layer, we can create a larger partition and point the overlay to this new partition, effectively expanding the overlay. This might be a bit convoluted.

Command Line Tools

  • lsblk

  • blkid

  • fdisk

  • mkswap

  • swapon

There are also some commands to view mount points: for example, df, mount -l, and view the contents of /etc/mtab; the latter two yield the same results.

First, lsblk lists the blocks, resulting in:

root@OpenWrt:~# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0    7:0    0 41.8M  0 loop /mnt/loop0
sda      8:0    1  3.8G  0 disk
├─sda1   8:1    1   16M  0 part /mnt/sda1
└─sda2   8:2    1  160M  0 part /rom

This is a soft router made from a USB stick, which is only 4G. However, it can be partitioned into several other sections. One is used to expand the overlay, while the rest is used as swap, preparing to install transmission, docker, and samba. A hard drive is mounted for network sharing.

To increase partitions: use the fdisk command. This command requires parameters, which is a hard disk device, such as /dev/sda. Then you enter the fdisk program’s commands until you exit fdisk. This is different from other commands.

root@OpenWrt:~#  fdisk /dev/sda

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

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

Command (m for help): p
Disk /dev/sda: 3.8 GiB, 4026531840 bytes, 7864320 sectors
Disk model: ProductCode
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: 0xd56b2d42

Device     Boot Start    End Sectors  Size Id Type
/dev/sda1  *      512  33279   32768   16M 83 Linux
/dev/sda2       33792 361471  327680  160M 83 Linux

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): 2
Value out of range.
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (3,4, default 3): 3
First sector (33280-7864319, default 362496):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (362496-7864319, default 7864319): +1G

Created a new partition 3 of type 'Linux' and of size 1 GiB.

Command (m for help): p
Disk /dev/sda: 3.8 GiB, 4026531840 bytes, 7864320 sectors
Disk model: ProductCode
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: 0xd56b2d42

Device     Boot  Start     End Sectors  Size Id Type
/dev/sda1  *       512   33279   32768   16M 83 Linux
/dev/sda2        33792  361471  327680  160M 83 Linux
/dev/sda3       362496 2459647 2097152    1G 83 Linux

Command (m for help): n
Partition type
   p   primary (3 primary, 0 extended, 1 free)
   e   extended (container for logical partitions)
Select (default e): p

Selected partition 4
First sector (33280-7864319, default 2459648):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2459648-7864319, default 7864319):

Created a new partition 4 of type 'Linux' and of size 2.6 GiB.

Command (m for help): p
Disk /dev/sda: 3.8 GiB, 4026531840 bytes, 7864320 sectors
Disk model: ProductCode
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: 0xd56b2d42

Device     Boot   Start     End Sectors  Size Id Type
/dev/sda1  *        512   33279   32768   16M 83 Linux
/dev/sda2         33792  361471  327680  160M 83 Linux
/dev/sda3        362496 2459647 2097152    1G 83 Linux
/dev/sda4       2459648 7864319 5404672  2.6G 83 Linux

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

At this point, the partitions have been completed and the partition table has been written.

Next, format the newly added partitions /dev/sda3 and /dev/sda4.

Format sda3 as ext4 and sda4 as swap, then turn on swap.

root@OpenWrt:~# mkfs.ext4 /dev/sda3
mke2fs 1.44.3 (10-July-2018)
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 13105391-0ee0-43e9-b6d9-c2d75053b951
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376

Allocating group tables: done
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

root@OpenWrt:~# mkswap /dev/sda4
Setting up swapspace version 1, size = 2767187968 bytes
root@OpenWrt:~# swapon /dev/sda4

Reboot the system.

The next step is to copy the contents of /overlay to sda3. However, you cannot directly copy to /dev/sda3, as it is just a hard disk device file, not a file system. Therefore, it needs to be mounted to a directory, usually requiring a file system identifier when mounting, so that a device can be mounted under a file system, allowing the entire system to see the files inside.

Mount /dev/sda3 to a directory; you can check where it was originally mounted using the df -h command. It should be seen that it was originally mounted under /mnt.

mount -t ext4 /dev/sda3 /mnt/sda3

If /mnt/sda3 does not exist, you can create it using mkdir /mnt/sda3.

Then check which directory loop0 points to and find that the overlay is in rom,

root@OpenWrt:/# df -h
Filesystem                Size      Used Available Use% Mounted on
/dev/root               118.3M    118.3M         0 100% /rom
tmpfs                   487.3M      1.9M    485.4M   0% /tmp
/dev/loop0               36.4M      1.9M     31.6M   6% /rom/overlay
overlayfs:/overlay       36.4M      1.9M     31.6M   6% /
/dev/sda1                15.7M      3.7M     11.7M  24% /boot
/dev/sda1                15.7M      3.7M     11.7M  24% /boot
tmpfs                   512.0K         0    512.0K   0% /dev
cgroup                  487.3M         0    487.3M   0% /sys/fs/cgroup
overlayfs:/overlay       36.4M      1.9M     31.6M   6% /opt/docker
/dev/loop0               36.4M      1.9M     31.6M   6% /mnt/loop0
/dev/sda1                15.7M      3.7M     11.7M  24% /mnt/sda1
/dev/sda3               975.9M      2.5M    906.2M   0% /mnt/sda3

Then copy all the contents of the overlay to sda3

root@OpenWrt:/rom/overlay# cp -r /rom/overlay/* /mnt/sda3

Then umount /dev/sda3. Check the mount point again with df -h to see that /mnt/sda3 is no longer there.

Exit the shell. Go to the router’s web interface: 【System】-【Mount Points】-【Add】 to enter the following interface. Select the 1G partition and mount it as overlay. This expands the overlay.

How to Expand OpenWrt Overlay and Enable Swap How to Expand OpenWrt Overlay and Enable Swap

Previous Recommendations

Common Advantages and Disadvantages of Open Source Router Systems (Firmware)

Is DD-WRT Good? Which is Better: ddwrt, OpenWrt, or Padavan?

OpenWrt System Same Network Segment Bridging Configuration Tutorial

Two Methods to Change LAN IP Address in OpenWrt (Direct Command Modification and Backend Modification)

Unlock SSH and Flash OpenWrt Firmware on Xiaomi Redmi Router AX3000 (WR30U Customized Version)

Leave a Comment

Your email address will not be published. Required fields are marked *