獲取連線到 Linux 機器的所有儲存裝置的列表

Nilesh Katuwal 2023年1月30日 2022年5月11日
  1. 使用 /Proc/Partitions 目錄獲取有關 Linux 中相關儲存裝置的資訊
  2. 在 Linux 中使用 fdisk -l 命令處理磁碟和分割槽
  3. 在 Linux 中使用 lsblk 命令列印分割槽層次結構
  4. まとめ
獲取連線到 Linux 機器的所有儲存裝置的列表

我們經常需要檢查機器上的儲存裝置。

當我們需要驗證系統上是否檢測到所有硬碟和 SSD 以及是否正確處理任何外部儲存裝置時,這尤其方便。

Linux 提供了幾個選項來顯示連結到系統的儲存裝置。我們將在本教程中逐一介紹。

使用 /Proc/Partitions 目錄獲取有關 Linux 中相關儲存裝置的資訊

每個 Linux 發行版都包含一個 /proc 目錄,其中包含各種檔案,這些檔案提供有關係統當前狀態的各種型別的資訊。

這是一個虛擬檔案系統。這意味著這些檔案在磁碟上不存在,但它們的路徑可以被任何應用程式或命令讀取為真正的檔案。

檔案 /proc/partitions 包含有關相關儲存裝置的資訊。因此,如果我們在 /proc/partitions 目錄上執行 cat 命令,我們將獲得以下資訊:

$ cat /proc/partitions

輸出:

major     minor  #blocks  name

   7        0          4 loop0
   7        1      56840 loop1
   7        2     168712 loop2
   7        3      63392 loop3
   7        4     224256 loop4
   7        5      66660 loop5
   7        6       6024 loop6
   7        7     224256 loop7
   8        0  250059096 sda
   8        1     524288 sda1
   8        2          1 sda2
   8        5  249532416 sda5
  11        0    1048575 sr0
   7        8      33072 loop8
   7        9     302812 loop9
   7       10     355308 loop10
   7       11      66776 loop11
   7       12     541348 loop12
   7       13      55540 loop13
   7       14      44308 loop14
   7       15     253856 loop15
   7       16      52180 loop16
   7       17     330648 loop17
   7       18     355364 loop18
   7       19      56820 loop19
   7       20     159352 loop20
   7       21      18404 loop21

另一方面,這種方法以塊的形式顯示輸出,並帶有每個分割槽的標籤。

在 Linux 中使用 fdisk -l 命令處理磁碟和分割槽

fdisk 是一個 Linux 命令,允許你使用磁碟和分割槽。除非你以 root 身份或使用 sudo 執行它,否則這可能會起作用:

$ sudo fdisk -l

輸出:

Disk /dev/loop0: 4 KiB, 4096 bytes, 8 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/loop1: 55.52 MiB, 58204160 bytes, 113680 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/loop2: 164.78 MiB, 172761088 bytes, 337424 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/loop3: 61.93 MiB, 64913408 bytes, 126784 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/loop4: 219 MiB, 229638144 bytes, 448512 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/loop5: 65.1 MiB, 68259840 bytes, 133320 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/loop6: 5.9 MiB, 6168576 bytes, 12048 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/loop7: 219 MiB, 229638144 bytes, 448512 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/sda: 238.49 GiB, 256060514304 bytes, 500118192 sectors
Disk model: Netac SSD 256GB 
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: 0x5c281f6c

Device     Boot   Start       End   Sectors  Size Id Type
/dev/sda1  *       2048   1050623   1048576  512M  b W95 FAT32
/dev/sda2       1052670 500117503 499064834  238G  5 Extended
/dev/sda5       1052672 500117503 499064832  238G 83 Linux

Disk /dev/loop8: 32.3 MiB, 33865728 bytes, 66144 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/loop9: 295.73 MiB, 310079488 bytes, 605624 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/loop10: 346.10 MiB, 363835392 bytes, 710616 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/loop11: 65.22 MiB, 68378624 bytes, 133552 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/loop12: 528.68 MiB, 554340352 bytes, 1082696 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/loop13: 54.24 MiB, 56872960 bytes, 111080 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/loop14: 43.28 MiB, 45371392 bytes, 88616 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/loop15: 247.93 MiB, 259948544 bytes, 507712 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/loop16: 50.98 MiB, 53432320 bytes, 104360 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/loop17: 322.92 MiB, 338583552 bytes, 661296 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/loop18: 347.4 MiB, 363892736 bytes, 710728 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/loop19: 55.5 MiB, 58183680 bytes, 113640 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/loop20: 155.63 MiB, 163176448 bytes, 318704 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/loop21: 17.10 MiB, 18845696 bytes, 36808 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

如上所示,結果非常詳細且格式正確。它列出了連線到系統的所有儲存裝置及其總大小、型號、標籤、分割槽和其他相關資訊。

在 Linux 中使用 lsblk 命令列印分割槽層次結構

$ lsblk

輸出:

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
loop0    7:0    0     4K  1 loop /snap/bare/5
loop1    7:1    0  55.5M  1 loop /snap/core18/2284
loop2    7:2    0 164.8M  1 loop /snap/gnome-3-28-1804/161
loop3    7:3    0  61.9M  1 loop /snap/core20/1270
loop4    7:4    0   219M  1 loop /snap/gnome-3-34-1804/77
loop5    7:5    0  65.1M  1 loop /snap/gtk-common-themes/1515
loop6    7:6    0   5.9M  1 loop /snap/notepad-plus-plus/335
loop7    7:7    0   219M  1 loop /snap/gnome-3-34-1804/72
loop8    7:8    0  32.3M  1 loop /snap/snapd/12704
loop9    7:9    0 295.7M  1 loop /snap/vlc/2344
loop10   7:10   0   347M  1 loop /snap/wine-platform-runtime/279
loop11   7:11   0  65.2M  1 loop /snap/gtk-common-themes/1519
loop12   7:12   0 528.7M  1 loop /snap/pycharm-community/265
loop13   7:13   0  54.2M  1 loop /snap/snap-store/558
loop14   7:14   0  43.3M  1 loop /snap/snapd/14295
loop15   7:15   0 247.9M  1 loop /snap/gnome-3-38-2004/87
loop16   7:16   0    51M  1 loop /snap/snap-store/547
loop17   7:17   0 322.9M  1 loop /snap/wine-platform-6-stable/14
loop18   7:18   0   347M  1 loop /snap/wine-platform-runtime/280
loop19   7:19   0  55.5M  1 loop /snap/core18/2253
loop20   7:20   0 155.6M  1 loop /snap/opera/158
loop21   7:21   0    18M  1 loop /snap/chromium-ffmpeg/24
sda      8:0    0 238.5G  0 disk 
├─sda1   8:1    0   512M  0 part /boot/efi
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   238G  0 part /
sr0     11:0    1  1024M  0 rom  

列印分割槽層次結構,我們可以看到附加了哪些磁碟以及它們下面存在哪些分割槽,如上所示。但是,僅列印裝置標籤,而不列印裝置名稱。

まとめ

本文研究了三種方法來列出連線到 Linux 系統的儲存裝置,使用 fdisk 和 parted 生成非常相似的詳細資料。

cat /proc/partitionslsblk 的結果相當簡短,我們可以在 bash 指令碼中使用它們進行進一步處理。