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.
35 mkfs (const char *fstype, const char *device,
36 const char **extra, size_t nr_extra)
38 const char *argv[MAX_ARGS];
47 /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes
48 * to every block and does bad block detection, neither of which
49 * are useful behaviour for virtual devices.
51 if (STREQ (fstype, "ntfs"))
54 /* mkfs.reiserfs produces annoying interactive prompts unless you
55 * tell it to be quiet.
57 if (STREQ (fstype, "reiserfs"))
61 if (STREQ (fstype, "jfs"))
64 /* For GFS, GFS2, assume a single node. */
65 if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
67 argv[i++] = "lock_nolock";
68 /* The man page says this is default, but it doesn't seem to be: */
71 /* Don't ask questions: */
75 for (j = 0; j < nr_extra; ++j)
84 r = commandv (NULL, &err, argv);
86 reply_with_error ("%s: %s: %s", fstype, device, err);
96 do_mkfs (const char *fstype, const char *device)
98 return mkfs (fstype, device, NULL, 0);
102 do_mkfs_b (const char *fstype, int blocksize, const char *device)
104 const char *extra[2];
107 if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
108 reply_with_error ("block size must be > 0 and a power of 2");
112 if (STREQ (fstype, "vfat") ||
113 STREQ (fstype, "msdos")) {
114 /* For VFAT map the blocksize into a cluster size. However we
115 * have to determine the block device sector size in order to do
118 int sectorsize = do_blockdev_getss (device);
119 if (sectorsize == -1)
122 int sectors_per_cluster = blocksize / sectorsize;
123 if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
124 reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
125 fstype, blocksize, sectorsize, sectors_per_cluster);
129 snprintf (n, sizeof n, "%d", sectors_per_cluster);
133 else if (STREQ (fstype, "ntfs")) {
134 /* For NTFS map the blocksize into a cluster size. */
135 snprintf (n, sizeof n, "%d", blocksize);
140 /* For all other filesystem types, try the -b option. */
141 snprintf (n, sizeof n, "%d", blocksize);
146 return mkfs (fstype, device, extra, 2);