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.
29 #include "optgroups.h"
32 optgroup_lvm2_available (void)
34 return prog_exists ("lvm");
37 /* LVM actions. Keep an eye on liblvm, although at the time
38 * of writing it hasn't progressed very far.
42 convert_lvm_output (char *out, const char *prefix)
46 int size = 0, alloc = 0;
53 pend = strchr (p, '\n'); /* Get the next line of output. */
59 while (*p && c_isspace (*p)) /* Skip any leading whitespace. */
62 /* Sigh, skip trailing whitespace too. "pvs", I'm looking at you. */
64 while (*p && c_isspace (p[len]))
67 if (!*p) { /* Empty line? Skip it. */
74 snprintf (buf, sizeof buf, "%s%s", prefix, p);
79 if (add_string (&r, &size, &alloc, str) == -1) {
89 if (add_string (&r, &size, &alloc, NULL) == -1)
92 sort_strings (r, size-1);
102 r = command (&out, &err,
103 "lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
105 reply_with_error ("%s", err);
113 return convert_lvm_output (out, NULL);
122 r = command (&out, &err,
123 "lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
125 reply_with_error ("%s", err);
133 return convert_lvm_output (out, NULL);
142 r = command (&out, &err,
144 "-o", "vg_name,lv_name", "--noheadings",
145 "--separator", "/", NULL);
147 reply_with_error ("%s", err);
155 return convert_lvm_output (out, "/dev/");
158 /* These were so complex to implement that I ended up auto-generating
159 * the code. That code is in stubs.c, and it is generated as usual
162 guestfs_int_lvm_pv_list *
165 return parse_command_line_pvs ();
168 guestfs_int_lvm_vg_list *
171 return parse_command_line_vgs ();
174 guestfs_int_lvm_lv_list *
177 return parse_command_line_lvs ();
181 do_pvcreate (const char *device)
186 r = command (NULL, &err,
187 "lvm", "pvcreate", device, NULL);
189 reply_with_error ("%s", err);
202 do_vgcreate (const char *volgroup, char *const *physvols)
208 argc = count_strings (physvols) + 3;
209 argv = malloc (sizeof (char *) * (argc + 1));
211 reply_with_perror ("malloc");
215 argv[1] = "vgcreate";
217 for (i = 3; i <= argc; ++i)
218 argv[i] = physvols[i-3];
220 r = commandv (NULL, &err, (const char * const*) argv);
222 reply_with_error ("%s", err);
235 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
241 snprintf (size, sizeof size, "%d", mbytes);
243 r = command (NULL, &err,
245 "-L", size, "-n", logvol, volgroup, NULL);
247 reply_with_error ("%s", err);
260 do_lvresize (const char *logvol, int mbytes)
266 snprintf (size, sizeof size, "%d", mbytes);
268 r = command (NULL, &err,
270 "-L", size, logvol, NULL);
272 reply_with_error ("%s", err);
282 do_lvresize_free (const char *logvol, int percent)
287 if (percent < 0 || percent > 100) {
288 reply_with_error ("percentage must be [0..100] (was %d)", percent);
293 snprintf (size, sizeof size, "+%d%%FREE", percent);
295 r = command (NULL, &err,
296 "lvm", "lvresize", "-l", size, logvol, NULL);
298 reply_with_error ("%s", err);
307 /* Super-dangerous command used for testing. It removes all
308 * LVs, VGs and PVs permanently.
311 do_lvm_remove_all (void)
322 for (i = 0; xs[i] != NULL; ++i) {
323 r = command (NULL, &err, "lvm", "lvremove", "-f", xs[i], NULL);
325 reply_with_error ("lvremove: %s: %s", xs[i], err);
339 for (i = 0; xs[i] != NULL; ++i) {
340 r = command (NULL, &err, "lvm", "vgremove", "-f", xs[i], NULL);
342 reply_with_error ("vgremove: %s: %s", xs[i], err);
356 for (i = 0; xs[i] != NULL; ++i) {
357 r = command (NULL, &err, "lvm", "pvremove", "-f", xs[i], NULL);
359 reply_with_error ("pvremove: %s: %s", xs[i], err);
370 /* There, that was easy, sorry about your data. */
375 do_lvremove (const char *device)
380 r = command (NULL, &err,
381 "lvm", "lvremove", "-f", device, NULL);
383 reply_with_error ("%s", err);
396 do_vgremove (const char *device)
401 r = command (NULL, &err,
402 "lvm", "vgremove", "-f", device, NULL);
404 reply_with_error ("%s", err);
417 do_pvremove (const char *device)
422 r = command (NULL, &err,
423 "lvm", "pvremove", "-ff", device, NULL);
425 reply_with_error ("%s", err);
438 do_pvresize (const char *device)
443 r = command (NULL, &err,
444 "lvm", "pvresize", device, NULL);
446 reply_with_error ("%s: %s", device, err);
456 do_vg_activate (int activate, char *const *volgroups)
462 argc = count_strings (volgroups) + 4;
463 argv = malloc (sizeof (char *) * (argc+1));
465 reply_with_perror ("malloc");
470 argv[1] = "vgchange";
472 argv[3] = activate ? "y" : "n";
473 for (i = 4; i <= argc; ++i)
474 argv[i] = volgroups[i-4];
476 r = commandv (NULL, &err, (const char * const*) argv);
478 reply_with_error ("vgchange: %s", err);
491 do_vg_activate_all (int activate)
493 char *empty[] = { NULL };
494 return do_vg_activate (activate, empty);
498 do_lvrename (const char *logvol, const char *newlogvol)
503 r = command (NULL, &err,
505 logvol, newlogvol, NULL);
507 reply_with_error ("%s -> %s: %s", logvol, newlogvol, err);
520 do_vgrename (const char *volgroup, const char *newvolgroup)
525 r = command (NULL, &err,
527 volgroup, newvolgroup, NULL);
529 reply_with_error ("%s -> %s: %s", volgroup, newvolgroup, err);
542 get_lvm_field (const char *cmd, const char *field, const char *device)
546 int r = command (&out, &err,
548 "--unbuffered", "--noheadings", "-o", field,
551 reply_with_error ("%s: %s", device, err);
560 return out; /* Caller frees. */
564 do_pvuuid (const char *device)
566 return get_lvm_field ("pvs", "pv_uuid", device);
570 do_vguuid (const char *vgname)
572 return get_lvm_field ("vgs", "vg_uuid", vgname);
576 do_lvuuid (const char *device)
578 return get_lvm_field ("lvs", "lv_uuid", device);
582 get_lvm_fields (const char *cmd, const char *field, const char *device)
586 int r = command (&out, &err,
588 "--unbuffered", "--noheadings", "-o", field,
591 reply_with_error ("%s: %s", device, err);
599 char **ret = split_lines (out);
606 for (i = 0; ret[i] != NULL; ++i)
613 do_vgpvuuids (const char *vgname)
615 return get_lvm_fields ("vgs", "pv_uuid", vgname);
619 do_vglvuuids (const char *vgname)
621 return get_lvm_fields ("vgs", "lv_uuid", vgname);
630 r = command (NULL, &err,
631 "lvm", "vgscan", NULL);
633 reply_with_error ("%s", err);