BASH find which port a USB drive is attached to by the PCI ID - linux

Basically I need to verify that a USB Drive is connected to a certain USB Port. I have the following:
USB Drives are labeled virtually:
White, Green, Red
I have 3 USB Ports that are also labeled physically:
White, Green, Red
Using BLKID I can receive information from the drives such as
BLKID Example
/dec/sdb1: SEC_TYPE="msdos" LABEL="WHTIE" UUID="78FE-870D" TYPE="vfat"
Therefore I know a lot about the drive itself by only knowing the label. Now I using LSPCI I can get the information about the USB port as I know the ID's of each bridge. For example:
LSPCI Example
0a.00.0 USB Controller: some info 4d88
So that last part 4d88 is the PCI ID.
So I know the PCI ID's of each port and need to match them up to the USB Drive itself such as:
4d88 = WHITE
4dC0 = GREEN
4d84 = RED
I don't know how to match / check that relationship. Any help will be appreciated.
ANSWER: Thanks for all the help.
#!/bin/bash
#variables
error="ERROR : Incorrect Command use 'usb_pci.sh'"
pci="USB PCI Check Successful"
errorstatus_white_pci="ERROR : USB PCI FAILED : WHITE Drive"
errorstatus_green_pci="ERROR : USB PCI FAILED : GREEN Drive"
errorstatus_red_pci="ERROR : USB PCI FAILED : RED Drive"
pci_check_white=4dc9
pci_check_green=4d81
pci_check_red=4dc5
#Takes USB label and gets /sys/block/sd?
echo "checking path for USB Label"
path_white=$(blkid | grep WHITE | cut -d : -f 1 | sed 's|[0-9]*$||; s|^/dev/|/sys/block/|')
echo "white: "$path_white
path_green=$(blkid | grep GREEN | cut -d : -f 1 | sed 's|[0-9]*$||; s|^/dev/|/sys/block/|')
echo "green: "$path_green
path_red=$(blkid | grep RED | cut -d : -f 1 | sed 's|[0-9]*$||; s|^/dev/|/sys/block/|')
echo "red: "$path_red
#Takes /sys/block/sd? and gets PCI Path xx:xx.x
echo "checking path to PCI path"
pci_path_white=$(ls -l ${path_white} | xargs | cut -d / -f 8 | cut -b 6-13)
echo "white: "$pci_path_white
pci_path_green=$(ls -l ${path_green} | xargs | cut -d / -f 8 | cut -b 6-13)
echo "green: "$pci_path_green
pci_path_red=$(ls -l ${path_red} | xargs | cut -d / -f 8 | cut -b 6-13)
echo "red: "$pci_path_red
#Takes xx:xx.x and gets the PCI Device ID xxxx
echo "checking PCI path to PCI Device ID"
pci_device_id_white=$(lspci -s ${pci_path_white} | tail -c -5)
echo "white: "$pci_device_id_white
pci_device_id_green=$(lspci -s ${pci_path_green} | tail -c -5)
echo "green: "$pci_device_id_green
pci_device_id_red=$(lspci -s ${pci_path_red} | tail -c -5)
echo "red: "$pci_device_id_red
#check if pci_device_id_xxxx = pci_check_xxxx
echo "checking PCI Device ID equals what it should"
if [ $pci_device_id_white = $pci_check_white ] ; then
echo "WHITE USB PCI Check Successful"
else
echo $errorstatus_white_pci
exit 1
fi
if [ $pci_device_id_green = $pci_check_green ] ; then
echo "GREEN USB PCI Check Successful"
else
echo $errorstatus_green_pci
exit 1
fi
if [ $pci_device_id_red = $pci_check_red ] ; then
echo "RED USB PCI Check Successful"
else
echo $errorstatus_red_pci
exit 1
fi
echo $pci
exit 0
Edit:
As requested dumps of lspci, lsusb, blkid.
lspci
00:00.0 Host bridge: Intel Corporation Unknown device 0104 (rev 09)
00:02.0 VGA compatible controller: Intel Corporation Unknown device 0126 (rev 09)
00:16.0 Communication controller: Intel Corporation Unknown device 1c3a (rev 04)
00:19.0 Ethernet controller: Intel Corporation 82579LM Gigabit Network Connection (rev 04)
00:1a.0 USB Controller: Intel Corporation Unknown device 1c2d (rev 04)
00:1b.0 Audio device: Intel Corporation Unknown device 1c20 (rev 04)
00:1c.0 PCI bridge: Intel Corporation Unknown device 1c10 (rev b4)
00:1c.2 PCI bridge: Intel Corporation Unknown device 1c14 (rev b4)
00:1c.3 PCI bridge: Intel Corporation Unknown device 1c16 (rev b4)
00:1c.5 PCI bridge: Intel Corporation Unknown device 1c1a (rev b4)
00:1d.0 USB Controller: Intel Corporation Unknown device 1c26 (rev 04)
00:1f.0 ISA bridge: Intel Corporation Unknown device 1c4f (rev 04)
00:1f.2 RAID bus controller: Intel Corporation Mobile 82801 SATA RAID Controller (rev 04)
00:1f.3 SMBus: Intel Corporation Unknown device 1c22 (rev 04)
08:00.0 PCI bridge: Integrated Device Technology, Inc. PES4T4 PCI Express Switch (rev 0e)
09:02.0 PCI bridge: Integrated Device Technology, Inc. PES4T4 PCI Express Switch (rev 0e)
09:03.0 PCI bridge: Integrated Device Technology, Inc. PES4T4 PCI Express Switch (rev 0e)
09:04.0 PCI bridge: Integrated Device Technology, Inc. PES4T4 PCI Express Switch (rev 0e)
0a:00.0 USB Controller: Unknown device 4d88
0a:00.1 USB Controller: Unknown device 4dc9
0a:00.2 System peripheral: Unknown device 4dca
0a:00.3 Communication controller: Unknown device 4d8b
0b:00.0 USB Controller: Unknown device 4dc0
0b:00.1 USB Controller: Unknown device 4d81
0b:00.2 System peripheral: Unknown device 4d8e
0b:00.3 Serial controller: Unknown device 4dcf
0c:00.0 USB Controller: Unknown device 4d84
0c:00.1 USB Controller: Unknown device 4dc5
0c:00.2 System peripheral: Unknown device 4dc6
0c:00.3 Communication controller: Unknown device 4d87
0d:00.0 SD Host controller: O2 Micro, Inc. Unknown device 8221 (rev 05)
0d:00.1 Mass storage controller: O2 Micro, Inc. Unknown device 8231 (rev 03)
lsusb
Bus 004 Device 001: ID 0000:0000
Bus 006 Device 001: ID 0000:0000
Bus 005 Device 001: ID 0000:0000
Bus 001 Device 002: ID 8087:0024
Bus 001 Device 001: ID 0000:0000
Bus 007 Device 001: ID 0000:0000
Bus 008 Device 001: ID 0000:0000
Bus 003 Device 003: ID 0718:063d Imation Corp.
Bus 003 Device 001: ID 0000:0000
Bus 002 Device 002: ID 8087:0024
Bus 002 Device 004: ID 0a5c:5800 Broadcom Corp. BCM5880 Secure Applications Processor
Bus 002 Device 001: ID 0000:0000
Bus 002 Device 003: ID 413c:3012 Dell Computer Corp. Optical Wheel Mouse
blkid
/dev/mapper/VolGroup00-LogVol01: TYPE="swap" UUID="6b361baf-08ae-4f2c-933b-5028c15b6fb5"
/dev/mapper/VolGroup00-LogVol00: UUID="d15840ac-0073-483d-b630-7d2a497eaac9" TYPE="ext3"
/dev/sda1: LABEL="/boot" UUID="39331b93-a08d-45b5-b1ea-fcc6586be7bd" TYPE="ext3"
/dev/VolGroup00/LogVol00: UUID="d15840ac-0073-483d-b630-7d2a497eaac9" TYPE="ext3"
/dev/VolGroup00/LogVol01: TYPE="swap" UUID="6b361baf-08ae-4f2c-933b-5028c15b6fb5"
/dev/sdb1: SEC_TYPE="msdos" LABEL="WHITE" UUID="78FE-870D" TYPE="vfat"
/dev/sdc1: SEC_TYPE="msdos" LABEL="GREEN" UUID="61FE-B32E" TYPE="vfat"
/dev/sdd1: SEC_TYPE="msdos" LABEL="RED" UUID="E5DB-4A1A" TYPE="vfat"

This is how I would do it on my system:
Step 1: Find the device node:
# blkid | grep MyDisk
/dev/sdj1: LABEL="MyDisk-0" UUID="4876-5945" TYPE="vfat"
The device node is /dev/sdj1.
Step 2: /sys is your friend:
# ll /sys/block/sdj
lrwxrwxrwx 1 root root 0 Feb 3 00:40 /sys/block/sdj -> ../devices/pci0000:00/0000:00:1a.7/usb1/1-2/1-2:1.0/host15/target15:0:0/15:0:0:0/block/sdj/
The symbolic link target above contains a lot of useful information, including the path from your flash drive back to the PCI bridge via the SCSI and USB subsystems.
Step 3: From the link target above isolate the PCI bus ID (00:1a.7 in this case) and check with lspci:
# lspci | grep 00:1a.7
00:1a.7 USB Controller: Intel Corporation 82801JI (ICH10 Family) USB2 EHCI Controller #2
If in doubt, inspect the lspci output visually...
EDIT:
Here is an extremely crude and extremely fragile way to automate the process above:
blkid |
grep Label |
cut -d : -f 1 |
sed 's|[0-9]*$||; s|^/dev/|/sys/block/|' |
xargs readlink |
cut -d / -f 4 |
xargs -n 1 lspci -s
NOTE: This works on my system, but it is by no means safe (or recommended):
It will break if a kernel update changes the layout of the /sys filesystem.
It can break if you have a different device layout, although you should be able to adjust for that.

Related

writing in /sys/bus/pci/... fails

Attempt to run the following command with root privilege on kernel 2.6.35 results in error:
% echo 0000:00:03.0 > /sys/bus/pci/drivers/foo/bind
-bash: echo: write error: No such device
UPDATE
The device does exist in /sys/bus/pci/devices/ the output of lspci is as follows:
% lspci -v -s 0000:00:03.0
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
Subsystem: Intel Corporation PRO/1000 MT Desktop Adapter
Flags: bus master, 66MHz, medium devsel, latency 64, IRQ 10
Memory at f0000000 (32-bit, non-prefetchable) [size=128K]
I/O ports at d010 [size=8]
Capabilities: <access denied>
Kernel driver in use: e1000
I think I resolved the issue. It appears that the driver requires to unbind device at first.
It also appears that the shell processes redirection (echo .. > /sys/bus/..) with user permission, and 'sudo' handles only the command, i.e. 'echo' but not the whole command line that follows it, therefore it has to be executed this way:
% sudo sh -c "echo 0000:00:03.0 > /sys/bus/pci/drivers/foo/unbind"
% sudo sh -c "echo 0000:00:03.0 > /sys/bus/pci/drivers/foo_new/bind"

Is this Possible to know device Mounting and Unmounting Time in Ubuntu?

from dmesg we can know that particular device has been mounted or unmounted.
But I want to know on which time the device has been mounted or unmounted.
Solution 1:
dmesg output isn't having human readable date-time information
Instead using dmesg you can use kernel log available, and filter it according to your need.
for e.g. Ubuntu, Debian stores kernel log at /var/log/kern.log
cat /var/log/kern.log | grep "usb"
It will give output like,
Apr 30 11:42:23 debian kernel: [ 1537.984584] usb 1-1.1: USB disconnect, device number 3
Apr 30 11:42:23 debian kernel: [ 1538.207012] usb 1-1.1: new low-speed USB device number 5 using ehci_hcd
Apr 30 11:42:29 debian kernel: [ 1543.409629] usb 1-1.1: new low-speed USB device number 6 using ehci_hcd
Apr 30 11:42:29 debian kernel: [ 1543.504880] usb 1-1.1: New USB device found, idVendor=04f3, idProduct=0235
Apr 30 11:42:29 debian kernel: [ 1543.504885] usb 1-1.1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
Apr 30 11:42:29 debian kernel: [ 1543.504888] usb 1-1.1: Product: OM
Solution 2:
I've found one perl script to convert dmesg date-time to human readable.
Try it,
#!/usr/bin/perl
use strict;
use warnings;
my #dmesg_new = ();
my $dmesg = "/bin/dmesg";
my #dmesg_old = `$dmesg`;
my $now = time();
my $uptime = `cat /proc/uptime | cut -d"." -f1`;
my $t_now = $now - $uptime;
sub format_time {
my #time = localtime $_[0];
$time[4]+=1; # Adjust Month
$time[5]+=1900; # Adjust Year
return sprintf '%4i-%02i-%02i %02i:%02i:%02i', #time[reverse 0..5];
}
foreach my $line ( #dmesg_old )
{
chomp( $line );
if( $line =~ m/\[\s*(\d+)\.(\d+)\](.*)/i )
{
# now - uptime + sekunden
my $t_time = format_time( $t_now + $1 );
push( #dmesg_new , "[$t_time] $3" );
}
}
print join( "\n", #dmesg_new );
print "\n";
Save and apply execute permission.
$chmod a+x script.pl
$./script.pl
[Sample output:]
[2014-04-30 11:17:27] eth0: no IPv6 routers present
[2014-04-30 11:42:18] hub 1-1:1.0: port 1 disabled by hub (EMI?), re-enabling...
[2014-04-30 11:42:18] usb 1-1.1: USB disconnect, device number 3
[2014-04-30 11:42:19] usb 1-1.1: new low-speed USB device number 5 using ehci_hcd
[2014-04-30 11:42:24] hub 1-1:1.0: unable to enumerate USB device on port 1
[2014-04-30 11:42:24] usb 1-1.1: new low-speed USB device number 6 using ehci_hcd
[2014-04-30 11:42:24] usb 1-1.1: New USB device found, idVendor=04f3, idProduct=0235
[2014-04-30 11:42:24] usb 1-1.1: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[2014-04-30 11:42:24] usb 1-1.1: Product: OM
[2014-04-30 11:42:24] input: OM as /devices/pci0000:00/0000:00:1a.0/usb1/1-1/1-1.1/1-1.1:1.0/input/input11
[2014-04-30 11:42:24] generic-usb 0003:04F3:0235.0004: input,hidraw0: USB HID v1.11 Mouse [OM] on usb-0000:00:1a.0-1.1/input0
Solution 3:
If your distro supports -T option for dmesg
Try dmesg -T. For me it worked on Debian, It should work for you too on Ubuntu. It enables time-stamp for output.
[From man page]
-T, --ctime
Print human readable timestamps. The timestamp could be inaccurate!
The time source used for the logs is not updated after system SUSPEND/RESUME.
In linux /var/log directory contains various log details. We can also get history of previous logs from this directory. Kernel zips the previous log details. In case of yours you have to open kern.log. But If you are looking for details which is not in kern.log, you can see kern.log.1 or in case you are interested in very old details, you have to unzip kern.log.2.gz

usb_modeswitch is hangs on manual switch

I have 3.2.27 Linux kernel with Busybox. I am using Raspberry PI. When I pluging my Huawei E303c dmesg showing
[ 4.569781] usb 1-1.2: new high-speed USB device number 5 using dwc_otg
[ 4.681078] usb 1-1.2: New USB device found, idVendor=12d1, idProduct=14fe
[ 4.690885] usb 1-1.2: New USB device strings: Mfr=2, Product=1, SerialNumber=0
[ 4.701143] usb 1-1.2: Product: HUAWEI Mobile
[ 4.708326] usb 1-1.2: Manufacturer: HUAWEI
[ 4.718185] scsi0 : usb-storage 1-1.2:1.0
[ 4.726518] scsi1 : usb-storage 1-1.2:1.1
[ 5.720951] scsi 0:0:0:0: CD-ROM HUAWEI Mass Storage 2.31 PQ: 0 ANSI: 2
[ 5.738561] scsi 1:0:0:0: Direct-Access HUAWEI SD Storage 2.31 PQ: 0 ANSI: 2
[ 5.755514] sd 1:0:0:0: [sda] Attached SCSI removable disk
The option kernel module is already running but usb-storage module not running. But when I enter usb_modeswitch -v 0x12d1 -p 0x14fe -H it's hangs like
Looking for default devices ...
Found default devices (1)
Accessing device 002 on bus 002 ...
Using endpoints 0x04 (out) and 0x83 (in)
Inquiring device details; driver will be detached ...
Looking for active driver ...
OK, driver found ("usb-storage")
OK, driver "usb-storage" detached
Can anyone give me a solution,
This has solve my issue
usb_modeswitch -c /etc/usb_modeswitch.conf -W -I
modprobe option
modprobe ppp_generic
My /etc/usb_modeswitch.conf looks like
# Huawei E303c
DefaultVendor= 0x12d1
DefaultProduct=0x14fe
TargetVendor= 0x12d1
TargetProductList="1001,1406,140b,140c,1412,141b,14ac,1506"
CheckSuccess=20
MessageEndpoint= 0x01
MessageContent="55534243123456780000000000000011062000000100000000000000000000"

bash, search for usb storage devices. output location

I am looking for a way to list any usb connected devices or removable storage media.
I will be using this list for a gtk boot media writer, so a user can easily write an iso to a usb.
This creates a perfect list of ALL partitions:
ls /dev | grep "[sh]d[a-z][1-9]"
How can I get a similar looking list that is only removable media?
On my system (Ubuntu 12.04), I can get a list of USB devices and partitions with
ls /dev/disk/by-path/*usb*
giving
/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0
/dev/disk/by-path/pci-0000:00:02.1-usb-0:1.1:1.0-scsi-0:0:0:0-part1
or partitions alone
ls /dev/disk/by-path/*usb*part*
These are symbolic links, pointing to the real device files, /dev/sdd and /dev/sdd1 for example.
I have tested this with a USB stick and an external USB hard disk only. I cannot say, whether or how this works with eSATA or Firewire disks.
Based on the answer of Olaf Dietsche, I end up with the following:
devs=`ls -al /dev/disk/by-path/*usb*part* 2>/dev/null | awk '{print($11)}'`; for dev in $devs; do dev=${dev##*\/}; echo -n "$dev ("; echo -n `mount | grep \`echo -E ${dev}\` | awk '{print($3)}'`; echo ")"; done
For me the above code is showing the usb devices and where they are mounted (between parentheses). It worked on Ubuntu 13.04 and 12.04.2, but I do not know if it will work on any other system.
Maybe you can use the output of lsusb command :
lsusb
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0db0:3870 Micro Star International
Bus 002 Device 003: ID 0000:0000
Bus 002 Device 004: ID 14cd:6116 Super Top M6116 SATA Bridge
To get the mounted path of usb storage use
mount|grep /media|awk '{print $3}'
explanation to command
mount will print all the mounted drives and grep will display only drives mounted in /media, (considering /media is default mount point ) this output is piped to awk which will print the mounted path of usb drive
bootiso BASH utility just do that when called with -l option. Here is the output of a slightly modified bash snippet:
/dev/sdd /dev/sde
Relevant snippet:
printUSBDevices() {
typeset -a usbDevices
typeset -a devices
getDeviceType() {
typeset deviceName=/sys/block/${1#/dev/}
typeset deviceType=$(udevadm info --query=property --path="$deviceName" | grep -Po 'ID_BUS=\K\w+')
echo "$deviceType"
}
mapfile -t devices < <(lsblk -o NAME,TYPE | grep --color=never -oP '^\K\w+(?=\s+disk$)')
for device in "${devices[#]}" ; do
if [ "$(getDeviceType "/dev/$device")" == "usb" ]; then
usbDevices+=("/dev/$device")
fi
done
echo "${usbDevices[#]}"
}
printUSBDevices

Searching for and echoing a portion of string in bash

What's the best way (in bash) to search in a string for a given pattern and echoing it without using awk? I'd like to use the standard command like grep, cat, cut, or sed.
Example:
I want to strip out the device addresses of all the USB controllers from the output of lspci command:
[artlab#localhost ~]$ lspci
00:00.0 Host bridge: Intel Corporation Core Processor DRAM Controller (rev 18)
00:1a.0 USB controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 05)
00:1c.0 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 1 (rev 05)
00:1c.4 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 5 (rev 05)
00:1c.5 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 6 (rev 05)
00:1d.0 USB controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 05)
00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev a5)
00:1f.0 ISA bridge: Intel Corporation 3400 Series Chipset LPC Interface Controller (rev 05)
00:1f.2 RAID bus controller: Intel Corporation 82801 SATA Controller [RAID mode] (rev 05)
00:1f.3 SMBus: Intel Corporation 5 Series/3400 Series Chipset SMBus Controller (rev 05)
02:00.0 Ethernet controller: Intel Corporation 82574L Gigabit Network Connection
03:00.0 Ethernet controller: Intel Corporation 82574L Gigabit Network Connection
04:03.0 VGA compatible controller: Matrox Graphics, Inc. MGA G200eW WPCM450 (rev 0a)
I'd like to retrieve (print out) the two strings:
00:1a.0
00:1d.0
I wonder who will come up with more possibilities.
lspci | grep 'USB Controller' | sed "s/ .*//"
lspci | grep 'USB Controller' | grep -o '^[^ ]*'
lspci | grep 'USB Controller' | while read id rest ; do echo $id ; done
lspci | grep 'USB Controller' | cut -f 1 -d ' '
sed -n '/USB controller/{s/ .*//;p}'
lspci | grep 'USB Controller' | cut -d' ' -f1

Resources