Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 742679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:46:32+00:00 2026-05-14T08:46:32+00:00

Is there any way to determine s/n of usb-drive in linux using C++ ?

  • 0

Is there any way to determine s/n of usb-drive in linux using C++ ?

If not C++ is there any other way different from hwinfo -disk and hdparm -i ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:46:32+00:00Added an answer on May 14, 2026 at 8:46 am

    I’ll try to summarize my experience regarding storage drive serial number retrieval on linux.
    I assume you want the serial number of the storage device identity (as per SCSI specification) not the serial number of the USB device (as per USB specification under Device Descriptor ), these two are different entities.

    NOTICE!
    Most devices tend to implement a serial number in the USB-Controller and leave the serial number of the inner SCSI-disk unimplemented.
    So if you want to uniquely identify an USB-device the best way is to create a string from the Device Descriptor (USB specification) like VendorId-ProductId-HardwareRevision-SerialNumber
    In the following I shall describe how to retrieve the SN of the storage drive, as asked.

    Drives fall in 2 categories (actually more, but let’s simplify): ATA-like (hda, hdb …) and SCSI-like (sda sdb …).
    USB drives fall in the second category, they are called SCSI attached disks.
    In both situation ioctl calls can be used to retrieve the required information (in our case the serial number).

    For SCSI devices (and these include USB drives) the Linux generic driver and it’s API is documented at tldp.
    The serial number on SCSI devices is available inside the Vital Product Data (short: VPD) and is retrievable by using the SCSI Inquiry Command.
    A commad line utility in linux that can fetch this VPD is sdparm:

    > yum install sdparm
    > sdparm --quiet --page=sn /dev/sda
        Unit serial number VPD page:
        3BT1ZQGR000081240XP7
    

    Note that not all devices have this serial number, the market is flooded with cheep knockoffs, and some usb flash disks return strange serials (for example my sandisk cruzer returns just the letter “u”). To overcome this some people choose to create a unique identifier by mixing different strings from VPD like Product ID, Vendor ID and Serial Number.

    Code in c:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <scsi/scsi.h>
    #include <scsi/sg.h>
    #include <sys/ioctl.h>
    
    int scsi_get_serial(int fd, void *buf, size_t buf_len) {
        // we shall retrieve page 0x80 as per http://en.wikipedia.org/wiki/SCSI_Inquiry_Command
        unsigned char inq_cmd[] = {INQUIRY, 1, 0x80, 0, buf_len, 0};
        unsigned char sense[32];
        struct sg_io_hdr io_hdr;
                int result;
    
        memset(&io_hdr, 0, sizeof (io_hdr));
        io_hdr.interface_id = 'S';
        io_hdr.cmdp = inq_cmd;
        io_hdr.cmd_len = sizeof (inq_cmd);
        io_hdr.dxferp = buf;
        io_hdr.dxfer_len = buf_len;
        io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
        io_hdr.sbp = sense;
        io_hdr.mx_sb_len = sizeof (sense);
        io_hdr.timeout = 5000;
    
        result = ioctl(fd, SG_IO, &io_hdr);
        if (result < 0)
            return result;
    
        if ((io_hdr.info & SG_INFO_OK_MASK) != SG_INFO_OK)
            return 1;
    
        return 0;
    }
    
    int main(int argc, char** argv) {
        char *dev = "/dev/sda";
        char scsi_serial[255];
        int rc;
        int fd;
    
        fd = open(dev, O_RDONLY | O_NONBLOCK);
        if (fd < 0) {
            perror(dev);
        }
    
        memset(scsi_serial, 0, sizeof (scsi_serial));
        rc = scsi_get_serial(fd, scsi_serial, 255);
        // scsi_serial[3] is the length of the serial number
        // scsi_serial[4] is serial number (raw, NOT null terminated)
        if (rc < 0) {
            printf("FAIL, rc=%d, errno=%d\n", rc, errno);
        } else
        if (rc == 1) {
            printf("FAIL, rc=%d, drive doesn't report serial number\n", rc);
        } else {
            if (!scsi_serial[3]) {
                printf("Failed to retrieve serial for %s\n", dev);
                return -1;
            }
            printf("Serial Number: %.*s\n", (size_t) scsi_serial[3], (char *) & scsi_serial[4]);
        }
        close(fd);
    
        return (EXIT_SUCCESS);
    }
    

    For the sake of completness i’ll also provide the code to retrieve the serial number for ATA devices (hda, hdb …). This will NOT work for USB devices.

    #include <stdlib.h>
    #include <stdio.h>
    #include <sys/ioctl.h>
    #include <linux/hdreg.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <string.h>
    #include <cctype>
    #include <unistd.h>
    
    int main(){
        struct hd_driveid *id;
        char *dev = "/dev/hda";
        int fd;
    
        fd = open(dev, O_RDONLY|O_NONBLOCK);
        if(fd < 0) {
            perror("cannot open");
        }
        if (ioctl(fd, HDIO_GET_IDENTITY, id) < 0) {
            close(fd);
            perror("ioctl error");
        } else {
            // if we want to retrieve only for removable drives use this branching
            if ((id->config & (1 << 7)) || (id->command_set_1 & 4)) {
                close(fd);
                printf("Serial Number: %s\n", id->serial_no);
            } else {
                perror("support not removable");
            }
            close(fd);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to determine whether a linux executable is using soft float
Is there any way to determine if the mouse scrolls up or down using
Possible Duplicate: Is there any way to determine text direction from CultureInfo in asp.net?
Is there any way to determine if a tableview header, not a section header,
Is there any way to determine in Javascript if an object was created using
is there any way to determine from an Excel session, which DLL an UDF
Is there any way to determine from within a required script, where that require
May I know is there any way to determine parent class from boost::any? #include
Is there any way to determine which version of Firebird SQL is running? Using
Is there any way to determine whether the browser is using hardware acceleration for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.