X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=daemon%2Flvm.c;h=70c3c9047a97c9400f162ef27987824d22f42d39;hp=82cdf3fb482080fcac6d94f05f6d486654b53bdc;hb=9be89728f2a0ff17d54a0dba981015fa6405eb50;hpb=50d2af4f8541a81d047753d7d300804ff649230c diff --git a/daemon/lvm.c b/daemon/lvm.c index 82cdf3f..70c3c90 100644 --- a/daemon/lvm.c +++ b/daemon/lvm.c @@ -20,6 +20,7 @@ #include #include +#include #include #include @@ -31,8 +32,7 @@ int optgroup_lvm2_available (void) { - int r = access ("/sbin/lvm", X_OK); - return r == 0; + return prog_exists ("lvm"); } /* LVM actions. Keep an eye on liblvm, although at the time @@ -101,7 +101,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); @@ -121,7 +121,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); @@ -141,7 +141,7 @@ do_lvs (void) int r; r = command (&out, &err, - "/sbin/lvm", "lvs", + "lvm", "lvs", "-o", "vg_name,lv_name", "--noheadings", "--separator", "/", NULL); if (r == -1) { @@ -185,7 +185,7 @@ do_pvcreate (const char *device) int r; r = command (NULL, &err, - "/sbin/lvm", "pvcreate", device, NULL); + "lvm", "pvcreate", device, NULL); if (r == -1) { reply_with_error ("%s", err); free (err); @@ -212,7 +212,7 @@ do_vgcreate (const char *volgroup, char *const *physvols) 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) @@ -242,7 +242,7 @@ do_lvcreate (const char *logvol, const char *volgroup, int mbytes) snprintf (size, sizeof size, "%d", mbytes); r = command (NULL, &err, - "/sbin/lvm", "lvcreate", + "lvm", "lvcreate", "-L", size, "-n", logvol, volgroup, NULL); if (r == -1) { reply_with_error ("%s", err); @@ -267,8 +267,34 @@ do_lvresize (const char *logvol, int mbytes) 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 ("%s", err); free (err); @@ -295,7 +321,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 +338,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 +355,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); @@ -353,7 +379,7 @@ do_lvremove (const char *device) int r; 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); @@ -374,7 +400,7 @@ do_vgremove (const char *device) 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); @@ -395,7 +421,7 @@ do_pvremove (const char *device) int r; 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); @@ -416,7 +442,30 @@ do_pvresize (const char *device) int r; r = command (NULL, &err, - "/sbin/lvm", "pvresize", device, NULL); + "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, + "lvm", "pvresize", + "--setphysicalvolumesize", buf, + device, NULL); if (r == -1) { reply_with_error ("%s: %s", device, err); free (err); @@ -441,7 +490,7 @@ do_vg_activate (int activate, char *const *volgroups) return -1; } - argv[0] = "/sbin/lvm"; + argv[0] = "lvm"; argv[1] = "vgchange"; argv[2] = "-a"; argv[3] = activate ? "y" : "n"; @@ -476,7 +525,7 @@ do_lvrename (const char *logvol, const char *newlogvol) int r; r = command (NULL, &err, - "/sbin/lvm", "lvrename", + "lvm", "lvrename", logvol, newlogvol, NULL); if (r == -1) { reply_with_error ("%s -> %s: %s", logvol, newlogvol, err); @@ -498,7 +547,7 @@ do_vgrename (const char *volgroup, const char *newvolgroup) int r; r = command (NULL, &err, - "/sbin/lvm", "vgrename", + "lvm", "vgrename", volgroup, newvolgroup, NULL); if (r == -1) { reply_with_error ("%s -> %s: %s", volgroup, newvolgroup, err); @@ -519,7 +568,7 @@ get_lvm_field (const char *cmd, const char *field, const char *device) char *out; char *err; int r = command (&out, &err, - "/sbin/lvm", cmd, + "lvm", cmd, "--unbuffered", "--noheadings", "-o", field, device, NULL); if (r == -1) { @@ -559,7 +608,7 @@ get_lvm_fields (const char *cmd, const char *field, const char *device) char *out; char *err; int r = command (&out, &err, - "/sbin/lvm", cmd, + "lvm", cmd, "--unbuffered", "--noheadings", "-o", field, device, NULL); if (r == -1) { @@ -595,3 +644,21 @@ 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; +}