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