Functions for getting and setting the ext2 UUID and label.
authorRichard Jones <rjones@redhat.com>
Mon, 27 Apr 2009 12:41:30 +0000 (13:41 +0100)
committerRichard Jones <rjones@redhat.com>
Mon, 27 Apr 2009 12:41:30 +0000 (13:41 +0100)
daemon/tune2fs.c
src/generator.ml

index 742a7d9..99c12f0 100644 (file)
@@ -115,3 +115,122 @@ do_tune2fs_l (const char *device)
 
   return ret;
 }
 
   return ret;
 }
+
+int
+do_set_e2label (const char *device, const char *label)
+{
+  int r;
+  char *err;
+
+  r = command (NULL, &err, "/sbin/e2label", device, label, NULL);
+  if (r == -1) {
+    reply_with_error ("e2label: %s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+char *
+do_get_e2label (const char *device)
+{
+  int r, len;
+  char *out, *err;
+
+  r = command (&out, &err, "/sbin/e2label", device, NULL);
+  if (r == -1) {
+    reply_with_error ("e2label: %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 */
+}
+
+int
+do_set_e2uuid (const char *device, const char *uuid)
+{
+  int r;
+  char *err;
+
+  r = command (NULL, &err, "/sbin/tune2fs", "-U", uuid, device, NULL);
+  if (r == -1) {
+    reply_with_error ("tune2fs -U: %s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+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.
+   */
+
+  r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
+  if (r == -1) {
+    reply_with_error ("tune2fs -l: %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 && 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 && (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 */
+}
index 5e2b4d4..a709376 100755 (executable)
@@ -1565,6 +1565,55 @@ The implementation uses the C<pvremove> command which refuses to
 wipe physical volumes that contain any volume groups, so you have
 to remove those first.");
 
 wipe physical volumes that contain any volume groups, so you have
 to remove those first.");
 
+  ("set_e2label", (RErr, [String "device"; String "label"]), 80, [],
+   [InitBasicFS, TestOutput (
+      [["set_e2label"; "/dev/sda1"; "testlabel"];
+       ["get_e2label"; "/dev/sda1"]], "testlabel")],
+   "set the ext2/3/4 filesystem label",
+   "\
+This sets the ext2/3/4 filesystem label of the filesystem on
+C<device> to C<label>.  Filesystem labels are limited to
+16 characters.
+
+You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2label>
+to return the existing label on a filesystem.");
+
+  ("get_e2label", (RString "label", [String "device"]), 81, [],
+   [],
+   "get the ext2/3/4 filesystem label",
+   "\
+This returns the ext2/3/4 filesystem label of the filesystem on
+C<device>.");
+
+  ("set_e2uuid", (RErr, [String "device"; String "uuid"]), 82, [],
+   [InitBasicFS, TestOutput (
+      [["set_e2uuid"; "/dev/sda1"; "a3a61220-882b-4f61-89f4-cf24dcc7297d"];
+       ["get_e2uuid"; "/dev/sda1"]], "a3a61220-882b-4f61-89f4-cf24dcc7297d");
+    InitBasicFS, TestOutput (
+      [["set_e2uuid"; "/dev/sda1"; "clear"];
+       ["get_e2uuid"; "/dev/sda1"]], "");
+    (* We can't predict what UUIDs will be, so just check the commands run. *)
+    InitBasicFS, TestRun (
+      [["set_e2uuid"; "/dev/sda1"; "random"]]);
+    InitBasicFS, TestRun (
+      [["set_e2uuid"; "/dev/sda1"; "time"]])],
+   "set the ext2/3/4 filesystem UUID",
+   "\
+This sets the ext2/3/4 filesystem UUID of the filesystem on
+C<device> to C<uuid>.  The format of the UUID and alternatives
+such as C<clear>, C<random> and C<time> are described in the
+L<tune2fs(8)> manpage.
+
+You can use either C<guestfs_tune2fs_l> or C<guestfs_get_e2uuid>
+to return the existing UUID of a filesystem.");
+
+  ("get_e2uuid", (RString "uuid", [String "device"]), 83, [],
+   [],
+   "get the ext2/3/4 filesystem UUID",
+   "\
+This returns the ext2/3/4 filesystem UUID of the filesystem on
+C<device>.");
+
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions
 ]
 
 let all_functions = non_daemon_functions @ daemon_functions