> For the complete documentation index, see [llms.txt](https://applezu.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://applezu.netdpi.net/linux-prog/linux-get-hd-serial.md).

# \[C] Linux 取得 HD 序號

```c
#include <stdio.h>
#include <linux/hdreg.h>
#include <fcntl.h>

int main(int argc,char **argv)
{
    int fd;
    struct hd_driveid id;
    fd = open (argv[1], O_RDONLYO_NONBLOCK);

    if ( ioctl(fd, HDIO_GET_IDENTITY, &id)) {

        printf("ERROR!!\n");
        exit(1);
    }

    printf("%s serial-> %s\n", argv[1], id.serial_no);

    close( fd );
}
```
