New API: is-lv: check if a block device is a logical volume (RHBZ#619793)
[libguestfs.git] / daemon / lvm.c
index b81badb..0df27e2 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include <string.h>
 #include <unistd.h>
-#include <ctype.h>
+#include <sys/stat.h>
 
 #include "daemon.h"
+#include "c-ctype.h"
 #include "actions.h"
+#include "optgroups.h"
+
+int
+optgroup_lvm2_available (void)
+{
+  return prog_exists ("lvm");
+}
 
 /* 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 +58,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. */
@@ -93,7 +102,7 @@ do_pvs (void)
   int r;
 
   r = command (&out, &err,
-              "/sbin/lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
+               "lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (out);
@@ -113,7 +122,7 @@ do_vgs (void)
   int r;
 
   r = command (&out, &err,
-              "/sbin/lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
+               "lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (out);
@@ -133,9 +142,9 @@ do_lvs (void)
   int r;
 
   r = command (&out, &err,
-              "/sbin/lvm", "lvs",
-              "-o", "vg_name,lv_name", "--noheadings",
-              "--separator", "/", NULL);
+               "lvm", "lvs",
+               "-o", "vg_name,lv_name", "--noheadings",
+               "--separator", "/", NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (out);
@@ -152,34 +161,32 @@ do_lvs (void)
  * the code.  That code is in stubs.c, and it is generated as usual
  * by generator.ml.
  */
-guestfs_lvm_int_pv_list *
+guestfs_int_lvm_pv_list *
 do_pvs_full (void)
 {
   return parse_command_line_pvs ();
 }
 
-guestfs_lvm_int_vg_list *
+guestfs_int_lvm_vg_list *
 do_vgs_full (void)
 {
   return parse_command_line_vgs ();
 }
 
-guestfs_lvm_int_lv_list *
+guestfs_int_lvm_lv_list *
 do_lvs_full (void)
 {
   return parse_command_line_lvs ();
 }
 
 int
-do_pvcreate (char *device)
+do_pvcreate (const char *device)
 {
   char *err;
   int r;
 
-  IS_DEVICE (device, -1);
-
   r = command (NULL, &err,
-              "/sbin/lvm", "pvcreate", device, NULL);
+               "lvm", "pvcreate", device, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -194,29 +201,25 @@ 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)
-    IS_DEVICE (physvols[i], -1);
-
   argc = count_strings (physvols) + 3;
   argv = malloc (sizeof (char *) * (argc + 1));
   if (argv == NULL) {
     reply_with_perror ("malloc");
     return -1;
   }
-  argv[0] = "/sbin/lvm";
+  argv[0] = "lvm";
   argv[1] = "vgcreate";
   argv[2] = volgroup;
   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 +234,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;
@@ -240,8 +243,8 @@ do_lvcreate (char *logvol, char *volgroup, int mbytes)
   snprintf (size, sizeof size, "%d", mbytes);
 
   r = command (NULL, &err,
-              "/sbin/lvm", "lvcreate",
-              "-L", size, "-n", logvol, volgroup, NULL);
+               "lvm", "lvcreate",
+               "-L", size, "-n", logvol, volgroup, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -256,21 +259,45 @@ 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];
 
-  IS_DEVICE (logvol, -1);
-
   snprintf (size, sizeof size, "%d", mbytes);
 
   r = command (NULL, &err,
-              "/sbin/lvm", "lvresize",
-              "-L", size, logvol, NULL);
+               "lvm", "lvresize",
+               "--force", "-L", size, logvol, NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+int
+do_lvresize_free (const char *logvol, int percent)
+{
+  char *err;
+  int r;
+
+  if (percent < 0 || percent > 100) {
+    reply_with_error ("percentage must be [0..100] (was %d)", percent);
+    return -1;
+  }
+
+  char size[64];
+  snprintf (size, sizeof size, "+%d%%FREE", percent);
+
+  r = command (NULL, &err,
+               "lvm", "lvresize", "-l", size, logvol, NULL);
   if (r == -1) {
-    reply_with_error ("lvresize: %s", err);
+    reply_with_error ("%s", err);
     free (err);
     return -1;
   }
@@ -295,7 +322,7 @@ do_lvm_remove_all (void)
     return -1;
 
   for (i = 0; xs[i] != NULL; ++i) {
-    r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
+    r = command (NULL, &err, "lvm", "lvremove", "-f", xs[i], NULL);
     if (r == -1) {
       reply_with_error ("lvremove: %s: %s", xs[i], err);
       free (err);
@@ -312,7 +339,7 @@ do_lvm_remove_all (void)
     return -1;
 
   for (i = 0; xs[i] != NULL; ++i) {
-    r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
+    r = command (NULL, &err, "lvm", "vgremove", "-f", xs[i], NULL);
     if (r == -1) {
       reply_with_error ("vgremove: %s: %s", xs[i], err);
       free (err);
@@ -329,7 +356,7 @@ do_lvm_remove_all (void)
     return -1;
 
   for (i = 0; xs[i] != NULL; ++i) {
-    r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
+    r = command (NULL, &err, "lvm", "pvremove", "-f", xs[i], NULL);
     if (r == -1) {
       reply_with_error ("pvremove: %s: %s", xs[i], err);
       free (err);
@@ -347,15 +374,13 @@ do_lvm_remove_all (void)
 }
 
 int
-do_lvremove (char *device)
+do_lvremove (const char *device)
 {
   char *err;
   int r;
 
-  IS_DEVICE (device, -1);
-
   r = command (NULL, &err,
-              "/sbin/lvm", "lvremove", "-f", device, NULL);
+               "lvm", "lvremove", "-f", device, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -370,13 +395,13 @@ do_lvremove (char *device)
 }
 
 int
-do_vgremove (char *device)
+do_vgremove (const char *device)
 {
   char *err;
   int r;
 
   r = command (NULL, &err,
-              "/sbin/lvm", "vgremove", "-f", device, NULL);
+               "lvm", "vgremove", "-f", device, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -391,15 +416,13 @@ do_vgremove (char *device)
 }
 
 int
-do_pvremove (char *device)
+do_pvremove (const char *device)
 {
   char *err;
   int r;
 
-  IS_DEVICE (device, -1);
-
   r = command (NULL, &err,
-              "/sbin/lvm", "pvremove", "-ff", device, NULL);
+               "lvm", "pvremove", "-ff", device, NULL);
   if (r == -1) {
     reply_with_error ("%s", err);
     free (err);
@@ -414,17 +437,38 @@ do_pvremove (char *device)
 }
 
 int
-do_pvresize (char *device)
+do_pvresize (const char *device)
 {
   char *err;
   int r;
 
-  IS_DEVICE (device, -1);
+  r = command (NULL, &err,
+               "lvm", "pvresize", device, NULL);
+  if (r == -1) {
+    reply_with_error ("%s: %s", device, err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+int
+do_pvresize_size (const char *device, int64_t size)
+{
+  char *err;
+  int r;
+
+  char buf[32];
+  snprintf (buf, sizeof buf, "%" PRIi64 "b", size);
 
   r = command (NULL, &err,
-              "/sbin/lvm", "pvresize", device, NULL);
+               "lvm", "pvresize",
+               "--setphysicalvolumesize", buf,
+               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 +478,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;
@@ -447,14 +491,14 @@ do_vg_activate (int activate, char **volgroups)
     return -1;
   }
 
-  argv[0] = "/sbin/lvm";
+  argv[0] = "lvm";
   argv[1] = "vgchange";
   argv[2] = "-a";
   argv[3] = activate ? "y" : "n";
   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 +518,194 @@ 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,
+               "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,
+               "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,
+                   "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,
+                   "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);
+}
+
+int
+do_vgscan (void)
+{
+  char *err;
+  int r;
+
+  r = command (NULL, &err,
+               "lvm", "vgscan", NULL);
+  if (r == -1) {
+    reply_with_error ("%s", err);
+    free (err);
+    return -1;
+  }
+
+  free (err);
+  return 0;
+}
+
+/* Test if a device is a logical volume (RHBZ#619793).
+ *
+ * This is harder than it should be.  A LV device like /dev/VG/LV is
+ * really a symlink to a device-mapper device like /dev/dm-0.  However
+ * at the device-mapper (kernel) level, nothing is really known about
+ * LVM (a userspace concept).  Therefore we use a convoluted method to
+ * determine this, by listing out known LVs and checking whether the
+ * rdev (major/minor) of the device we are passed matches any of them.
+ *
+ * Note use of 'stat' instead of 'lstat' so that symlinks are fully
+ * resolved.
+ */
+int
+do_is_lv (const char *device)
+{
+  struct stat stat1, stat2;
+
+  int r = stat (device, &stat1);
+  if (r == -1) {
+    reply_with_perror ("stat: %s", device);
+    return -1;
+  }
+
+  char **lvs = do_lvs ();
+  if (lvs == NULL)
+    return -1;
+
+  size_t i;
+  for (i = 0; lvs[i] != NULL; ++i) {
+    r = stat (lvs[i], &stat2);
+    if (r == -1) {
+      reply_with_perror ("stat: %s", lvs[i]);
+      free_strings (lvs);
+      return -1;
+    }
+    if (stat1.st_rdev == stat2.st_rdev) { /* found it */
+      free_strings (lvs);
+      return 1;
+    }
+  }
+
+  /* not found */
+  free_strings (lvs);
+  return 0;
+}