daemon: Use gnulib futimens module.
[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++] = "/sbin/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 ("mkfs: %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 blocksize_s[32];
106
107   snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
108
109   extra[0] = "-b";
110   extra[1] = blocksize_s;
111
112   return mkfs (fstype, device, extra, 2);
113 }