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