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_lvm_int_pv_list *
158 return parse_command_line_pvs ();
161 guestfs_lvm_int_vg_list *
164 return parse_command_line_vgs ();
167 guestfs_lvm_int_lv_list *
170 return parse_command_line_lvs ();
174 do_pvcreate (const char *device)
179 r = command (NULL, &err,
180 "/sbin/lvm", "pvcreate", device, NULL);
182 reply_with_error ("%s", err);
192 do_vgcreate (const char *volgroup, char * const* const physvols)
198 argc = count_strings (physvols) + 3;
199 argv = malloc (sizeof (char *) * (argc + 1));
201 reply_with_perror ("malloc");
204 argv[0] = "/sbin/lvm";
205 argv[1] = "vgcreate";
207 for (i = 3; i <= argc; ++i)
208 argv[i] = physvols[i-3];
210 r = commandv (NULL, &err, argv);
212 reply_with_error ("%s", err);
222 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
228 snprintf (size, sizeof size, "%d", mbytes);
230 r = command (NULL, &err,
231 "/sbin/lvm", "lvcreate",
232 "-L", size, "-n", logvol, volgroup, NULL);
234 reply_with_error ("%s", err);
243 /* Super-dangerous command used for testing. It removes all
244 * LVs, VGs and PVs permanently.
247 do_lvm_remove_all (void)
258 for (i = 0; xs[i] != NULL; ++i) {
259 r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
261 reply_with_error ("lvremove: %s: %s", xs[i], err);
275 for (i = 0; xs[i] != NULL; ++i) {
276 r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
278 reply_with_error ("vgremove: %s: %s", xs[i], err);
292 for (i = 0; xs[i] != NULL; ++i) {
293 r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
295 reply_with_error ("pvremove: %s: %s", xs[i], err);
304 /* There, that was easy, sorry about your data. */
309 do_lvremove (const char *device)
314 r = command (NULL, &err,
315 "/sbin/lvm", "lvremove", "-f", device, NULL);
317 reply_with_error ("%s", err);
327 do_vgremove (const char *device)
332 r = command (NULL, &err,
333 "/sbin/lvm", "vgremove", "-f", device, NULL);
335 reply_with_error ("%s", err);
345 do_pvremove (const char *device)
350 r = command (NULL, &err,
351 "/sbin/lvm", "pvremove", "-ff", device, NULL);
353 reply_with_error ("%s", err);