Raspberry PiをSDメモリカード/USBメモリのデュアル運用

Raspberry Piのサイネージクライアントとしての運用を目指していましたが、ここで少しブレイク。

Raspberry Piは通常、SDメモリカードをすべてのファイルシステムとして動作させます。

今回手元に、使わなくなった128MBのSDメモリカードと4GBのUSBメモリがあったので、SDカードを/bootファイルシステム、USBメモリをrootファイルシステムとして利用することにしました。

今回は導入方法をレポートします。

①USBメモリへRaspbianのimgファイルをW32DiskImagerで書き込みます。

すると、/bootパーティション(FAT)とrootパーティション(ext4)が作成されます。
Windows環境では、ext4が読めないため、/bootパーティション(FAT)だけが見えている状態となります。

②USBメモリの/bootパーティションの内容(FAT)を、SDメモリカード(FAT)へコピーします。
必要に応じて、あらかじめSDメモリカードをフォーマットしておきます。

③コピーしたSDメモリカードの中にあるcmdline.txtをテキストエディタで開き、以下の項目を変更します。
root=/dev/sda2
※USBメモリをsdaデバイスとして認識する場合。また、/bootパーティションが1番目のパーティションとなるため、rootファイルシステムシステムは2番目のパーティションとなる

④SDメモリカード・USBメモリをRaspberry Piへ挿入し、電源を入れる

⑤起動後、/etc/fstabのrootパーティションの内容を以下のように変更する
$ sudo vi /etc/fstab
/dev/sda2  /               ext4    defaults,noatime  0       1

※/boot/cmdline.txtと整合性をとるため。SDメモリカード(/dev/mmcblk0p2)は使用しない。

⑥Win32DiskImagerで書き込んだrootファイルシステムはデフォルトで2GBとなり最大限使用されないため、rootファイルシステム(ext4)をUSBメモリの最大まで拡張する。
$ sudo fdisk /dev/sda

Command (m for help): p
現状を確認する。

Disk /dev/sda: 4009 MB, 4009754624 bytes
124 heads, 62 sectors/track, 1018 cylinders, total 7831552 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5098

Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sda2          122880     5785599     2831360   83  Linux

total 7831552に対して、/dev/sda2のEndは5785599であり、最大限利用できていない。

Command (m for help): d
Partition number (1-4): 2

パーティション番号2を削除する。

Command (m for help): n
Partition type:
p   primary (1 primary, 0 extended, 3 free)
e   extended
Select (default p): p
Partition number (1-4, default 2): 2
First sector (2048-7831551, default 2048): 122880
Last sector, +sectors or +size{K,M,G} (122880-7831551, default 7831551):
Using default value 7831551

パーティションを新規に作成する。
First sectorは、削除前の値(122880)に合わせて指定し、Last sectorは最大値(7831551)を指定する

Command (m for help): p

Disk /dev/sda: 4009 MB, 4009754624 bytes
124 heads, 62 sectors/track, 1018 cylinders, total 7831552 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000b5098

Device Boot      Start         End      Blocks   Id  System
/dev/sda1            8192      122879       57344    c  W95 FAT32 (LBA)
/dev/sda2          122880     7831551     3854336   83  Linux

パーティションが拡大されたことが分かります。
変更を確定させます。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

$ sudo reboot
変更を有効化するため、再起動します。

pi@raspberrypi ~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
rootfs           2721336 2123704    439680  83% /
/dev/root        2721336 2123704    439680  83% /
devtmpfs          219832       0    219832   0% /dev
tmpfs              44800     220     44580   1% /run
tmpfs               5120       0      5120   0% /run/lock
tmpfs              89580       0     89580   0% /run/shm
/dev/mmcblk0p1    125344    9728    115616   8% /boot

パーティションが拡大されただけで、ファイルシステムがまだ拡大されていません。
ext4ファイルシステムを拡大します。

pi@raspberrypi ~ $ sudo resize2fs /dev/sda2
resize2fs 1.42.5 (29-Jul-2012)
Filesystem at /dev/sda2 is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/sda2 is now 963584 blocks long.

オプション指定なしで、最大サイズへ拡大されます。

pi@raspberrypi ~ $ df
Filesystem     1K-blocks    Used Available Use% Mounted on
rootfs           3728136 2125084   1406272  61% /
/dev/root        3728136 2125084   1406272  61% /
devtmpfs          219832       0    219832   0% /dev
tmpfs              44800     220     44580   1% /run
tmpfs               5120       0      5120   0% /run/lock
tmpfs              89580       0     89580   0% /run/shm
/dev/mmcblk0p1    125344    9728    115616   8% /boot

これで、ext4ファイルシステムが拡大され、USBメモリを最大限利用できます。

このRaspberry Piを使って、こちらはサーバ構築を目指します。