如何可靠地获取已连接的 USB 驱动器的名称?

如何可靠地获取已连接的 USB 驱动器的名称?

我想获取(最终在systemd脚本中)已连接的 USB 驱动器(USB 棒或 SSD 驱动器)的设备名称。我该怎么做可靠地? 我们可以假设只有一个驱动器连接,但UUID在这种情况下我们不知道它或其他任何信息。我​​的意思是这样的:

lsblk | grep disk | awk '{print $1}' | tail -n1

不过,我不确定 USB 驱动器是否总是位于该列表中的最后一个。

答案1

如果我理解正确的话,请使用available columns打印lsblkUSB 的完整信息:

例如:

lsblk -p -o NAME,SERIAL,VENDOR,MODEL,TRAN | grep usb

其输出结果如下:

/dev/sdc    000FEA0AB9F5BA60641508A8 Kingston DT 101 G2        usb

Available columns (for --output):

        NAME  device name
       KNAME  internal kernel device name
     MAJ:MIN  major:minor device number
      FSTYPE  filesystem type
  MOUNTPOINT  where the device is mounted
       LABEL  filesystem LABEL
        UUID  filesystem UUID
    PARTTYPE  partition type UUID
   PARTLABEL  partition LABEL
    PARTUUID  partition UUID
   PARTFLAGS  partition flags
          RA  read-ahead of the device
          RO  read-only device
          RM  removable device
     HOTPLUG  removable or hotplug device (usb, pcmcia, ...)
       MODEL  device identifier
      SERIAL  disk serial number
        SIZE  size of the device
       STATE  state of the device
       OWNER  user name
       GROUP  group name
        MODE  device node permissions
   ALIGNMENT  alignment offset
      MIN-IO  minimum I/O size
      OPT-IO  optimal I/O size
     PHY-SEC  physical sector size
     LOG-SEC  logical sector size
        ROTA  rotational device
       SCHED  I/O scheduler name
     RQ-SIZE  request queue size
        TYPE  device type
    DISC-ALN  discard alignment offset
   DISC-GRAN  discard granularity
    DISC-MAX  discard max bytes
   DISC-ZERO  discard zeroes data
       WSAME  write same max bytes
         WWN  unique storage identifier
        RAND  adds randomness
      PKNAME  internal parent kernel device name
        HCTL  Host:Channel:Target:Lun for SCSI
        TRAN  device transport type
  SUBSYSTEMS  de-duplicated chain of subsystems
         REV  device revision
      VENDOR  device vendor

相关内容