]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
065efd9df17bd0dc8509de821e005127872c5880
[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 <errno.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 #include <unistd.h>
19
20 #include <uuid/uuid.h>
21
22 #include "ccan/darray/darray.h"
23
24 #include "cmds.h"
25 #include "libbcachefs.h"
26 #include "crypto.h"
27 #include "libbcachefs/opts.h"
28 #include "libbcachefs/super-io.h"
29 #include "libbcachefs/util.h"
30
31 #define OPTS                                                                    \
32 t("bcachefs format - create a new bcachefs filesystem on one or more devices")  \
33 t("Usage: bcachefs format [OPTION]... <devices>")                               \
34 t("")                                                                           \
35 x('b',  block_size,             "size",                 NULL)                   \
36 x(0,    btree_node_size,        "size",                 "Default 256k")         \
37 x(0,    metadata_checksum_type, "(none|crc32c|crc64)",  NULL)                   \
38 x(0,    data_checksum_type,     "(none|crc32c|crc64)",  NULL)                   \
39 x(0,    compression_type,       "(none|lz4|gzip)",      NULL)                   \
40 x(0,    background_compression_type,    "(none|lz4|gzip)",      NULL)           \
41 x(0,    replicas,               "#",                    NULL)                   \
42 x(0,    data_replicas,          "#",                    NULL)                   \
43 x(0,    metadata_replicas,      "#",                    NULL)                   \
44 x(0,    foreground_target,      "target",               NULL)                   \
45 x(0,    background_target,      "target",               NULL)                   \
46 x(0,    promote_target,         "target",               NULL)                   \
47 x(0,    encrypted,              NULL,                   "Enable whole filesystem encryption (chacha20/poly1305)")\
48 x(0,    no_passphrase,          NULL,                   "Don't encrypt master encryption key")\
49 x('e',  error_action,           "(continue|remount-ro|panic)", NULL)            \
50 x('L',  label,                  "label",                NULL)                   \
51 x('U',  uuid,                   "uuid",                 NULL)                   \
52 x('f',  force,                  NULL,                   NULL)                   \
53 t("")                                                                           \
54 t("Device specific options:")                                                   \
55 x(0,    fs_size,                "size",                 "Size of filesystem on device")\
56 x(0,    bucket_size,            "size",                 "Bucket size")          \
57 x('g',  group,                  "label",                "Disk group")\
58 x(0,    discard,                NULL,                   NULL)                   \
59 x(0,    data_allowed,           "journal,btree,data",   "Allowed types of data on this device")\
60 t("Device specific options must come before corresponding devices, e.g.")       \
61 t("  bcachefs format --group cache /dev/sdb --tier 1 /dev/sdc")                 \
62 t("")                                                                           \
63 x('q',  quiet,                  NULL,                   "Only print errors")    \
64 x('h',  help,                   NULL,                   "Display this help and exit")
65
66 static void usage(void)
67 {
68 #define t(text)                         puts(text "\n")
69 #define x(shortopt, longopt, arg, help) do {                            \
70         OPTS
71 #undef x
72 #undef t
73
74         puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
75              "Usage: bcachefs format [OPTION]... <devices>\n"
76              "\n"
77              "Options:\n"
78              "  -b, --block=size\n"
79              "      --btree_node=size       Btree node size, default 256k\n"
80              "      --metadata_checksum_type=(none|crc32c|crc64)\n"
81              "      --data_checksum_type=(none|crc32c|crc64)\n"
82              "      --compression_type=(none|lz4|gzip|zstd)\n"
83              "      --background_compression_type=(none|lz4|gzip|zstd)\n"
84              "      --data_replicas=#       Number of data replicas\n"
85              "      --metadata_replicas=#   Number of metadata replicas\n"
86              "      --replicas=#            Sets both data and metadata replicas\n"
87              "      --encrypted             Enable whole filesystem encryption (chacha20/poly1305)\n"
88              "      --no_passphrase         Don't encrypt master encryption key\n"
89              "      --error_action=(continue|remount-ro|panic)\n"
90              "                              Action to take on filesystem error\n"
91              "  -L, --label=label\n"
92              "  -U, --uuid=uuid\n"
93              "  -f, --force\n"
94              "\n"
95              "Device specific options:\n"
96              "      --fs_size=size          Size of filesystem on device\n"
97              "      --bucket=size           Bucket size\n"
98              "      --discard               Enable discards\n"
99              "  -g, --group=label           Disk group\n"
100              "\n"
101              "  -q, --quiet                 Only print errors\n"
102              "  -h, --help                  Display this help and exit\n"
103              "\n"
104              "Device specific options must come before corresponding devices, e.g.\n"
105              "  bcachefs format --group cache /dev/sdb --tier 1 /dev/sdc\n"
106              "\n"
107              "Report bugs to <linux-bcache@vger.kernel.org>");
108 }
109
110 enum {
111         O_no_opt = 1,
112 #define t(text)
113 #define x(shortopt, longopt, arg, help) O_##longopt,
114         OPTS
115 #undef x
116 #undef t
117 };
118
119 static const struct option format_opts[] = {
120 #define t(text)
121 #define x(shortopt, longopt, arg, help) {                               \
122         .name           = #longopt,                                     \
123         .has_arg        = arg ? required_argument : no_argument,        \
124         .flag           = NULL,                                         \
125         .val            = O_##longopt,                                  \
126 },
127         OPTS
128 #undef x
129 #undef t
130         { NULL }
131 };
132
133 u64 read_flag_list_or_die(char *opt, const char * const list[],
134                           const char *msg)
135 {
136         u64 v = bch2_read_flag_list(opt, list);
137         if (v == (u64) -1)
138                 die("Bad %s %s", msg, opt);
139
140         return v;
141 }
142
143 int cmd_format(int argc, char *argv[])
144 {
145         darray(struct dev_opts) devices;
146         struct format_opts opts = format_opts_default();
147         struct dev_opts dev_opts = dev_opts_default(), *dev;
148         bool force = false, no_passphrase = false, quiet = false;
149         int opt;
150
151         darray_init(devices);
152
153         while ((opt = getopt_long(argc, argv,
154                                   "-b:e:L:U:ft:qh",
155                                   format_opts,
156                                   NULL)) != -1)
157                 switch (opt) {
158                 case O_block_size:
159                 case 'b':
160                         opts.block_size =
161                                 hatoi_validate(optarg, "block size");
162                         break;
163                 case O_btree_node_size:
164                         opts.btree_node_size =
165                                 hatoi_validate(optarg, "btree node size");
166                         break;
167                 case O_metadata_checksum_type:
168                         opts.meta_csum_type =
169                                 read_string_list_or_die(optarg,
170                                                 bch2_csum_types, "checksum type");
171                         break;
172                 case O_data_checksum_type:
173                         opts.data_csum_type =
174                                 read_string_list_or_die(optarg,
175                                                 bch2_csum_types, "checksum type");
176                         break;
177                 case O_compression_type:
178                         opts.compression_type =
179                                 read_string_list_or_die(optarg,
180                                                 bch2_compression_types,
181                                                 "compression type");
182                         break;
183                 case O_background_compression_type:
184                         opts.background_compression_type =
185                                 read_string_list_or_die(optarg,
186                                                 bch2_compression_types,
187                                                 "compression type");
188                         break;
189                 case O_data_replicas:
190                         if (kstrtouint(optarg, 10, &opts.data_replicas) ||
191                             !opts.data_replicas ||
192                             opts.data_replicas > BCH_REPLICAS_MAX)
193                                 die("invalid replicas");
194                         break;
195                 case O_metadata_replicas:
196                         if (kstrtouint(optarg, 10, &opts.meta_replicas) ||
197                             !opts.meta_replicas ||
198                             opts.meta_replicas > BCH_REPLICAS_MAX)
199                                 die("invalid replicas");
200                         break;
201                 case O_replicas:
202                         if (kstrtouint(optarg, 10, &opts.data_replicas) ||
203                             !opts.data_replicas ||
204                             opts.data_replicas > BCH_REPLICAS_MAX)
205                                 die("invalid replicas");
206                         opts.meta_replicas = opts.data_replicas;
207                         break;
208                 case O_foreground_target:
209                         opts.foreground_target = strdup(optarg);
210                         break;
211                 case O_background_target:
212                         opts.background_target = strdup(optarg);
213                         break;
214                 case O_promote_target:
215                         opts.promote_target = strdup(optarg);
216                         break;
217                 case O_encrypted:
218                         opts.encrypted = true;
219                         break;
220                 case O_no_passphrase:
221                         no_passphrase = true;
222                         break;
223                 case O_error_action:
224                 case 'e':
225                         opts.on_error_action =
226                                 read_string_list_or_die(optarg,
227                                                 bch2_error_actions, "error action");
228                         break;
229                 case O_label:
230                 case 'L':
231                         opts.label = strdup(optarg);
232                         break;
233                 case O_uuid:
234                 case 'U':
235                         if (uuid_parse(optarg, opts.uuid.b))
236                                 die("Bad uuid");
237                         break;
238                 case O_force:
239                 case 'f':
240                         force = true;
241                         break;
242                 case O_fs_size:
243                         if (bch2_strtoull_h(optarg, &dev_opts.size))
244                                 die("invalid filesystem size");
245
246                         dev_opts.size >>= 9;
247                         break;
248                 case O_bucket_size:
249                         dev_opts.bucket_size =
250                                 hatoi_validate(optarg, "bucket size");
251                         break;
252                 case O_group:
253                 case 'g':
254                         dev_opts.group = strdup(optarg);
255                         break;
256                 case O_discard:
257                         dev_opts.discard = true;
258                         break;
259                 case O_data_allowed:
260                         dev_opts.data_allowed =
261                                 read_flag_list_or_die(optarg,
262                                         bch2_data_types, "data type");
263                         break;
264                 case O_no_opt:
265                         dev_opts.path = strdup(optarg);
266                         darray_append(devices, dev_opts);
267                         dev_opts.size = 0;
268                         break;
269                 case O_quiet:
270                 case 'q':
271                         quiet = true;
272                         break;
273                 case O_help:
274                 case 'h':
275                         usage();
276                         exit(EXIT_SUCCESS);
277                         break;
278                 }
279
280         if (!darray_size(devices))
281                 die("Please supply a device");
282
283         if (opts.encrypted && !no_passphrase)
284                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
285
286         darray_foreach(dev, devices)
287                 dev->fd = open_for_format(dev->path, force);
288
289         struct bch_sb *sb =
290                 bch2_format(opts, devices.item, darray_size(devices));
291
292         if (!quiet)
293                 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
294         free(sb);
295
296         if (opts.passphrase) {
297                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
298                 free(opts.passphrase);
299         }
300
301         return 0;
302 }
303
304 static void show_super_usage(void)
305 {
306         puts("bcachefs show-super \n"
307              "Usage: bcachefs show-super [OPTION].. device\n"
308              "\n"
309              "Options:\n"
310              "  -f, --fields=(fields)       list of sections to print\n"
311              "  -l, --layout                print superblock layout\n"
312              "  -h, --help                  display this help and exit\n"
313              "Report bugs to <linux-bcache@vger.kernel.org>");
314         exit(EXIT_SUCCESS);
315 }
316
317 int cmd_show_super(int argc, char *argv[])
318 {
319         static const struct option longopts[] = {
320                 { "fields",                     1, NULL, 'f' },
321                 { "layout",                     0, NULL, 'l' },
322                 { "help",                       0, NULL, 'h' },
323                 { NULL }
324         };
325         unsigned fields = 1 << BCH_SB_FIELD_members;
326         bool print_layout = false;
327         int opt;
328
329         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
330                 switch (opt) {
331                 case 'f':
332                         fields = !strcmp(optarg, "all")
333                                 ? ~0
334                                 : read_flag_list_or_die(optarg,
335                                         bch2_sb_fields, "superblock field");
336                         break;
337                 case 'l':
338                         print_layout = true;
339                         break;
340                 case 'h':
341                         show_super_usage();
342                         break;
343                 }
344         args_shift(optind);
345
346         char *dev = arg_pop();
347         if (!dev)
348                 die("please supply a device");
349         if (argc)
350                 die("too many arguments");
351
352         struct bch_opts opts = bch2_opts_empty();
353
354         opt_set(opts, noexcl,   true);
355         opt_set(opts, nochanges, true);
356
357         struct bch_sb_handle sb;
358         int ret = bch2_read_super(dev, &opts, &sb);
359         if (ret)
360                 die("Error opening %s: %s", dev, strerror(-ret));
361
362         bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
363         bch2_free_super(&sb);
364         return 0;
365 }