inspect: Refuse to download software hive if it is huge.
[libguestfs.git] / daemon / ext2.c
index ff6459b..725352e 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include <string.h>
 #include <unistd.h>
 
-#include "../src/guestfs_protocol.h"
+#include "guestfs_protocol.h"
 #include "daemon.h"
 #include "c-ctype.h"
 #include "actions.h"
 
+/* Confirmed this is true up to ext4 from the Linux sources. */
+#define EXT2_LABEL_MAX 16
+
 /* Choose which tools like mke2fs to use.  For RHEL 5 (only) there
  * is a special set of tools which support ext2/3/4.  eg. On RHEL 5,
  * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
@@ -43,11 +47,11 @@ e2prog (char *name)
   p++;
 
   *p = '4';
-  if (access (name, X_OK) == 0)
+  if (prog_exists (name))
     return 0;
 
   *p = '2';
-  if (access (name, X_OK) == 0)
+  if (prog_exists (name))
     return 0;
 
   reply_with_error ("cannot find required program %s", name);
@@ -63,7 +67,7 @@ do_tune2fs_l (const char *device)
   char **ret = NULL;
   int size = 0, alloc = 0;
 
-  char prog[] = "/sbin/tune2fs";
+  char prog[] = "tune2fs";
   if (e2prog (prog) == -1)
     return NULL;
 
@@ -151,10 +155,16 @@ do_set_e2label (const char *device, const char *label)
   int r;
   char *err;
 
-  char prog[] = "/sbin/e2label";
+  char prog[] = "e2label";
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   r = command (NULL, &err, prog, device, label, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
@@ -169,29 +179,7 @@ do_set_e2label (const char *device, const char *label)
 char *
 do_get_e2label (const char *device)
 {
-  int r, len;
-  char *out, *err;
-
-  char prog[] = "/sbin/e2label";
-  if (e2prog (prog) == -1)
-    return NULL;
-
-  r = command (&out, &err, prog, device, NULL);
-  if (r == -1) {
-    reply_with_error ("%s", err);
-    free (out);
-    free (err);
-    return NULL;
-  }
-
-  free (err);
-
-  /* Remove any trailing \n from the label. */
-  len = strlen (out);
-  if (len > 0 && out[len-1] == '\n')
-    out[len-1] = '\0';
-
-  return out;                  /* caller frees */
+  return do_vfs_label (device);
 }
 
 int
@@ -200,7 +188,7 @@ do_set_e2uuid (const char *device, const char *uuid)
   int r;
   char *err;
 
-  char prog[] = "/sbin/tune2fs";
+  char prog[] = "tune2fs";
   if (e2prog (prog) == -1)
     return -1;
 
@@ -218,78 +206,55 @@ do_set_e2uuid (const char *device, const char *uuid)
 char *
 do_get_e2uuid (const char *device)
 {
+  return do_vfs_uuid (device);
+}
+
+int
+do_resize2fs (const char *device)
+{
+  char *err;
   int r;
-  char *out, *err, *p, *q;
 
-  /* It's not so straightforward to get the volume UUID.  We have
-   * to use tune2fs -l and then look for a particular string in
-   * the output.
-   */
-  char prog[] = "/sbin/tune2fs";
+  char prog[] = "resize2fs";
   if (e2prog (prog) == -1)
-    return NULL;
+    return -1;
 
-  r = command (&out, &err, prog, "-l", device, NULL);
+  r = command (NULL, &err, prog, device, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
-    free (out);
     free (err);
-    return NULL;
+    return -1;
   }
 
   free (err);
-
-  /* Look for /\nFilesystem UUID:\s+/ in the output. */
-  p = strstr (out, "\nFilesystem UUID:");
-  if (p == NULL) {
-    reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  p += 17;
-  while (*p && c_isspace (*p))
-    p++;
-  if (!*p) {
-    reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  /* Now 'p' hopefully points to the start of the UUID. */
-  q = p;
-  while (*q && (c_isxdigit (*q) || *q == '-'))
-    q++;
-  if (!*q) {
-    reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
-    free (out);
-    return NULL;
-  }
-
-  *q = '\0';
-
-  p = strdup (p);
-  if (!p) {
-    reply_with_perror ("strdup");
-    free (out);
-    return NULL;
-  }
-
-  free (out);
-  return p;                    /* caller frees */
+  return 0;
 }
 
 int
-do_resize2fs (const char *device)
+do_resize2fs_size (const char *device, int64_t size)
 {
   char *err;
   int r;
 
-  char prog[] = "/sbin/resize2fs";
+  char prog[] = "resize2fs";
   if (e2prog (prog) == -1)
     return -1;
 
-  r = command (NULL, &err, prog, device, NULL);
+  /* resize2fs itself may impose additional limits.  Since we are
+   * going to use the 'K' suffix however we can only work with whole
+   * kilobytes.
+   */
+  if (size & 1023) {
+    reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
+                      size);
+    return -1;
+  }
+  size /= 1024;
+
+  char buf[32];
+  snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
+
+  r = command (NULL, &err, prog, device, buf, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -306,12 +271,19 @@ do_e2fsck_f (const char *device)
   char *err;
   int r;
 
-  char prog[] = "/sbin/e2fsck";
+  char prog[] = "e2fsck";
   if (e2prog (prog) == -1)
     return -1;
 
-  r = command (NULL, &err, prog, "-p", "-f", device, NULL);
-  if (r == -1) {
+  /* 0 = no errors, 1 = errors corrected.
+   *
+   * >= 4 means uncorrected or other errors.
+   *
+   * 2, 3 means errors were corrected and we require a reboot.  This is
+   * a difficult corner case.
+   */
+  r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
+  if (r == -1 || r >= 2) {
     reply_with_error ("%s", err);
     free (err);
     return -1;
@@ -327,7 +299,7 @@ do_mke2journal (int blocksize, const char *device)
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;
 
@@ -353,10 +325,16 @@ do_mke2journal_L (int blocksize, const char *label, const char *device)
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   char blocksize_s[32];
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
 
@@ -380,7 +358,7 @@ do_mke2journal_U (int blocksize, const char *uuid, const char *device)
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;
 
@@ -408,7 +386,7 @@ do_mke2fs_J (const char *fstype, int blocksize, const char *device,
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;
 
@@ -439,10 +417,16 @@ do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;
 
+  if (strlen (label) > EXT2_LABEL_MAX) {
+    reply_with_error ("%s: ext2 labels are limited to %d bytes",
+                      label, EXT2_LABEL_MAX);
+    return -1;
+  }
+
   char blocksize_s[32];
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
 
@@ -470,7 +454,7 @@ do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
   char *err;
   int r;
 
-  char prog[] = "/sbin/mke2fs";
+  char prog[] = "mke2fs";
   if (e2prog (prog) == -1)
     return -1;