Ubuntu

外部USB-HDD斷電後變成/dev/sda,如何將其固定到/dev/sdc?

  • February 25, 2020

今天保險絲燒斷,關閉了一個小型伺服器。

這台伺服器有一個 USB-HDD 連接到它,但自從我大約 3-4 個月前連接它以來,我從未完全關閉伺服器(沒有電),只是偶爾發出一個sudo reboot.

這個 USB-HDD 列在下面/dev/sdc,所以我有一個通過sudo mount /dev/sdc1 /media/hdd5-usb-1.

在 處還有一個掛載的 SSD /dev/sdb,所以正確的fdisk -l樣子如下

Disk /dev/sda: 238.5 GiB, 256060514304 bytes, 500118192 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: 0x000ece66

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sda1  *      2048    499711    497664   243M 83 Linux
/dev/sda2       501758 500117503 499615746 238.2G  5 Extended
/dev/sda5       501760 500117503 499615744 238.2G 8e Linux LVM


Disk /dev/sdb: 477 GiB, 512110190592 bytes, 1000215216 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: gpt
Disk identifier: CD3F3DA1-2901-45A0-B6E9-2BEEA36F3E78

Device     Start        End    Sectors  Size Type
/dev/sdb1   2048 1000214527 1000212480  477G Linux filesystem


Disk /dev/mapper/n3150--vg-root: 222.4 GiB, 238769143808 bytes, 466345984 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 /dev/mapper/n3150--vg-swap_1: 15.9 GiB, 17028874240 bytes, 33259520 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 /dev/sdc: 4.6 TiB, 5000981077504 bytes, 9767541167 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes
Disklabel type: gpt
Disk identifier: 0B9E1A83-476F-47AE-8234-2D29497F55CA

Device     Start        End    Sectors  Size Type
/dev/sdc1   2048 9767541133 9767539086  4.6T Linux filesystem

在斷電和伺服器自動啟動後,條目如下所示:

Disk /dev/sda: 4.6 TiB, 5000981077504 bytes, 9767541167 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 33553920 bytes
Disklabel type: gpt
Disk identifier: 0B9E1A83-476F-47AE-8234-2D29497F55CA

Device     Start        End    Sectors  Size Type
/dev/sda1   2048 9767541133 9767539086  4.6T Linux filesystem


Disk /dev/sdb: 238.5 GiB, 256060514304 bytes, 500118192 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: 0x000ece66

Device     Boot  Start       End   Sectors   Size Id Type
/dev/sdb1  *      2048    499711    497664   243M 83 Linux
/dev/sdb2       501758 500117503 499615746 238.2G  5 Extended
/dev/sdb5       501760 500117503 499615744 238.2G 8e Linux LVM


Disk /dev/sdc: 477 GiB, 512110190592 bytes, 1000215216 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: gpt
Disk identifier: CD3F3DA1-2901-45A0-B6E9-2BEEA36F3E78

Device     Start        End    Sectors  Size Type
/dev/sdc1   2048 1000214527 1000212480  477G Linux filesystem


Disk /dev/mapper/n3150--vg-root: 222.4 GiB, 238769143808 bytes, 466345984 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 /dev/mapper/n3150--vg-swap_1: 15.9 GiB, 17028874240 bytes, 33259520 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

顯然,這有一些非常討厭的問題,因為 SSD 上的數據庫在預期/dev/sdb1/media/ssd1突然出現在引導分區,而被認為是 USB 硬碟的實際上是安裝的 SSD。

我怎樣才能防止這樣的事情再次發生?如何將硬體“固定”到特定/dev/sd*條目?

您根本不應該嘗試獲取固定/dev/sdX名稱。您不應該直接使用這樣的名稱。

您應該做的是通過在/dev/disk/. 在可用的選項中,/dev/disk/by-id/有與磁碟設備的序列號相關的條目。因此,即使是兩個相同的磁碟也可以毫無歧義地加以區分。請注意,/dev/disk/樹中的所有條目都只是實際/dev/sdX條目的符號連結,但正如您所發現的,它們可以是動態的:系統上出現或檢測的順序對於 來說確實/dev/sdX很重要,對於/dev/disk/(或至少對於/dev/disk/by-id/)。

那裡的一些條目具有等效的/etc/fstab語法。LABEL= UUID= PARTUUID= PARTLABEL=匹配他們的等價物/dev/disk/by-{label,uuid,partlabel,partuuid}/。這是fstab(5)聯機幫助頁的內容:

LABEL=<label>或者UUID=<uuid>可以代替設備名稱給出。這是推薦的方法,因為設備名稱通常與硬體檢測順序一致,並且在添加或刪除其他磁碟時會發生變化。例如,LABEL=BootUUID=3e6be9de-8139-11d1-9106-a43f08d823a6。(使用特定於文件系統的工具,如 e2label(8)、xfs_admin(8) 或 fatlabel(8) 來在文件系統上設置 LABEL)。

這是一個關於這些條目的有趣 Archlinux wiki 頁面:

https://wiki.archlinux.org/index.php/Persistent_block_device_naming

引用自:https://unix.stackexchange.com/questions/569603