f7889da46b709149305efcacf8dbcbbf6886cc83
[libguestfs.git] / daemon / ext2.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "guestfs_protocol.h"
28 #include "daemon.h"
29 #include "c-ctype.h"
30 #include "actions.h"
31
32 /* Confirmed this is true up to ext4 from the Linux sources. */
33 #define EXT2_LABEL_MAX 16
34
35 /* Choose which tools like mke2fs to use.  For RHEL 5 (only) there
36  * is a special set of tools which support ext2/3/4.  eg. On RHEL 5,
37  * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
38  *
39  * We specify e4fsprogs in the package list to ensure it is loaded
40  * if it exists.
41  */
42 int
43 e2prog (char *name)
44 {
45   char *p = strstr (name, "e2");
46   if (!p) return 0;
47   p++;
48
49   *p = '4';
50   if (prog_exists (name))
51     return 0;
52
53   *p = '2';
54   if (prog_exists (name))
55     return 0;
56
57   reply_with_error ("cannot find required program %s", name);
58   return -1;
59 }
60
61 char **
62 do_tune2fs_l (const char *device)
63 {
64   int r;
65   char *out, *err;
66   char *p, *pend, *colon;
67   char **ret = NULL;
68   int size = 0, alloc = 0;
69
70   char prog[] = "tune2fs";
71   if (e2prog (prog) == -1)
72     return NULL;
73
74   r = command (&out, &err, prog, "-l", device, NULL);
75   if (r == -1) {
76     reply_with_error ("%s", err);
77     free (err);
78     free (out);
79     return NULL;
80   }
81   free (err);
82
83   p = out;
84
85   /* Discard the first line if it contains "tune2fs ...". */
86   if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
87     p = strchr (p, '\n');
88     if (p) p++;
89     else {
90       reply_with_error ("truncated output");
91       free (out);
92       return NULL;
93     }
94   }
95
96   /* Read the lines and split into "key: value". */
97   while (*p) {
98     pend = strchrnul (p, '\n');
99     if (*pend == '\n') {
100       *pend = '\0';
101       pend++;
102     }
103
104     if (!*p) continue;
105
106     colon = strchr (p, ':');
107     if (colon) {
108       *colon = '\0';
109
110       do { colon++; } while (*colon && c_isspace (*colon));
111
112       if (add_string (&ret, &size, &alloc, p) == -1) {
113         free (out);
114         return NULL;
115       }
116       if (STREQ (colon, "<none>") ||
117           STREQ (colon, "<not available>") ||
118           STREQ (colon, "(none)")) {
119         if (add_string (&ret, &size, &alloc, "") == -1) {
120           free (out);
121           return NULL;
122         }
123       } else {
124         if (add_string (&ret, &size, &alloc, colon) == -1) {
125           free (out);
126           return NULL;
127         }
128       }
129     }
130     else {
131       if (add_string (&ret, &size, &alloc, p) == -1) {
132         free (out);
133         return NULL;
134       }
135       if (add_string (&ret, &size, &alloc, "") == -1) {
136         free (out);
137         return NULL;
138       }
139     }
140
141     p = pend;
142   }
143
144   free (out);
145
146   if (add_string (&ret, &size, &alloc, NULL) == -1)
147     return NULL;
148
149   return ret;
150 }
151
152 int
153 do_set_e2label (const char *device, const char *label)
154 {
155   int r;
156   char *err;
157
158   char prog[] = "e2label";
159   if (e2prog (prog) == -1)
160     return -1;
161
162   if (strlen (label) > EXT2_LABEL_MAX) {
163     reply_with_error ("%s: ext2 labels are limited to %d bytes",
164                       label, EXT2_LABEL_MAX);
165     return -1;
166   }
167
168   r = command (NULL, &err, prog, device, label, NULL);
169   if (r == -1) {
170     reply_with_error ("%s", err);
171     free (err);
172     return -1;
173   }
174
175   free (err);
176   return 0;
177 }
178
179 char *
180 do_get_e2label (const char *device)
181 {
182   return do_vfs_label (device);
183 }
184
185 int
186 do_set_e2uuid (const char *device, const char *uuid)
187 {
188   int r;
189   char *err;
190
191   char prog[] = "tune2fs";
192   if (e2prog (prog) == -1)
193     return -1;
194
195   r = command (NULL, &err, prog, "-U", uuid, device, NULL);
196   if (r == -1) {
197     reply_with_error ("%s", err);
198     free (err);
199     return -1;
200   }
201
202   free (err);
203   return 0;
204 }
205
206 char *
207 do_get_e2uuid (const char *device)
208 {
209   return do_vfs_uuid (device);
210 }
211
212 int
213 do_resize2fs (const char *device)
214 {
215   char *err;
216   int r;
217
218   char prog[] = "resize2fs";
219   if (e2prog (prog) == -1)
220     return -1;
221
222   r = command (NULL, &err, prog, device, NULL);
223   if (r == -1) {
224     reply_with_error ("%s", err);
225     free (err);
226     return -1;
227   }
228
229   free (err);
230   return 0;
231 }
232
233 int
234 do_resize2fs_size (const char *device, int64_t size)
235 {
236   char *err;
237   int r;
238
239   char prog[] = "resize2fs";
240   if (e2prog (prog) == -1)
241     return -1;
242
243   /* resize2fs itself may impose additional limits.  Since we are
244    * going to use the 'K' suffix however we can only work with whole
245    * kilobytes.
246    */
247   if (size & 1023) {
248     reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
249                       size);
250     return -1;
251   }
252   size /= 1024;
253
254   char buf[32];
255   snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
256
257   r = command (NULL, &err, prog, device, buf, NULL);
258   if (r == -1) {
259     reply_with_error ("%s", err);
260     free (err);
261     return -1;
262   }
263
264   free (err);
265   return 0;
266 }
267
268 int
269 do_resize2fs_M (const char *device)
270 {
271   char *err;
272   int r;
273
274   char prog[] = "resize2fs";
275   if (e2prog (prog) == -1)
276     return -1;
277
278   r = command (NULL, &err, prog, "-M" , device, NULL);
279   if (r == -1) {
280     reply_with_error ("%s", err);
281     free (err);
282     return -1;
283   }
284
285   free (err);
286   return 0;
287 }
288
289 int
290 do_e2fsck_f (const char *device)
291 {
292   char *err;
293   int r;
294
295   char prog[] = "e2fsck";
296   if (e2prog (prog) == -1)
297     return -1;
298
299   /* 0 = no errors, 1 = errors corrected.
300    *
301    * >= 4 means uncorrected or other errors.
302    *
303    * 2, 3 means errors were corrected and we require a reboot.  This is
304    * a difficult corner case.
305    */
306   r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
307   if (r == -1 || r >= 2) {
308     reply_with_error ("%s", err);
309     free (err);
310     return -1;
311   }
312
313   free (err);
314   return 0;
315 }
316
317 int
318 do_mke2journal (int blocksize, const char *device)
319 {
320   char *err;
321   int r;
322
323   char prog[] = "mke2fs";
324   if (e2prog (prog) == -1)
325     return -1;
326
327   char blocksize_s[32];
328   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
329
330   r = command (NULL, &err,
331                prog, "-O", "journal_dev", "-b", blocksize_s,
332                device, NULL);
333   if (r == -1) {
334     reply_with_error ("%s", err);
335     free (err);
336     return -1;
337   }
338
339   free (err);
340   return 0;
341 }
342
343 int
344 do_mke2journal_L (int blocksize, const char *label, const char *device)
345 {
346   char *err;
347   int r;
348
349   char prog[] = "mke2fs";
350   if (e2prog (prog) == -1)
351     return -1;
352
353   if (strlen (label) > EXT2_LABEL_MAX) {
354     reply_with_error ("%s: ext2 labels are limited to %d bytes",
355                       label, EXT2_LABEL_MAX);
356     return -1;
357   }
358
359   char blocksize_s[32];
360   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
361
362   r = command (NULL, &err,
363                prog, "-O", "journal_dev", "-b", blocksize_s,
364                "-L", label,
365                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_mke2journal_U (int blocksize, const char *uuid, const char *device)
378 {
379   char *err;
380   int r;
381
382   char prog[] = "mke2fs";
383   if (e2prog (prog) == -1)
384     return -1;
385
386   char blocksize_s[32];
387   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
388
389   r = command (NULL, &err,
390                prog, "-O", "journal_dev", "-b", blocksize_s,
391                "-U", uuid,
392                device, NULL);
393   if (r == -1) {
394     reply_with_error ("%s", err);
395     free (err);
396     return -1;
397   }
398
399   free (err);
400   return 0;
401 }
402
403 int
404 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
405              const char *journal)
406 {
407   char *err;
408   int r;
409
410   char prog[] = "mke2fs";
411   if (e2prog (prog) == -1)
412     return -1;
413
414   char blocksize_s[32];
415   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
416
417   int len = strlen (journal);
418   char jdev[len+32];
419   snprintf (jdev, len+32, "device=%s", journal);
420
421   r = command (NULL, &err,
422                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
423                device, NULL);
424   if (r == -1) {
425     reply_with_error ("%s", err);
426     free (err);
427     return -1;
428   }
429
430   free (err);
431   return 0;
432 }
433
434 int
435 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
436               const char *label)
437 {
438   char *err;
439   int r;
440
441   char prog[] = "mke2fs";
442   if (e2prog (prog) == -1)
443     return -1;
444
445   if (strlen (label) > EXT2_LABEL_MAX) {
446     reply_with_error ("%s: ext2 labels are limited to %d bytes",
447                       label, EXT2_LABEL_MAX);
448     return -1;
449   }
450
451   char blocksize_s[32];
452   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
453
454   int len = strlen (label);
455   char jdev[len+32];
456   snprintf (jdev, len+32, "device=LABEL=%s", label);
457
458   r = command (NULL, &err,
459                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
460                device, NULL);
461   if (r == -1) {
462     reply_with_error ("%s", err);
463     free (err);
464     return -1;
465   }
466
467   free (err);
468   return 0;
469 }
470
471 int
472 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
473               const char *uuid)
474 {
475   char *err;
476   int r;
477
478   char prog[] = "mke2fs";
479   if (e2prog (prog) == -1)
480     return -1;
481
482   char blocksize_s[32];
483   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
484
485   int len = strlen (uuid);
486   char jdev[len+32];
487   snprintf (jdev, len+32, "device=UUID=%s", uuid);
488
489   r = command (NULL, &err,
490                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
491                device, NULL);
492   if (r == -1) {
493     reply_with_error ("%s", err);
494     free (err);
495     return -1;
496   }
497
498   free (err);
499   return 0;
500 }