1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31 #include "optgroups.h"
34 optgroup_lvm2_available (void)
36 return prog_exists ("lvm");
39 /* LVM actions. Keep an eye on liblvm, although at the time
40 * of writing it hasn't progressed very far.
44 convert_lvm_output (char *out, const char *prefix)
48 int size = 0, alloc = 0;
55 pend = strchr (p, '\n'); /* Get the next line of output. */
61 while (*p && c_isspace (*p)) /* Skip any leading whitespace. */
64 /* Sigh, skip trailing whitespace too. "pvs", I'm looking at you. */
66 while (*p && c_isspace (p[len]))
69 if (!*p) { /* Empty line? Skip it. */
76 snprintf (buf, sizeof buf, "%s%s", prefix, p);
81 if (add_string (&r, &size, &alloc, str) == -1) {
91 if (add_string (&r, &size, &alloc, NULL) == -1)
94 sort_strings (r, size-1);
104 r = command (&out, &err,
105 "lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
107 reply_with_error ("%s", err);
115 return convert_lvm_output (out, NULL);
124 r = command (&out, &err,
125 "lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
127 reply_with_error ("%s", err);
135 return convert_lvm_output (out, NULL);
144 r = command (&out, &err,
146 "-o", "vg_name,lv_name", "--noheadings",
147 "--separator", "/", NULL);
149 reply_with_error ("%s", err);
157 return convert_lvm_output (out, "/dev/");
160 /* These were so complex to implement that I ended up auto-generating
161 * the code. That code is in stubs.c, and it is generated as usual
164 guestfs_int_lvm_pv_list *
167 return parse_command_line_pvs ();
170 guestfs_int_lvm_vg_list *
173 return parse_command_line_vgs ();
176 guestfs_int_lvm_lv_list *
179 return parse_command_line_lvs ();
183 do_pvcreate (const char *device)
188 r = command (NULL, &err,
189 "lvm", "pvcreate", device, NULL);
191 reply_with_error ("%s", err);
204 do_vgcreate (const char *volgroup, char *const *physvols)
210 argc = count_strings (physvols) + 3;
211 argv = malloc (sizeof (char *) * (argc + 1));
213 reply_with_perror ("malloc");
217 argv[1] = "vgcreate";
219 for (i = 3; i <= argc; ++i)
220 argv[i] = physvols[i-3];
222 r = commandv (NULL, &err, (const char * const*) argv);
224 reply_with_error ("%s", err);
237 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
243 snprintf (size, sizeof size, "%d", mbytes);
245 r = command (NULL, &err,
247 "-L", size, "-n", logvol, volgroup, NULL);
249 reply_with_error ("%s", err);
262 do_lvresize (const char *logvol, int mbytes)
268 snprintf (size, sizeof size, "%d", mbytes);
270 r = command (NULL, &err,
272 "--force", "-L", size, logvol, NULL);
274 reply_with_error ("%s", err);
284 do_lvresize_free (const char *logvol, int percent)
289 if (percent < 0 || percent > 100) {
290 reply_with_error ("percentage must be [0..100] (was %d)", percent);
295 snprintf (size, sizeof size, "+%d%%FREE", percent);
297 r = command (NULL, &err,
298 "lvm", "lvresize", "-l", size, logvol, NULL);
300 reply_with_error ("%s", err);
309 /* Super-dangerous command used for testing. It removes all
310 * LVs, VGs and PVs permanently.
313 do_lvm_remove_all (void)
324 for (i = 0; xs[i] != NULL; ++i) {
325 /* Deactivate the LV first. On Ubuntu, lvremove '-f' option
326 * does not remove active LVs reliably.
328 (void) command (NULL, NULL, "lvm", "lvchange", "-an", xs[i], NULL);
331 r = command (NULL, &err, "lvm", "lvremove", "-f", xs[i], NULL);
333 reply_with_error ("lvremove: %s: %s", xs[i], err);
347 for (i = 0; xs[i] != NULL; ++i) {
348 /* Deactivate the VG first, see note above. */
349 (void) command (NULL, NULL, "lvm", "vgchange", "-an", xs[i], NULL);
352 r = command (NULL, &err, "lvm", "vgremove", "-f", xs[i], NULL);
354 reply_with_error ("vgremove: %s: %s", xs[i], err);
368 for (i = 0; xs[i] != NULL; ++i) {
369 r = command (NULL, &err, "lvm", "pvremove", "-f", xs[i], NULL);
371 reply_with_error ("pvremove: %s: %s", xs[i], err);
382 /* There, that was easy, sorry about your data. */
387 do_lvremove (const char *device)
392 r = command (NULL, &err,
393 "lvm", "lvremove", "-f", device, NULL);
395 reply_with_error ("%s", err);
408 do_vgremove (const char *device)
413 r = command (NULL, &err,
414 "lvm", "vgremove", "-f", device, NULL);
416 reply_with_error ("%s", err);
429 do_pvremove (const char *device)
434 r = command (NULL, &err,
435 "lvm", "pvremove", "-ff", device, NULL);
437 reply_with_error ("%s", err);
450 do_pvresize (const char *device)
455 r = command (NULL, &err,
456 "lvm", "pvresize", device, NULL);
458 reply_with_error ("%s: %s", device, err);
468 do_pvresize_size (const char *device, int64_t size)
474 snprintf (buf, sizeof buf, "%" PRIi64 "b", size);
476 r = command (NULL, &err,
478 "--setphysicalvolumesize", buf,
481 reply_with_error ("%s: %s", device, err);
491 do_vg_activate (int activate, char *const *volgroups)
497 argc = count_strings (volgroups) + 4;
498 argv = malloc (sizeof (char *) * (argc+1));
500 reply_with_perror ("malloc");
505 argv[1] = "vgchange";
507 argv[3] = activate ? "y" : "n";
508 for (i = 4; i <= argc; ++i)
509 argv[i] = volgroups[i-4];
511 r = commandv (NULL, &err, (const char * const*) argv);
513 reply_with_error ("vgchange: %s", err);
526 do_vg_activate_all (int activate)
528 char *empty[] = { NULL };
529 return do_vg_activate (activate, empty);
533 do_lvrename (const char *logvol, const char *newlogvol)
538 r = command (NULL, &err,
540 logvol, newlogvol, NULL);
542 reply_with_error ("%s -> %s: %s", logvol, newlogvol, err);
555 do_vgrename (const char *volgroup, const char *newvolgroup)
560 r = command (NULL, &err,
562 volgroup, newvolgroup, NULL);
564 reply_with_error ("%s -> %s: %s", volgroup, newvolgroup, err);
577 get_lvm_field (const char *cmd, const char *field, const char *device)
581 int r = command (&out, &err,
583 "--unbuffered", "--noheadings", "-o", field,
586 reply_with_error ("%s: %s", device, err);
595 return out; /* Caller frees. */
599 do_pvuuid (const char *device)
601 return get_lvm_field ("pvs", "pv_uuid", device);
605 do_vguuid (const char *vgname)
607 return get_lvm_field ("vgs", "vg_uuid", vgname);
611 do_lvuuid (const char *device)
613 return get_lvm_field ("lvs", "lv_uuid", device);
617 get_lvm_fields (const char *cmd, const char *field, const char *device)
621 int r = command (&out, &err,
623 "--unbuffered", "--noheadings", "-o", field,
626 reply_with_error ("%s: %s", device, err);
634 char **ret = split_lines (out);
641 for (i = 0; ret[i] != NULL; ++i)
648 do_vgpvuuids (const char *vgname)
650 return get_lvm_fields ("vgs", "pv_uuid", vgname);
654 do_vglvuuids (const char *vgname)
656 return get_lvm_fields ("vgs", "lv_uuid", vgname);
665 r = command (NULL, &err,
666 "lvm", "vgscan", NULL);
668 reply_with_error ("%s", err);
677 /* Convert a non-canonical LV path like /dev/mapper/vg-lv or /dev/dm-0
678 * to a canonical one.
680 * This is harder than it should be. A LV device like /dev/VG/LV is
681 * really a symlink to a device-mapper device like /dev/dm-0. However
682 * at the device-mapper (kernel) level, nothing is really known about
683 * LVM (a userspace concept). Therefore we use a convoluted method to
684 * determine this, by listing out known LVs and checking whether the
685 * rdev (major/minor) of the device we are passed matches any of them.
687 * Note use of 'stat' instead of 'lstat' so that symlinks are fully
691 * 1 = conversion was successful, path is an LV
692 * '*ret' is set to the updated path if 'ret' is non-NULL.
693 * 0 = path is not an LV
694 * -1 = error, reply_with_* has been called
698 lv_canonical (const char *device, char **ret)
700 struct stat stat1, stat2;
702 int r = stat (device, &stat1);
704 reply_with_perror ("stat: %s", device);
708 char **lvs = do_lvs ();
713 for (i = 0; lvs[i] != NULL; ++i) {
714 r = stat (lvs[i], &stat2);
716 reply_with_perror ("stat: %s", lvs[i]);
720 if (stat1.st_rdev == stat2.st_rdev) { /* found it */
722 *ret = strdup (lvs[i]);
724 reply_with_perror ("strdup");
739 /* Test if a device is a logical volume (RHBZ#619793). */
741 do_is_lv (const char *device)
743 return lv_canonical (device, NULL);
746 /* Return canonical name of LV to caller (RHBZ#638899). */
748 do_lvm_canonical_lv_name (const char *device)
751 int r = lv_canonical (device, &canonical);
756 reply_with_error ("%s: not a logical volume", device);
760 return canonical; /* caller frees */