[Raspberry Pi] 設定開機自動掛載 exFAT 格式行動硬碟

1. 接上行動硬碟,檢查一下被分配到哪個裝置代號

1
2
# 查看分割表狀態
sudo fdisk -l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Disk /dev/mmcblk0: 29.7 GiB, 31914983424 bytes, 62333952 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
Disklabel type: dos
Disk identifier: 0xda84cd12

Device Boot Start End Sectors Size Id Type
/dev/mmcblk0p1 * 2048 526335 524288 256M c W95 FAT32 (LBA)
/dev/mmcblk0p2 526336 62333918 61807583 29.5G 83 Linux


Disk /dev/sda: 1.8 TiB, 2000398934016 bytes, 3907029168 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: dos
Disk identifier: 0x2ac1c561

Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 3907026943 3907024896 1.8T c W95 FAT32 (LBA)

2. 將硬碟分割表設定成 GPT,並建立一個完整的磁碟分割

1
2
# 設定分割表
sudo fdisk /dev/sda
  1. 輸入 g 設定成 GPT 分割表
    1
    2
    Command (m for help): g
    Created a new GPT disklabel (GUID: 2BD8CEBF-23A4-4C49-B0E5-3D8A45B6E883).
  2. 輸入 n 建立新磁碟分區
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Command (m for help): n
    Partition number (1-128, default 1): 1
    First sector (2048-3907029134, default 2048): 2048
    Last sector, +sectors or +size{K,M,G,T,P} (2048-3907029134, default 3907029134): 3907029134

    Created a new partition 1 of type 'Linux filesystem' and of size 1.8 TiB.
    Partition #1 contains a exfat signature.

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

    The signature will be removed by a write command.
  3. 輸入 p 檢查分區狀況,沒問題後輸入 w 寫入
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    Command (m for help): p
    Disk /dev/sda: 1.8 TiB, 2000398934016 bytes, 3907029168 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: 2BD8CEBF-23A4-4C49-B0E5-3D8A45B6E883

    Device Start End Sectors Size Type
    /dev/sda1 2048 3907029134 3907027087 1.8T Linux filesystem

    Filesystem/RAID signature on partition 1 will be wiped.

    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.

3. 將硬碟掛載到 /media/external-hd 下,並設定開機自動掛載

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 安裝 exFAT 相關套件
sudo apt install exfat-fuse exfat-utils

# 將硬碟格式化成 exFAT 格式
sudo mkfs.exfat /dev/sda1

# 建立掛載資料夾
mkdir /media/external-hd/

# 編輯 fstab 設定開機自動掛載
sudo vim /etc/fstab

# 將以下內容增加到 /etc/fstab 最尾行
/dev/sda1 /media/external-hd/ exfat defaults 0 0

# 將硬碟掛載上去
sudo mount -a

4. 檢查掛載狀況

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
ubuntu@raspberry-pi:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 1.8T 0 disk
└─sda1 8:1 0 1.8T 0 part /media/external-hd
mmcblk0 179:0 0 29.7G 0 disk
├─mmcblk0p1 179:1 0 256M 0 part /boot/firmware
└─mmcblk0p2 179:2 0 29.5G 0 part /

ubuntu@raspberry-pi:~$ df -h
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 380M 7.0M 373M 2% /run
/dev/mmcblk0p2 29G 3.1G 25G 12% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mmcblk0p1 253M 116M 137M 46% /boot/firmware
/dev/sda1 1.9T 61M 1.9T 1% /media/external-hd
tmpfs 380M 0 380M 0% /run/user/1000