tune2fs-l: Add a test.
[libguestfs.git] / daemon / ext2.c
index 3a075e5..f7889da 100644 (file)
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #include <config.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.
@@ -36,7 +39,7 @@
  * We specify e4fsprogs in the package list to ensure it is loaded
  * if it exists.
  */
-static int
+int
 e2prog (char *name)
 {
   char *p = strstr (name, "e2");
@@ -156,6 +159,12 @@ do_set_e2label (const char *device, const char *label)
   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);
@@ -170,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[] = "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
@@ -219,65 +206,7 @@ do_set_e2uuid (const char *device, const char *uuid)
 char *
 do_get_e2uuid (const char *device)
 {
-  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[] = "tune2fs";
-  if (e2prog (prog) == -1)
-    return NULL;
-
-  r = command (&out, &err, prog, "-l", device, NULL);
-  if (r == -1) {
-    reply_with_error ("%s", err);
-    free (out);
-    free (err);
-    return NULL;
-  }
-
-  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 do_vfs_uuid (device);
 }
 
 int
@@ -337,6 +266,27 @@ do_resize2fs_size (const char *device, int64_t size)
 }
 
 int
+do_resize2fs_M (const char *device)
+{
+  char *err;
+  int r;
+
+  char prog[] = "resize2fs";
+  if (e2prog (prog) == -1)
+    return -1;
+
+  r = command (NULL, &err, prog, "-M" , device, NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+int
 do_e2fsck_f (const char *device)
 {
   char *err;
@@ -400,6 +350,12 @@ do_mke2journal_L (int blocksize, const char *label, const char *device)
   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);
 
@@ -486,6 +442,12 @@ do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
   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);