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