1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
26 #include "../src/guestfs_protocol.h"
31 /* Confirmed this is true up to ext4 from the Linux sources. */
32 #define EXT2_LABEL_MAX 16
34 /* Choose which tools like mke2fs to use. For RHEL 5 (only) there
35 * is a special set of tools which support ext2/3/4. eg. On RHEL 5,
36 * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
38 * We specify e4fsprogs in the package list to ensure it is loaded
44 char *p = strstr (name, "e2");
49 if (prog_exists (name))
53 if (prog_exists (name))
56 reply_with_error ("cannot find required program %s", name);
61 do_tune2fs_l (const char *device)
65 char *p, *pend, *colon;
67 int size = 0, alloc = 0;
69 char prog[] = "tune2fs";
70 if (e2prog (prog) == -1)
73 r = command (&out, &err, prog, "-l", device, NULL);
75 reply_with_error ("%s", err);
84 /* Discard the first line if it contains "tune2fs ...". */
85 if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
89 reply_with_error ("truncated output");
95 /* Read the lines and split into "key: value". */
97 pend = strchrnul (p, '\n');
105 colon = strchr (p, ':');
109 do { colon++; } while (*colon && c_isspace (*colon));
111 if (add_string (&ret, &size, &alloc, p) == -1) {
115 if (STREQ (colon, "<none>") ||
116 STREQ (colon, "<not available>") ||
117 STREQ (colon, "(none)")) {
118 if (add_string (&ret, &size, &alloc, "") == -1) {
123 if (add_string (&ret, &size, &alloc, colon) == -1) {
130 if (add_string (&ret, &size, &alloc, p) == -1) {
134 if (add_string (&ret, &size, &alloc, "") == -1) {
145 if (add_string (&ret, &size, &alloc, NULL) == -1)
152 do_set_e2label (const char *device, const char *label)
157 char prog[] = "e2label";
158 if (e2prog (prog) == -1)
161 if (strlen (label) > EXT2_LABEL_MAX) {
162 reply_with_error ("%s: ext2 labels are limited to %d bytes",
163 label, EXT2_LABEL_MAX);
167 r = command (NULL, &err, prog, device, label, NULL);
169 reply_with_error ("%s", err);
179 do_get_e2label (const char *device)
184 char prog[] = "e2label";
185 if (e2prog (prog) == -1)
188 r = command (&out, &err, prog, device, NULL);
190 reply_with_error ("%s", err);
198 /* Remove any trailing \n from the label. */
200 if (len > 0 && out[len-1] == '\n')
203 return out; /* caller frees */
207 do_set_e2uuid (const char *device, const char *uuid)
212 char prog[] = "tune2fs";
213 if (e2prog (prog) == -1)
216 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
218 reply_with_error ("%s", err);
228 do_get_e2uuid (const char *device)
231 char *out, *err, *p, *q;
233 /* It's not so straightforward to get the volume UUID. We have
234 * to use tune2fs -l and then look for a particular string in
237 char prog[] = "tune2fs";
238 if (e2prog (prog) == -1)
241 r = command (&out, &err, prog, "-l", device, NULL);
243 reply_with_error ("%s", err);
251 /* Look for /\nFilesystem UUID:\s+/ in the output. */
252 p = strstr (out, "\nFilesystem UUID:");
254 reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
260 while (*p && c_isspace (*p))
263 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
268 /* Now 'p' hopefully points to the start of the UUID. */
270 while (*q && (c_isxdigit (*q) || *q == '-'))
273 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
282 reply_with_perror ("strdup");
288 return p; /* caller frees */
292 do_resize2fs (const char *device)
297 char prog[] = "resize2fs";
298 if (e2prog (prog) == -1)
301 r = command (NULL, &err, prog, device, NULL);
303 reply_with_error ("%s", err);
313 do_e2fsck_f (const char *device)
318 char prog[] = "e2fsck";
319 if (e2prog (prog) == -1)
322 /* 0 = no errors, 1 = errors corrected.
324 * >= 4 means uncorrected or other errors.
326 * 2, 3 means errors were corrected and we require a reboot. This is
327 * a difficult corner case.
329 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
330 if (r == -1 || r >= 2) {
331 reply_with_error ("%s", err);
341 do_mke2journal (int blocksize, const char *device)
346 char prog[] = "mke2fs";
347 if (e2prog (prog) == -1)
350 char blocksize_s[32];
351 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
353 r = command (NULL, &err,
354 prog, "-O", "journal_dev", "-b", blocksize_s,
357 reply_with_error ("%s", err);
367 do_mke2journal_L (int blocksize, const char *label, const char *device)
372 char prog[] = "mke2fs";
373 if (e2prog (prog) == -1)
376 if (strlen (label) > EXT2_LABEL_MAX) {
377 reply_with_error ("%s: ext2 labels are limited to %d bytes",
378 label, EXT2_LABEL_MAX);
382 char blocksize_s[32];
383 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
385 r = command (NULL, &err,
386 prog, "-O", "journal_dev", "-b", blocksize_s,
390 reply_with_error ("%s", err);
400 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
405 char prog[] = "mke2fs";
406 if (e2prog (prog) == -1)
409 char blocksize_s[32];
410 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
412 r = command (NULL, &err,
413 prog, "-O", "journal_dev", "-b", blocksize_s,
417 reply_with_error ("%s", err);
427 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
433 char prog[] = "mke2fs";
434 if (e2prog (prog) == -1)
437 char blocksize_s[32];
438 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
440 int len = strlen (journal);
442 snprintf (jdev, len+32, "device=%s", journal);
444 r = command (NULL, &err,
445 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
448 reply_with_error ("%s", err);
458 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
464 char prog[] = "mke2fs";
465 if (e2prog (prog) == -1)
468 if (strlen (label) > EXT2_LABEL_MAX) {
469 reply_with_error ("%s: ext2 labels are limited to %d bytes",
470 label, EXT2_LABEL_MAX);
474 char blocksize_s[32];
475 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
477 int len = strlen (label);
479 snprintf (jdev, len+32, "device=LABEL=%s", label);
481 r = command (NULL, &err,
482 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
485 reply_with_error ("%s", err);
495 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
501 char prog[] = "mke2fs";
502 if (e2prog (prog) == -1)
505 char blocksize_s[32];
506 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
508 int len = strlen (uuid);
510 snprintf (jdev, len+32, "device=UUID=%s", uuid);
512 r = command (NULL, &err,
513 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
516 reply_with_error ("%s", err);