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.
26 #include "../src/guestfs_protocol.h"
31 /* Choose which tools like mke2fs to use. For RHEL 5 (only) there
32 * is a special set of tools which support ext2/3/4. eg. On RHEL 5,
33 * mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
35 * We specify e4fsprogs in the package list to ensure it is loaded
41 char *p = strstr (name, "e2");
46 if (prog_exists (name))
50 if (prog_exists (name))
53 reply_with_error ("cannot find required program %s", name);
58 do_tune2fs_l (const char *device)
62 char *p, *pend, *colon;
64 int size = 0, alloc = 0;
66 char prog[] = "tune2fs";
67 if (e2prog (prog) == -1)
70 r = command (&out, &err, prog, "-l", device, NULL);
72 reply_with_error ("%s", err);
81 /* Discard the first line if it contains "tune2fs ...". */
82 if (STRPREFIX (p, "tune2fs ") || STRPREFIX (p, "tune4fs ")) {
86 reply_with_error ("truncated output");
92 /* Read the lines and split into "key: value". */
94 pend = strchrnul (p, '\n');
102 colon = strchr (p, ':');
106 do { colon++; } while (*colon && c_isspace (*colon));
108 if (add_string (&ret, &size, &alloc, p) == -1) {
112 if (STREQ (colon, "<none>") ||
113 STREQ (colon, "<not available>") ||
114 STREQ (colon, "(none)")) {
115 if (add_string (&ret, &size, &alloc, "") == -1) {
120 if (add_string (&ret, &size, &alloc, colon) == -1) {
127 if (add_string (&ret, &size, &alloc, p) == -1) {
131 if (add_string (&ret, &size, &alloc, "") == -1) {
142 if (add_string (&ret, &size, &alloc, NULL) == -1)
149 do_set_e2label (const char *device, const char *label)
154 char prog[] = "e2label";
155 if (e2prog (prog) == -1)
158 r = command (NULL, &err, prog, device, label, NULL);
160 reply_with_error ("%s", err);
170 do_get_e2label (const char *device)
175 char prog[] = "e2label";
176 if (e2prog (prog) == -1)
179 r = command (&out, &err, prog, device, NULL);
181 reply_with_error ("%s", err);
189 /* Remove any trailing \n from the label. */
191 if (len > 0 && out[len-1] == '\n')
194 return out; /* caller frees */
198 do_set_e2uuid (const char *device, const char *uuid)
203 char prog[] = "tune2fs";
204 if (e2prog (prog) == -1)
207 r = command (NULL, &err, prog, "-U", uuid, device, NULL);
209 reply_with_error ("%s", err);
219 do_get_e2uuid (const char *device)
222 char *out, *err, *p, *q;
224 /* It's not so straightforward to get the volume UUID. We have
225 * to use tune2fs -l and then look for a particular string in
228 char prog[] = "tune2fs";
229 if (e2prog (prog) == -1)
232 r = command (&out, &err, prog, "-l", device, NULL);
234 reply_with_error ("%s", err);
242 /* Look for /\nFilesystem UUID:\s+/ in the output. */
243 p = strstr (out, "\nFilesystem UUID:");
245 reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
251 while (*p && c_isspace (*p))
254 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
259 /* Now 'p' hopefully points to the start of the UUID. */
261 while (*q && (c_isxdigit (*q) || *q == '-'))
264 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
273 reply_with_perror ("strdup");
279 return p; /* caller frees */
283 do_resize2fs (const char *device)
288 char prog[] = "resize2fs";
289 if (e2prog (prog) == -1)
292 r = command (NULL, &err, prog, device, NULL);
294 reply_with_error ("%s", err);
304 do_e2fsck_f (const char *device)
309 char prog[] = "e2fsck";
310 if (e2prog (prog) == -1)
313 /* 0 = no errors, 1 = errors corrected.
315 * >= 4 means uncorrected or other errors.
317 * 2, 3 means errors were corrected and we require a reboot. This is
318 * a difficult corner case.
320 r = commandr (NULL, &err, prog, "-p", "-f", device, NULL);
321 if (r == -1 || r >= 2) {
322 reply_with_error ("%s", err);
332 do_mke2journal (int blocksize, const char *device)
337 char prog[] = "mke2fs";
338 if (e2prog (prog) == -1)
341 char blocksize_s[32];
342 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
344 r = command (NULL, &err,
345 prog, "-O", "journal_dev", "-b", blocksize_s,
348 reply_with_error ("%s", err);
358 do_mke2journal_L (int blocksize, const char *label, const char *device)
363 char prog[] = "mke2fs";
364 if (e2prog (prog) == -1)
367 char blocksize_s[32];
368 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
370 r = command (NULL, &err,
371 prog, "-O", "journal_dev", "-b", blocksize_s,
375 reply_with_error ("%s", err);
385 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
390 char prog[] = "mke2fs";
391 if (e2prog (prog) == -1)
394 char blocksize_s[32];
395 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
397 r = command (NULL, &err,
398 prog, "-O", "journal_dev", "-b", blocksize_s,
402 reply_with_error ("%s", err);
412 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
418 char prog[] = "mke2fs";
419 if (e2prog (prog) == -1)
422 char blocksize_s[32];
423 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
425 int len = strlen (journal);
427 snprintf (jdev, len+32, "device=%s", journal);
429 r = command (NULL, &err,
430 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
433 reply_with_error ("%s", err);
443 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
449 char prog[] = "mke2fs";
450 if (e2prog (prog) == -1)
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);