あっきぃ日誌

鉄道ブログのような技術系ブログのようななにか

USB SSDにRaspberry Pi OSとUSBメモリ的な領域を作って使う

先日書いたPi4のUSB MSDブートの記事に貼ってたUSB SSDが届きました。会社宛に……。なので、昨日は半月ぶりに渋谷に出て回収してきました。

すでに480GB版にはRaspberry Pi OS 64bit版が入っており、アレが動くコレが動かないなどの検証にだいぶ使い込まれているのですが、480GBも使い切ってはおらず、今回の250GBも使い切ることはほぼないはずなので、パーティションの切り方を考えてみようと思いました。

250GB版にはRaspberry Pi OS 32bit(いわゆるRaspbian)を入れます。まずはEtcherでデスクトップ版を普通に焼きます。

焼き上がると/bootと/の2つのパーティションが作られます。ここで普通に起動*1してしまえばリサイズされて250GBドーンと使われてしまうので、リサイズを阻止しつつ手動でパーティションのサイズ変更をやって、後ろに自由に使えるExFAT領域を作ってみました。/は128GBに拡張し、残りがExFATとなりました。。

適当に起動済みのRaspberry Pi環境に、焼いたあとのUSB SSDを差し込み、fdiskを起動します。記録とってなかったので、以下の出力は雑コラ的に再現しました。

$ sudo fdisk /dev/sdb

Command (m for help): p
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Disk model: SSD-PSM/N       
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0xXXXXXXXX

Device     Boot     Start     End  Sectors   Size Id Type
/dev/sdb1            8192  532479   524288   256M  c W95 FAT32 (LBA)
/dev/sdb2          532480 7479295  6946816   3.3G 83 Linux

/パーティション(2)を消して、大きいパーティションを作ります。First Sectorは元のStartの位置にします。

Command (m for help): d
Partition number (1,2, default 2): 2

Partition 2 has been deleted.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-7479295, default 2048): 532480  ★元のStartの位置に!
Last sector, +/-sectors or +/-size{K,M,G,T,P} (532480-488397167, default 488397167): +128G  ★今回は128GBで指定

Created a new partition 2 of type 'Linux' and of size 128 GiB.
Partition #2 contains a ext4 signature.

Do you want to remove the signature? [Y]es/[N]o: N  ★Nにする

で、後ろの余った部分をExFATパーティションとして作りました。

Command (m for help): p
Device     Boot     Start       End   Sectors   Size Id Type
/dev/sdb2          532480 268955639 268423160   128G 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): p
Partition number (3,4, default 3): 
First sector (2048-488397167, default 2048): 268955640  ★sdb2のend+1
Last sector, +/-sectors or +/-size{K,M,G,T,P} (268955640-488397167, default 488397167):

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


Command (m for help): t  ★パーティションタイプをexFATにする
Partition number (1-3, default 3): 3
Hex code (type L to list all codes): 7

Changed type of partition 'Linux' to 'HPFS/NTFS/exFAT'.

最終的にこうなりました。

Command (m for help): p
Disk /dev/sdb: 232.9 GiB, 250059350016 bytes, 488397168 sectors
Disk model: SSD-PSM/N       
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 33553920 bytes
Disklabel type: dos
Disk identifier: 0x9fb1957f

Device     Boot     Start       End   Sectors   Size Id Type
/dev/sdb1            8192    532479    524288   256M  c W95 FAT32 (LBA)
/dev/sdb2          532480 268955639 268423160   128G 83 Linux
/dev/sdb3       268955640 488397167 219441528 104.7G  7 HPFS/NTFS/exFAT

拡張した/パーティションに合わせてファイルシステムを拡張するために、以下の通りコマンドを実行します。

$ sudo e2fsck -f /dev/sdb2
$ sudo resize2fs /dev/sdb2

初回起動時の/パーティションの拡張を阻止するために、/dev/sdb2をマウントして、/mnt/etc/init.d/resize2fs_once を消します。

$ sudo mount /dev/sdb2 /mnt
$ sudo rm /mnt/etc/init.d/resize2fs_once
$ sudo umount /mnt

これでRaspberry Pi OS側の準備はOKです。3B+でUSB MSDブートすると、初回はディスクのチェックやら、(わたしの場合は)初回の起動に失敗したりしましたが、2回目以降は普通に起動しました。もしかすると、resize2fs_onceのスクリプトの中に「update-rc.d resize2fs_once remove」という処理があったので、本当はこの処理が必要な可能性はあります。なので、スクリプトを消すのではなく、スクリプトの中にあるresize2fsを実行している行だけコメントアウトしても良いかもしれませんね。

起動後はExFATパーティションの用意。2020年時点でのRaspberry Pi OS 32bitはまだカーネル4.9系なので、exfat-fuseを使う必要があります。カーネルが5.4になればexFATがサポートされるので、fuseを入れくても動くようになるんじゃないかと思います。exFATWindows,Linux,Mac全部で扱えるので、FAT32以外でファイルの受け渡しにはもってこいですね。

※ここはUSB SSDでブートした想定なのでsdbではなくsda。
$ sudo apt update
$ sudo apt install exfat-fuse

$ sudo mkfs.exfat /dev/sda3

あとはRaspberry Pi OSをいいかんじにカスタマイズして出来上がりです。

f:id:Akkiesoft:20200712131519j:plain
レーシングミクは偶数年が当たり年(個人の感想です)

VScodeOSS版がラズピッピにインストールできて、ブラウザ等々もサクサク動くので、ラズパイとUSB SSDLinuxデスクトップ元年もあるんじゃないでしょうか、しらんけど。

WindowsMacのドライブアイコンカスタマイズ

/bootとexFATパーティションにそれぞれアイコンも設定するとより良い感じになります。Macの場合は、アイコンにしたい画像をプレビュー.appでコピーしておいて、ボリュームを右クリックして情報の表示を選択。画面の左上のアイコンをクリックして貼り付けで設定できます。

f:id:Akkiesoft:20200712132304j:plain

Windowsicoファイルを用意の上Autorun.infで設定。USB SSDに入っているファイルを見ながら書けば設定できますね。

f:id:Akkiesoft:20200712133018p:plain

*1:3B+に差し込んでUSB MSDブート