Don't show empty CD devices (RHBZ#514505).
[libguestfs.git] / daemon / devsparts.c
index b0d7956..a198ccd 100644 (file)
@@ -1,5 +1,5 @@
 /* libguestfs - the guestfsd daemon
 /* libguestfs - the guestfsd daemon
- * Copyright (C) 2009 Red Hat Inc. 
+ * Copyright (C) 2009 Red Hat Inc.
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -37,6 +37,7 @@ do_list_devices (void)
   DIR *dir;
   struct dirent *d;
   char buf[256];
   DIR *dir;
   struct dirent *d;
   char buf[256];
+  int fd;
 
   dir = opendir ("/sys/block");
   if (!dir) {
 
   dir = opendir ("/sys/block");
   if (!dir) {
@@ -45,8 +46,22 @@ do_list_devices (void)
   }
 
   while ((d = readdir (dir)) != NULL) {
   }
 
   while ((d = readdir (dir)) != NULL) {
-    if (strncmp (d->d_name, "sd", 2) == 0) {
+    if (strncmp (d->d_name, "sd", 2) == 0 ||
+       strncmp (d->d_name, "hd", 2) == 0 ||
+       strncmp (d->d_name, "vd", 2) == 0) {
       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
       snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
+
+      /* RHBZ#514505: Some versions of qemu <= 0.10 device to add a
+       * CD-ROM device even though we didn't request it.  Try to
+       * detect this by seeing if the device contains media.
+       */
+      fd = open (buf, O_RDONLY);
+      if (fd == -1) {
+       perror (buf);
+       continue;
+      }
+      close (fd);
+
       if (add_string (&r, &size, &alloc, buf) == -1) {
        closedir (dir);
        return NULL;
       if (add_string (&r, &size, &alloc, buf) == -1) {
        closedir (dir);
        return NULL;
@@ -77,6 +92,7 @@ do_list_partitions (void)
   DIR *dir, *dir2;
   struct dirent *d;
   char buf[256], devname[256];
   DIR *dir, *dir2;
   struct dirent *d;
   char buf[256], devname[256];
+  int fd;
 
   dir = opendir ("/sys/block");
   if (!dir) {
 
   dir = opendir ("/sys/block");
   if (!dir) {
@@ -85,7 +101,22 @@ do_list_partitions (void)
   }
 
   while ((d = readdir (dir)) != NULL) {
   }
 
   while ((d = readdir (dir)) != NULL) {
-    if (strncmp (d->d_name, "sd", 2) == 0) {
+    if (strncmp (d->d_name, "sd", 2) == 0 ||
+       strncmp (d->d_name, "hd", 2) == 0 ||
+       strncmp (d->d_name, "vd", 2) == 0) {
+      snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
+
+      /* RHBZ#514505: Some versions of qemu <= 0.10 device to add a
+       * CD-ROM device even though we didn't request it.  Try to
+       * detect this by seeing if the device contains media.
+       */
+      fd = open (buf, O_RDONLY);
+      if (fd == -1) {
+       perror (buf);
+       continue;
+      }
+      close (fd);
+
       strncpy (devname, d->d_name, sizeof devname);
       devname[sizeof devname - 1] = '\0';
 
       strncpy (devname, d->d_name, sizeof devname);
       devname[sizeof devname - 1] = '\0';
 
@@ -131,3 +162,22 @@ do_list_partitions (void)
   sort_strings (r, size-1);
   return r;
 }
   sort_strings (r, size-1);
   return r;
 }
+
+int
+do_mkfs (char *fstype, char *device)
+{
+  char *err;
+  int r;
+
+  IS_DEVICE (device, -1);
+
+  r = command (NULL, &err, "/sbin/mkfs", "-t", fstype, device, NULL);
+  if (r == -1) {
+    reply_with_error ("mkfs: %s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}