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)
173 return do_vfs_label (device);
177 do_set_e2uuid (const char *device, const char *uuid)
182 char prog[] = "tune2fs";
183 if (e2prog (prog) == -1)
186 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
188 reply_with_error ("%s", err);
198 do_get_e2uuid (const char *device)
200 return do_vfs_uuid (device);
204 do_resize2fs (const char *device)
209 char prog[] = "resize2fs";
210 if (e2prog (prog) == -1)
213 r = command (NULL, &err, prog, device, NULL);
215 reply_with_error ("%s", err);
225 do_resize2fs_size (const char *device, int64_t size)
230 char prog[] = "resize2fs";
231 if (e2prog (prog) == -1)
234 /* resize2fs itself may impose additional limits. Since we are
235 * going to use the 'K' suffix however we can only work with whole
239 reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
246 snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
248 r = command (NULL, &err, prog, device, buf, NULL);
250 reply_with_error ("%s", err);
260 do_e2fsck_f (const char *device)
265 char prog[] = "e2fsck";
266 if (e2prog (prog) == -1)
269 /* 0 = no errors, 1 = errors corrected.
271 * >= 4 means uncorrected or other errors.
273 * 2, 3 means errors were corrected and we require a reboot. This is
274 * a difficult corner case.
276 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
277 if (r == -1 || r >= 2) {
278 reply_with_error ("%s", err);
288 do_mke2journal (int blocksize, const char *device)
293 char prog[] = "mke2fs";
294 if (e2prog (prog) == -1)
297 char blocksize_s[32];
298 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
300 r = command (NULL, &err,
301 prog, "-O", "journal_dev", "-b", blocksize_s,
304 reply_with_error ("%s", err);
314 do_mke2journal_L (int blocksize, const char *label, const char *device)
319 char prog[] = "mke2fs";
320 if (e2prog (prog) == -1)
323 char blocksize_s[32];
324 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
326 r = command (NULL, &err,
327 prog, "-O", "journal_dev", "-b", blocksize_s,
331 reply_with_error ("%s", err);
341 do_mke2journal_U (int blocksize, const char *uuid, 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,
358 reply_with_error ("%s", err);
368 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
374 char prog[] = "mke2fs";
375 if (e2prog (prog) == -1)
378 char blocksize_s[32];
379 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
381 int len = strlen (journal);
383 snprintf (jdev, len+32, "device=%s", journal);
385 r = command (NULL, &err,
386 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
389 reply_with_error ("%s", err);
399 do_mke2fs_JL (const char *fstype, int blocksize, 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 int len = strlen (label);
414 snprintf (jdev, len+32, "device=LABEL=%s", label);
416 r = command (NULL, &err,
417 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
420 reply_with_error ("%s", err);
430 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
436 char prog[] = "mke2fs";
437 if (e2prog (prog) == -1)
440 char blocksize_s[32];
441 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
443 int len = strlen (uuid);
445 snprintf (jdev, len+32, "device=UUID=%s", uuid);
447 r = command (NULL, &err,
448 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
451 reply_with_error ("%s", err);