Operating System - HP-UX
1833788 Members
2664 Online
110063 Solutions
New Discussion

Retrieving disk serial number programmatically

 
SOLVED
Go to solution
Nikhil_1
Advisor

Retrieving disk serial number programmatically

Hello,

I am trying to retrieve the disk serial number programmatically.
I saw a post

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xc4bf0bce6f33d6118fff0090279cd0f9,00.html

that gives a way (run cstm tool and parse the output).

Can we retrieve the serial number by calling some API in C
or a kernel call?

How does cstm find the serial number? (I tried running tusc,
but couldn't extract much info). Is it doing ioctl? If yes,
then what IOCTL code can be used to get the serial number?

Any help will be appreciated.

With regards,
Nikhil
10 REPLIES 10
Robin Wakefield
Honored Contributor
Solution

Re: Retrieving disk serial number programmatically

Hi Nikhil,

I put the following together, it gives you most of the serial number - I haven't had time to work out the rest, but should get you going:

#include
#include
#include
#include
#include
#include

int main(int argc,char *argv[])
{
struct inquiry inquiry;
char *dev;
int fd;
if (argc != 2)
{
fprintf(stderr, "Usage: %s \n", argv[0]);
exit(1);
}
if (argc == 2)
dev=argv[1];
/* open the device */
if ((fd=open(dev,O_RDONLY))<0)
{
perror("open failed");
exit(errno);
}

if ((ioctl(fd,SIOC_INQUIRY,&inquiry))<0)
{
perror("ioctl(SIOC_INQUIRY) failed");
exit(errno);
}
printf("\nVendor ID = %s\n",inquiry.vendor_id);
printf("Product ID=%s\n",inquiry.product_id);
printf("Vendor Spec=%s\n",inquiry.vendor_spec);
printf("Rev Number=%s\n",inquiry.rev_num);
exit(0) ;
}

Rgds, Robin
Vincent Fleming
Honored Contributor

Re: Retrieving disk serial number programmatically

Nikhil,

The inquiry string will not give the S/N of the device... see my reply to your post in the Storage forum.

Vince
No matter where you go, there you are.
Jean-Louis Phelix
Honored Contributor

Re: Retrieving disk serial number programmatically

Vincent,

I'm suprised because Robin's answer as well as mine (Nikhil posted several questions) are both giving the same SN as cstm. In fact the structure member name I use is even called 'serial' ... But Robin's solution gives more detailed information.

Regards.

#include
#include
#include
#include
#include
#include
#include
#include
#define SIOC_SERIAL _IOR('S', 254, struct scsi_serial)

main (argc, argv)
int argc;
char *argv[];
{
int Fd, Inq;
struct scsi_serial Serial;

if ( (Fd = open (argv[1], O_RDONLY)) < 0 )
{
perror ("open");
exit (1);
}

if ( (Inq = ioctl (Fd, SIOC_SERIAL, &Serial)) != 0 )
{
perror ("ioctl SIOC_SERIAL");
}
else
{
printf ("Serial number for '%s' is '%s'\n", argv[1], Serial.serial);
}
}
It works for me (© Bill McNAMARA ...)
Robin Wakefield
Honored Contributor

Re: Retrieving disk serial number programmatically

Jean-Louis,

Mine only shows the 1st 8 chars. Yours is correct.

Rgds, Robin
Nikhil_1
Advisor

Re: Retrieving disk serial number programmatically

Thank you all for your help.
I am able to get the SCSI disk serial number using ioctl.

With regards,
Nikhil
Stefan Farrelly
Honored Contributor

Re: Retrieving disk serial number programmatically

Jean-Louis,

whats the compile options for your program ? I cant get it to compile, tried everything.

Cheers,

Stefan
Im from Palmerston North, New Zealand, but somehow ended up in London...
Jean-Louis Phelix
Honored Contributor

Re: Retrieving disk serial number programmatically

Stephan,

I'm quite surprised because my compile options are strictly defaults ... since I only use 'make prog'. I wrote it on a 11.11 system. Give me you error messages if you want.

Regards.
It works for me (© Bill McNAMARA ...)
Stefan Farrelly
Honored Contributor

Re: Retrieving disk serial number programmatically

Hi Jean-Louis,

Im trying it on 11.0;

> cc -o s serialnumber.c
cc: "serialnumber.c", line 16: error 1574: Unknown size for "Serial".
cc: "serialnumber.c", line 24: error 1594: The sizeof operator cannot be applied to types with unknown size.
cc: "serialnumber.c", line 30: error 1530: Undefined struct or union.

My 11i system is unavailable so cant try it there. Robins program compiles fine on 11.0 without any options. I would love to be able to compile yours on 11.0 also.

Thanks,

Stefan
Im from Palmerston North, New Zealand, but somehow ended up in London...
Jean-Louis Phelix
Honored Contributor

Re: Retrieving disk serial number programmatically

Hi Stefan,

I can't believe it ... Even on my 11.11 it gives the same messages. Yesterday I spent all my time looking for Harry's new hat and grep'ing in include files to find related structures. But I can see that Nikhil had the same problem. I can't find out what have changed between last source working version and my post, but I'm sure I didn't use any compile option. Anyway, declaring the structure in the source is not clean but works ... It-s defined in /usr/include/sys/scsi_ctl.h but seems to be active only if you have also _KERNEL defined. But with this flag, you get in big other troubles when compiling. I'am sorry !

#include
#include
#include
#include

struct scsi_serial
{
ubit8 device_type;
ubit8 page_code;
ubit8 reserved;
ubit8 page_length;
ubit8 serial[252];
};

#define SIOC_SERIAL _IOR('S', 254, struct scsi_serial)

main (argc, argv)
int argc;
char *argv[];
{
int Fd, Inq;
struct scsi_serial Serial;

if ( (Fd = open (argv[1], O_RDONLY)) < 0 )
{
perror ("open");
exit (1);
}

if ( (Inq = ioctl (Fd, SIOC_SERIAL, &Serial)) != 0 )
{
perror ("ioctl SIOC_SERIAL");
}
else
{
printf ("Serial number for '%s' is '%s'\n", argv[1], Serial.serial);
}
}

It works for me (© Bill McNAMARA ...)
Nikhil_1
Advisor

Re: Retrieving disk serial number programmatically

Jean-Louis Phelix wrote:
> Anyway, declaring the structure in the source is not clean but works

This is exactly what I did. I used g++3.2 as well as aCC 3.31.

Thanks,
Nikhil