幫 Ubuntu 加上新硬碟

當你的電腦灌好 Ubuntu 處理好 錯誤的硬碟分割 的時候
就是來把你的資料碟掛上系統的時候了!

確認硬碟資訊

首先我們可以用 lsblk 來列出所有的裝置,包括還沒有 mount 起來的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 55.4M 1 loop /snap/core18/2128
loop1 7:1 0 55.5M 1 loop /snap/core18/2253
loop2 7:2 0 67.2M 1 loop /snap/lxd/21835
loop3 7:3 0 32.3M 1 loop /snap/snapd/12704
loop4 7:4 0 61.9M 1 loop /snap/core20/1270
loop5 7:5 0 70.3M 1 loop /snap/lxd/21029
loop6 7:6 0 43.3M 1 loop /snap/snapd/14295
sda 8:0 0 1.8T 0 disk
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 930G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 930G 0 lvm /

可以發現第 10 行的 sda 還沒有被 mount 起來,容量也符合新買的 2T 硬碟

好,接下來我們用 fdisk -l /dev/sda 看一下分割資訊

1
2
3
4
5
6
7
> sudo fdisk -l /dev/sda
[sudo] password for codingman:
Disk /dev/sda: 1.84 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: TOSHIBA DT02ABA2
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes

看起來沒問題,但沒有任何分割資訊

分割硬碟

現在我們使用 fdisk /dev/sda 來幫硬碟分割一下

  1. 新增分割區,輸入 n 按 Enter
  2. 接下來因為我是資料碟選 primary 才對,選 p 按 Enter
  3. 選擇 Partition number,看起來最多可以 4 個,但我是資料碟全部一起就好
    選 1 按 Enter
  4. 選擇開始位置 First sector,這裏用預設值就可以了,直接按 Enter
  5. 選擇結束位置 Last sector,如果要用全部容量,就直接按 Enter
    如果要指定分割區的大小,就用 +size{K,M,G} 的形式指定,例如指定為 100G 的大小就輸入 +100G 再按 Enter
  6. 最後將選擇結果寫入硬碟,輸入 w 再按 Enter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.34).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0x4557981f.

Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-3907029167, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3907029167, default 3907029167):

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

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

接著我們再用一次 fdisk -l /dev/sda 看一下分割資訊

1
2
3
4
5
6
7
8
9
10
11
> sudo fdisk -l /dev/sda
Disk /dev/sda: 1.84 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: TOSHIBA DT02ABA2
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: 0x4557981f

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 3907029167 3907027120 1.8T 83 Linux

太棒了,看起來分割得很順利

格式化

接下來記得在 mount 之前先格式化一下

1
mkfs -t ext4 /dev/sda

掛載硬碟

OK,進入到 mount 的步驟了

先創一個資料夾作為等一下新硬碟的路徑,在這裡我們用 /data

1
sudo mkdir /data

使用 sudo lsblk -o NAME,UUID 看一下硬碟的 UUID

1
2
3
4
5
NAME   UUID
sda
└─sda 43162d9b-368a-49be-8f45-73df1529185a
sdb
└─sdb1 abcdef12-3456-7890-abcd-ef1234567890

在這裡我們得到了 /dev/sda 的 UUID 為 43162d9b-368a-49be-8f45-73df1529185a

來把硬碟的 UUID 還有剛剛創建的路徑 /data 寫進 /etc/fstab 裡面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
# <file system> <mount point> <type> <options> <dump> <pass>
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-are0EThQSeIaHUqInASIcJ738TIRHn81ZKWme0k38SHcWlAc1i7mtjEgQTWQS6XP / ext4 defaults 0 1
# /boot was on /dev/nvme0n1p2 during curtin installation
/dev/disk/by-uuid/a099d71a-efc5-4432-9ab9-b271eadf5067 /boot ext4 defaults 0 1
# /boot/efi was on /dev/nvme0n1p1 during curtin installation
/dev/disk/by-uuid/66F8-3AE3 /boot/efi vfat defaults 0 1
/swap.img none swap sw 0 0
/dev/disk/by-uuid/43162d9b-368a-49be-8f45-73df1529185a /data ext4 defaults 0 0

這邊就依樣畫葫蘆填上去在最後面即可

最後把硬碟 mount 起來

1
sudo mount /data

用最一開始教的 lsblk 確認

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
loop0 7:0 0 55.4M 1 loop /snap/core18/2128
loop1 7:1 0 55.5M 1 loop /snap/core18/2253
loop2 7:2 0 67.2M 1 loop /snap/lxd/21835
loop3 7:3 0 32.3M 1 loop /snap/snapd/12704
loop4 7:4 0 61.9M 1 loop /snap/core20/1270
loop5 7:5 0 70.3M 1 loop /snap/lxd/21029
loop6 7:6 0 43.3M 1 loop /snap/snapd/14295
sda 8:0 0 1.8T 0 disk /data
nvme0n1 259:0 0 931.5G 0 disk
├─nvme0n1p1 259:1 0 512M 0 part /boot/efi
├─nvme0n1p2 259:2 0 1G 0 part /boot
└─nvme0n1p3 259:3 0 930G 0 part
└─ubuntu--vg-ubuntu--lv 253:0 0 930G 0 lvm /

已經可以看到 /dev/sda 已經 mount 在 /data 底下了

完成!

參考文件:
https://blog.gtwang.org/linux/linux-add-format-mount-harddisk/

也許你也會想看看