Add IS_DEVICE checks for all calls which take a device parameter.
[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   IS_DEVICE (device, -1);
180
181   r = command (NULL, &err,
182                "/sbin/lvm", "pvcreate", device, NULL);
183   if (r == -1) {
184     reply_with_error ("%s", err);
185     free (err);
186     return -1;
187   }
188
189   free (err);
190   return 0;
191 }
192
193 int
194 do_vgcreate (const char *volgroup, char * const* const physvols)
195 {
196   char *err;
197   int r, argc, i;
198   const char **argv;
199
200   Xphysvols;
201
202   argc = count_strings (physvols) + 3;
203   argv = malloc (sizeof (char *) * (argc + 1));
204   if (argv == NULL) {
205     reply_with_perror ("malloc");
206     return -1;
207   }
208   argv[0] = "/sbin/lvm";
209   argv[1] = "vgcreate";
210   argv[2] = volgroup;
211   for (i = 3; i <= argc; ++i)
212     argv[i] = physvols[i-3];
213
214   r = commandv (NULL, &err, argv);
215   if (r == -1) {
216     reply_with_error ("%s", err);
217     free (err);
218     return -1;
219   }
220
221   free (err);
222   return 0;
223 }
224
225 int
226 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
227 {
228   char *err;
229   int r;
230   char size[64];
231
232   snprintf (size, sizeof size, "%d", mbytes);
233
234   r = command (NULL, &err,
235                "/sbin/lvm", "lvcreate",
236                "-L", size, "-n", logvol, volgroup, NULL);
237   if (r == -1) {
238     reply_with_error ("%s", err);
239     free (err);
240     return -1;
241   }
242
243   free (err);
244   return 0;
245 }
246
247 int
248 do_lvresize (const char *logvol, int mbytes)
249 {
250   char *err;
251   int r;
252   char size[64];
253
254   IS_DEVICE (logvol, -1);
255
256   snprintf (size, sizeof size, "%d", mbytes);
257
258   r = command (NULL, &err,
259                "/sbin/lvm", "lvresize",
260                "-L", size, logvol, NULL);
261   if (r == -1) {
262     reply_with_error ("lvresize: %s", err);
263     free (err);
264     return -1;
265   }
266
267   free (err);
268   return 0;
269 }
270
271 /* Super-dangerous command used for testing.  It removes all
272  * LVs, VGs and PVs permanently.
273  */
274 int
275 do_lvm_remove_all (void)
276 {
277   char **xs;
278   int i, r;
279   char *err;
280
281   /* Remove LVs. */
282   xs = do_lvs ();
283   if (xs == NULL)
284     return -1;
285
286   for (i = 0; xs[i] != NULL; ++i) {
287     r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
288     if (r == -1) {
289       reply_with_error ("lvremove: %s: %s", xs[i], err);
290       free (err);
291       free_strings (xs);
292       return -1;
293     }
294     free (err);
295   }
296   free_strings (xs);
297
298   /* Remove VGs. */
299   xs = do_vgs ();
300   if (xs == NULL)
301     return -1;
302
303   for (i = 0; xs[i] != NULL; ++i) {
304     r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
305     if (r == -1) {
306       reply_with_error ("vgremove: %s: %s", xs[i], err);
307       free (err);
308       free_strings (xs);
309       return -1;
310     }
311     free (err);
312   }
313   free_strings (xs);
314
315   /* Remove PVs. */
316   xs = do_pvs ();
317   if (xs == NULL)
318     return -1;
319
320   for (i = 0; xs[i] != NULL; ++i) {
321     r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
322     if (r == -1) {
323       reply_with_error ("pvremove: %s: %s", xs[i], err);
324       free (err);
325       free_strings (xs);
326       return -1;
327     }
328     free (err);
329   }
330   free_strings (xs);
331
332   /* There, that was easy, sorry about your data. */
333   return 0;
334 }
335
336 int
337 do_lvremove (const char *device)
338 {
339   char *err;
340   int r;
341
342   IS_DEVICE (device, -1);
343
344   r = command (NULL, &err,
345                "/sbin/lvm", "lvremove", "-f", device, NULL);
346   if (r == -1) {
347     reply_with_error ("%s", err);
348     free (err);
349     return -1;
350   }
351
352   free (err);
353   return 0;
354 }
355
356 int
357 do_vgremove (const char *device)
358 {
359   char *err;
360   int r;
361
362   IS_DEVICE (device, -1);
363
364   r = command (NULL, &err,
365                "/sbin/lvm", "vgremove", "-f", device, NULL);
366   if (r == -1) {
367     reply_with_error ("%s", err);
368     free (err);
369     return -1;
370   }
371
372   free (err);
373   return 0;
374 }
375
376 int
377 do_pvremove (const char *device)
378 {
379   char *err;
380   int r;
381
382   IS_DEVICE (device, -1);
383
384   r = command (NULL, &err,
385                "/sbin/lvm", "pvremove", "-ff", device, NULL);
386   if (r == -1) {
387     reply_with_error ("%s", err);
388     free (err);
389     return -1;
390   }
391
392   free (err);
393   return 0;
394 }
395
396 int
397 do_pvresize (const char *device)
398 {
399   char *err;
400   int r;
401
402   IS_DEVICE (device, -1);
403
404   r = command (NULL, &err,
405                "/sbin/lvm", "pvresize", device, NULL);
406   if (r == -1) {
407     reply_with_error ("pvresize: %s: %s", device, err);
408     free (err);
409     return -1;
410   }
411
412   free (err);
413   return 0;
414 }
415
416 int
417 do_vg_activate (int activate, char * const* const volgroups)
418 {
419   char *err;
420   int r, i, argc;
421   const char **argv;
422
423   argc = count_strings (volgroups) + 4;
424   argv = malloc (sizeof (char *) * (argc+1));
425   if (argv == NULL) {
426     reply_with_perror ("malloc");
427     return -1;
428   }
429
430   argv[0] = "/sbin/lvm";
431   argv[1] = "vgchange";
432   argv[2] = "-a";
433   argv[3] = activate ? "y" : "n";
434   for (i = 4; i <= argc; ++i)
435     argv[i] = volgroups[i-4];
436
437   r = commandv (NULL, &err, argv);
438   if (r == -1) {
439     reply_with_error ("vgchange: %s", err);
440     free (err);
441     return -1;
442   }
443
444   free (err);
445   return 0;
446 }
447
448 int
449 do_vg_activate_all (int activate)
450 {
451   char *empty[] = { NULL };
452   return do_vg_activate (activate, empty);
453 }