Make lsblk list devices by-id

14,392

Solution 1

The by-id names consists of the drive model together with the serial something which lsblk can be instructed to list:

lsblk -o name,model,serial

The output of this command will look something like this:

NAME   MODEL            SERIAL
sda    SAMSUNG HD203WI  S1UYJ1VZ500792                                       
├─sda1                  
└─sda9                  
sdb    ST500DM002-1BD14 W2APGFP8
├─sdb1                  
└─sdb9                  
sdc    ST500DM002-1BD14 W2APGFS0
├─sdc1                  
└─sdc9 

For posterity here's also a longer command with some commonly used columns:

sudo lsblk -o name,size,fstype,label,model,serial,mountpoint

The output of which could be:

NAME     SIZE FSTYPE            LABEL         MODEL            SERIAL          MOUNTPOINT
sda      1,8T zfs_member                      SAMSUNG HD203WI  S1UYJ1VZ500792
├─sda1   1,8T zfs_member        storage                                        /home    
└─sda9     8M zfs_member                                                       
sdb    465,8G btrfs                           ST500DM002-1BD14 W2APGFP8        
├─sdb1 465,8G btrfs                                                            
└─sdb9     8M btrfs                                                            
sdc    465,8G btrfs                           ST500DM002-1BD14 W2APGFS0        
├─sdc1 465,8G btrfs             rpool                                          /      
└─sdc9     8M btrfs 

Solution 2

As found here, the device ids can be seen by ls -l /dev/disk/by-id.
So, Your task could be accomplished e.g. by something like:

lsblk |awk 'NR==1{print $0" DEVICE-ID(S)"}NR>1{dev=$1;gsub("[^[:alnum:]]","",dev);printf $0"\t\t";system("find /dev/disk/by-id -lname \"*"dev"\" -printf \" %p\"");print "";}'

or

lsblk -r|awk 'NR==1{print $0" DEVICE-ID(S)"}NR>1{dev=$1;printf $0" ";system("find /dev/disk/by-id -lname \"*"dev"\" -printf \" %p\"");print "";}'
Share:
14,392

Related videos on Youtube

Rovanion
Author by

Rovanion

Updated on September 18, 2022

Comments

  • Rovanion
    Rovanion almost 2 years

    I'm constantly having the situation where I want to correlate the output of lsblk which prints devices in a tree with their name in the scheme of /dev/sdXY with the drives /dev/disk/by-id/ names.

  • Gerald Schade
    Gerald Schade over 5 years
    Unfortunately, on my current opensuse and ubuntu systems, lsblk -o serial did not output anything. Furtheron, the output of lsblk -o model did not exacty match that in /dev/disk/by-id/.
  • Sridhar Sarnobat
    Sridhar Sarnobat about 2 years
    I can't believe I've been relying on df for so long. I need to start leveraging lsblk with these extra options. It would make me a lot less paranoid when doing destructive operations like dd, rsync --delete, mkfs, sgdisk etc. etc.