New APIs: Query the relationship between LVM objects.
[libguestfs.git] / daemon / lvm.c
index 8f575c4..82cdf3f 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <ctype.h>
 
 #include "daemon.h"
+#include "c-ctype.h"
 #include "actions.h"
+#include "optgroups.h"
+
+int
+optgroup_lvm2_available (void)
+{
+  int r = access ("/sbin/lvm", X_OK);
+  return r == 0;
+}
 
 /* LVM actions.  Keep an eye on liblvm, although at the time
  * of writing it hasn't progressed very far.
  */
 
 static char **
-convert_lvm_output (char *out, char *prefix)
+convert_lvm_output (char *out, const char *prefix)
 {
   char *p, *pend;
   char **r = NULL;
@@ -49,12 +57,12 @@ convert_lvm_output (char *out, char *prefix)
       pend++;
     }
 
-    while (*p && isspace (*p)) /* Skip any leading whitespace. */
+    while (*p && c_isspace (*p))       /* Skip any leading whitespace. */
       p++;
 
     /* Sigh, skip trailing whitespace too.  "pvs", I'm looking at you. */
     len = strlen (p)-1;
-    while (*p && isspace (p[len]))
+    while (*p && c_isspace (p[len]))
       p[len--] = '\0';
 
     if (!*p) {                 /* Empty line?  Skip it. */
@@ -171,13 +179,11 @@ do_lvs_full (void)
 }
 
 int
-do_pvcreate (char *device)
+do_pvcreate (const char *device)
 {
   char *err;
   int r;
 
-  RESOLVE_DEVICE (device, return -1);
-
   r = command (NULL, &err,
                "/sbin/lvm", "pvcreate", device, NULL);
   if (r == -1) {
@@ -194,16 +200,12 @@ do_pvcreate (char *device)
 }
 
 int
-do_vgcreate (char *volgroup, char **physvols)
+do_vgcreate (const char *volgroup, char *const *physvols)
 {
   char *err;
   int r, argc, i;
   const char **argv;
 
-  /* Check they are devices and also do device name translation. */
-  for (i = 0; physvols[i] != NULL; ++i)
-    RESOLVE_DEVICE (physvols[i], return -1);
-
   argc = count_strings (physvols) + 3;
   argv = malloc (sizeof (char *) * (argc + 1));
   if (argv == NULL) {
@@ -216,7 +218,7 @@ do_vgcreate (char *volgroup, char **physvols)
   for (i = 3; i <= argc; ++i)
     argv[i] = physvols[i-3];
 
-  r = commandv (NULL, &err, argv);
+  r = commandv (NULL, &err, (const char * const*) argv);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -231,7 +233,7 @@ do_vgcreate (char *volgroup, char **physvols)
 }
 
 int
-do_lvcreate (char *logvol, char *volgroup, int mbytes)
+do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
 {
   char *err;
   int r;
@@ -256,21 +258,19 @@ do_lvcreate (char *logvol, char *volgroup, int mbytes)
 }
 
 int
-do_lvresize (char *logvol, int mbytes)
+do_lvresize (const char *logvol, int mbytes)
 {
   char *err;
   int r;
   char size[64];
 
-  RESOLVE_DEVICE (logvol, return -1);
-
   snprintf (size, sizeof size, "%d", mbytes);
 
   r = command (NULL, &err,
                "/sbin/lvm", "lvresize",
                "-L", size, logvol, NULL);
   if (r == -1) {
-    reply_with_error ("lvresize: %s", err);
+    reply_with_error ("%s", err);
     free (err);
     return -1;
   }
@@ -347,13 +347,11 @@ do_lvm_remove_all (void)
 }
 
 int
-do_lvremove (char *device)
+do_lvremove (const char *device)
 {
   char *err;
   int r;
 
-  RESOLVE_DEVICE (device, return -1);
-
   r = command (NULL, &err,
                "/sbin/lvm", "lvremove", "-f", device, NULL);
   if (r == -1) {
@@ -370,7 +368,7 @@ do_lvremove (char *device)
 }
 
 int
-do_vgremove (char *device)
+do_vgremove (const char *device)
 {
   char *err;
   int r;
@@ -391,13 +389,11 @@ do_vgremove (char *device)
 }
 
 int
-do_pvremove (char *device)
+do_pvremove (const char *device)
 {
   char *err;
   int r;
 
-  RESOLVE_DEVICE (device, return -1);
-
   r = command (NULL, &err,
                "/sbin/lvm", "pvremove", "-ff", device, NULL);
   if (r == -1) {
@@ -414,17 +410,15 @@ do_pvremove (char *device)
 }
 
 int
-do_pvresize (char *device)
+do_pvresize (const char *device)
 {
   char *err;
   int r;
 
-  RESOLVE_DEVICE (device, return -1);
-
   r = command (NULL, &err,
                "/sbin/lvm", "pvresize", device, NULL);
   if (r == -1) {
-    reply_with_error ("pvresize: %s: %s", device, err);
+    reply_with_error ("%s: %s", device, err);
     free (err);
     return -1;
   }
@@ -434,7 +428,7 @@ do_pvresize (char *device)
 }
 
 int
-do_vg_activate (int activate, char **volgroups)
+do_vg_activate (int activate, char *const *volgroups)
 {
   char *err;
   int r, i, argc;
@@ -454,7 +448,7 @@ do_vg_activate (int activate, char **volgroups)
   for (i = 4; i <= argc; ++i)
     argv[i] = volgroups[i-4];
 
-  r = commandv (NULL, &err, argv);
+  r = commandv (NULL, &err, (const char * const*) argv);
   if (r == -1) {
     reply_with_error ("vgchange: %s", err);
     free (err);
@@ -474,3 +468,130 @@ do_vg_activate_all (int activate)
   char *empty[] = { NULL };
   return do_vg_activate (activate, empty);
 }
+
+int
+do_lvrename (const char *logvol, const char *newlogvol)
+{
+  char *err;
+  int r;
+
+  r = command (NULL, &err,
+               "/sbin/lvm", "lvrename",
+               logvol, newlogvol, NULL);
+  if (r == -1) {
+    reply_with_error ("%s -> %s: %s", logvol, newlogvol, err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+
+  udev_settle ();
+
+  return 0;
+}
+
+int
+do_vgrename (const char *volgroup, const char *newvolgroup)
+{
+  char *err;
+  int r;
+
+  r = command (NULL, &err,
+               "/sbin/lvm", "vgrename",
+               volgroup, newvolgroup, NULL);
+  if (r == -1) {
+    reply_with_error ("%s -> %s: %s", volgroup, newvolgroup, err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+
+  udev_settle ();
+
+  return 0;
+}
+
+static char *
+get_lvm_field (const char *cmd, const char *field, const char *device)
+{
+  char *out;
+  char *err;
+  int r = command (&out, &err,
+                   "/sbin/lvm", cmd,
+                   "--unbuffered", "--noheadings", "-o", field,
+                   device, NULL);
+  if (r == -1) {
+    reply_with_error ("%s: %s", device, err);
+    free (out);
+    free (err);
+    return NULL;
+  }
+
+  free (err);
+
+  trim (out);
+  return out;                   /* Caller frees. */
+}
+
+char *
+do_pvuuid (const char *device)
+{
+  return get_lvm_field ("pvs", "pv_uuid", device);
+}
+
+char *
+do_vguuid (const char *vgname)
+{
+  return get_lvm_field ("vgs", "vg_uuid", vgname);
+}
+
+char *
+do_lvuuid (const char *device)
+{
+  return get_lvm_field ("lvs", "lv_uuid", device);
+}
+
+static char **
+get_lvm_fields (const char *cmd, const char *field, const char *device)
+{
+  char *out;
+  char *err;
+  int r = command (&out, &err,
+                   "/sbin/lvm", cmd,
+                   "--unbuffered", "--noheadings", "-o", field,
+                   device, NULL);
+  if (r == -1) {
+    reply_with_error ("%s: %s", device, err);
+    free (out);
+    free (err);
+    return NULL;
+  }
+
+  free (err);
+
+  char **ret = split_lines (out);
+  free (out);
+
+  if (ret == NULL)
+    return NULL;
+
+  size_t i;
+  for (i = 0; ret[i] != NULL; ++i)
+    trim (ret[i]);
+
+  return ret;
+}
+
+char **
+do_vgpvuuids (const char *vgname)
+{
+  return get_lvm_fields ("vgs", "pv_uuid", vgname);
+}
+
+char **
+do_vglvuuids (const char *vgname)
+{
+  return get_lvm_fields ("vgs", "lv_uuid", vgname);
+}