Solved lsblk reacts very slowly

Solved issue

84773

New Member
Joined
Apr 22, 2024
Messages
4
Reaction score
4
Credits
35
Hello forum,

I am using kern.log and lsblk to determine which USB stick (label) is plugged into which USB port.

I use "lsblk -J -o KNAME,PKNAME,LABEL,SERIAL" for this. On my previous computer (Raspi) the whole thing worked quite quickly. But now I have switched to a faster x86 computer with Ubuntu 22.04. Now it takes about 7 seconds from recognizing the USB stick in kern.log until the USB stick is listed in lsblk. Does anyone know of a way to speed up this process?
 


Try resetting USB's with

Bash:
sudo usbreset

Then after that run lsblk again

The usbreset command is part of usbutils package, so if you don't have it sudo apt install usbutils
 
thanks CaffeineAddict for the hint

Unfortunately this has not changed anything. lsblk needs approx. 7 seconds with or without usbreset
 
@84773 wrote:
I am using kern.log and lsblk to determine which USB stick (label) is plugged into which USB port.
There are a few ways of determining which usb is plugged into which port.

One way is to determine the usb controller based number when plugging the usb in, then inspecting the details of the physical location in the /sys directory.

To do it this way, run in a terminal:
Code:
dmesg -w
*(See below for how to run it as user rather than root, if you wish to do that)

Then plug the usb in, and observe the output on the terminal screen. The following sort of output appears pretty much immediately:
Code:
[49881.113615] usb 1-6: new high-speed USB device number 9 using xhci_hcd
[49881.262768] usb 1-6: New USB device found, idVendor=0781, idProduct=55a1, bcdDevice= 1.00
[49881.262784] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[49881.262789] usb 1-6: Product: Cruzer Spark
[49881.262793] usb 1-6: Manufacturer: SanDisk
[49881.262797] usb 1-6: SerialNumber: 03025011071521122934
[49881.266685] usb-storage 1-6:1.0: USB Mass Storage device detected
[49881.267682] scsi host9: usb-storage 1-6:1.0
[49882.271659] scsi 9:0:0:0: Direct-Access     SanDisk  Cruzer Spark     1.00 PQ: 0 ANSI: 6
[49882.272261] sd 9:0:0:0: Attached scsi generic sg1 type 0
[49882.273188] sd 9:0:0:0: [sda] 30031872 512-byte logical blocks: (15.4 GB/14.3 GiB)
[49882.274602] sd 9:0:0:0: [sda] Write Protect is off
[49882.274619] sd 9:0:0:0: [sda] Mode Sense: 43 00 00 00
[49882.274997] sd 9:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[49882.297398]  sda: sda1
[49882.297790] sd 9:0:0:0: [sda] Attached SCSI removable disk

The output shows the usb controller details for this usb are: 1-6:1.0.

To close the dmesg output, press: cntl+c.

Navigate to the system details as follows for this bus controller:
Code:
cd /sys/bus/usb/devices/1-6:1.0/physical_location

List the files at the directory named "physical_location":
Code:
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ ls -al
total 0
-r--r--r-- 1 root root 4.0K Apr 22 20:43 dock
-r--r--r-- 1 root root 4.0K Apr 22 20:43 horizontal_position
-r--r--r-- 1 root root 4.0K Apr 22 20:43 lid
-r--r--r-- 1 root root 4.0K Apr 22 20:43 panel
-r--r--r-- 1 root root 4.0K Apr 22 20:43 vertical_position

Then you can interrogate each file with the cat command to tell you where the port is, like as follows:
Code:
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat dock
no
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat horizontal_position
left
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat lid
no
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat panel
top
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat vertical_position
upper

The result is that the usb port has been identified on this box. No other usb port will have these physical locations.

*If the dmesg command asks to be run by root (which is often the default), and you wish to run the command as user, you can alter that to allow the user to run it by running the following as root:
Code:
echo 0 > /proc/sys/kernel/dmesg_restrict
Then, to update the system, run as root:
Code:
sysctl --system
 
Last edited:
You are right. The problem was really this one USB stick that I used for testing. It works much faster with other sticks. Thanks for the tip.
I suggest you back up data from that USB and reformat it.

Example to create GPT partition table with single partition (replace /dev/sdX with USB drive)

Bash:
sudo fdisk /dev/sdX
g # to create GPT partition table
n # to create a new partition
w # to write changes to disk

Example to format USB to ext4 filesystem (replace dev/sdXn with newly created partition on that USB)

Bash:
# -c check the partition for bad blocks before formatting
# If this option is specified twice, then a slower read-write test is used instead of a fast read-only test
# -L Partition label
# -b Specify the size of blocks in bytes (check with fdisk)
# -t Specify the filesystem type (i.e., ext2, ext3, ext4, etc.)
# -v Verbose execution
sudo mkfs.ext4 -c -v -t ext4 -L "Data" -b 4096 /dev/sdXn

Then try again with lsblk

If this doesn't fix your USB it's faulty and ready for dumpster.
 
@84773 wrote:

There are a few ways of determining which usb is plugged into which port.

One way is to determine the usb controller based number when plugging the usb in, then inspecting the details of the physical location in the /sys directory.

To do it this way, run in a terminal:
Code:
dmesg -w
*(See below for how to run it as user rather than root, if you wish to do that)

Then plug the usb in, and observe the output on the terminal screen. The following sort of output appears pretty much immediately:
Code:
[49881.113615] usb 1-6: new high-speed USB device number 9 using xhci_hcd
[49881.262768] usb 1-6: New USB device found, idVendor=0781, idProduct=55a1, bcdDevice= 1.00
[49881.262784] usb 1-6: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[49881.262789] usb 1-6: Product: Cruzer Spark
[49881.262793] usb 1-6: Manufacturer: SanDisk
[49881.262797] usb 1-6: SerialNumber: 03025011071521122934
[49881.266685] usb-storage 1-6:1.0: USB Mass Storage device detected
[49881.267682] scsi host9: usb-storage 1-6:1.0
[49882.271659] scsi 9:0:0:0: Direct-Access     SanDisk  Cruzer Spark     1.00 PQ: 0 ANSI: 6
[49882.272261] sd 9:0:0:0: Attached scsi generic sg1 type 0
[49882.273188] sd 9:0:0:0: [sda] 30031872 512-byte logical blocks: (15.4 GB/14.3 GiB)
[49882.274602] sd 9:0:0:0: [sda] Write Protect is off
[49882.274619] sd 9:0:0:0: [sda] Mode Sense: 43 00 00 00
[49882.274997] sd 9:0:0:0: [sda] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[49882.297398]  sda: sda1
[49882.297790] sd 9:0:0:0: [sda] Attached SCSI removable disk

The output shows the usb controller details for this usb are: 1-6:1.0.

To close the dmesg output, press: cntl+c.

Navigate to the system details as follows for this bus controller:
Code:
cd /sys/bus/usb/devices/1-6:1.0/physical_location

List the files at the directory named "physical_location":
Code:
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ ls -al
total 0
-r--r--r-- 1 root root 4.0K Apr 22 20:43 dock
-r--r--r-- 1 root root 4.0K Apr 22 20:43 horizontal_position
-r--r--r-- 1 root root 4.0K Apr 22 20:43 lid
-r--r--r-- 1 root root 4.0K Apr 22 20:43 panel
-r--r--r-- 1 root root 4.0K Apr 22 20:43 vertical_position

Then you can interrogate each file with the cat command to tell you where the port is, like as follows:
Code:
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat dock
no
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat horizontal_position
left
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat lid
no
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat panel
top
[tom@min /sys/bus/usb/devices/1-6:1.0/physical_location]$ cat vertical_position
upper

The result is that the usb port has been identified on this box. No other usb port will have these physical locations.

*If the dmesg command asks to be run by root (which is often the default), and you wish to run the command as user, you can alter that to allow the user to run it by running the following as root:
Code:
echo 0 > /proc/sys/kernel/dmesg_restrict
Then, to update the system, run as root:
Code:
sysctl --system
thank you osprey. Interesting approach, but the reason why I use lsblk is that I want to read the label of the USB stick.

Also, I can't find the "physical_location" directory.

[14576.180432] usb 3-14: USB disconnect, [B][COLOR=rgb(44, 130, 201)]device number 47[/COLOR][/B]
[14594.508263] usb 3-14: new high-speed USB [COLOR=rgb(41, 105, 176)][B]device number 48[/B][/COLOR] using xhci_hcd
[14594.665787] usb 3-14: New USB device found, idVendor=0951, idProduct=172b, bcdDevice= 0.01
[14594.665792] usb 3-14: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[14594.665794] usb 3-14: Product: DataTraveler 70
[14594.665796] usb 3-14: Manufacturer: Kingston
[14594.665798] usb 3-14: SerialNumber: 1831BF21E8DEF780A96C0099
[14594.741687] usb-storage [COLOR=rgb(184, 49, 47)][B]3-14:1.0[/B][/COLOR]: USB Mass Storage device detected
[14594.741989] scsi host6: usb-storage 3-14:1.0
[14595.760817] scsi 6:0:0:0: Direct-Access Kingston DataTraveler 70 PQ: 0 ANSI: 6
[14595.761066] sd 6:0:0:0: Attached scsi generic sg2 type 0
[14595.761537] sd 6:0:0:0: [sdc] 120845300 512-byte logical blocks: (61.9 GB/57.6 GiB)
[14595.761816] sd 6:0:0:0: [sdc] Write Protect is off
[14595.761819] sd 6:0:0:0: [sdc] Mode Sense: 4f 00 00 00
[14595.762061] sd 6:0:0:0: [sdc] Write cache: disabled, read cache: enabled, doesn't support DPO or FUA
[14595.766010] sdc: sdc1
[14595.767146] sd 6:0:0:0: [sdc] Attached SCSI removable disk

christos@evnipcus03:~$ ll /sys/bus/usb/devices/[COLOR=rgb(184, 49, 47)][B]3-14:1.0[/B][/COLOR]/
total 0
drwxr-xr-x 6 root root 0 Apr 22 15:04 ./
drwxr-xr-x 5 root root 0 Apr 22 15:04 ../
-rw-r--r-- 1 root root 4096 Apr 22 15:05 authorized
-r--r--r-- 1 root root 4096 Apr 22 15:05 bAlternateSetting
-r--r--r-- 1 root root 4096 Apr 22 15:04 bInterfaceClass
-r--r--r-- 1 root root 4096 Apr 22 15:04 bInterfaceNumber
-r--r--r-- 1 root root 4096 Apr 22 15:05 bInterfaceProtocol
-r--r--r-- 1 root root 4096 Apr 22 15:04 bInterfaceSubClass
-r--r--r-- 1 root root 4096 Apr 22 15:05 bNumEndpoints
lrwxrwxrwx 1 root root 0 Apr 22 15:04 driver -> ../../../../../../bus/usb/drivers/usb-storage/
drwxr-xr-x 3 root root 0 Apr 22 15:05 ep_02/
drwxr-xr-x 3 root root 0 Apr 22 15:05 ep_81/
lrwxrwxrwx 1 root root 0 Apr 22 15:05 firmware_node -> ../../../../../LNXSYSTM:00/LNXSYBUS:00/PNP0A08:00/device:27/device:28/device:36/
drwxr-xr-x 5 root root 0 Apr 22 15:04 host6/
-r--r--r-- 1 root root 4096 Apr 22 15:05 modalias
drwxr-xr-x 2 root root 0 Apr 22 15:05 power/
lrwxrwxrwx 1 root root 0 Apr 22 15:04 subsystem -> ../../../../../../bus/usb/
-r--r--r-- 1 root root 4096 Apr 22 15:05 supports_autosuspend
-rw-r--r-- 1 root root 4096 Apr 22 15:04 uevent

Every time I plug in the stick, the device number is incremented. Even if it is the same stick. Do you know is there a limit to this counter and does it have to be reset manually? I once had such a problem with a Windows driver for USB devices. It stopped counting at 1000 and no longer accepted new devices.
 
@84773 wrote:
I can't find the "physical_location" directory.
Try:
Code:
cd /sys/bus/usb/devices/3-14:1.0/physical_location
duplicating the situation which the output in post #8 is reported.

In relation to the device number being incremented, that is normal and a function of the fact that the kernel has to see each instance of the device separately and not be confused. The kernel won't re-apply the details of the same usb that is withdrawn and inserted again, but rather, sees each insertion occurrence as a separate process. That makes sense, because the usb that is withdrawn may have had alterations to it by the time it's re-inserted, so it's treated as a different device, and so allocated a new device number. It may however, appear on the same bus as before.
 
Last edited:

Members online


Latest posts

Top