Add: lvresize, resize2fs commands.
[libguestfs.git] / daemon / lvm.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc. 
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <ctype.h>
26
27 #include "daemon.h"
28 #include "actions.h"
29
30 /* LVM actions.  Keep an eye on liblvm, although at the time
31  * of writing it hasn't progressed very far.
32  */
33
34 static char **
35 convert_lvm_output (char *out, char *prefix)
36 {
37   char *p, *pend;
38   char **r = NULL;
39   int size = 0, alloc = 0;
40   int len;
41   char buf[256];
42   char *str;
43
44   p = out;
45   while (p) {
46     pend = strchr (p, '\n');    /* Get the next line of output. */
47     if (pend) {
48       *pend = '\0';
49       pend++;
50     }
51
52     while (*p && isspace (*p))  /* Skip any leading whitespace. */
53       p++;
54
55     /* Sigh, skip trailing whitespace too.  "pvs", I'm looking at you. */
56     len = strlen (p)-1;
57     while (*p && isspace (p[len]))
58       p[len--] = '\0';
59
60     if (!*p) {                  /* Empty line?  Skip it. */
61       p = pend;
62       continue;
63     }
64
65     /* Prefix? */
66     if (prefix) {
67       snprintf (buf, sizeof buf, "%s%s", prefix, p);
68       str = buf;
69     } else
70       str = p;
71
72     if (add_string (&r, &size, &alloc, str) == -1) {
73       free (out);
74       return NULL;
75     }
76
77     p = pend;
78   }
79
80   free (out);
81
82   if (add_string (&r, &size, &alloc, NULL) == -1)
83     return NULL;
84
85   sort_strings (r, size-1);
86   return r;
87 }
88
89 char **
90 do_pvs (void)
91 {
92   char *out, *err;
93   int r;
94
95   r = command (&out, &err,
96                "/sbin/lvm", "pvs", "-o", "pv_name", "--noheadings", NULL);
97   if (r == -1) {
98     reply_with_error ("%s", err);
99     free (out);
100     free (err);
101     return NULL;
102   }
103
104   free (err);
105
106   return convert_lvm_output (out, NULL);
107 }
108
109 char **
110 do_vgs (void)
111 {
112   char *out, *err;
113   int r;
114
115   r = command (&out, &err,
116                "/sbin/lvm", "vgs", "-o", "vg_name", "--noheadings", NULL);
117   if (r == -1) {
118     reply_with_error ("%s", err);
119     free (out);
120     free (err);
121     return NULL;
122   }
123
124   free (err);
125
126   return convert_lvm_output (out, NULL);
127 }
128
129 char **
130 do_lvs (void)
131 {
132   char *out, *err;
133   int r;
134
135   r = command (&out, &err,
136                "/sbin/lvm", "lvs",
137                "-o", "vg_name,lv_name", "--noheadings",
138                "--separator", "/", NULL);
139   if (r == -1) {
140     reply_with_error ("%s", err);
141     free (out);
142     free (err);
143     return NULL;
144   }
145
146   free (err);
147
148   return convert_lvm_output (out, "/dev/");
149 }
150
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
153  * by generator.ml.
154  */
155 guestfs_lvm_int_pv_list *
156 do_pvs_full (void)
157 {
158   return parse_command_line_pvs ();
159 }
160
161 guestfs_lvm_int_vg_list *
162 do_vgs_full (void)
163 {
164   return parse_command_line_vgs ();
165 }
166
167 guestfs_lvm_int_lv_list *
168 do_lvs_full (void)
169 {
170   return parse_command_line_lvs ();
171 }
172
173 int
174 do_pvcreate (const char *device)
175 {
176   char *err;
177   int r;
178
179   r = command (NULL, &err,
180                "/sbin/lvm", "pvcreate", device, NULL);
181   if (r == -1) {
182     reply_with_error ("%s", err);
183     free (err);
184     return -1;
185   }
186
187   free (err);
188   return 0;
189 }
190
191 int
192 do_vgcreate (const char *volgroup, char * const* const physvols)
193 {
194   char *err;
195   int r, argc, i;
196   const char **argv;
197
198   argc = count_strings (physvols) + 3;
199   argv = malloc (sizeof (char *) * (argc + 1));
200   if (argv == NULL) {
201     reply_with_perror ("malloc");
202     return -1;
203   }
204   argv[0] = "/sbin/lvm";
205   argv[1] = "vgcreate";
206   argv[2] = volgroup;
207   for (i = 3; i <= argc; ++i)
208     argv[i] = physvols[i-3];
209
210   r = commandv (NULL, &err, argv);
211   if (r == -1) {
212     reply_with_error ("%s", err);
213     free (err);
214     return -1;
215   }
216
217   free (err);
218   return 0;
219 }
220
221 int
222 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
223 {
224   char *err;
225   int r;
226   char size[64];
227
228   snprintf (size, sizeof size, "%d", mbytes);
229
230   r = command (NULL, &err,
231                "/sbin/lvm", "lvcreate",
232                "-L", size, "-n", logvol, volgroup, NULL);
233   if (r == -1) {
234     reply_with_error ("%s", err);
235     free (err);
236     return -1;
237   }
238
239   free (err);
240   return 0;
241 }
242
243 int
244 do_lvresize (const char *logvol, int mbytes)
245 {
246   char *err;
247   int r;
248   char size[64];
249
250   IS_DEVICE (logvol, -1);
251
252   snprintf (size, sizeof size, "%d", mbytes);
253
254   r = command (NULL, &err,
255                "/sbin/lvm", "lvresize",
256                "-L", size, logvol, NULL);
257   if (r == -1) {
258     reply_with_error ("lvresize: %s", err);
259     free (err);
260     return -1;
261   }
262
263   free (err);
264   return 0;
265 }
266
267 /* Super-dangerous command used for testing.  It removes all
268  * LVs, VGs and PVs permanently.
269  */
270 int
271 do_lvm_remove_all (void)
272 {
273   char **xs;
274   int i, r;
275   char *err;
276
277   /* Remove LVs. */
278   xs = do_lvs ();
279   if (xs == NULL)
280     return -1;
281
282   for (i = 0; xs[i] != NULL; ++i) {
283     r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
284     if (r == -1) {
285       reply_with_error ("lvremove: %s: %s", xs[i], err);
286       free (err);
287       free_strings (xs);
288       return -1;
289     }
290     free (err);
291   }
292   free_strings (xs);
293
294   /* Remove VGs. */
295   xs = do_vgs ();
296   if (xs == NULL)
297     return -1;
298
299   for (i = 0; xs[i] != NULL; ++i) {
300     r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
301     if (r == -1) {
302       reply_with_error ("vgremove: %s: %s", xs[i], err);
303       free (err);
304       free_strings (xs);
305       return -1;
306     }
307     free (err);
308   }
309   free_strings (xs);
310
311   /* Remove PVs. */
312   xs = do_pvs ();
313   if (xs == NULL)
314     return -1;
315
316   for (i = 0; xs[i] != NULL; ++i) {
317     r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
318     if (r == -1) {
319       reply_with_error ("pvremove: %s: %s", xs[i], err);
320       free (err);
321       free_strings (xs);
322       return -1;
323     }
324     free (err);
325   }
326   free_strings (xs);
327
328   /* There, that was easy, sorry about your data. */
329   return 0;
330 }
331
332 int
333 do_lvremove (const char *device)
334 {
335   char *err;
336   int r;
337
338   r = command (NULL, &err,
339                "/sbin/lvm", "lvremove", "-f", device, NULL);
340   if (r == -1) {
341     reply_with_error ("%s", err);
342     free (err);
343     return -1;
344   }
345
346   free (err);
347   return 0;
348 }
349
350 int
351 do_vgremove (const char *device)
352 {
353   char *err;
354   int r;
355
356   r = command (NULL, &err,
357                "/sbin/lvm", "vgremove", "-f", device, NULL);
358   if (r == -1) {
359     reply_with_error ("%s", err);
360     free (err);
361     return -1;
362   }
363
364   free (err);
365   return 0;
366 }
367
368 int
369 do_pvremove (const char *device)
370 {
371   char *err;
372   int r;
373
374   r = command (NULL, &err,
375                "/sbin/lvm", "pvremove", "-ff", device, NULL);
376   if (r == -1) {
377     reply_with_error ("%s", err);
378     free (err);
379     return -1;
380   }
381
382   free (err);
383   return 0;
384 }
385
386 int
387 do_pvresize (const char *device)
388 {
389   char *err;
390   int r;
391
392   r = command (NULL, &err,
393                "/sbin/lvm", "pvresize", device, NULL);
394   if (r == -1) {
395     reply_with_error ("pvresize: %s: %s", device, err);
396     free (err);
397     return -1;
398   }
399
400   free (err);
401   return 0;
402 }
403
404 int
405 do_vg_activate (int activate, char * const* const volgroups)
406 {
407   char *err;
408   int r, i, argc;
409   const char **argv;
410
411   argc = count_strings (volgroups) + 4;
412   argv = malloc (sizeof (char *) * (argc+1));
413   if (argv == NULL) {
414     reply_with_perror ("malloc");
415     return -1;
416   }
417
418   argv[0] = "/sbin/lvm";
419   argv[1] = "vgchange";
420   argv[2] = "-a";
421   argv[3] = activate ? "y" : "n";
422   for (i = 4; i <= argc; ++i)
423     argv[i] = volgroups[i-4];
424
425   r = commandv (NULL, &err, argv);
426   if (r == -1) {
427     reply_with_error ("vgchange: %s", err);
428     free (err);
429     return -1;
430   }
431
432   free (err);
433   return 0;
434 }
435
436 int
437 do_vg_activate_all (int activate)
438 {
439   char *empty[] = { NULL };
440   return do_vg_activate (activate, empty);
441 }