LoginSignup
6
3

More than 5 years have passed since last update.

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

Posted at

はじめに

OpenWrtのrootfsをUSBメモリで拡張する方法でrootfsを拡張しましたが、いろいろ欲張ろうとするとメモリ不足になるので、更にUSBメモリでswapを確保することにしました。
ルータはGL.iNet GL-AR150を利用しています。

参考:
- OpwnWrt - Extroot configuration
- Zopfcode - 【前編】ルーターのrootfsを拡張してPythonをビルドする

OSバージョンを確認します

root@OpenWrt:~# cat /etc/openwrt_release
DISTRIB_ID='OpenWrt'
DISTRIB_RELEASE='18.06.1'
DISTRIB_REVISION='r7258-5eb055306f'
DISTRIB_TARGET='ar71xx/generic'
DISTRIB_ARCH='mips_24kc'
DISTRIB_DESCRIPTION='OpenWrt 18.06.1 r7258-5eb055306f'
DISTRIB_TAINTS=''

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

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

USBメモリ(/dev/sda)をフォーマットします

:one:現在のシステム設定を確認します
root@OpenWrt:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 2560      2560         0 100% /rom
tmpfs                    30160       420     29740   1% /tmp
/dev/mtdblock5           12288      2196     10092  18% /overlay
overlayfs:/overlay       12288      2196     10092  18% /
tmpfs                      512         0       512   0% /dev

root@OpenWrt:~# dmesg | grep sd
[   16.101834] random: ubusd: uninitialized urandom read (4 bytes read)
[   16.109496] random: ubusd: uninitialized urandom read (4 bytes read)
[   16.201745] random: ubusd: uninitialized urandom read (4 bytes read)
[  156.982667] sd 0:0:0:0: [sda] 15633408 512-byte logical blocks: (8.00 GB/7.45 GiB)
[  156.991501] sd 0:0:0:0: [sda] Write Protect is off
[  156.995029] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
[  157.001802] sd 0:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[  157.036142]  sda: sda1
[  157.045232] sd 0:0:0:0: [sda] Attached SCSI disk
:two:フォーマットを開始します
root@OpenWrt:~# gdisk /dev/sda
GPT fdisk (gdisk) version 1.0.3

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
Model: Cruzer Fit      
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 6478B437-EECD-AA48-A2EC-56C50B396DFD
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
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
:three:既存パーテイションを削除します
Command (? for help): d
Using 1

Command (? for help): p
Disk /dev/sda: 15633408 sectors, 7.5 GiB
Model: Cruzer Fit      
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 6478B437-EECD-AA48-A2EC-56C50B396DFD
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
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

:four:rootfs用パーティションを作成します
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}: +7G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

:five:swap用パーティションを作成します
Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-15633374, default = 14682112) or {+-}size{KMGTP}: 
Last sector (14682112-15633374, default = 15633374) or {+-}size{KMGTP}: 
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

:six:設定内容を保存します
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.
:seven:rootfs用ファイル・システムを作成します
root@OpenWrt:~# mkfs.ext4 /dev/sda1
mke2fs 1.44.1 (24-Mar-2018)
/dev/sda1 contains a ext4 file system
    last mounted on /tmp/extroot/overlay on Thu Jan  1 09:00:13 1970
Proceed anyway? (y,N) y
Creating filesystem with 1835008 4k blocks and 458752 inodes
Filesystem UUID: 6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done 
:eight:swap用ファイル・システムを作成します
root@OpenWrt:~# mkswap /dev/sda2
Setting up swapspace version 1, size = 487042560 bytes
:nine:設定内容を確認します
root@OpenWrt:~# block info | grep "/dev/sd"
/dev/sda1: UUID="6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8" VERSION="1.0" TYPE="ext4"
/dev/sda2: VERSION="1" TYPE="swap"

root@OpenWrt:~# block detect
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  '/mnt/sda1'
    option  uuid    '6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8'
    option  enabled '0'

config 'swap'
    option  device  '/dev/sda2'
    option  enabled '0'

システム起動時に/dev/sda1と/dev/sda2を自動マウントするようにします

:one:fstabを変更します
root@OpenWrt:~# block detect > /etc/config/fstab
root@OpenWrt:~# uci set fstab.@mount[0].enabled='1'
root@OpenWrt:~# uci set fstab.@swap[0].enabled='1'
root@OpenWrt:~# uci commit
:two:再起動します
root@OpenWrt:~# reboot
:three:自動マウントできました
root@OpenWrt:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 2560      2560         0 100% /rom
tmpfs                    30160       420     29740   1% /tmp
/dev/mtdblock5           12288      2196     10092  18% /overlay
overlayfs:/overlay       12288      2196     10092  18% /
tmpfs                      512         0       512   0% /dev
/dev/sda1              7159288     32244   6743660   0% /mnt/sda1

root@OpenWrt:~# 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='6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8'
fstab.@mount[0].enabled='1'
fstab.@swap[0]=swap
fstab.@swap[0].device='/dev/sda2'
fstab.@swap[0].enabled='1'

root@OpenWrt:~# block info
/dev/mtdblock4: UUID="9e82422c-eee4f4a6-d814e9ad-6c8c5a7c" VERSION="4.0" MOUNT="/rom" TYPE="squashfs"
/dev/mtdblock5: MOUNT="/overlay" TYPE="jffs2"
/dev/sda1: UUID="6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8" VERSION="1.0" MOUNT="/mnt/sda1" TYPE="ext4"
/dev/sda2: VERSION="1" TYPE="swap"

/dev/sda1をrootfsに切り替え、/dev/sda2でswapを確保します

:one:現在のrootfs(/overlay)を/mnt/sda1にコピーします
root@OpenWrt:~# tar -C /overlay -cvf - . | tar -C /mnt/sda1 -xf -
:two:fstabを変更します
root@OpenWrt:~# block detect > /etc/config/fstab
root@OpenWrt:~# sed -i s/option$'\t'enabled$'\t'\'0\'/option$'\t'enabled$'\t'\'1\'/ /etc/config/fstab
root@OpenWrt:~# sed -i s#/mnt/sda1#/overlay# /etc/config/fstab
root@OpenWrt:~# 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    '6c7a2efd-414b-4f7d-a0f1-1d24eabc69d8'
    option  enabled '1'

config 'swap'
    option  device  '/dev/sda2'
    option  enabled '1'
:three:再起動します
root@OpenWrt:~# reboot

できました

root@OpenWrt:~# df
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/root                 2560      2560         0 100% /rom
tmpfs                    30160       412     29748   1% /tmp
/dev/sda1              7159288     36624   6739280   1% /overlay
overlayfs:/overlay     7159288     36624   6739280   1% /
tmpfs                      512         0       512   0% /dev
/dev/sda1              7159288     36624   6739280   1% /mnt/sda1

root@OpenWrt:~# free
             total       used       free     shared    buffers     cached
Mem:         60320      25112      35208        412       3780       7724
-/+ buffers/cache:      13608      46712
Swap:       475624          0     475624
6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3