2

我认为我有一个很好的方法来确定哪些驱动器是内部驱动器,哪些是外部驱动器(USB)。直到我遇到这个:

/var/log/messages*

    Sep 24 03:40:02 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Sep 24 03:40:02 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 21 14:57:01 myHostName kernel: sd 6:0:0:0: [sdb] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 22 06:45:39 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 22 15:06:30 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 22 15:06:30 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 25 15:13:06 myHostName kernel: sd 7:0:0:0: [sdc] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 26 15:19:39 myHostName kernel: sd 0:0:0:0: [sda] 3907029164 512-byte logical blocks: (2.00 TB/1.81 TiB)
    Jul 26 15:19:39 myHostName kernel: sd 1:0:0:0: [sdb] 976773168 512-byte logical blocks: (500 GB/465 GiB)
    Jul 26 15:23:28 myHostName kernel: sd 0:0:0:0: [sda] 976773168 512-byte logical blocks: (500 GB/465 GiB)

此系统上的 SDA 和 SDB 驱动器从内部交换到外部!有人知道我如何识别 Linux 中哪些设备是内部设备,哪些是外部设备?我需要查看历史,所以我真的需要能够回顾以前安装的设备。我的旧方法是查看忽略 sda 的所有“sdb-sdX”。这在上面的机器上是有缺陷的。我什至不知道它是如何做到的。一直认为 SDA 是第一个内部设备。

4

2 回答 2

3

我用它来确定驱动器是否可弹出(USB):-

diskutil info /dev/disk[n|s{n}] | grep Ejectable

磁盘工具为您提供所有磁盘的序列号。

于 2013-09-30T16:22:58.463 回答
0

在我的 RHEL6 系统上,我能够执行以下查询来获得我正在寻找的输出:

cat /var/log/messages* | egrep -i 'usb [0-9]-|mounted|logical blocks' > ${USBDATA}
cat ${USBDATA} | grep "New USB device found" > ${NewUSBDev}


while read line; do

    MONTH="`echo ${line} | awk '{print $1}'`" ;  # -- Jul
    DAY="`echo ${line} | awk '{print $2}'`" # -- day_
    HOUR="`echo ${line} | awk '{print $3}' | cut -d':' -f1`:" # -- hour:
    MIN="`echo ${line} | awk '{print $3}' | cut -d':' -f2`:" # -- hour:

    # -- echo "M:[$MONTH]  D:[$DAY]  H:[${HOUR}${MIN}]"

    cat ${USBDATA} | grep ${MONTH} | grep "${DAY} ${HOUR}${MIN}" > ${CHUNK}

    awk '/New USB device found/{p++}p==1' ${CHUNK} > ${TMPFILE}
    cp ${TMPFILE} ${CHUNK}

    if [ "$(cat $CHUNK| egrep 'MiB|GiB|TiB')" ] && ! [ "$(cat $CHUNK | grep 'Mounted root filesystem')" ] ;then
        # -- cat ${CHUNK}
        STR="$(less ${CHUNK}|grep 'idProduct' | head -1 | awk '{print $1" "$2" "$3" "$12" "$13}')"
        STR="$STR $(less ${CHUNK}|grep 'Product:' | awk '{print ", "$9" "$10" "$11}')"
        STR="$STR $(less ${CHUNK}|grep 'Manufacturer:' | awk '{print ", "$9" "$10" "$11}')"
        STR="$STR $(less ${CHUNK}|grep 'SerialNumber:' | awk '{print ", "$9}')"
        STR="$STR $(less ${CHUNK}|grep 'logical blocks:' | head -1 | awk '{print ", "$13" "$14" "$15" "}')"
        echo "${STR}"
        echo "${STR}" >> ${DEVICES}
    fi

    [ -f ${CHUNK} ] && rm ${CHUNK}

done < ${NewUSBDev}
echo "----------------------------------------------"
cat ${DEVICES} | sort -M -k 2 > ${SORTEDDEV}
cat ${SORTEDDEV}

基本上,它有点难看,但它会搜索所有“找到的新 USB 设备”并列出包含它的行。然后取出与该时间戳匹配的所有内容。然后在“找到新的 USB 设备”之前提取所有内容,并在“找到新的 USB 设备”进行第二次匹配之后提取所有内容。

样本 ${CHUNK} 的数据如下所示:

    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: New USB device found, idVendor=1058, idProduct=071a
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: Product: My Passport 071A
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: Manufacturer: Western Digital
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: SerialNumber: 575846314136304130353235
    Sep 30 09:55:10 oc7446274465 kernel: usb 2-1: configuration #1 chosen from 1 choice
    Sep 30 09:55:22 oc7446274465 kernel: sd 5:0:0:0: [sdb] 1465092096 512-byte logical blocks: (750 GB/698 GiB)

将此数据放入临时文件中,然后 grep 出重要位。可以肯定地对此进行改进,但目前可以使用(上述代码中未包含 grepping 部分):

样本输出:

    Sep 10 16:19:14 idVendor=8564, idProduct=1000 , Mass Storage Device , JetFlash   , 2978449637 , (31.6 GB/29.4 GiB) 
    Sep 14 15:15:04 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (20.0 MB/19.1 MiB) 
    Sep 17 19:38:04 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (31.9 GB/29.7 GiB) 
    Sep 19 05:23:07 idVendor=054c, idProduct=0544 , DSC-HX9V   , Sony   , D89BE0866 , (31.9 GB/29.7 GiB) 
    Sep 30 09:55:10 idVendor=1058, idProduct=071a , My Passport 071A , Western Digital  , 57584631413630XXXXXX235 , (750 GB/698 GiB) 

因此我得到了时间、ID 信息、产品名称、序列号等。我可能应该包括检查数据是否丢失。但你明白了。感谢您的输入。

于 2013-10-03T16:37:25.270 回答