Remove several unused local variables.
[libguestfs.git] / daemon / mkfs.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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 {
38   const char *argv[MAX_ARGS];
39   size_t i = 0;
40   char blocksize_str[32];
41   int r;
42   char *err;
43
44   argv[i++] = "mkfs";
45   argv[i++] = "-t";
46   argv[i++] = fstype;
47
48   /* mkfs.ntfs requires the -Q argument otherwise it writes zeroes
49    * to every block and does bad block detection, neither of which
50    * are useful behaviour for virtual devices.
51    */
52   if (STREQ (fstype, "ntfs"))
53     argv[i++] = "-Q";
54
55   /* mkfs.reiserfs produces annoying interactive prompts unless you
56    * tell it to be quiet.
57    */
58   if (STREQ (fstype, "reiserfs"))
59     argv[i++] = "-f";
60
61   /* Same for JFS. */
62   if (STREQ (fstype, "jfs"))
63     argv[i++] = "-f";
64
65   /* For GFS, GFS2, assume a single node. */
66   if (STREQ (fstype, "gfs") || STREQ (fstype, "gfs2")) {
67     argv[i++] = "-p";
68     argv[i++] = "lock_nolock";
69     /* The man page says this is default, but it doesn't seem to be: */
70     argv[i++] = "-j";
71     argv[i++] = "1";
72     /* Don't ask questions: */
73     argv[i++] = "-O";
74   }
75
76   /* Process blocksize parameter if set. */
77   if (optargs_bitmask & GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK) {
78     if (blocksize <= 0 || !is_power_of_2 (blocksize)) {
79       reply_with_error ("block size must be > 0 and a power of 2");
80       return -1;
81     }
82
83     if (STREQ (fstype, "vfat") ||
84         STREQ (fstype, "msdos")) {
85       /* For VFAT map the blocksize into a cluster size.  However we
86        * have to determine the block device sector size in order to do
87        * this.
88        */
89       int sectorsize = do_blockdev_getss (device);
90       if (sectorsize == -1)
91         return -1;
92
93       int sectors_per_cluster = blocksize / sectorsize;
94       if (sectors_per_cluster < 1 || sectors_per_cluster > 128) {
95         reply_with_error ("unsupported cluster size for %s filesystem (requested cluster size = %d, sector size = %d, trying sectors per cluster = %d)",
96                           fstype, blocksize, sectorsize, sectors_per_cluster);
97         return -1;
98       }
99
100       snprintf (blocksize_str, sizeof blocksize_str, "%d", sectors_per_cluster);
101       argv[i++] = "-s";
102       argv[i++] = blocksize_str;
103     }
104     else if (STREQ (fstype, "ntfs")) {
105       /* For NTFS map the blocksize into a cluster size. */
106       snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
107       argv[i++] = "-c";
108       argv[i++] = blocksize_str;
109     }
110     else {
111       /* For all other filesystem types, try the -b option. */
112       snprintf (blocksize_str, sizeof blocksize_str, "%d", blocksize);
113       argv[i++] = "-b";
114       argv[i++] = blocksize_str;
115     }
116   }
117
118   argv[i++] = device;
119   argv[i++] = NULL;
120
121   if (i > MAX_ARGS)
122     abort ();
123
124   r = commandv (NULL, &err, argv);
125   if (r == -1) {
126     reply_with_error ("%s: %s: %s", fstype, device, err);
127     free (err);
128     return -1;
129   }
130
131   free (err);
132   return 0;
133 }
134
135 int
136 do_mkfs (const char *fstype, const char *device)
137 {
138   optargs_bitmask = 0;
139   return do_mkfs_opts (fstype, device, 0);
140 }
141
142 int
143 do_mkfs_b (const char *fstype, int blocksize, const char *device)
144 {
145   optargs_bitmask = GUESTFS_MKFS_OPTS_BLOCKSIZE_BITMASK;
146   return do_mkfs_opts (fstype, device, blocksize);
147 }