How to find out if SCSI device (say /dev/sda) is a disk or not via ioctl calls or other ?
I have tried the following but the ioctl
call fails. My /dev/sda is a USB flash disk.
#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 main(int argc, char** argv) {
char *dev = "/dev/sda";
struct sg_scsi_id m_id;
int rc;
int fd;
fd = open(dev, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
perror(dev);
}
memset(&m_id, 0, sizeof (m_id));
rc = ioctl(fd, SG_GET_SCSI_ID, &m_id);
if (rc < 0) {
close(fd);
printf("FAIL: ioctl SG_GET_SCSI_ID, rc=%d, errno=%d\n", rc, errno);
} else {
if (m_id.scsi_type == TYPE_DISK || m_id.scsi_type == 14) {
printf("OK: is disk\n");
} else {
printf("OK: is NOT disk\n");
}
}
close(fd);
return (EXIT_SUCCESS);
}
// result is: FAIL: ioctl SG_GET_SCSI_ID, rc=-1, errno=22