Create ubifs volumes in Linux (OpenWRT)

When using a router flashed with OpenWRT, you may find that the capacity of the filesystem is much smaller than the real capacity. For example, the router I use is Netgear WNDR4300 which has a 128MB Nand flash. But when using the 'df -h' command to inspect the space usage status, I surprisingly found that the space of the total mounting point '/' is only 14MB.

So to find out the reason and try to expand usable space, I used the command "dmesg" and "cat /proc/mtd" to collect the information of mtd devices.

[    0.634792] nand: device found, Manufacturer ID: 0x2c, Chip ID: 0xf1
[    0.641287] nand: Micron NAND 128MiB 3,3V 8-bit
[    0.645888] nand: 128 MiB, SLC, erase size: 128 KiB, page size: 2048, OOB size: 64
[    0.653625] Scanning device for bad blocks
[    0.661590] random: fast init done
[    0.744785] 12 cmdlinepart partitions found on MTD device ar934x-nfc
[    0.751249] Creating 12 MTD partitions on "ar934x-nfc":
[    0.756560] 0x000000000000-0x000000040000 : "u-boot"
[    0.763602] 0x000000040000-0x000000080000 : "u-boot-env"
[    0.771110] 0x000000080000-0x0000000c0000 : "caldata"
[    0.778401] 0x0000000c0000-0x000000140000 : "pot"
[    0.785241] 0x000000140000-0x000000340000 : "language"
[    0.792653] 0x000000340000-0x0000003c0000 : "config"
[    0.799845] 0x0000003c0000-0x0000006c0000 : "traffic_meter"
[    0.807612] 0x0000006c0000-0x0000008c0000 : "kernel"
[    0.814824] 0x0000008c0000-0x000001fc0000 : "ubi"
[    0.821866] 0x0000006c0000-0x000001fc0000 : "firmware"
[    1.184297] 0x000001fc0000-0x000002000000 : "caldata_backup"
[    1.192006] 0x000002000000-0x000008000000 : "reserved"


root@OpenWrt:~# cat /proc/mtd 
dev:    size   erasesize  name
mtd0: 00040000 00020000 "u-boot"
mtd1: 00040000 00020000 "u-boot-env"
mtd2: 00040000 00020000 "caldata"
mtd3: 00080000 00020000 "pot"
mtd4: 00200000 00020000 "language"
mtd5: 00080000 00020000 "config"
mtd6: 00300000 00020000 "traffic_meter"
mtd7: 00200000 00020000 "kernel"
mtd8: 01700000 00020000 "ubi"
mtd9: 01900000 00020000 "firmware"
mtd10: 00040000 00020000 "caldata_backup"
mtd11: 06000000 00020000 "reserved"

It showed clearly that the largest part mtd11 0x000002000000-0x000008000000 is marked as "reserved" which is 0x8000000-0x2000000 = 0x6000000 bytes or 6x16^6/1024/1024 = 96MB.

Now let's see if mtd11 is mounted.
root@OpenWrt:~# ls /dev
bus                 memory_bandwidth    mtd2ro              mtd7ro              mtdblock3           port                ttyS11              ttyS7               urandom
console             mtd0                mtd3                mtd8                mtdblock4           ppp                 ttyS12              ttyS8               watchdog
cpu_dma_latency     mtd0ro              mtd3ro              mtd8ro              mtdblock5           ptmx                ttyS13              ttyS9               zero
full                mtd1                mtd4                mtd9                mtdblock6           pts                 ttyS14              ubi0
gpiochip0           mtd10               mtd4ro              mtd9ro              mtdblock7           random              ttyS15              ubi0_0
gpiochip1           mtd10ro             mtd5                mtdblock0           mtdblock8           shm                 ttyS2               ubi0_1
gpiochip2           mtd11               mtd5ro              mtdblock1           mtdblock9           tty                 ttyS3              
hwrng               mtd11ro             mtd6                mtdblock10          network_latency     ttyS0               ttyS4              
kmsg                mtd1ro              mtd6ro              mtdblock11          network_throughput  ttyS1               ttyS5               ubi_ctrl
log                 mtd2                mtd7                mtdblock2           null                ttyS10              ttyS6               ubiblock0_0


root@OpenWrt:~# mount
/dev/root on /rom type squashfs (ro,relatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,noatime)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,noatime)
tmpfs on /tmp type tmpfs (rw,nosuid,nodev,noatime)
/dev/ubi0_1 on /overlay type ubifs (rw,noatime)
overlayfs:/overlay on / type overlay (rw,noatime,lowerdir=/,upperdir=/overlay/upper,workdir=/overlay/work)
tmpfs on /dev type tmpfs (rw,nosuid,relatime,size=512k,mode=755)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,mode=600,ptmxmode=000)
debugfs on /sys/kernel/debug type debugfs (rw,noatime)

root@OpenWrt:~# ubinfo
ubinfo 
UBI version:                    1
Count of UBI devices:           1
UBI control device major/minor: 10:59
Present UBI devices:            ubi0
root@OpenWrt:~# cat /sys/devices/virtual/ubi/ubi0/mtd_num
8

It's very clear that mtd11 is not mounted. I can see that mtd8 attached as ubi0 and then mounted as '/overlay' on '/'. So what I need to do is format mtd11 as ubifs and mount it to a directory.

First of all, make a directory for mounting.
#mkdir /data_disk

Then, make the ubifs I need.
# ubiformat /dev/mtd11 
# ubiattach -p /dev/mtd11 
# ubimkvol /dev/ubi1 -N data -s 90MiB 
# mount -t ubifs ubi1:data /data_disk 

Now let's check again.

root@OpenWrt:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 2560      2560         0 100% /rom
tmpfs                    62512       624     61888   1% /tmp
/dev/ubi0_1              14332     13396       168  99% /overlay
overlayfs:/overlay       14332     13396       168  99% /
tmpfs                      512         0       512   0% /dev
ubi1:data                83376        56     79028   0% /data_disk

Success!
Finally, add the following lines to /etc/rc.local to make it mount when router starts.
ubiattach -p /dev/mtd11
mount -t ubifs ubi1:data /data_disk



评论

此博客中的热门博文

Openwrt路由器上配置shadowsocks透明代理+gfwlist(PAC)

Configure shadowsocks transparent proxy + gfwlist(PAC) on OpenWRT Router

Configure V2Ray(Shadowsocks mode) + 加速:普通BBR/魔改BBR on KVM VPS (Tested on Ubuntu 16.04)