查找连接到 Linux 机器的所有存储设备

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/200960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:36:53  来源:igfitidea点击:

Find all storage devices attached to a Linux machine

linuxbashshell

提问by warren

I have a need to find all of the writable storage devices attached to a given machine, whether or notthey are mounted.

我需要找到连接到给定机器的所有可写存储设备,无论它们是否已安装。

The dopey way to do this would be to tryevery entry in /devthat corresponds to a writable devices (hd* and sd*)......

这样做的笨方法是尝试/dev与可写设备相对应的每个条目(hd* and sd*)......

Is there a better solution, or should I stick with this one?

有没有更好的解决方案,还是我应该坚持使用这个解决方案?

采纳答案by Steve Baker

/proc/partitionswill list all the block devices and partitions that the system recognizes. You can then try using file -s <device>to determine what kind of filesystem is present on the partition, if any.

/proc/partitions将列出系统识别的所有块设备和分区。然后,您可以尝试使用file -s <device>来确定分区上存在哪种文件系统(如果有)。

回答by Mark Baker

Modern linux systems will normally only have entries in /dev for devices that exist, so going through hda* and sda* as you suggest would work fairly well.

现代 linux 系统通常只在 /dev 中包含存在设备的条目,因此按照您的建议通过 hda* 和 sda* 会工作得很好。

Otherwise, there may be something in /proc you can use. From a quick look in there, I'd have said /proc/partitions looks like it could do what you need.

否则,您可以使用 /proc 中的某些内容。快速浏览一下,我会说 /proc/partitions 看起来可以满足您的需求。

回答by Mihai Limb??an

ls /sys/block

ls /sys/block

回答by Mark Baker

libsysfs does look potentially useful, but not directly from a shell script. There's a program that comes with it called systool which will do what you want, though it may be easier to just look in /sys directly rather than using another program to do it for you.

libsysfs 看起来确实很有用,但不是直接来自 shell 脚本。它附带了一个名为 systool 的程序,它可以执行您想要的操作,尽管直接查看 /sys 可能比使用其他程序为您执行此操作更容易。

回答by ZungBang

Using HAL (kernel 2.6.17 and up):

使用 HAL(内核 2.6.17 及更高版本):


#! /bin/bash
hal-find-by-property --key volume.fsusage --string filesystem |
while read udi ; do
    # ignore optical discs
    if [[ "$(hal-get-property --udi $udi --key volume.is_disc)" == "false" ]]; then
        dev=$(hal-get-property --udi $udi --key block.device)   
        fs=$(hal-get-property --udi $udi --key volume.fstype) 
        echo $dev": "$fs
    fi 
done

回答by Maxim Veksler

You can always do fdisk -lwhich seems to work pretty well, even on strange setups such as EC2 xvda devices.

fdisk -l即使在 EC2 xvda 设备之类的奇怪设置上,您也可以随时执行似乎运行良好的操作。

Here is a dump for a m1.large instance:

这是一个 m1.large 实例的转储:

root@ip-10-126-247-82:~# fdisk -l

Disk /dev/xvda1: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 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 identifier: 0x00000000

Disk /dev/xvda1 doesn't contain a valid partition table

Disk /dev/xvda2: 365.0 GB, 365041287168 bytes
255 heads, 63 sectors/track, 44380 cylinders, total 712971264 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 identifier: 0x00000000

Disk /dev/xvda2 doesn't contain a valid partition table

Disk /dev/xvda3: 939 MB, 939524096 bytes
255 heads, 63 sectors/track, 114 cylinders, total 1835008 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 identifier: 0x00000000

Disk /dev/xvda3 doesn't contain a valid partition table

While mountsays:

虽然mount说:

root@ip-10-126-247-82:~# mount
/dev/xvda1 on / type ext4 (rw)
proc on /proc type proc (rw,noexec,nosuid,nodev)
sysfs on /sys type sysfs (rw,noexec,nosuid,nodev)
fusectl on /sys/fs/fuse/connections type fusectl (rw)
none on /sys/kernel/debug type debugfs (rw)
none on /sys/kernel/security type securityfs (rw)
udev on /dev type devtmpfs (rw,mode=0755)
devpts on /dev/pts type devpts (rw,noexec,nosuid,gid=5,mode=0620)
tmpfs on /run type tmpfs (rw,noexec,nosuid,size=10%,mode=0755)
none on /run/lock type tmpfs (rw,noexec,nosuid,nodev,size=5242880)
none on /run/shm type tmpfs (rw,nosuid,nodev)
/dev/xvda2 on /mnt type ext3 (rw)

And /proc/partitionssays:

/proc/partitions说:

root@ip-10-126-247-82:~# cat /proc/partitions
major minor  #blocks  name

 202        1   10485760 xvda1
 202        2  356485632 xvda2
 202        3     917504 xvda3

Side Note

边注

How fdisk -lworks is something I would love to know myself.

如何fdisk -l工作是我很想知道我自己。

回答by THESorcerer

you can also try lsblk... is in util-linux ... but i have a question too

你也可以试试lsblk... 在 util-linux ... 但我也有一个问题

fdisk -l /dev/sdl

no result

没有结果

grep sdl /proc/partitions      
   8      176   15632384 sdl
   8      177   15628288 sdl1

lsblk | grep sdl
sdl       8:176  1  14.9G  0 disk  
`-sdl1    8:177  1  14.9G  0 part  

fdisk is good but not that good ... seems like it cannot "see" everything

fdisk 很好,但不是那么好......似乎它无法“看到”一切

in my particular example i have a stick that have also a card reader build in it and i can see only the stick using fdisk:

在我的特定示例中,我有一根棍子,其中还内置了读卡器,并且使用 fdisk 只能看到棍子:

fdisk -l /dev/sdk

Disk /dev/sdk: 15.9 GB, 15931539456 bytes
255 heads, 63 sectors/track, 1936 cylinders, total 31116288 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 identifier: 0xbe24be24

   Device Boot      Start         End      Blocks   Id  System
/dev/sdk1   *        8192    31116287    15554048    c  W95 FAT32 (LBA)

but not the card (card being /dev/sdl)

但不是卡(卡是/dev/sdl)

also, file -s is inefficient ...

此外,文件 -s 效率低下......

file -s /dev/sdl1
/dev/sdl1: sticky x86 boot sector, code offset 0x52, OEM-ID "NTFS    ", sectors/cluster 8, reserved sectors 0, Media descriptor 0xf8, heads 255, hidden sectors 8192, dos < 4.0 BootSector (0x0)

that's nice ... BUT

那很好……但是

fdisk -l /dev/sdb
/dev/sdb1            2048   156301487    78149720   fd  Linux raid autodetect
/dev/sdb2       156301488   160086527     1892520   82  Linux swap / Solaris

file -s /dev/sdb1
/dev/sdb1: sticky 
parted /dev/sdl print

Model: Mass Storage Device (scsi)
Disk /dev/sdl: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      4194kB  16.0GB  16.0GB  primary  ntfs




arted /dev/sdb print 
Model: ATA Maxtor 6Y080P0 (scsi)
Disk /dev/sdb: 82.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system     Flags
 1      1049kB  80.0GB  80.0GB  primary                  raid
 2      80.0GB  82.0GB  1938MB  primary  linux-swap(v1)

to see information about a disk that cannot be accesed by fdisk, you can use parted:

要查看有关 fdisk 无法访问的磁盘的信息,您可以使用 parted:

##代码##