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_resize2fs_M (const char *device)
274 char prog[] = "resize2fs";
275 if (e2prog (prog) == -1)
278 r = command (NULL, &err, prog, "-M" , device, NULL);
280 reply_with_error ("%s", err);
290 do_e2fsck_f (const char *device)
295 char prog[] = "e2fsck";
296 if (e2prog (prog) == -1)
299 /* 0 = no errors, 1 = errors corrected.
301 * >= 4 means uncorrected or other errors.
303 * 2, 3 means errors were corrected and we require a reboot. This is
304 * a difficult corner case.
306 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
307 if (r == -1 || r >= 2) {
308 reply_with_error ("%s", err);
318 do_mke2journal (int blocksize, const char *device)
323 char prog[] = "mke2fs";
324 if (e2prog (prog) == -1)
327 char blocksize_s[32];
328 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
330 r = command (NULL, &err,
331 prog, "-O", "journal_dev", "-b", blocksize_s,
334 reply_with_error ("%s", err);
344 do_mke2journal_L (int blocksize, const char *label, const char *device)
349 char prog[] = "mke2fs";
350 if (e2prog (prog) == -1)
353 if (strlen (label) > EXT2_LABEL_MAX) {
354 reply_with_error ("%s: ext2 labels are limited to %d bytes",
355 label, EXT2_LABEL_MAX);
359 char blocksize_s[32];
360 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
362 r = command (NULL, &err,
363 prog, "-O", "journal_dev", "-b", blocksize_s,
367 reply_with_error ("%s", err);
377 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
382 char prog[] = "mke2fs";
383 if (e2prog (prog) == -1)
386 char blocksize_s[32];
387 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
389 r = command (NULL, &err,
390 prog, "-O", "journal_dev", "-b", blocksize_s,
394 reply_with_error ("%s", err);
404 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
410 char prog[] = "mke2fs";
411 if (e2prog (prog) == -1)
414 char blocksize_s[32];
415 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
417 int len = strlen (journal);
419 snprintf (jdev, len+32, "device=%s", journal);
421 r = command (NULL, &err,
422 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
425 reply_with_error ("%s", err);
435 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
441 char prog[] = "mke2fs";
442 if (e2prog (prog) == -1)
445 if (strlen (label) > EXT2_LABEL_MAX) {
446 reply_with_error ("%s: ext2 labels are limited to %d bytes",
447 label, EXT2_LABEL_MAX);
451 char blocksize_s[32];
452 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
454 int len = strlen (label);
456 snprintf (jdev, len+32, "device=LABEL=%s", label);
458 r = command (NULL, &err,
459 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
462 reply_with_error ("%s", err);
472 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
478 char prog[] = "mke2fs";
479 if (e2prog (prog) == -1)
482 char blocksize_s[32];
483 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
485 int len = strlen (uuid);
487 snprintf (jdev, len+32, "device=UUID=%s", uuid);
489 r = command (NULL, &err,
490 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
493 reply_with_error ("%s", err);