Better generation of recipes page.
[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   argv[0] = "/sbin/lvm";
201   argv[1] = "vgcreate";
202   argv[2] = volgroup;
203   for (i = 3; i <= argc; ++i)
204     argv[i] = physvols[i-3];
205
206   r = commandv (NULL, &err, argv);
207   if (r == -1) {
208     reply_with_error ("%s", err);
209     free (err);
210     return -1;
211   }
212
213   free (err);
214   return 0;
215 }
216
217 int
218 do_lvcreate (const char *logvol, const char *volgroup, int mbytes)
219 {
220   char *err;
221   int r;
222   char size[64];
223
224   snprintf (size, sizeof size, "%d", mbytes);
225
226   r = command (NULL, &err,
227                "/sbin/lvm", "lvcreate",
228                "-L", size, "-n", logvol, volgroup, NULL);
229   if (r == -1) {
230     reply_with_error ("%s", err);
231     free (err);
232     return -1;
233   }
234
235   free (err);
236   return 0;
237 }
238
239 /* Super-dangerous command used for testing.  It removes all
240  * LVs, VGs and PVs permanently.
241  */
242 int
243 do_lvm_remove_all (void)
244 {
245   char **xs;
246   int i, r;
247   char *err;
248
249   /* Remove LVs. */
250   xs = do_lvs ();
251   if (xs == NULL)
252     return -1;
253
254   for (i = 0; xs[i] != NULL; ++i) {
255     r = command (NULL, &err, "/sbin/lvm", "lvremove", "-f", xs[i], NULL);
256     if (r == -1) {
257       reply_with_error ("lvremove: %s: %s", xs[i], err);
258       free (err);
259       free_strings (xs);
260       return -1;
261     }
262     free (err);
263   }
264   free_strings (xs);
265
266   /* Remove VGs. */
267   xs = do_vgs ();
268   if (xs == NULL)
269     return -1;
270
271   for (i = 0; xs[i] != NULL; ++i) {
272     r = command (NULL, &err, "/sbin/lvm", "vgremove", "-f", xs[i], NULL);
273     if (r == -1) {
274       reply_with_error ("vgremove: %s: %s", xs[i], err);
275       free (err);
276       free_strings (xs);
277       return -1;
278     }
279     free (err);
280   }
281   free_strings (xs);
282
283   /* Remove PVs. */
284   xs = do_pvs ();
285   if (xs == NULL)
286     return -1;
287
288   for (i = 0; xs[i] != NULL; ++i) {
289     r = command (NULL, &err, "/sbin/lvm", "pvremove", "-f", xs[i], NULL);
290     if (r == -1) {
291       reply_with_error ("pvremove: %s: %s", xs[i], err);
292       free (err);
293       free_strings (xs);
294       return -1;
295     }
296     free (err);
297   }
298   free_strings (xs);
299
300   /* There, that was easy, sorry about your data. */
301   return 0;
302 }