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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "guestfs_protocol.h"
32 /* Confirmed this is true up to ext4 from the Linux sources. */
33 #define EXT2_LABEL_MAX 16
37 /* Choose which tools like mke2fs to use. For RHEL 5 (only) there
38 * is a special set of tools which support ext2/3/4. eg. On RHEL 5,
39 * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
41 * We specify e4fsprogs in the package list to ensure it is loaded
47 char *p = strstr (name, "e2");
52 if (prog_exists (name))
56 if (prog_exists (name))
59 reply_with_error ("cannot find required program %s", name);
64 do_tune2fs_l (const char *device)
68 char *p, *pend, *colon;
70 int size = 0, alloc = 0;
72 char prog[] = "tune2fs";
73 if (e2prog (prog) == -1)
76 r = command (&out, &err, prog, "-l", device, NULL);
78 reply_with_error ("%s", err);
87 /* Discard the first line if it contains "tune2fs ...". */
88 if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
92 reply_with_error ("truncated output");
98 /* Read the lines and split into "key: value". */
100 pend = strchrnul (p, '\n');
108 colon = strchr (p, ':');
112 do { colon++; } while (*colon && c_isspace (*colon));
114 if (add_string (&ret, &size, &alloc, p) == -1) {
118 if (STREQ (colon, "<none>") ||
119 STREQ (colon, "<not available>") ||
120 STREQ (colon, "(none)")) {
121 if (add_string (&ret, &size, &alloc, "") == -1) {
126 if (add_string (&ret, &size, &alloc, colon) == -1) {
133 if (add_string (&ret, &size, &alloc, p) == -1) {
137 if (add_string (&ret, &size, &alloc, "") == -1) {
148 if (add_string (&ret, &size, &alloc, NULL) == -1)
155 do_set_e2label (const char *device, const char *label)
160 char prog[] = "e2label";
161 if (e2prog (prog) == -1)
164 if (strlen (label) > EXT2_LABEL_MAX) {
165 reply_with_error ("%s: ext2 labels are limited to %d bytes",
166 label, EXT2_LABEL_MAX);
170 r = command (NULL, &err, prog, device, label, NULL);
172 reply_with_error ("%s", err);
182 do_get_e2label (const char *device)
184 return do_vfs_label (device);
188 do_set_e2uuid (const char *device, const char *uuid)
193 char prog[] = "tune2fs";
194 if (e2prog (prog) == -1)
197 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
199 reply_with_error ("%s", err);
209 do_get_e2uuid (const char *device)
211 return do_vfs_uuid (device);
215 do_resize2fs (const char *device)
220 char prog[] = "resize2fs";
221 if (e2prog (prog) == -1)
224 r = command (NULL, &err, prog, device, NULL);
226 reply_with_error ("%s", err);
236 do_resize2fs_size (const char *device, int64_t size)
241 char prog[] = "resize2fs";
242 if (e2prog (prog) == -1)
245 /* resize2fs itself may impose additional limits. Since we are
246 * going to use the 'K' suffix however we can only work with whole
250 reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
257 snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
259 r = command (NULL, &err, prog, device, buf, NULL);
261 reply_with_error ("%s", err);
271 do_resize2fs_M (const char *device)
276 char prog[] = "resize2fs";
277 if (e2prog (prog) == -1)
280 r = command (NULL, &err, prog, "-M" , device, NULL);
282 reply_with_error ("%s", err);
292 do_e2fsck_f (const char *device)
297 char prog[] = "e2fsck";
298 if (e2prog (prog) == -1)
301 /* 0 = no errors, 1 = errors corrected.
303 * >= 4 means uncorrected or other errors.
305 * 2, 3 means errors were corrected and we require a reboot. This is
306 * a difficult corner case.
308 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
309 if (r == -1 || r >= 2) {
310 reply_with_error ("%s", err);
320 do_mke2journal (int blocksize, const char *device)
325 char prog[] = "mke2fs";
326 if (e2prog (prog) == -1)
329 char blocksize_s[32];
330 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
332 r = command (NULL, &err,
333 prog, "-O", "journal_dev", "-b", blocksize_s,
336 reply_with_error ("%s", err);
346 do_mke2journal_L (int blocksize, const char *label, const char *device)
351 char prog[] = "mke2fs";
352 if (e2prog (prog) == -1)
355 if (strlen (label) > EXT2_LABEL_MAX) {
356 reply_with_error ("%s: ext2 labels are limited to %d bytes",
357 label, EXT2_LABEL_MAX);
361 char blocksize_s[32];
362 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
364 r = command (NULL, &err,
365 prog, "-O", "journal_dev", "-b", blocksize_s,
369 reply_with_error ("%s", err);
379 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
384 char prog[] = "mke2fs";
385 if (e2prog (prog) == -1)
388 char blocksize_s[32];
389 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
391 r = command (NULL, &err,
392 prog, "-O", "journal_dev", "-b", blocksize_s,
396 reply_with_error ("%s", err);
406 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
412 char prog[] = "mke2fs";
413 if (e2prog (prog) == -1)
416 char blocksize_s[32];
417 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
419 int len = strlen (journal);
421 snprintf (jdev, len+32, "device=%s", journal);
423 r = command (NULL, &err,
424 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
427 reply_with_error ("%s", err);
437 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
443 char prog[] = "mke2fs";
444 if (e2prog (prog) == -1)
447 if (strlen (label) > EXT2_LABEL_MAX) {
448 reply_with_error ("%s: ext2 labels are limited to %d bytes",
449 label, EXT2_LABEL_MAX);
453 char blocksize_s[32];
454 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
456 int len = strlen (label);
458 snprintf (jdev, len+32, "device=LABEL=%s", label);
460 r = command (NULL, &err,
461 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
464 reply_with_error ("%s", err);
474 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
480 char prog[] = "mke2fs";
481 if (e2prog (prog) == -1)
484 char blocksize_s[32];
485 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
487 int len = strlen (uuid);
489 snprintf (jdev, len+32, "device=UUID=%s", uuid);
491 r = command (NULL, &err,
492 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
495 reply_with_error ("%s", err);
504 /* Takes optional arguments, consult optargs_bitmask. */
506 do_tune2fs (const char *device, /* only required parameter */
510 const char *errorbehavior,
512 int intervalbetweenchecks,
513 int reservedblockspercentage,
514 const char *lastmounteddirectory,
515 int64_t reservedblockscount,
518 const char *argv[MAX_ARGS];
522 char prog[] = "tune2fs";
523 char maxmountcount_s[64];
524 char mountcount_s[64];
526 char intervalbetweenchecks_s[64];
527 char reservedblockspercentage_s[64];
528 char reservedblockscount_s[64];
531 if (e2prog (prog) == -1)
534 ADD_ARG (argv, i, prog);
536 if (optargs_bitmask & GUESTFS_TUNE2FS_FORCE_BITMASK) {
538 ADD_ARG (argv, i, "-f");
541 if (optargs_bitmask & GUESTFS_TUNE2FS_MAXMOUNTCOUNT_BITMASK) {
542 if (maxmountcount < 0) {
543 reply_with_error ("maxmountcount cannot be negative");
546 ADD_ARG (argv, i, "-c");
547 snprintf (maxmountcount_s, sizeof maxmountcount_s, "%d", maxmountcount);
548 ADD_ARG (argv, i, maxmountcount_s);
551 if (optargs_bitmask & GUESTFS_TUNE2FS_MOUNTCOUNT_BITMASK) {
552 if (mountcount < 0) {
553 reply_with_error ("mountcount cannot be negative");
556 ADD_ARG (argv, i, "-C");
557 snprintf (mountcount_s, sizeof mountcount_s, "%d", mountcount);
558 ADD_ARG (argv, i, mountcount_s);
561 if (optargs_bitmask & GUESTFS_TUNE2FS_ERRORBEHAVIOR_BITMASK) {
562 if (STRNEQ (errorbehavior, "continue") &&
563 STRNEQ (errorbehavior, "remount-ro") &&
564 STRNEQ (errorbehavior, "panic")) {
565 reply_with_error ("invalid errorbehavior parameter: %s", errorbehavior);
568 ADD_ARG (argv, i, "-e");
569 ADD_ARG (argv, i, errorbehavior);
572 if (optargs_bitmask & GUESTFS_TUNE2FS_GROUP_BITMASK) {
574 reply_with_error ("group cannot be negative");
577 ADD_ARG (argv, i, "-g");
578 snprintf (group_s, sizeof group_s, "%" PRIi64, group);
579 ADD_ARG (argv, i, group_s);
582 if (optargs_bitmask & GUESTFS_TUNE2FS_INTERVALBETWEENCHECKS_BITMASK) {
583 if (intervalbetweenchecks < 0) {
584 reply_with_error ("intervalbetweenchecks cannot be negative");
587 ADD_ARG (argv, i, "-i");
588 if (intervalbetweenchecks > 0) {
589 /* -i <NN>s is not documented in the man page, but has been
590 * supported in tune2fs for several years.
592 snprintf (intervalbetweenchecks_s, sizeof intervalbetweenchecks_s,
593 "%ds", intervalbetweenchecks);
594 ADD_ARG (argv, i, intervalbetweenchecks_s);
597 ADD_ARG (argv, i, "0");
600 if (optargs_bitmask & GUESTFS_TUNE2FS_RESERVEDBLOCKSPERCENTAGE_BITMASK) {
601 if (reservedblockspercentage < 0) {
602 reply_with_error ("reservedblockspercentage cannot be negative");
605 ADD_ARG (argv, i, "-m");
606 snprintf (reservedblockspercentage_s, sizeof reservedblockspercentage_s,
607 "%d", reservedblockspercentage);
608 ADD_ARG (argv, i, reservedblockspercentage_s);
611 if (optargs_bitmask & GUESTFS_TUNE2FS_LASTMOUNTEDDIRECTORY_BITMASK) {
612 ADD_ARG (argv, i, "-M");
613 ADD_ARG (argv, i, lastmounteddirectory);
616 if (optargs_bitmask & GUESTFS_TUNE2FS_RESERVEDBLOCKSCOUNT_BITMASK) {
617 if (reservedblockscount < 0) {
618 reply_with_error ("reservedblockscount cannot be negative");
621 ADD_ARG (argv, i, "-r");
622 snprintf (reservedblockscount_s, sizeof reservedblockscount_s,
623 "%" PRIi64, reservedblockscount);
624 ADD_ARG (argv, i, reservedblockscount_s);
627 if (optargs_bitmask & GUESTFS_TUNE2FS_USER_BITMASK) {
629 reply_with_error ("user cannot be negative");
632 ADD_ARG (argv, i, "-u");
633 snprintf (user_s, sizeof user_s, "%" PRIi64, user);
634 ADD_ARG (argv, i, user_s);
637 ADD_ARG (argv, i, device);
638 ADD_ARG (argv, i, NULL);
640 r = commandv (NULL, &err, argv);
642 reply_with_error ("%s: %s: %s", prog, device, err);