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 (access (name, X_OK) == 0)
50 if (access (name, X_OK) == 0)
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[] = "/sbin/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[] = "/sbin/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[] = "/sbin/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[] = "/sbin/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[] = "/sbin/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[] = "/sbin/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[] = "/sbin/e2fsck";
310 if (e2prog (prog) == -1)
313 r = command (NULL, &err, prog, "-p", "-f", device, NULL);
315 reply_with_error ("%s", err);
325 do_mke2journal (int blocksize, const char *device)
330 char prog[] = "/sbin/mke2fs";
331 if (e2prog (prog) == -1)
334 char blocksize_s[32];
335 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
337 r = command (NULL, &err,
338 prog, "-O", "journal_dev", "-b", blocksize_s,
341 reply_with_error ("%s", err);
351 do_mke2journal_L (int blocksize, const char *label, const char *device)
356 char prog[] = "/sbin/mke2fs";
357 if (e2prog (prog) == -1)
360 char blocksize_s[32];
361 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
363 r = command (NULL, &err,
364 prog, "-O", "journal_dev", "-b", blocksize_s,
368 reply_with_error ("%s", err);
378 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
383 char prog[] = "/sbin/mke2fs";
384 if (e2prog (prog) == -1)
387 char blocksize_s[32];
388 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
390 r = command (NULL, &err,
391 prog, "-O", "journal_dev", "-b", blocksize_s,
395 reply_with_error ("%s", err);
405 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
411 char prog[] = "/sbin/mke2fs";
412 if (e2prog (prog) == -1)
415 char blocksize_s[32];
416 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
418 int len = strlen (journal);
420 snprintf (jdev, len+32, "device=%s", journal);
422 r = command (NULL, &err,
423 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
426 reply_with_error ("%s", err);
436 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
442 char prog[] = "/sbin/mke2fs";
443 if (e2prog (prog) == -1)
446 char blocksize_s[32];
447 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
449 int len = strlen (label);
451 snprintf (jdev, len+32, "device=LABEL=%s", label);
453 r = command (NULL, &err,
454 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
457 reply_with_error ("%s", err);
467 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
473 char prog[] = "/sbin/mke2fs";
474 if (e2prog (prog) == -1)
477 char blocksize_s[32];
478 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
480 int len = strlen (uuid);
482 snprintf (jdev, len+32, "device=UUID=%s", uuid);
484 r = command (NULL, &err,
485 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
488 reply_with_error ("%s", err);