代码之家  ›  专栏  ›  技术社区  ›  warren

查找连接到Linux计算机的所有存储设备[关闭]

  •  104
  • warren  · 技术社区  · 17 年前

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

    做这件事的愚蠢方法是 尝试 每个条目 /dev 对应于可写设备的( hd* and sd* ……

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

    7 回复  |  直到 8 年前
        1
  •  75
  •   Giacomo1968 Arvind    10 年前

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

        2
  •  68
  •   Maxim Veksler    8 年前

    你总是可以的 fdisk -l 即使在EC2 XVDA设备等奇怪的设置上,它也能很好地工作。

    以下是M1的转储。大型实例:

    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
    

    同时 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)
    

    /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
    

    边注

    怎么用? fDCL 工作是我想了解自己的东西。

        3
  •  35
  •   THESorcerer    13 年前

    你也可以试试 LSBLK …在Util Linux中…但我也有个问题

    fdisk -l /dev/sdl
    

    没有结果

    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很好,但没那么好…好像什么都看不见

    在我的特定示例中,我有一个棒,其中还内置了一个读卡器,我只能看到使用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)
    

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

    而且,文件-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)
    

    太好了…但是

    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 \0
    

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

    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)
    
        4
  •  9
  •   Mihai Limbășan    17 年前

    ls /sys/block

        5
  •  7
  •   ZungBang    17 年前

    使用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
    
        6
  •  4
  •   Mark Baker    17 年前

    现代Linux系统通常只在/dev中为现有的设备提供条目,因此,按照您的建议,通过hda*和sda*可以很好地工作。

    否则,可能在/proc中有您可以使用的东西。从这里快速查看,我会说/proc/partitions看起来可以满足您的需要。

        7
  •  2
  •   Mark Baker    17 年前

    libsysfs看起来确实很有用,但不能直接从shell脚本中使用。有一个叫做Systool的程序,它可以做你想做的事情,尽管直接查看/sys比使用其他程序为你做更容易。