获取连接到 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 脚本中使用它们进行进一步处理。