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