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