New API: pread-device, partial read for devices.
[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., 675 Mass Ave, Cambridge, MA 02139, 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 "../src/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 static 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_e2fsck_f (const char *device)
270 {
271   char *err;
272   int r;
273
274   char prog[] = "e2fsck";
275   if (e2prog (prog) == -1)
276     return -1;
277
278   /* 0 = no errors, 1 = errors corrected.
279    *
280    * >= 4 means uncorrected or other errors.
281    *
282    * 2, 3 means errors were corrected and we require a reboot.  This is
283    * a difficult corner case.
284    */
285   r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
286   if (r == -1 || r >= 2) {
287     reply_with_error ("%s", err);
288     free (err);
289     return -1;
290   }
291
292   free (err);
293   return 0;
294 }
295
296 int
297 do_mke2journal (int blocksize, const char *device)
298 {
299   char *err;
300   int r;
301
302   char prog[] = "mke2fs";
303   if (e2prog (prog) == -1)
304     return -1;
305
306   char blocksize_s[32];
307   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
308
309   r = command (NULL, &err,
310                prog, "-O", "journal_dev", "-b", blocksize_s,
311                device, NULL);
312   if (r == -1) {
313     reply_with_error ("%s", err);
314     free (err);
315     return -1;
316   }
317
318   free (err);
319   return 0;
320 }
321
322 int
323 do_mke2journal_L (int blocksize, const char *label, const char *device)
324 {
325   char *err;
326   int r;
327
328   char prog[] = "mke2fs";
329   if (e2prog (prog) == -1)
330     return -1;
331
332   if (strlen (label) > EXT2_LABEL_MAX) {
333     reply_with_error ("%s: ext2 labels are limited to %d bytes",
334                       label, EXT2_LABEL_MAX);
335     return -1;
336   }
337
338   char blocksize_s[32];
339   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
340
341   r = command (NULL, &err,
342                prog, "-O", "journal_dev", "-b", blocksize_s,
343                "-L", label,
344                device, NULL);
345   if (r == -1) {
346     reply_with_error ("%s", err);
347     free (err);
348     return -1;
349   }
350
351   free (err);
352   return 0;
353 }
354
355 int
356 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
357 {
358   char *err;
359   int r;
360
361   char prog[] = "mke2fs";
362   if (e2prog (prog) == -1)
363     return -1;
364
365   char blocksize_s[32];
366   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
367
368   r = command (NULL, &err,
369                prog, "-O", "journal_dev", "-b", blocksize_s,
370                "-U", uuid,
371                device, NULL);
372   if (r == -1) {
373     reply_with_error ("%s", err);
374     free (err);
375     return -1;
376   }
377
378   free (err);
379   return 0;
380 }
381
382 int
383 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
384              const char *journal)
385 {
386   char *err;
387   int r;
388
389   char prog[] = "mke2fs";
390   if (e2prog (prog) == -1)
391     return -1;
392
393   char blocksize_s[32];
394   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
395
396   int len = strlen (journal);
397   char jdev[len+32];
398   snprintf (jdev, len+32, "device=%s", journal);
399
400   r = command (NULL, &err,
401                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
402                device, NULL);
403   if (r == -1) {
404     reply_with_error ("%s", err);
405     free (err);
406     return -1;
407   }
408
409   free (err);
410   return 0;
411 }
412
413 int
414 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
415               const char *label)
416 {
417   char *err;
418   int r;
419
420   char prog[] = "mke2fs";
421   if (e2prog (prog) == -1)
422     return -1;
423
424   if (strlen (label) > EXT2_LABEL_MAX) {
425     reply_with_error ("%s: ext2 labels are limited to %d bytes",
426                       label, EXT2_LABEL_MAX);
427     return -1;
428   }
429
430   char blocksize_s[32];
431   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
432
433   int len = strlen (label);
434   char jdev[len+32];
435   snprintf (jdev, len+32, "device=LABEL=%s", label);
436
437   r = command (NULL, &err,
438                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
439                device, NULL);
440   if (r == -1) {
441     reply_with_error ("%s", err);
442     free (err);
443     return -1;
444   }
445
446   free (err);
447   return 0;
448 }
449
450 int
451 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
452               const char *uuid)
453 {
454   char *err;
455   int r;
456
457   char prog[] = "mke2fs";
458   if (e2prog (prog) == -1)
459     return -1;
460
461   char blocksize_s[32];
462   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
463
464   int len = strlen (uuid);
465   char jdev[len+32];
466   snprintf (jdev, len+32, "device=UUID=%s", uuid);
467
468   r = command (NULL, &err,
469                prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
470                device, NULL);
471   if (r == -1) {
472     reply_with_error ("%s", err);
473     free (err);
474     return -1;
475   }
476
477   free (err);
478   return 0;
479 }