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.
27 #include "guestfs_protocol.h"
32 /* Confirmed this is true up to ext4 from the Linux sources. */
33 #define EXT2_LABEL_MAX 16
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.
39 * We specify e4fsprogs in the package list to ensure it is loaded
45 char *p = strstr (name, "e2");
50 if (prog_exists (name))
54 if (prog_exists (name))
57 reply_with_error ("cannot find required program %s", name);
62 do_tune2fs_l (const char *device)
66 char *p, *pend, *colon;
68 int size = 0, alloc = 0;
70 char prog[] = "tune2fs";
71 if (e2prog (prog) == -1)
74 r = command (&out, &err, prog, "-l", device, NULL);
76 reply_with_error ("%s", err);
85 /* Discard the first line if it contains "tune2fs ...". */
86 if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
90 reply_with_error ("truncated output");
96 /* Read the lines and split into "key: value". */
98 pend = strchrnul (p, '\n');
106 colon = strchr (p, ':');
110 do { colon++; } while (*colon && c_isspace (*colon));
112 if (add_string (&ret, &size, &alloc, p) == -1) {
116 if (STREQ (colon, "<none>") ||
117 STREQ (colon, "<not available>") ||
118 STREQ (colon, "(none)")) {
119 if (add_string (&ret, &size, &alloc, "") == -1) {
124 if (add_string (&ret, &size, &alloc, colon) == -1) {
131 if (add_string (&ret, &size, &alloc, p) == -1) {
135 if (add_string (&ret, &size, &alloc, "") == -1) {
146 if (add_string (&ret, &size, &alloc, NULL) == -1)
153 do_set_e2label (const char *device, const char *label)
158 char prog[] = "e2label";
159 if (e2prog (prog) == -1)
162 if (strlen (label) > EXT2_LABEL_MAX) {
163 reply_with_error ("%s: ext2 labels are limited to %d bytes",
164 label, EXT2_LABEL_MAX);
168 r = command (NULL, &err, prog, device, label, NULL);
170 reply_with_error ("%s", err);
180 do_get_e2label (const char *device)
182 return do_vfs_label (device);
186 do_set_e2uuid (const char *device, const char *uuid)
191 char prog[] = "tune2fs";
192 if (e2prog (prog) == -1)
195 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
197 reply_with_error ("%s", err);
207 do_get_e2uuid (const char *device)
209 return do_vfs_uuid (device);
213 do_resize2fs (const char *device)
218 char prog[] = "resize2fs";
219 if (e2prog (prog) == -1)
222 r = command (NULL, &err, prog, device, NULL);
224 reply_with_error ("%s", err);
234 do_resize2fs_size (const char *device, int64_t size)
239 char prog[] = "resize2fs";
240 if (e2prog (prog) == -1)
243 /* resize2fs itself may impose additional limits. Since we are
244 * going to use the 'K' suffix however we can only work with whole
248 reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
255 snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
257 r = command (NULL, &err, prog, device, buf, NULL);
259 reply_with_error ("%s", err);
269 do_e2fsck_f (const char *device)
274 char prog[] = "e2fsck";
275 if (e2prog (prog) == -1)
278 /* 0 = no errors, 1 = errors corrected.
280 * >= 4 means uncorrected or other errors.
282 * 2, 3 means errors were corrected and we require a reboot. This is
283 * a difficult corner case.
285 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
286 if (r == -1 || r >= 2) {
287 reply_with_error ("%s", err);
297 do_mke2journal (int blocksize, const char *device)
302 char prog[] = "mke2fs";
303 if (e2prog (prog) == -1)
306 char blocksize_s[32];
307 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
309 r = command (NULL, &err,
310 prog, "-O", "journal_dev", "-b", blocksize_s,
313 reply_with_error ("%s", err);
323 do_mke2journal_L (int blocksize, const char *label, const char *device)
328 char prog[] = "mke2fs";
329 if (e2prog (prog) == -1)
332 if (strlen (label) > EXT2_LABEL_MAX) {
333 reply_with_error ("%s: ext2 labels are limited to %d bytes",
334 label, EXT2_LABEL_MAX);
338 char blocksize_s[32];
339 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
341 r = command (NULL, &err,
342 prog, "-O", "journal_dev", "-b", blocksize_s,
346 reply_with_error ("%s", err);
356 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
361 char prog[] = "mke2fs";
362 if (e2prog (prog) == -1)
365 char blocksize_s[32];
366 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
368 r = command (NULL, &err,
369 prog, "-O", "journal_dev", "-b", blocksize_s,
373 reply_with_error ("%s", err);
383 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
389 char prog[] = "mke2fs";
390 if (e2prog (prog) == -1)
393 char blocksize_s[32];
394 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
396 int len = strlen (journal);
398 snprintf (jdev, len+32, "device=%s", journal);
400 r = command (NULL, &err,
401 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
404 reply_with_error ("%s", err);
414 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
420 char prog[] = "mke2fs";
421 if (e2prog (prog) == -1)
424 if (strlen (label) > EXT2_LABEL_MAX) {
425 reply_with_error ("%s: ext2 labels are limited to %d bytes",
426 label, EXT2_LABEL_MAX);
430 char blocksize_s[32];
431 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
433 int len = strlen (label);
435 snprintf (jdev, len+32, "device=LABEL=%s", label);
437 r = command (NULL, &err,
438 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
441 reply_with_error ("%s", err);
451 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
457 char prog[] = "mke2fs";
458 if (e2prog (prog) == -1)
461 char blocksize_s[32];
462 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
464 int len = strlen (uuid);
466 snprintf (jdev, len+32, "device=UUID=%s", uuid);
468 r = command (NULL, &err,
469 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
472 reply_with_error ("%s", err);