1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 /* Takes optional arguments, consult optargs_bitmask. */
36 do_mkfs_opts (const char *fstype, const char *device, int blocksize,
37 const char *features, int inode, int sectorsize)
39 const char *argv[MAX_ARGS];
41 char blocksize_str[32];
43 char sectorsize_str[32];
46 char mke2fs[] = "mke2fs";
49 if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
50 STREQ (fstype, "ext4"))
53 /* For ext2/3/4 run the mke2fs program directly. This is because
54 * the mkfs program "eats" some options, in particular the -F
58 if (e2prog (mke2fs) == -1)
60 ADD_ARG (argv, i, mke2fs);
63 ADD_ARG (argv, i, "mkfs");
65 ADD_ARG (argv, i, "-t");
66 ADD_ARG (argv, i, fstype);
68 /* Force mke2fs to create a filesystem, even if it thinks it
69 * shouldn't (RHBZ#690819).
72 ADD_ARG (argv, i, "-F");
74 /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes
75 * to every block and does bad block detection, neither of which
76 * are useful behaviour for virtual devices.
78 if (STREQ (fstype, "ntfs"))
79 ADD_ARG (argv, i, "-Q");
81 /* mkfs.reiserfs produces annoying interactive prompts unless you
82 * tell it to be quiet.
83 * mkfs.jfs is the same
84 * mkfs.xfs must force to make xfs filesystem when the device already
85 * has a filesystem on it
87 if (STREQ (fstype, "reiserfs") || STREQ (fstype, "jfs") ||
88 STREQ (fstype, "xfs"))
89 ADD_ARG(argv, i, "-f");
91 /* For GFS, GFS2, assume a single node. */
92 if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
93 ADD_ARG (argv, i, "-p");
94 ADD_ARG (argv, i, "lock_nolock");
95 /* The man page says this is default, but it doesn't seem to be: */
96 ADD_ARG (argv, i, "-j");
97 ADD_ARG (argv, i, "1");
98 /* Don't ask questions: */
99 ADD_ARG (argv, i, "-O");
102 /* Process blocksize parameter if set. */
103 if (optargs_bitmask & GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK) {
104 if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
105 reply_with_error ("block size must be > 0 and a power of 2");
109 if (STREQ (fstype, "vfat") ||
110 STREQ (fstype, "msdos")) {
111 /* For VFAT map the blocksize into a cluster size. However we
112 * have to determine the block device sector size in order to do
115 int sectorsize = do_blockdev_getss (device);
116 if (sectorsize == -1)
119 int sectors_per_cluster = blocksize / sectorsize;
120 if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
121 reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
122 fstype, blocksize, sectorsize, sectors_per_cluster);
126 snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
127 ADD_ARG (argv, i, "-s");
128 ADD_ARG (argv, i, blocksize_str);
130 else if (STREQ (fstype, "ntfs")) {
131 /* For NTFS map the blocksize into a cluster size. */
132 snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
133 ADD_ARG (argv, i, "-c");
134 ADD_ARG (argv, i, blocksize_str);
137 /* For all other filesystem types, try the -b option. */
138 snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
139 ADD_ARG (argv, i, "-b");
140 ADD_ARG (argv, i, blocksize_str);
144 if (optargs_bitmask & GUESTFS_MKFS_OPTS_FEATURES_BITMASK) {
145 ADD_ARG (argv, i, "-O");
146 ADD_ARG (argv, i, features);
149 if (optargs_bitmask & GUESTFS_MKFS_OPTS_INODE_BITMASK) {
151 reply_with_error ("inode size (-I) can only be set on ext2/3/4 filesystems");
156 reply_with_error ("inode size must be larger than zero");
160 snprintf (inode_str, sizeof inode_str, "%d", inode);
161 ADD_ARG (argv, i, "-I");
162 ADD_ARG (argv, i, inode_str);
165 if (optargs_bitmask & GUESTFS_MKFS_OPTS_SECTORSIZE_BITMASK) {
166 if (!STREQ (fstype, "ufs")) {
167 reply_with_error ("sector size (-S) can only be set on ufs filesystems");
171 if (sectorsize <= 0) {
172 reply_with_error ("sector size must be larger than zero");
176 snprintf (sectorsize_str, sizeof sectorsize_str, "%d", sectorsize);
177 ADD_ARG (argv, i, "-S");
178 ADD_ARG (argv, i, sectorsize_str);
181 ADD_ARG (argv, i, device);
182 ADD_ARG (argv, i, NULL);
184 r = commandv (NULL, &err, argv);
186 reply_with_error ("%s: %s: %s", fstype, device, err);
196 do_mkfs (const char *fstype, const char *device)
199 return do_mkfs_opts (fstype, device, 0, 0, 0, 0);
203 do_mkfs_b (const char *fstype, int blocksize, const char *device)
205 optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
206 return do_mkfs_opts (fstype, device, blocksize, 0, 0, 0);