/* example: retrieve serial number from virtio block device */ #include #include #include #include #define iswhite(c) (!('!' <= (c) && (c) <= '~')) #ifndef VBLK_GET_SN #define VBLK_GET_SN ((unsigned int)('V' << 24 | 'B' << 16 | 'L' << 8 | 'K')) #endif /* get virtblk drive serial# */ int main(int ac, char ***av) { int fd, nb, i; unsigned char sn[30]; unsigned char *p; sn[0] = sizeof (sn); if ((fd = open("/dev/vda", O_RDONLY)) < 0) perror("can't open device"), exit(1); else if ((nb = ioctl(fd, VBLK_GET_SN, &sn)) < 0) perror("can't ioctl device"), exit(1); printf("returned %d bytes:\n", nb); for (p = sn, i = nb; 0 <= --i; ++p) printf("%02x%c", *p, i ? ' ' : '\t'); for (p = sn, i = nb; 0 <= --i; ++p) printf("%c%s", iswhite(*p) ? '.' : *p, i ? "" : "\n"); return (0); }