Change to using ext2-based, cached supermin appliance.
[libguestfs.git] / daemon / devsparts.c
index 324b887..95e4a68 100644 (file)
@@ -1,5 +1,5 @@
 /* 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
 #include "daemon.h"
 #include "actions.h"
 
-char **
-do_list_devices (void)
+typedef int (*block_dev_func_t)(const char *dev,
+                                char ***r, int *size, int *alloc);
+
+/* Execute a given function for each discovered block device */
+static char**
+foreach_block_device (block_dev_func_t func)
 {
   char **r = NULL;
   int size = 0, alloc = 0;
+
   DIR *dir;
-  struct dirent *d;
-  char buf[256];
+  int err = 0;
 
   dir = opendir ("/sys/block");
   if (!dir) {
@@ -44,152 +48,145 @@ do_list_devices (void)
     return NULL;
   }
 
-  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);
+
+      /* Ignore the root device. */
+      if (is_root_device (dev_path))
+        continue;
+
+      /* 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;
       }
     }
   }
 
-  if (add_string (&r, &size, &alloc, NULL) == -1) {
-    closedir (dir);
+  /* Check readdir didn't fail */
+  if(0 != errno) {
+    reply_with_perror ("readdir: /sys/block");
+    free_stringslen(r, size);
     return NULL;
   }
 
+  /* Close the directory handle */
   if (closedir (dir) == -1) {
     reply_with_perror ("closedir: /sys/block");
-    free_strings (r);
+    free_stringslen(r, size);
     return NULL;
   }
 
-  sort_strings (r, size-1);
-  return r;
-}
-
-char **
-do_list_partitions (void)
-{
-  char **r = NULL;
-  int size = 0, alloc = 0;
-  DIR *dir, *dir2;
-  struct dirent *d;
-  char buf[256], devname[256];
-
-  dir = opendir ("/sys/block");
-  if (!dir) {
-    reply_with_perror ("opendir: /sys/block");
+  /* Free the result list on error */
+  if(err) {
+    free_stringslen(r, size);
     return NULL;
   }
 
-  while ((d = readdir (dir)) != NULL) {
-    if (strncmp (d->d_name, "sd", 2) == 0) {
-      strncpy (devname, d->d_name, sizeof devname);
-      devname[sizeof devname - 1] = '\0';
-
-      snprintf (buf, sizeof buf, "/sys/block/%s", devname);
-
-      dir2 = opendir (buf);
-      if (!dir2) {
-       reply_with_perror ("opendir: %s", buf);
-       free_stringslen (r, size);
-       return NULL;
-      }
-      while ((d = readdir (dir2)) != NULL) {
-       if (strncmp (d->d_name, devname, strlen (devname)) == 0) {
-         snprintf (buf, sizeof buf, "/dev/%s", d->d_name);
-
-         if (add_string (&r, &size, &alloc, buf) == -1) {
-           closedir (dir2);
-           closedir (dir);
-           return NULL;
-         }
-       }
-      }
-
-      if (closedir (dir2) == -1) {
-       reply_with_perror ("closedir: /sys/block/%s", devname);
-       free_stringslen (r, size);
-       return NULL;
-      }
-    }
-  }
+  /* Sort the devices */
+  sort_strings (r, size);
 
+  /* NULL terminate the list */
   if (add_string (&r, &size, &alloc, NULL) == -1) {
-    closedir (dir);
-    return NULL;
-  }
-
-  if (closedir (dir) == -1) {
-    reply_with_perror ("closedir: /sys/block");
-    free_strings (r);
     return NULL;
   }
 
-  sort_strings (r, size-1);
   return r;
 }
 
-int
-do_mkfs (const char *fstype, const char *device)
+/* Add a device to the list of devices */
+static int
+add_device(const char *device,
+           char ***const r, int *const size, int *const alloc)
 {
-  char *err;
-  int r;
+  char dev_path[256];
+  snprintf (dev_path, sizeof dev_path, "/dev/%s", device);
 
-  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);
+  if (add_string (r, size, alloc, dev_path) == -1) {
     return -1;
   }
 
-  free (err);
   return 0;
 }
 
-int
-do_sfdisk (const char *device, int cyls, int heads, int sectors,
-          char * const* const lines)
+char **
+do_list_devices (void)
+{
+  return foreach_block_device(add_device);
+}
+
+static int
+add_partitions(const char *device,
+               char ***const r, int *const size, int *const alloc)
 {
-  FILE *fp;
-  char buf[256];
-  int i;
-
-  IS_DEVICE (device, -1);
-
-  /* Safe because of IS_DEVICE above. */
-  strcpy (buf, "/sbin/sfdisk");
-  if (cyls)
-    sprintf (buf + strlen (buf), " -C %d", cyls);
-  if (heads)
-    sprintf (buf + strlen (buf), " -H %d", heads);
-  if (sectors)
-    sprintf (buf + strlen (buf), " -S %d", sectors);
-  sprintf (buf + strlen (buf), " %s", device);
-
-  fp = popen (buf, "w");
-  if (fp == NULL) {
-    reply_with_perror (buf);
+  char devdir[256];
+
+  /* Open the device's directory under /sys/block */
+  snprintf (devdir, sizeof devdir, "/sys/block/%s", device);
+
+  DIR *dir = opendir (devdir);
+  if (!dir) {
+    reply_with_perror ("opendir: %s", devdir);
+    free_stringslen (*r, *size);
     return -1;
   }
 
-  for (i = 0; lines[i] != NULL; ++i) {
-    if (fprintf (fp, "%s\n", lines[i]) < 0) {
-      reply_with_perror (buf);
-      fclose (fp);
-      return -1;
+  /* Look in /sys/block/<device>/ for entries starting with <device>
+   * e.g. /sys/block/sda/sda1
+   */
+  errno = 0;
+  struct dirent *d;
+  while ((d = readdir (dir)) != NULL) {
+    if (STREQLEN (d->d_name, device, strlen (device))) {
+      char part[256];
+      snprintf (part, sizeof part, "/dev/%s", d->d_name);
+
+      if (add_string (r, size, alloc, part) == -1) {
+        closedir (dir);
+        return -1;
+      }
     }
   }
 
-  if (fclose (fp) == EOF) {
-    reply_with_perror (buf);
-    fclose (fp);
+  /* Check if readdir failed */
+  if(0 != errno) {
+      reply_with_perror ("readdir: %s", devdir);
+      free_stringslen(*r, *size);
+      return -1;
+  }
+
+  /* Close the directory handle */
+  if (closedir (dir) == -1) {
+    reply_with_perror ("closedir: /sys/block/%s", device);
+    free_stringslen (*r, *size);
     return -1;
   }
 
   return 0;
 }
+
+char **
+do_list_partitions (void)
+{
+  return foreach_block_device(add_partitions);
+}