Change to using ext2-based, cached supermin appliance.
[libguestfs.git] / daemon / guestfsd.c
index ef28d9b..4e29338 100644 (file)
@@ -41,6 +41,7 @@
 #include <sys/wait.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
+#include <errno.h>
 
 #ifdef HAVE_PRINTF_H
 # include <printf.h>
@@ -73,6 +74,12 @@ static char *read_cmdline (void);
 # define MAX(a,b) ((a)>(b)?(a):(b))
 #endif
 
+/* If root device is an ext2 filesystem, this is the major and minor.
+ * This is so we can ignore this device from the point of view of the
+ * user, eg. in guestfs_list_devices and many other places.
+ */
+static dev_t root_device = 0;
+
 int verbose = 0;
 
 static int print_shell_quote (FILE *stream, const struct printf_info *info, const void *const *args);
@@ -162,6 +169,10 @@ main (int argc, char *argv[])
 #endif
 #endif
 
+  struct stat statbuf;
+  if (stat ("/", &statbuf) == 0)
+    root_device = statbuf.st_dev;
+
   for (;;) {
     c = getopt_long (argc, argv, options, long_options, NULL);
     if (c == -1) break;
@@ -448,6 +459,22 @@ read_cmdline (void)
   return r;
 }
 
+/* Return true iff device is the root device (and therefore should be
+ * ignored from the point of view of user calls).
+ */
+int
+is_root_device (const char *device)
+{
+  struct stat statbuf;
+  if (stat (device, &statbuf) == -1) {
+    perror (device);
+    return 0;
+  }
+  if (statbuf.st_rdev == root_device)
+    return 1;
+  return 0;
+}
+
 /* Turn "/path" into "/sysroot/path".
  *
  * Caller must check for NULL and call reply_with_perror ("malloc")
@@ -543,16 +570,23 @@ add_string (char ***argv, int *size, int *alloc, const char *str)
   return 0;
 }
 
-int
+size_t
 count_strings (char *const *argv)
 {
-  int argc;
+  size_t argc;
 
   for (argc = 0; argv[argc] != NULL; ++argc)
     ;
   return argc;
 }
 
+/* http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2 */
+int
+is_power_of_2 (unsigned long v)
+{
+  return v && ((v & (v - 1)) == 0);
+}
+
 static int
 compare (const void *vp1, const void *vp2)
 {
@@ -758,20 +792,20 @@ commandrvf (char **stdoutput, char **stderror, int flags,
    */
 
   if (pipe (so_fd) == -1 || pipe (se_fd) == -1) {
-    perror ("pipe");
+    error (0, errno, "pipe");
     abort ();
   }
 
   if (flag_copy_stdin) {
     if (pipe (stdin_fd) == -1) {
-      perror ("pipe");
+      error (0, errno, "pipe");
       abort ();
     }
   }
 
   pid = fork ();
   if (pid == -1) {
-    perror ("fork");
+    error (0, errno, "fork");
     abort ();
   }
 
@@ -805,7 +839,7 @@ commandrvf (char **stdoutput, char **stderror, int flags,
 
     stdin_pid = fork ();
     if (stdin_pid == -1) {
-      perror ("fork");
+      error (0, errno, "fork");
       abort ();
     }