0

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
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.

  • 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.

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

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.