Solved Hard Drive Serial Number

How can I grab the hard drive serial number in C on a machine running FreeBSD? The hard drive in question is an mSATA SSD. Specifically the TS256GMSA370.

In my searching I found the following on this forum: Thread 9548

I receive the error 'Inappropriate ioctl for device' when running the following:
PHP:
ioctl(fd, IOCATAGPARM, &atap);

From my understanding, this code is for ATA devices. When running camcontrol I receive the following:
Code:
camcontrol identify /dev/ada0
protocol         ATA/ATAPI-9 SATA 3.x
device model     TS256GMSA370
Now, I can, as a last resort, execute the camcontrol command within C and extract the serial this way. However, I'd prefer to grab the serial natively in C.

Is there an ioctl command that will allow me to grab the serial from an mSATA SSD, or perhaps another method besides ioctl?

Solution
/usr/src/usr.sbin/diskinfo/diskinfo.c has
Code:
...
if (ioctl(fd, DIOCGIDENT, ident) == 0)
...
 
dmesg | grep -B1 'Serial Number'
geom disk list where ident is the serial.
sysctl kern.geom.confxml | grep -B20 -A10 ident might give you a hint to hex addresses.
 
All drives, including ATA, are accessed through cam(4) these days. So there's no difference between SCSI and ATA anymore.
 
dmesg | grep -B1 'Serial Number'
geom disk list where ident is the serial.
sysctl kern.geom.confxml | grep -B20 -A10 ident might give you a hint to hex addresses.

Thank you. geom seems like a nice alternative to using cammodel.

All drives, including ATA, are accessed through cam(4) these days. So there's no difference between SCSI and ATA anymore.

Thank you. Doing some searching led me to Chapter 12. Common Access Method SCSI Controllers. I'll do some reading on this manual to see if I can come up with a way to extract the serial using cam.
 
/usr/src/usr.sbin/diskinfo/diskinfo.c has
Code:
...
if (ioctl(fd, DIOCGIDENT, ident) == 0)
...
 
/usr/src/usr.sbin/diskinfo/diskinfo.c has
Code:
...
if (ioctl(fd, DIOCGIDENT, ident) == 0)
...

Thank you! DIOCGIDENT indeed grabs the serial for the drive. I'm still going to finish reading up on CAM, as I'm still in the middle of that, but I shall use DIOCGIDENT as it's exactly what I was looking for.
 
Back
Top