RHEL 5: Use mke4fs on RHEL 5 as replacement for mke2fs.
[libguestfs.git] / daemon / mkfs.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2011 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28
29 #include "daemon.h"
30 #include "actions.h"
31
32 #define MAX_ARGS 16
33
34 /* Takes optional arguments, consult optargs_bitmask. */
35 int
36 do_mkfs_opts (const char *fstype, const char *device, int blocksize, const char *features)
37 {
38   const char *argv[MAX_ARGS];
39   size_t i = 0;
40   char blocksize_str[32];
41   int r;
42   char *err;
43
44   char mke2fs[] = "mke2fs";
45   if (e2prog (mke2fs) == -1)
46     return -1;
47
48   /* For ext2/3/4 run the mke2fs program directly.  This is because
49    * the mkfs program "eats" some options, in particular the -F
50    * option.
51    */
52   if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
53       STREQ (fstype, "ext4"))
54     argv[i++] = mke2fs;
55   else
56     argv[i++] = "mkfs";
57
58   argv[i++] = "-t";
59   argv[i++] = fstype;
60
61   /* Force mke2fs to create a filesystem, even if it thinks it
62    * shouldn't (RHBZ#690819).
63    */
64   if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
65       STREQ (fstype, "ext4"))
66     argv[i++] = "-F";
67
68   /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes
69    * to every block and does bad block detection, neither of which
70    * are useful behaviour for virtual devices.
71    */
72   if (STREQ (fstype, "ntfs"))
73     argv[i++] = "-Q";
74
75   /* mkfs.reiserfs produces annoying interactive prompts unless you
76    * tell it to be quiet.
77    */
78   if (STREQ (fstype, "reiserfs"))
79     argv[i++] = "-f";
80
81   /* Same for JFS. */
82   if (STREQ (fstype, "jfs"))
83     argv[i++] = "-f";
84
85   /* For GFS, GFS2, assume a single node. */
86   if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
87     argv[i++] = "-p";
88     argv[i++] = "lock_nolock";
89     /* The man page says this is default, but it doesn't seem to be: */
90     argv[i++] = "-j";
91     argv[i++] = "1";
92     /* Don't ask questions: */
93     argv[i++] = "-O";
94   }
95
96   /* Process blocksize parameter if set. */
97   if (optargs_bitmask & GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK) {
98     if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
99       reply_with_error ("block size must be > 0 and a power of 2");
100       return -1;
101     }
102
103     if (STREQ (fstype, "vfat") ||
104         STREQ (fstype, "msdos")) {
105       /* For VFAT map the blocksize into a cluster size.  However we
106        * have to determine the block device sector size in order to do
107        * this.
108        */
109       int sectorsize = do_blockdev_getss (device);
110       if (sectorsize == -1)
111         return -1;
112
113       int sectors_per_cluster = blocksize / sectorsize;
114       if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
115         reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
116                           fstype, blocksize, sectorsize, sectors_per_cluster);
117         return -1;
118       }
119
120       snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
121       argv[i++] = "-s";
122       argv[i++] = blocksize_str;
123     }
124     else if (STREQ (fstype, "ntfs")) {
125       /* For NTFS map the blocksize into a cluster size. */
126       snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
127       argv[i++] = "-c";
128       argv[i++] = blocksize_str;
129     }
130     else {
131       /* For all other filesystem types, try the -b option. */
132       snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
133       argv[i++] = "-b";
134       argv[i++] = blocksize_str;
135     }
136   }
137
138   if (optargs_bitmask & GUESTFS_MKFS_OPTS_FEATURES_BITMASK) {
139      argv[i++] = "-O";
140      argv[i++] = features;
141   }
142
143   argv[i++] = device;
144   argv[i++] = NULL;
145
146   if (i > MAX_ARGS)
147     abort ();
148
149   r = commandv (NULL, &err, argv);
150   if (r == -1) {
151     reply_with_error ("%s: %s: %s", fstype, device, err);
152     free (err);
153     return -1;
154   }
155
156   free (err);
157   return 0;
158 }
159
160 int
161 do_mkfs (const char *fstype, const char *device)
162 {
163   optargs_bitmask = 0;
164   return do_mkfs_opts (fstype, device, 0, 0);
165 }
166
167 int
168 do_mkfs_b (const char *fstype, int blocksize, const char *device)
169 {
170   optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
171   return do_mkfs_opts (fstype, device, blocksize, 0);
172 }