1

I know that for a disk in linux, we can use commands such as hdparm or smartctl to get its basic information. But for the implementation of these commands, I think all of them are implemented by reading the corresponding file of a disk in linux. For example, to get the size of a device /dev/sda, we can use cat /sys/block/sda/size instead of using command such hdparm or smartctl. So the question is how to get the serial number of a disk by reading its corresponding file directly.

  • 1
    grep SERIAL_SHORT /run/udev/data/b8* (your first drive would be b8:0, the second b8:16, the third b8:32 etc) – don_crissti Nov 30 '15 at 3:06
  • i don't think disk serial # resides in an existing file somewhere (such as under /proc). When using smartcl -all /dev/sda or hdparm -I those get the information directly from the drive (from the sata/sas controller). – ron Nov 25 '19 at 23:04
  • similiarly if you have N disks as a raid 5/6 behind a controller, the serial numbers of each disk is not in some file but rather can be obtained with smartctl or hdparm. linux has no reason to go fetch disk serial number and save it in some file. – ron Nov 25 '19 at 23:11
0

You could also use /proc/partitions to find that info.

That file gives you the size in number of blocks that all your partitions have, But also gives you the same information for your entire disks.

| improve this answer | |
  • I don't need information of number of blocks but the serial number. – Jin Cai Nov 30 '15 at 5:57
0

You could use strace to find how smartctl get the serial number of a disk. CMD line likes this: strace smartctl -a /dev/sda. Maybe it just get the serial by ioctl.

| improve this answer | |
0

I just have a try. It do get the serial by ioctl. This is some output of strace:

open("/dev/sda", O_RDONLY|O_NONBLOCK)   = 3
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[6]=[12, 00, 00, 00, 24, 00], mx_sb_len=32, iovec_count=0, dxfer_len=36, timeout=20000, flags=0, data[36]=["\0\0\5\2[\0\0\0ATA     WDC WD5000LPVX-0"...], status=00, masked_status=00, sb[0]=[], host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB\377?7\310\20\0\0\0\0\0?\0\0\0\0\0\0\0    W -DXW11"...], status=00, masked_status=00, sb[0]=[], host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
ioctl(3, SG_IO, {'S', SG_DXFER_FROM_DEV, cmd[16]=[85, 08, 0e, 00, 00, 00, 01, 00, 00, 00, 00, 00, 00, 00, ec, 00], mx_sb_len=32, iovec_count=0, dxfer_len=512, timeout=20000, flags=0, data[512]=["zB\377?7\310\20\0\0\0\0\0?\0\0\0\0\0\0\0    W -DXW11"...], status=00, masked_status=00, sb[0]=[], host_status=0, driver_status=0, resid=0, duration=0, info=0}) = 0
| improve this answer | |
0

Firstly, you can use lsblk command to do that. Step one is get all of SCSI devices only. Also all partitions, slaves and holder devices are ignored.

~$ lsblk -S  
NAME HCTL       TYPE VENDOR   MODEL             REV TRAN  
sdb  1:0:1:0    disk Msft     Virtual Disk     1.0  
sr0  5:0:0:0    rom  Msft     Virtual CD/ROM   1.0  ata  
sdc  3:0:0:10   disk Msft     Virtual Disk     1.0  
sda  0:0:0:0    disk Msft     Virtual Disk     1.0  

Then you get the serial number for each disk

~$ lsblk --nodeps -no serial /dev/sda  
600224807cc344ea899e344614ead465  

And use strace command to see deeply how does lsblk get the serial number? I did do this, but no sure what I see was right. You can try it for you.

lsblk needs to be able to lookup sysfs - a filesystem for exporting kernel objects - path by major:minor, which is done done by using /sys/dev/block.

Also you can refer man page of sysfs at here.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.