psss / rpms / libguestfs

Forked from rpms/libguestfs 5 years ago
Clone
0a21fb0
From c4e8aa245a243b020d6b7bd832674be871a43610 Mon Sep 17 00:00:00 2001
0a21fb0
From: Richard Jones <rjones@redhat.com>
0a21fb0
Date: Wed, 24 Mar 2010 22:02:34 +0000
0a21fb0
Subject: [PATCH] Use ext4 dev tools on RHEL 5 (RHBZ#576688).
0a21fb0
0a21fb0
---
0a21fb0
 daemon/ext2.c |  136 +++++++++++++++++++++++++++++++++++---------------------
0a21fb0
 1 files changed, 85 insertions(+), 51 deletions(-)
0a21fb0
0a21fb0
diff --git a/daemon/ext2.c b/daemon/ext2.c
0a21fb0
index 85ddce5..ff6459b 100644
0a21fb0
--- a/daemon/ext2.c
0a21fb0
+++ b/daemon/ext2.c
0a21fb0
@@ -28,6 +28,32 @@
0a21fb0
 #include "c-ctype.h"
0a21fb0
 #include "actions.h"
0a21fb0
 
0a21fb0
+/* Choose which tools like mke2fs to use.  For RHEL 5 (only) there
0a21fb0
+ * is a special set of tools which support ext2/3/4.  eg. On RHEL 5,
0a21fb0
+ * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
0a21fb0
+ *
0a21fb0
+ * We specify e4fsprogs in the package list to ensure it is loaded
0a21fb0
+ * if it exists.
0a21fb0
+ */
0a21fb0
+static int
0a21fb0
+e2prog (char *name)
0a21fb0
+{
0a21fb0
+  char *p = strstr (name, "e2");
0a21fb0
+  if (!p) return 0;
0a21fb0
+  p++;
0a21fb0
+
0a21fb0
+  *p = '4';
0a21fb0
+  if (access (name, X_OK) == 0)
0a21fb0
+    return 0;
0a21fb0
+
0a21fb0
+  *p = '2';
0a21fb0
+  if (access (name, X_OK) == 0)
0a21fb0
+    return 0;
0a21fb0
+
0a21fb0
+  reply_with_error ("cannot find required program %s", name);
0a21fb0
+  return -1;
0a21fb0
+}
0a21fb0
+
0a21fb0
 char **
0a21fb0
 do_tune2fs_l (const char *device)
0a21fb0
 {
0a21fb0
@@ -37,7 +63,11 @@ do_tune2fs_l (const char *device)
0a21fb0
   char **ret = NULL;
0a21fb0
   int size = 0, alloc = 0;
0a21fb0
 
0a21fb0
-  r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
0a21fb0
+  char prog[] = "/sbin/tune2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return NULL;
0a21fb0
+
0a21fb0
+  r = command (&out, &err, prog, "-l", device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (err);
0a21fb0
@@ -49,7 +79,7 @@ do_tune2fs_l (const char *device)
0a21fb0
   p = out;
0a21fb0
 
0a21fb0
   /* Discard the first line if it contains "tune2fs ...". */
0a21fb0
-  if (STREQLEN (p, "tune2fs ", 8)) {
0a21fb0
+  if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
0a21fb0
     p = strchr (p, '\n');
0a21fb0
     if (p) p++;
0a21fb0
     else {
0a21fb0
@@ -121,7 +151,11 @@ do_set_e2label (const char *device, const char *label)
0a21fb0
   int r;
0a21fb0
   char *err;
0a21fb0
 
0a21fb0
-  r = command (NULL, &err, "/sbin/e2label", device, label, NULL);
0a21fb0
+  char prog[] = "/sbin/e2label";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
+  r = command (NULL, &err, prog, device, label, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (err);
0a21fb0
@@ -138,7 +172,11 @@ do_get_e2label (const char *device)
0a21fb0
   int r, len;
0a21fb0
   char *out, *err;
0a21fb0
 
0a21fb0
-  r = command (&out, &err, "/sbin/e2label", device, NULL);
0a21fb0
+  char prog[] = "/sbin/e2label";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return NULL;
0a21fb0
+
0a21fb0
+  r = command (&out, &err, prog, device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (out);
0a21fb0
@@ -162,7 +200,11 @@ do_set_e2uuid (const char *device, const char *uuid)
0a21fb0
   int r;
0a21fb0
   char *err;
0a21fb0
 
0a21fb0
-  r = command (NULL, &err, "/sbin/tune2fs", "-U", uuid, device, NULL);
0a21fb0
+  char prog[] = "/sbin/tune2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
+  r = command (NULL, &err, prog, "-U", uuid, device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (err);
0a21fb0
@@ -183,8 +225,11 @@ do_get_e2uuid (const char *device)
0a21fb0
    * to use tune2fs -l and then look for a particular string in
0a21fb0
    * the output.
0a21fb0
    */
0a21fb0
+  char prog[] = "/sbin/tune2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return NULL;
0a21fb0
 
0a21fb0
-  r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
0a21fb0
+  r = command (&out, &err, prog, "-l", device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (out);
0a21fb0
@@ -240,7 +285,11 @@ do_resize2fs (const char *device)
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
-  r = command (NULL, &err, "/sbin/resize2fs", device, NULL);
0a21fb0
+  char prog[] = "/sbin/resize2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
+  r = command (NULL, &err, prog, device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (err);
0a21fb0
@@ -257,7 +306,11 @@ do_e2fsck_f (const char *device)
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
-  r = command (NULL, &err, "/sbin/e2fsck", "-p", "-f", device, NULL);
0a21fb0
+  char prog[] = "/sbin/e2fsck";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
+  r = command (NULL, &err, prog, "-p", "-f", device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
     free (err);
0a21fb0
@@ -274,11 +327,15 @@ do_mke2journal (int blocksize, const char *device)
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
 
0a21fb0
   r = command (NULL, &err,
0a21fb0
-               "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
+               prog, "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
                device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
     reply_with_error ("%s", err);
0a21fb0
@@ -296,11 +353,15 @@ do_mke2journal_L (int blocksize, const char *label, const char *device)
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
 
0a21fb0
   r = command (NULL, &err,
0a21fb0
-               "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
+               prog, "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
                "-L", label,
0a21fb0
                device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
@@ -319,11 +380,15 @@ do_mke2journal_U (int blocksize, const char *uuid, const char *device)
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
+
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
 
0a21fb0
   r = command (NULL, &err,
0a21fb0
-               "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
+               prog, "-O", "journal_dev", "-b", blocksize_s,
0a21fb0
                "-U", uuid,
0a21fb0
                device, NULL);
0a21fb0
   if (r == -1) {
0a21fb0
@@ -336,40 +401,6 @@ do_mke2journal_U (int blocksize, const char *uuid, const char *device)
0a21fb0
   return 0;
0a21fb0
 }
0a21fb0
 
0a21fb0
-/* Run mke2fs to create a filesystem of type fstype, where fstype
0a21fb0
- * is the string "ext2", "ext3" or "ext4".
0a21fb0
- *
0a21fb0
- * This is more complex than it seems.
0a21fb0
- *
0a21fb0
- * On RHEL 5, the -t option was deprecated.  Moreover RHEL <= 5.4
0a21fb0
- * systems have a bug where the -t option doesn't work (it doesn't
0a21fb0
- * correctly ignore the following argument).
0a21fb0
- *
0a21fb0
- * On RHEL 5, to create an ext4dev filesystem you have to use
0a21fb0
- * the special command /sbin/mke4fs.  This can also create ext2/3
0a21fb0
- * using the '-t fstype' option.
0a21fb0
- *
0a21fb0
- * On Fedora 11+, mke4fs was renamed mke2fs, and it can use the
0a21fb0
- * '-t fstype' option to specify the filesystem type.
0a21fb0
- *
0a21fb0
- * So it seems best to run /sbin/mke4fs if it exists, or /sbin/mke2fs
0a21fb0
- * otherwise.  We specify e4fsprogs in the package list to ensure it
0a21fb0
- * is loaded if it exists.
0a21fb0
- */
0a21fb0
-static const char *
0a21fb0
-get_mke2fs (void)
0a21fb0
-{
0a21fb0
-  static const char *const progs[] = { "/sbin/mke4fs", "/sbin/mke2fs", NULL };
0a21fb0
-  int i;
0a21fb0
-
0a21fb0
-  for (i = 0; progs[i]; ++i)
0a21fb0
-    if (access (progs[i], F_OK) == 0)
0a21fb0
-      return progs[i];
0a21fb0
-
0a21fb0
-  reply_with_error ("no mke2fs binary found in appliance");
0a21fb0
-  return NULL;
0a21fb0
-}
0a21fb0
-
0a21fb0
 int
0a21fb0
 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
0a21fb0
              const char *journal)
0a21fb0
@@ -377,8 +408,9 @@ do_mke2fs_J (const char *fstype, int blocksize, const char *device,
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
-  const char *prog = get_mke2fs ();
0a21fb0
-  if (!prog) return -1;
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
 
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
@@ -407,8 +439,9 @@ do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
-  const char *prog = get_mke2fs ();
0a21fb0
-  if (!prog) return -1;
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
 
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
@@ -437,8 +470,9 @@ do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
0a21fb0
   char *err;
0a21fb0
   int r;
0a21fb0
 
0a21fb0
-  const char *prog = get_mke2fs ();
0a21fb0
-  if (!prog) return -1;
0a21fb0
+  char prog[] = "/sbin/mke2fs";
0a21fb0
+  if (e2prog (prog) == -1)
0a21fb0
+    return -1;
0a21fb0
 
0a21fb0
   char blocksize_s[32];
0a21fb0
   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
0a21fb0
-- 
0a21fb0
1.6.6.1
0a21fb0