daemon: Define safe ADD_ARG macro for constructing arg lists on the stack.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 64
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, int sectorsize)
38 {
39   const char *argv[MAX_ARGS];
40   size_t i = 0;
41   char blocksize_str[32];
42   char inode_str[32];
43   char sectorsize_str[32];
44   int r;
45   char *err;
46   char mke2fs[] = "mke2fs";
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     if (e2prog (mke2fs) == -1)
55       return -1;
56     ADD_ARG (argv, i, mke2fs);
57   }
58   else
59     ADD_ARG (argv, i, "mkfs");
60
61   ADD_ARG (argv, i, "-t");
62   ADD_ARG (argv, i, fstype);
63
64   /* Force mke2fs to create a filesystem, even if it thinks it
65    * shouldn't (RHBZ#690819).
66    */
67   if (STREQ (fstype, "ext2") || STREQ (fstype, "ext3") ||
68       STREQ (fstype, "ext4"))
69     ADD_ARG (argv, i, "-F");
70
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.
74    */
75   if (STREQ (fstype, "ntfs"))
76     ADD_ARG (argv, i, "-Q");
77
78   /* mkfs.reiserfs produces annoying interactive prompts unless you
79    * tell it to be quiet.
80    */
81   if (STREQ (fstype, "reiserfs"))
82     ADD_ARG (argv, i, "-f");
83
84   /* Same for JFS. */
85   if (STREQ (fstype, "jfs"))
86     ADD_ARG (argv, i, "-f");
87
88   /* For GFS, GFS2, assume a single node. */
89   if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
90     ADD_ARG (argv, i, "-p");
91     ADD_ARG (argv, i, "lock_nolock");
92     /* The man page says this is default, but it doesn't seem to be: */
93     ADD_ARG (argv, i, "-j");
94     ADD_ARG (argv, i, "1");
95     /* Don't ask questions: */
96     ADD_ARG (argv, i, "-O");
97   }
98
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");
103       return -1;
104     }
105
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
110        * this.
111        */
112       int sectorsize = do_blockdev_getss (device);
113       if (sectorsize == -1)
114         return -1;
115
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);
120         return -1;
121       }
122
123       snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
124       ADD_ARG (argv, i, "-s");
125       ADD_ARG (argv, i, blocksize_str);
126     }
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);
130       ADD_ARG (argv, i, "-c");
131       ADD_ARG (argv, i, blocksize_str);
132     }
133     else {
134       /* For all other filesystem types, try the -b option. */
135       snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
136       ADD_ARG (argv, i, "-b");
137       ADD_ARG (argv, i, blocksize_str);
138     }
139   }
140
141   if (optargs_bitmask & GUESTFS_MKFS_OPTS_FEATURES_BITMASK) {
142     ADD_ARG (argv, i, "-O");
143     ADD_ARG (argv, i, features);
144   }
145
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");
150       return -1;
151     }
152
153     if (inode <= 0) {
154       reply_with_error ("inode size must be larger than zero");
155       return -1;
156     }
157
158     snprintf (inode_str, sizeof inode_str, "%d", inode);
159     ADD_ARG (argv, i, "-I");
160     ADD_ARG (argv, i, inode_str);
161   }
162
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");
166       return -1;
167     }
168
169     if (sectorsize <= 0) {
170       reply_with_error ("sector size must be larger than zero");
171       return -1;
172     }
173
174     snprintf (sectorsize_str, sizeof sectorsize_str, "%d", sectorsize);
175     ADD_ARG (argv, i, "-S");
176     ADD_ARG (argv, i, sectorsize_str);
177   }
178
179   ADD_ARG (argv, i, device);
180   ADD_ARG (argv, i, NULL);
181
182   r = commandv (NULL, &err, argv);
183   if (r == -1) {
184     reply_with_error ("%s: %s: %s", fstype, device, err);
185     free (err);
186     return -1;
187   }
188
189   free (err);
190   return 0;
191 }
192
193 int
194 do_mkfs (const char *fstype, const char *device)
195 {
196   optargs_bitmask = 0;
197   return do_mkfs_opts (fstype, device, 0, 0, 0, 0);
198 }
199
200 int
201 do_mkfs_b (const char *fstype, int blocksize, const char *device)
202 {
203   optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
204   return do_mkfs_opts (fstype, device, blocksize, 0, 0, 0);
205 }