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