ioctl question(s)

D. Hugh Redelmeier hugh-pmF8o41NoarQT0dZR+AlfA at public.gmane.org
Sat Sep 16 06:35:45 UTC 2006


| From: Madison Kelly <linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org>

|   I have been trying to find ways of identifying partitions and I have
| *finally* found that it *is* possible to read the hard drive serial number of
| a drive behind a USB/IDE adapter. The 'scsiinfo -a /dev/sda' call returns it,
| though slightly mangled (ie: real s/n is "abcdefgh" but is shown as
| "badcfehg"). Regardless, I now know it _is_ possible!

This is byteswapped.  Could be a bug or a feature.  Perhaps worth
investigating -- if it is a bug, it would be good to report it.

I seem to recall that the SCSI standard is big-endian.  The x86 is
little-endian.  This might be a sufficient explanation.

Compare and contrast the output of hdparm -i and hdparm -I.  Read the
hdparm manual on this point.

|   So I've been doing more research and I keep seeing references to the 'ioctl'
| call, but I know very little (though at least some) C.

$ man 2 ioctl

It is a system call and is documented where system calls should be
documented.

Having said that, there are a great many different ioctls,
distinguished by the second parameter.

Each driver may well define its own ioctls, or implement some
conventional ones that similar drivers implement.  The first argument
influences the interpretation of the second argument.  So hunting down
the documentation for the specific ioctl isn't easy.

Furthermore, Linux driver folks don't always take the obligation to
document seriously.

| For example, I have
| read that 'hdparm' finds an IDE disk's serial number by calling "ioctl 0x30d".
| Of course I am not sure how to do this or how to use 'strace' on 'scsiinfo' to
| figure out where/what it is calling when it gets the serial number off the
| disk behind the USB/IDE adapter (probably a scsi call?).

strace is a good spelunking tool for sure.  Consider moving on to the
source if you want to see how to code that call.  Pay particular
attention to the #include statements

|   So then, given the many dire warnings I have read about messing with ioctl,
| I was hoping someone here could give me a hand with some sample code (I am not
| trying to be lazy, just cautious!).

See above: Linux is all sample code.

|   How could I, for example, call "getsn /dev/hda" and have it return what is
| returned when "ioctl 0x30d" is called? I am looking only for the most simple,
| basic working code that I can build on.
| 
|  In the meantime I am going to try to learn 'strace' enough to see how
| 'scsiinfo' gets the HDD's serial number! :)

You might consider just scripting invocations of these various
utilities.
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list