OpenWrtのrootfsをUSBメモリで拡張する方法

はじめに
---

Wi-FiルータにOpenWrtをインストールしても、ROM領域が少ないのでいろいろパッケージをインストールできません。そこでUSBメモリを使いrootfsを拡張してみました。
今回はUSBメモリ全体をrootfsにしています。

参考：
- [OpwnWrt - Extroot configuration](https://openwrt.org/docs/guide-user/additional-software/extroot_configuration)
- [Zopfcode - 【前編】ルーターのrootfsを拡張してPythonをビルドする](http://www.zopfco.de/entry/2018/12/16/125210)

必要なパッケージをインストールします
---

```shell-session
root@GL-AR150:~# cat /etc/openwrt_release
DISTRIB_ID='LEDE'
DISTRIB_RELEASE='17.01.4'
DISTRIB_REVISION='r3560-79f57e422d'
DISTRIB_CODENAME='reboot'
DISTRIB_TARGET='ar71xx/generic'
DISTRIB_ARCH='mips_24kc'
DISTRIB_DESCRIPTION='LEDE Reboot 17.01.4 r3560-79f57e422d'
DISTRIB_TAINTS='busybox override'

root@GL-AR150:~# opkg update
root@GL-AR150:~# opkg install kmod-usb-storage
root@GL-AR150:~# opkg install usbutils
root@GL-AR150:~# opkg install gdisk
root@GL-AR150:~# opkg install block-mount
root@GL-AR150:~# opkg install e2fsprogs
root@GL-AR150:~# opkg install kmod-fs-ext4
root@GL-AR150:~# reboot
```

USBメモリ(/dev/sda)をフォーマットし、システム起動時に自動マウント(/mnt/sda1)されるようにします
---

```shell-session
root@GL-AR150:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 8192      8192         0 100% /rom
tmpfs                    30088       260     29828   1% tmp
/dev/mtdblock5            6400      5312      1088  83% /overlay
overlayfs:/overlay        6400      5312      1088  83% /
tmpfs                      512         0       512   0% /dev
root@GL-AR150:~# lsusb -t
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci-platform/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 480M
root@GL-AR150:~# ls -l /dev/sd*
brw-------    1 root     root        8,   0 Jan  1  1970 /dev/sda
brw-------    1 root     root        8,   1 Jan  1  1970 /dev/sda1

root@GL-AR150:~# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.1

Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/sda: 15633408 sectors, 7.5 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 6478B437-EECD-AA48-A2EC-56C50B396DFD
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 15633374
Partitions will be aligned on 2048-sector boundaries
Total free space is 15633341 sectors (7.5 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

Command (? for help): n
Partition number (1-128, default 1): 
First sector (34-15633374, default = 2048) or {+-}size{KMGTP}: 
Last sector (2048-15633374, default = 15633374) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Command (? for help): p
Disk /dev/sda: 15633408 sectors, 7.5 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 6478B437-EECD-AA48-A2EC-56C50B396DFD
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 15633374
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        15633374   7.5 GiB     8300  Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): Y
OK; writing new GUID partition table (GPT) to /dev/sda.
The operation has completed successfully.

root@GL-AR150:~# mkfs.ext4 /dev/sda1

root@GL-AR150:~# block info | grep "/dev/sd"
/dev/sda1: UUID="ad3607ef-35a3-43dd-b014-8deca070c780" VERSION="1.0" TYPE="ext4"

root@GL-AR150:~# block detect > /etc/config/fstab
root@GL-AR150:~# uci set fstab.@mount[0].enabled='1'
root@GL-AR150:~# uci commit
root@GL-AR150:~# reboot
```

```shell-session
root@GL-AR150:~# uci show fstab
fstab.@global[0]=global
fstab.@global[0].anon_swap='0'
fstab.@global[0].anon_mount='0'
fstab.@global[0].auto_swap='1'
fstab.@global[0].auto_mount='1'
fstab.@global[0].delay_root='5'
fstab.@global[0].check_fs='0'
fstab.@mount[0]=mount
fstab.@mount[0].target='/mnt/sda1'
fstab.@mount[0].uuid='ad3607ef-35a3-43dd-b014-8deca070c780'
fstab.@mount[0].enabled='1'
root@GL-AR150:~# block info
/dev/mtdblock4: UUID="93bc83d3-4b85b167-6f217cd4-25ceb869" VERSION="4.0" MOUNT="/rom" TYPE="squashfs"
/dev/mtdblock5: MOUNT="/overlay" TYPE="jffs2"
/dev/sda1: UUID="ad3607ef-35a3-43dd-b014-8deca070c780" VERSION="1.0" MOUNT="/mnt/sda1" TYPE="ext4"
root@GL-AR150:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 8192      8192         0 100% /rom
tmpfs                    30088       260     29828   1% /tmp
/dev/mtdblock5            6400      5312      1088  83% /overlay
overlayfs:/overlay        6400      5312      1088  83% /
tmpfs                      512         0       512   0% /dev
/dev/sda1              7627412     34368   7185880   0% /mnt/sda1
```

現在のrootfs(/overlay)をUSBメモリ(/mnt/sda1)にコピーし、USBメモリをrootfsに切り替えます
---

```shell-session
root@GL-AR150:~# tar -C /overlay -cvf - . | tar -C /mnt/sda1 -xf -
root@GL-AR150:~# block detect > /etc/config/fstab
root@GL-AR150:~# sed -i s/option$'\t'enabled$'\t'\'0\'/option$'\t'enabled$'\t'\'1\'/ /etc/config/fstab
root@GL-AR150:~# sed -i s#/mnt/sda1#/overlay# /etc/config/fstab
root@GL-AR150:~# cat /etc/config/fstab
config 'global'
	option	anon_swap	'0'
	option	anon_mount	'0'
	option	auto_swap	'1'
	option	auto_mount	'1'
	option	delay_root	'5'
	option	check_fs	'0'

config 'mount'
	option	target	'/overlay'
	option	uuid	'ad3607ef-35a3-43dd-b014-8deca070c780'
	option	enabled	'1'
root@GL-AR150:~# reboot
```

できました
---

```shell-session
root@GL-AR150:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 8192      8192         0 100% /rom
tmpfs                    30088       252     29836   1% /tmp
/dev/sda1              7627412     47652   7172596   1% /overlay
overlayfs:/overlay     7627412     47652   7172596   1% /
tmpfs                      512         0       512   0% /dev
```