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.
30 /* LVM actions. Keep an eye on liblvm, although at the time
31 * of writing it hasn't progressed very far.
35 convert_lvm_output (char *out, char *prefix)
39 int size = 0, alloc = 0;
46 pend = strchr (p, '\n'); /* Get the next line of output. */
52 while (*p && isspace (*p)) /* Skip any leading whitespace. */
55 /* Sigh, skip trailing whitespace too. "pvs", I'm looking at you. */
57 while (*p && isspace (p[len]))
60 if (!*p) { /* Empty line? Skip it. */
67 snprintf (buf, sizeof buf, "%s%s", prefix, p);
72 if (add_string (&r, &size, &alloc, str) == -1) {
82 if (add_string (&r, &size, &alloc, NULL) == -1)
85 sort_strings (r, size-1);
95 r = command (&out, &err,
96 "/sbin/lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
98 reply_with_error ("%s", err);
106 return convert_lvm_output (out, NULL);
115 r = command (&out, &err,
116 "/sbin/lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
118 reply_with_error ("%s", err);
126 return convert_lvm_output (out, NULL);
135 r = command (&out, &err,
137 "-o", "vg_name,lv_name", "--noheadings",
138 "--separator", "/", NULL);
140 reply_with_error ("%s", err);
148 return convert_lvm_output (out, "/dev/");
151 /* These were so complex to implement that I ended up auto-generating
152 * the code. That code is in stubs.c, and it is generated as usual
155 guestfs_int_lvm_pv_list *
158 return parse_command_line_pvs ();
161 guestfs_int_lvm_vg_list *
164 return parse_command_line_vgs ();
167 guestfs_int_lvm_lv_list *
170 return parse_command_line_lvs ();
174 do_pvcreate (char *device)
179 r = command (NULL, &err,
180 "/sbin/lvm", "pvcreate", device, NULL);
182 reply_with_error ("%s", err);
195 do_vgcreate (char *volgroup, char **physvols)
201 /* Check they are devices and also do device name translation. */
202 for (i = 0; physvols[i] != NULL; ++i)
203 RESOLVE_DEVICE (physvols[i], return -1);
205 argc = count_strings (physvols) + 3;
206 argv = malloc (sizeof (char *) * (argc + 1));
208 reply_with_perror ("malloc");
211 argv[0] = "/sbin/lvm";
212 argv[1] = "vgcreate";
214 for (i = 3; i <= argc; ++i)
215 argv[i] = physvols[i-3];
217 r = commandv (NULL, &err, argv);
219 reply_with_error ("%s", err);
232 do_lvcreate (char *logvol, char *volgroup, int mbytes)
238 snprintf (size, sizeof size, "%d", mbytes);
240 r = command (NULL, &err,
241 "/sbin/lvm", "lvcreate",
242 "-L", size, "-n", logvol, volgroup, NULL);
244 reply_with_error ("%s", err);
257 do_lvresize (char *logvol, int mbytes)
263 RESOLVE_DEVICE (logvol, return -1);
265 snprintf (size, sizeof size, "%d", mbytes);
267 r = command (NULL, &err,
268 "/sbin/lvm", "lvresize",
269 "-L", size, logvol, NULL);
271 reply_with_error ("lvresize: %s", err);
280 /* Super-dangerous command used for testing. It removes all
281 * LVs, VGs and PVs permanently.
284 do_lvm_remove_all (void)
295 for (i = 0; xs[i] != NULL; ++i) {
296 r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
298 reply_with_error ("lvremove: %s: %s", xs[i], err);
312 for (i = 0; xs[i] != NULL; ++i) {
313 r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
315 reply_with_error ("vgremove: %s: %s", xs[i], err);
329 for (i = 0; xs[i] != NULL; ++i) {
330 r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
332 reply_with_error ("pvremove: %s: %s", xs[i], err);
343 /* There, that was easy, sorry about your data. */
348 do_lvremove (char *device)
353 r = command (NULL, &err,
354 "/sbin/lvm", "lvremove", "-f", device, NULL);
356 reply_with_error ("%s", err);
369 do_vgremove (char *device)
374 r = command (NULL, &err,
375 "/sbin/lvm", "vgremove", "-f", device, NULL);
377 reply_with_error ("%s", err);
390 do_pvremove (char *device)
395 r = command (NULL, &err,
396 "/sbin/lvm", "pvremove", "-ff", device, NULL);
398 reply_with_error ("%s", err);
411 do_pvresize (char *device)
416 r = command (NULL, &err,
417 "/sbin/lvm", "pvresize", device, NULL);
419 reply_with_error ("pvresize: %s: %s", device, err);
429 do_vg_activate (int activate, char **volgroups)
435 argc = count_strings (volgroups) + 4;
436 argv = malloc (sizeof (char *) * (argc+1));
438 reply_with_perror ("malloc");
442 argv[0] = "/sbin/lvm";
443 argv[1] = "vgchange";
445 argv[3] = activate ? "y" : "n";
446 for (i = 4; i <= argc; ++i)
447 argv[i] = volgroups[i-4];
449 r = commandv (NULL, &err, argv);
451 reply_with_error ("vgchange: %s", err);
464 do_vg_activate_all (int activate)
466 char *empty[] = { NULL };
467 return do_vg_activate (activate, empty);