新しいノートブック

Android大好き

Raspberry Pi3で外付けHDDをマウントする

RaspberryでNASを構築するため、外付けHDDをマウントします。 今回はこちらのHDDを使用しました。
I-O DATA HDD 外付けハードディスク 4TB USB3.0

HDDの接続

外付けHDDをUSB接続し認識されているか確認。

# ディスクをリスト表示
sudo fdisk -l | grep /dev/sda

/dev/sda1      34     262177     262144  128M Microsoft reserved
/dev/sda2  264192 7814036063 7813771872  3.7T Microsoft basic data

USB給電タイプの場合、Raspberryからの供給電力に制限があり、必要電力に足りなくなる場合があるので注意が必要です。 Raspberry側のconfigを弄ることで供給電力の変更もできますが、設定次第で動作が不安定になることもあるので、ここらへんは購入前に調査しておく必要があります。

HDDをフォーマット

パーティションファイルシステムの設定を行います。

# パーティションテーブルの設定を行います
sudo fdisk /dev/sda

# d でパーティションを削除
Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

# p で現在の状態を確認
Command (m for help): p
Disk /dev/sda: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F3BA4D9C-C2F7-43FF-837C-D3BFBFBF5C1D

# n でパーティションを作成 その後の設定で何も入力しない場合全てデフォルトとなる
Command (m for help): n
Partition number (1-128, default 1):
First sector (34-7814037134, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-7814037134, default 7814037134):

Created a new partition 1 of type 'Linux filesystem' and of size 3.7 TiB.

# p でもう一度確認 パーティションが追加されています
Command (m for help): p
Disk /dev/sda: 3.7 TiB, 4000787030016 bytes, 7814037168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disklabel type: gpt
Disk identifier: F3BA4D9C-C2F7-43FF-837C-D3BFBFBF5C1D

Device     Start        End    Sectors  Size Type
/dev/sda1   2048 7814037134 7814035087  3.7T Linux filesystem


# w で変更を書き込みます
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.

ext4でフォーマットする

ext4でフォーマットするとLinuxシステムでの読み込みがNTFSより早くなります。その代わりWindowsで認識できなくなります。

sudo mkfs.ext4 /dev/sda1

Creating filesystem with 976754385 4k blocks and 244195328 inodes
Filesystem UUID: fa16b72b-a2d0-4331-9693-e417727f3b53
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
    102400000, 214990848, 512000000, 550731776, 644972544

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

マウントする

/etc/fstabにデバイスを登録することにより、起動時に自動マウントすることができます。 またデバイスのパスではなくUUIDを登録しておくことにより、デバイスのパスが変更されてもマウントすることが可能です。

マウントポイントを作成

必要に応じてマウントポイントを作成します。今回は/mnt/hdd1としました。

sudo mkdir /mnt/hdd1

UUIDを調べる

# ブロックデバイスの情報を表示する
sudo blkid /dev/sda1
/dev/sda1: UUID="fbab8a4f-3211-4b03-b53a-1579c93054aa" TYPE="ext4"

fstabを編集

fstabでの設定が正しくない場合、再起動時にemergency modeで起動されます。 ハマるとずっと起動できないといったことになるので、必ずバックアップをとっておく。 また、オプションにnofailを指定することで、エラーの設定項目を無視して起動してくれる。

# バックアップをとっておく
cd /etc
sudo cp fstab fstab.bak

# fstabを編集しマウント設定を追加
sudo vim fstab
# デバイス名 マウントポイント ファイルシステム オプション dump指定 fsck指定
UUID=fbab8a4f-3211-4b03-b53a-1579c93054aa /mnt/hdd1 ext4 nofail 0 0

# 再起動
sudo reboot

確認

マウントされていることを確認します。

df | grep /mnt/hdd1
/dev/sda1       3845578572   69632 3650141680    1% /mnt/hdd1

参考URL