- while ((d = readdir (dir)) != NULL) {
- if (strncmp (d->d_name, "sd", 2) == 0) {
- snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
- if (add_string (&r, &size, &alloc, buf) == -1) {
- closedir (dir);
- return NULL;
+ while(1) {
+ errno = 0;
+ struct dirent *d = readdir(dir);
+ if(NULL == d) break;
+
+ if (STREQLEN (d->d_name, "sd", 2) ||
+ STREQLEN (d->d_name, "hd", 2) ||
+ STREQLEN (d->d_name, "vd", 2) ||
+ STREQLEN (d->d_name, "sr", 2)) {
+ char dev_path[256];
+ snprintf (dev_path, sizeof dev_path, "/dev/%s", d->d_name);
+
+ /* RHBZ#514505: Some versions of qemu <= 0.10 add a
+ * CD-ROM device even though we didn't request it. Try to
+ * detect this by seeing if the device contains media.
+ */
+ int fd = open (dev_path, O_RDONLY);
+ if (fd == -1) {
+ perror (dev_path);
+ continue;
+ }
+ close (fd);
+
+ /* Call the map function for this device */
+ if((*func)(d->d_name, &r, &size, &alloc) != 0) {
+ err = 1;
+ break;