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 "../src/guestfs_protocol.h"
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.
36 * We specify e4fsprogs in the package list to ensure it is loaded
42 char *p = strstr (name, "e2");
47 if (prog_exists (name))
51 if (prog_exists (name))
54 reply_with_error ("cannot find required program %s", name);
59 do_tune2fs_l (const char *device)
63 char *p, *pend, *colon;
65 int size = 0, alloc = 0;
67 char prog[] = "tune2fs";
68 if (e2prog (prog) == -1)
71 r = command (&out, &err, prog, "-l", device, NULL);
73 reply_with_error ("%s", err);
82 /* Discard the first line if it contains "tune2fs ...". */
83 if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
87 reply_with_error ("truncated output");
93 /* Read the lines and split into "key: value". */
95 pend = strchrnul (p, '\n');
103 colon = strchr (p, ':');
107 do { colon++; } while (*colon && c_isspace (*colon));
109 if (add_string (&ret, &size, &alloc, p) == -1) {
113 if (STREQ (colon, "<none>") ||
114 STREQ (colon, "<not available>") ||
115 STREQ (colon, "(none)")) {
116 if (add_string (&ret, &size, &alloc, "") == -1) {
121 if (add_string (&ret, &size, &alloc, colon) == -1) {
128 if (add_string (&ret, &size, &alloc, p) == -1) {
132 if (add_string (&ret, &size, &alloc, "") == -1) {
143 if (add_string (&ret, &size, &alloc, NULL) == -1)
150 do_set_e2label (const char *device, const char *label)
155 char prog[] = "e2label";
156 if (e2prog (prog) == -1)
159 r = command (NULL, &err, prog, device, label, NULL);
161 reply_with_error ("%s", err);
171 do_get_e2label (const char *device)
176 char prog[] = "e2label";
177 if (e2prog (prog) == -1)
180 r = command (&out, &err, prog, device, NULL);
182 reply_with_error ("%s", err);
190 /* Remove any trailing \n from the label. */
192 if (len > 0 && out[len-1] == '\n')
195 return out; /* caller frees */
199 do_set_e2uuid (const char *device, const char *uuid)
204 char prog[] = "tune2fs";
205 if (e2prog (prog) == -1)
208 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
210 reply_with_error ("%s", err);
220 do_get_e2uuid (const char *device)
223 char *out, *err, *p, *q;
225 /* It's not so straightforward to get the volume UUID. We have
226 * to use tune2fs -l and then look for a particular string in
229 char prog[] = "tune2fs";
230 if (e2prog (prog) == -1)
233 r = command (&out, &err, prog, "-l", device, NULL);
235 reply_with_error ("%s", err);
243 /* Look for /\nFilesystem UUID:\s+/ in the output. */
244 p = strstr (out, "\nFilesystem UUID:");
246 reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
252 while (*p && c_isspace (*p))
255 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
260 /* Now 'p' hopefully points to the start of the UUID. */
262 while (*q && (c_isxdigit (*q) || *q == '-'))
265 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
274 reply_with_perror ("strdup");
280 return p; /* caller frees */
284 do_resize2fs (const char *device)
289 char prog[] = "resize2fs";
290 if (e2prog (prog) == -1)
293 r = command (NULL, &err, prog, device, NULL);
295 reply_with_error ("%s", err);
305 do_resize2fs_size (const char *device, int64_t size)
310 char prog[] = "resize2fs";
311 if (e2prog (prog) == -1)
314 /* resize2fs itself may impose additional limits. Since we are
315 * going to use the 'K' suffix however we can only work with whole
319 reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
326 snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
328 r = command (NULL, &err, prog, device, buf, NULL);
330 reply_with_error ("%s", err);
340 do_e2fsck_f (const char *device)
345 char prog[] = "e2fsck";
346 if (e2prog (prog) == -1)
349 /* 0 = no errors, 1 = errors corrected.
351 * >= 4 means uncorrected or other errors.
353 * 2, 3 means errors were corrected and we require a reboot. This is
354 * a difficult corner case.
356 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
357 if (r == -1 || r >= 2) {
358 reply_with_error ("%s", err);
368 do_mke2journal (int blocksize, const char *device)
373 char prog[] = "mke2fs";
374 if (e2prog (prog) == -1)
377 char blocksize_s[32];
378 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
380 r = command (NULL, &err,
381 prog, "-O", "journal_dev", "-b", blocksize_s,
384 reply_with_error ("%s", err);
394 do_mke2journal_L (int blocksize, const char *label, const char *device)
399 char prog[] = "mke2fs";
400 if (e2prog (prog) == -1)
403 char blocksize_s[32];
404 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
406 r = command (NULL, &err,
407 prog, "-O", "journal_dev", "-b", blocksize_s,
411 reply_with_error ("%s", err);
421 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
426 char prog[] = "mke2fs";
427 if (e2prog (prog) == -1)
430 char blocksize_s[32];
431 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
433 r = command (NULL, &err,
434 prog, "-O", "journal_dev", "-b", blocksize_s,
438 reply_with_error ("%s", err);
448 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
454 char prog[] = "mke2fs";
455 if (e2prog (prog) == -1)
458 char blocksize_s[32];
459 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
461 int len = strlen (journal);
463 snprintf (jdev, len+32, "device=%s", journal);
465 r = command (NULL, &err,
466 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
469 reply_with_error ("%s", err);
479 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
485 char prog[] = "mke2fs";
486 if (e2prog (prog) == -1)
489 char blocksize_s[32];
490 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
492 int len = strlen (label);
494 snprintf (jdev, len+32, "device=LABEL=%s", label);
496 r = command (NULL, &err,
497 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
500 reply_with_error ("%s", err);
510 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
516 char prog[] = "mke2fs";
517 if (e2prog (prog) == -1)
520 char blocksize_s[32];
521 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
523 int len = strlen (uuid);
525 snprintf (jdev, len+32, "device=UUID=%s", uuid);
527 r = command (NULL, &err,
528 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
531 reply_with_error ("%s", err);