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"
32 do_tune2fs_l (const char *device)
36 char *p, *pend, *colon;
38 int size = 0, alloc = 0;
40 r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
42 reply_with_error ("tune2fs: %s", err);
51 /* Discard the first line if it contains "tune2fs ...". */
52 if (STREQLEN (p, "tune2fs ", 8)) {
56 reply_with_error ("tune2fs: truncated output");
62 /* Read the lines and split into "key: value". */
64 pend = strchrnul (p, '\n');
72 colon = strchr (p, ':');
76 do { colon++; } while (*colon && c_isspace (*colon));
78 if (add_string (&ret, &size, &alloc, p) == -1) {
82 if (STREQ (colon, "<none>") ||
83 STREQ (colon, "<not available>") ||
84 STREQ (colon, "(none)")) {
85 if (add_string (&ret, &size, &alloc, "") == -1) {
90 if (add_string (&ret, &size, &alloc, colon) == -1) {
97 if (add_string (&ret, &size, &alloc, p) == -1) {
101 if (add_string (&ret, &size, &alloc, "") == -1) {
112 if (add_string (&ret, &size, &alloc, NULL) == -1)
119 do_set_e2label (const char *device, const char *label)
124 r = command (NULL, &err, "/sbin/e2label", device, label, NULL);
126 reply_with_error ("e2label: %s", err);
136 do_get_e2label (const char *device)
141 r = command (&out, &err, "/sbin/e2label", device, NULL);
143 reply_with_error ("e2label: %s", err);
151 /* Remove any trailing \n from the label. */
153 if (len > 0 && out[len-1] == '\n')
156 return out; /* caller frees */
160 do_set_e2uuid (const char *device, const char *uuid)
165 r = command (NULL, &err, "/sbin/tune2fs", "-U", uuid, device, NULL);
167 reply_with_error ("tune2fs -U: %s", err);
177 do_get_e2uuid (const char *device)
180 char *out, *err, *p, *q;
182 /* It's not so straightforward to get the volume UUID. We have
183 * to use tune2fs -l and then look for a particular string in
187 r = command (&out, &err, "/sbin/tune2fs", "-l", device, NULL);
189 reply_with_error ("tune2fs -l: %s", err);
197 /* Look for /\nFilesystem UUID:\s+/ in the output. */
198 p = strstr (out, "\nFilesystem UUID:");
200 reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
206 while (*p && c_isspace (*p))
209 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
214 /* Now 'p' hopefully points to the start of the UUID. */
216 while (*q && (c_isxdigit (*q) || *q == '-'))
219 reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
228 reply_with_perror ("strdup");
234 return p; /* caller frees */
238 do_resize2fs (const char *device)
243 r = command (NULL, &err, "/sbin/resize2fs", device, NULL);
245 reply_with_error ("resize2fs: %s", err);
255 do_e2fsck_f (const char *device)
260 r = command (NULL, &err, "/sbin/e2fsck", "-p", "-f", device, NULL);
262 reply_with_error ("e2fsck: %s", err);
272 do_mke2journal (int blocksize, const char *device)
277 char blocksize_s[32];
278 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
280 r = command (NULL, &err,
281 "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
284 reply_with_error ("mke2journal: %s", err);
294 do_mke2journal_L (int blocksize, const char *label, const char *device)
299 char blocksize_s[32];
300 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
302 r = command (NULL, &err,
303 "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
307 reply_with_error ("mke2journal_L: %s", err);
317 do_mke2journal_U (int blocksize, const char *uuid, const char *device)
322 char blocksize_s[32];
323 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
325 r = command (NULL, &err,
326 "/sbin/mke2fs", "-O", "journal_dev", "-b", blocksize_s,
330 reply_with_error ("mke2journal_U: %s", err);
339 /* Run mke2fs to create a filesystem of type fstype, where fstype
340 * is the string "ext2", "ext3" or "ext4".
342 * This is more complex than it seems.
344 * On RHEL 5, the -t option was deprecated. Moreover RHEL <= 5.4
345 * systems have a bug where the -t option doesn't work (it doesn't
346 * correctly ignore the following argument).
348 * On RHEL 5, to create an ext4dev filesystem you have to use
349 * the special command /sbin/mke4fs. This can also create ext2/3
350 * using the '-t fstype' option.
352 * On Fedora 11+, mke4fs was renamed mke2fs, and it can use the
353 * '-t fstype' option to specify the filesystem type.
355 * So it seems best to run /sbin/mke4fs if it exists, or /sbin/mke2fs
356 * otherwise. We specify e4fsprogs in the package list to ensure it
357 * is loaded if it exists.
362 static const char *const progs[] = { "/sbin/mke4fs", "/sbin/mke2fs", NULL };
365 for (i = 0; progs[i]; ++i)
366 if (access (progs[i], F_OK) == 0)
369 reply_with_error ("mke2fs: no mke2fs binary found in appliance");
374 do_mke2fs_J (const char *fstype, int blocksize, const char *device,
380 const char *prog = get_mke2fs ();
381 if (!prog) return -1;
383 char blocksize_s[32];
384 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
386 int len = strlen (journal);
388 snprintf (jdev, len+32, "device=%s", journal);
390 r = command (NULL, &err,
391 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
394 reply_with_error ("mke2fs_J: %s", err);
404 do_mke2fs_JL (const char *fstype, int blocksize, const char *device,
410 const char *prog = get_mke2fs ();
411 if (!prog) return -1;
413 char blocksize_s[32];
414 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
416 int len = strlen (label);
418 snprintf (jdev, len+32, "device=LABEL=%s", label);
420 r = command (NULL, &err,
421 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
424 reply_with_error ("mke2fs_JL: %s", err);
434 do_mke2fs_JU (const char *fstype, int blocksize, const char *device,
440 const char *prog = get_mke2fs ();
441 if (!prog) return -1;
443 char blocksize_s[32];
444 snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
446 int len = strlen (uuid);
448 snprintf (jdev, len+32, "device=UUID=%s", uuid);
450 r = command (NULL, &err,
451 prog, "-t", fstype, "-J", jdev, "-b", blocksize_s,
454 reply_with_error ("mke2fs_JU: %s", err);