]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
Update email address
[bcachefs-tools-debian] / cmd_format.c
1 /*
2  * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3  *          Gabriel de Perthuis <g2p.code@gmail.com>
4  *          Jacob Malevich <jam@datera.io>
5  *
6  * GPLv2
7  */
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <uuid/uuid.h>
22
23 #include "cmds.h"
24 #include "libbcachefs.h"
25 #include "crypto.h"
26 #include "libbcachefs/darray.h"
27 #include "libbcachefs/opts.h"
28 #include "libbcachefs/super-io.h"
29 #include "libbcachefs/util.h"
30
31 #define OPTS                                            \
32 x(0,    replicas,               required_argument)      \
33 x(0,    encrypted,              no_argument)            \
34 x(0,    no_passphrase,          no_argument)            \
35 x('L',  fs_label,               required_argument)      \
36 x('U',  uuid,                   required_argument)      \
37 x(0,    fs_size,                required_argument)      \
38 x(0,    superblock_size,        required_argument)      \
39 x(0,    bucket_size,            required_argument)      \
40 x('l',  label,                  required_argument)      \
41 x(0,    discard,                no_argument)            \
42 x(0,    data_allowed,           required_argument)      \
43 x(0,    durability,             required_argument)      \
44 x(0,    version,                required_argument)      \
45 x(0,    no_initialize,          no_argument)            \
46 x('f',  force,                  no_argument)            \
47 x('q',  quiet,                  no_argument)            \
48 x('v',  verbose,                no_argument)            \
49 x('h',  help,                   no_argument)
50
51 static void usage(void)
52 {
53         puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
54              "Usage: bcachefs format [OPTION]... <devices>\n"
55              "\n"
56              "Options:");
57
58         bch2_opts_usage(OPT_FORMAT);
59
60         puts(
61              "      --replicas=#            Sets both data and metadata replicas\n"
62              "      --encrypted             Enable whole filesystem encryption (chacha20/poly1305)\n"
63              "      --no_passphrase         Don't encrypt master encryption key\n"
64              "  -L, --fs_label=label\n"
65              "  -U, --uuid=uuid\n"
66              "      --superblock_size=size\n"
67              "\n"
68              "Device specific options:");
69
70         bch2_opts_usage(OPT_DEVICE);
71
72         puts("  -l, --label=label           Disk label\n"
73              "\n"
74              "  -f, --force\n"
75              "  -q, --quiet                 Only print errors\n"
76              "  -v, --verbose               Verbose filesystem initialization\n"
77              "  -h, --help                  Display this help and exit\n"
78              "\n"
79              "Device specific options must come before corresponding devices, e.g.\n"
80              "  bcachefs format --label cache /dev/sdb /dev/sdc\n"
81              "\n"
82              "Report bugs to <linux-bcachefs@vger.kernel.org>");
83 }
84
85 enum {
86         O_no_opt = 1,
87 #define x(shortopt, longopt, arg)       O_##longopt,
88         OPTS
89 #undef x
90 };
91
92 #define x(shortopt, longopt, arg) {                     \
93         .name           = #longopt,                     \
94         .has_arg        = arg,                          \
95         .flag           = NULL,                         \
96         .val            = O_##longopt,                  \
97 },
98 static const struct option format_opts[] = {
99         OPTS
100         { NULL }
101 };
102 #undef x
103
104 u64 read_flag_list_or_die(char *opt, const char * const list[],
105                           const char *msg)
106 {
107         u64 v = bch2_read_flag_list(opt, list);
108         if (v == (u64) -1)
109                 die("Bad %s %s", msg, opt);
110
111         return v;
112 }
113
114 int cmd_format(int argc, char *argv[])
115 {
116         DARRAY(struct dev_opts) devices = { 0 };
117         DARRAY(char *) device_paths = { 0 };
118         struct format_opts opts = format_opts_default();
119         struct dev_opts dev_opts = dev_opts_default(), *dev;
120         bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
121         unsigned v;
122         int opt;
123
124         struct bch_opt_strs fs_opt_strs =
125                 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
126         struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
127
128         while ((opt = getopt_long(argc, argv,
129                                   "-L:U:g:fqhv",
130                                   format_opts,
131                                   NULL)) != -1)
132                 switch (opt) {
133                 case O_replicas:
134                         if (kstrtouint(optarg, 10, &v) ||
135                             !v ||
136                             v > BCH_REPLICAS_MAX)
137                                 die("invalid replicas");
138
139                         opt_set(fs_opts, metadata_replicas, v);
140                         opt_set(fs_opts, data_replicas, v);
141                         break;
142                 case O_encrypted:
143                         opts.encrypted = true;
144                         break;
145                 case O_no_passphrase:
146                         no_passphrase = true;
147                         break;
148                 case O_fs_label:
149                 case 'L':
150                         opts.label = optarg;
151                         break;
152                 case O_uuid:
153                 case 'U':
154                         if (uuid_parse(optarg, opts.uuid.b))
155                                 die("Bad uuid");
156                         break;
157                 case O_force:
158                 case 'f':
159                         force = true;
160                         break;
161                 case O_fs_size:
162                         if (bch2_strtoull_h(optarg, &dev_opts.size))
163                                 die("invalid filesystem size");
164                         break;
165                 case O_superblock_size:
166                         if (bch2_strtouint_h(optarg, &opts.superblock_size))
167                                 die("invalid filesystem size");
168
169                         opts.superblock_size >>= 9;
170                         break;
171                 case O_bucket_size:
172                         if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
173                                 die("bad bucket_size %s", optarg);
174                         break;
175                 case O_label:
176                 case 'l':
177                         dev_opts.label = optarg;
178                         break;
179                 case O_discard:
180                         dev_opts.discard = true;
181                         break;
182                 case O_data_allowed:
183                         dev_opts.data_allowed =
184                                 read_flag_list_or_die(optarg,
185                                         bch2_data_types, "data type");
186                         break;
187                 case O_durability:
188                         if (kstrtouint(optarg, 10, &dev_opts.durability) ||
189                             dev_opts.durability > BCH_REPLICAS_MAX)
190                                 die("invalid durability");
191                         break;
192                 case O_version:
193                         if (kstrtouint(optarg, 10, &opts.version))
194                                 die("invalid version");
195                         break;
196                 case O_no_initialize:
197                         initialize = false;
198                         break;
199                 case O_no_opt:
200                         darray_push(&device_paths, optarg);
201                         dev_opts.path = optarg;
202                         darray_push(&devices, dev_opts);
203                         dev_opts.size = 0;
204                         break;
205                 case O_quiet:
206                 case 'q':
207                         quiet = true;
208                         break;
209                 case 'v':
210                         verbose = true;
211                 case O_help:
212                 case 'h':
213                         usage();
214                         exit(EXIT_SUCCESS);
215                         break;
216                 case '?':
217                         exit(EXIT_FAILURE);
218                         break;
219                 }
220
221         if (!devices.nr)
222                 die("Please supply a device");
223
224         if (opts.encrypted && !no_passphrase) {
225                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
226                 initialize = false;
227         }
228
229         darray_for_each(devices, dev)
230                 dev->fd = open_for_format(dev->path, force);
231
232         struct bch_sb *sb =
233                 bch2_format(fs_opt_strs,
234                             fs_opts,
235                             opts,
236                             devices.data, devices.nr);
237         bch2_opt_strs_free(&fs_opt_strs);
238
239         if (!quiet) {
240                 struct printbuf buf = PRINTBUF;
241
242                 buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
243
244                 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members);
245                 printf("%s", buf.buf);
246
247                 printbuf_exit(&buf);
248         }
249         free(sb);
250
251         if (opts.passphrase) {
252                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
253                 free(opts.passphrase);
254         }
255
256         darray_exit(&devices);
257
258         if (initialize) {
259                 struct bch_opts mount_opts = bch2_opts_empty();
260
261
262                 opt_set(mount_opts, verbose, verbose);
263
264                 /*
265                  * Start the filesystem once, to allocate the journal and create
266                  * the root directory:
267                  */
268                 struct bch_fs *c = bch2_fs_open(device_paths.data,
269                                                 device_paths.nr,
270                                                 mount_opts);
271                 if (IS_ERR(c))
272                         die("error opening %s: %s", device_paths.data[0],
273                             strerror(-PTR_ERR(c)));
274
275                 bch2_fs_stop(c);
276         }
277
278         darray_exit(&device_paths);
279
280         return 0;
281 }
282
283 static void show_super_usage(void)
284 {
285         puts("bcachefs show-super \n"
286              "Usage: bcachefs show-super [OPTION].. device\n"
287              "\n"
288              "Options:\n"
289              "  -f, --fields=(fields)       list of sections to print\n"
290              "  -l, --layout                print superblock layout\n"
291              "  -h, --help                  display this help and exit\n"
292              "Report bugs to <linux-bcachefs@vger.kernel.org>");
293         exit(EXIT_SUCCESS);
294 }
295
296 int cmd_show_super(int argc, char *argv[])
297 {
298         static const struct option longopts[] = {
299                 { "fields",                     1, NULL, 'f' },
300                 { "layout",                     0, NULL, 'l' },
301                 { "help",                       0, NULL, 'h' },
302                 { NULL }
303         };
304         unsigned fields = 1 << BCH_SB_FIELD_members;
305         bool print_layout = false;
306         int opt;
307
308         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
309                 switch (opt) {
310                 case 'f':
311                         fields = !strcmp(optarg, "all")
312                                 ? ~0
313                                 : read_flag_list_or_die(optarg,
314                                         bch2_sb_fields, "superblock field");
315                         break;
316                 case 'l':
317                         print_layout = true;
318                         break;
319                 case 'h':
320                         show_super_usage();
321                         break;
322                 }
323         args_shift(optind);
324
325         char *dev = arg_pop();
326         if (!dev)
327                 die("please supply a device");
328         if (argc)
329                 die("too many arguments");
330
331         struct bch_opts opts = bch2_opts_empty();
332
333         opt_set(opts, noexcl,   true);
334         opt_set(opts, nochanges, true);
335
336         struct bch_sb_handle sb;
337         int ret = bch2_read_super(dev, &opts, &sb);
338         if (ret)
339                 die("Error opening %s: %s", dev, strerror(-ret));
340
341         struct printbuf buf = PRINTBUF;
342
343         buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
344
345         bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
346         printf("%s", buf.buf);
347
348         bch2_free_super(&sb);
349         printbuf_exit(&buf);
350         return 0;
351 }