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