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., 675 Mass Ave, Cambridge, MA 02139, 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";
48 /* For ext2/3/4 run the mke2fs program directly. This is because
49 * the mkfs program "eats" some options, in particular the -F
52 if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
53 STREQ (fstype, "ext4")) {
54 if (e2prog (mke2fs) == -1)
64 /* Force mke2fs to create a filesystem, even if it thinks it
65 * shouldn't (RHBZ#690819).
67 if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
68 STREQ (fstype, "ext4"))
71 /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes
72 * to every block and does bad block detection, neither of which
73 * are useful behaviour for virtual devices.
75 if (STREQ (fstype, "ntfs"))
78 /* mkfs.reiserfs produces annoying interactive prompts unless you
79 * tell it to be quiet.
81 if (STREQ (fstype, "reiserfs"))
85 if (STREQ (fstype, "jfs"))
88 /* For GFS, GFS2, assume a single node. */
89 if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
91 argv[i++] = "lock_nolock";
92 /* The man page says this is default, but it doesn't seem to be: */
95 /* Don't ask questions: */
99 /* Process blocksize parameter if set. */
100 if (optargs_bitmask & GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK) {
101 if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
102 reply_with_error ("block size must be > 0 and a power of 2");
106 if (STREQ (fstype, "vfat") ||
107 STREQ (fstype, "msdos")) {
108 /* For VFAT map the blocksize into a cluster size. However we
109 * have to determine the block device sector size in order to do
112 int sectorsize = do_blockdev_getss (device);
113 if (sectorsize == -1)
116 int sectors_per_cluster = blocksize / sectorsize;
117 if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
118 reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
119 fstype, blocksize, sectorsize, sectors_per_cluster);
123 snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
125 argv[i++] = blocksize_str;
127 else if (STREQ (fstype, "ntfs")) {
128 /* For NTFS map the blocksize into a cluster size. */
129 snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
131 argv[i++] = blocksize_str;
134 /* For all other filesystem types, try the -b option. */
135 snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
137 argv[i++] = blocksize_str;
141 if (optargs_bitmask & GUESTFS_MKFS_OPTS_FEATURES_BITMASK) {
143 argv[i++] = features;
146 if (optargs_bitmask & GUESTFS_MKFS_OPTS_INODE_BITMASK) {
147 if (!STREQ (fstype, "ext2") && !STREQ (fstype, "ext3") &&
148 !STREQ (fstype, "ext4")) {
149 reply_with_error ("inode size (-I) can only be set on ext2/3/4 filesystems");
154 reply_with_error ("inode size must be larger than zero");
158 snprintf (inode_str, sizeof inode_str, "%d", inode);
160 argv[i++] = inode_str;
163 if (optargs_bitmask & GUESTFS_MKFS_OPTS_SECTORSIZE_BITMASK) {
164 if (!STREQ (fstype, "ufs")) {
165 reply_with_error ("sector size (-S) can only be set on ufs filesystems");
169 if (sectorsize <= 0) {
170 reply_with_error ("sector size must be larger than zero");
174 snprintf (sectorsize_str, sizeof sectorsize_str, "%d", sectorsize);
176 argv[i++] = sectorsize_str;
185 r = commandv (NULL, &err, argv);
187 reply_with_error ("%s: %s: %s", fstype, device, err);
197 do_mkfs (const char *fstype, const char *device)
200 return do_mkfs_opts (fstype, device, 0, 0, 0, 0);
204 do_mkfs_b (const char *fstype, int blocksize, const char *device)
206 optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
207 return do_mkfs_opts (fstype, device, blocksize, 0, 0, 0);