2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
17 #include <sys/types.h>
20 #include <uuid/uuid.h>
22 #include "ccan/darray/darray.h"
25 #include "libbcachefs.h"
27 #include "libbcachefs/opts.h"
28 #include "libbcachefs/super-io.h"
29 #include "libbcachefs/util.h"
32 t("bcachefs format - create a new bcachefs filesystem on one or more devices") \
33 t("Usage: bcachefs format [OPTION]... <devices>") \
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) \
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") \
64 x('q', quiet, NULL, "Only print errors") \
65 x('h', help, NULL, "Display this help and exit")
67 static void usage(void)
69 #define t(text) puts(text "\n")
70 #define x(shortopt, longopt, arg, help) do { \
75 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
76 "Usage: bcachefs format [OPTION]... <devices>\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 " --error_action=(continue|remount-ro|panic)\n"
91 " Action to take on filesystem error\n"
92 " -L, --label=label\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"
103 " -q, --quiet Only print errors\n"
104 " -h, --help Display this help and exit\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"
109 "Report bugs to <linux-bcache@vger.kernel.org>");
115 #define x(shortopt, longopt, arg, help) O_##longopt,
121 static const struct option format_opts[] = {
123 #define x(shortopt, longopt, arg, help) { \
125 .has_arg = arg ? required_argument : no_argument, \
127 .val = O_##longopt, \
135 u64 read_flag_list_or_die(char *opt, const char * const list[],
138 u64 v = bch2_read_flag_list(opt, list);
140 die("Bad %s %s", msg, opt);
145 int cmd_format(int argc, char *argv[])
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;
153 darray_init(devices);
155 while ((opt = getopt_long(argc, argv,
163 hatoi_validate(optarg, "block size");
165 case O_btree_node_size:
166 opts.btree_node_size =
167 hatoi_validate(optarg, "btree node size");
169 case O_metadata_checksum_type:
170 opts.meta_csum_type =
171 read_string_list_or_die(optarg,
172 bch2_csum_types, "checksum type");
174 case O_data_checksum_type:
175 opts.data_csum_type =
176 read_string_list_or_die(optarg,
177 bch2_csum_types, "checksum type");
179 case O_compression_type:
180 opts.compression_type =
181 read_string_list_or_die(optarg,
182 bch2_compression_types,
185 case O_background_compression_type:
186 opts.background_compression_type =
187 read_string_list_or_die(optarg,
188 bch2_compression_types,
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");
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");
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;
210 case O_foreground_target:
211 opts.foreground_target = strdup(optarg);
213 case O_background_target:
214 opts.background_target = strdup(optarg);
216 case O_promote_target:
217 opts.promote_target = strdup(optarg);
220 opts.encrypted = true;
222 case O_no_passphrase:
223 no_passphrase = true;
227 opts.on_error_action =
228 read_string_list_or_die(optarg,
229 bch2_error_actions, "error action");
233 opts.label = strdup(optarg);
237 if (uuid_parse(optarg, opts.uuid.b))
245 if (bch2_strtoull_h(optarg, &dev_opts.size))
246 die("invalid filesystem size");
251 dev_opts.bucket_size =
252 hatoi_validate(optarg, "bucket size");
256 dev_opts.group = strdup(optarg);
259 dev_opts.discard = true;
262 dev_opts.data_allowed =
263 read_flag_list_or_die(optarg,
264 bch2_data_types, "data type");
267 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
268 dev_opts.durability > BCH_REPLICAS_MAX)
269 die("invalid durability");
272 dev_opts.path = strdup(optarg);
273 darray_append(devices, dev_opts);
287 if (!darray_size(devices))
288 die("Please supply a device");
290 if (opts.encrypted && !no_passphrase)
291 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
293 darray_foreach(dev, devices)
294 dev->fd = open_for_format(dev->path, force);
297 bch2_format(opts, devices.item, darray_size(devices));
300 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
303 if (opts.passphrase) {
304 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
305 free(opts.passphrase);
311 static void show_super_usage(void)
313 puts("bcachefs show-super \n"
314 "Usage: bcachefs show-super [OPTION].. device\n"
317 " -f, --fields=(fields) list of sections to print\n"
318 " -l, --layout print superblock layout\n"
319 " -h, --help display this help and exit\n"
320 "Report bugs to <linux-bcache@vger.kernel.org>");
324 int cmd_show_super(int argc, char *argv[])
326 static const struct option longopts[] = {
327 { "fields", 1, NULL, 'f' },
328 { "layout", 0, NULL, 'l' },
329 { "help", 0, NULL, 'h' },
332 unsigned fields = 1 << BCH_SB_FIELD_members;
333 bool print_layout = false;
336 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
339 fields = !strcmp(optarg, "all")
341 : read_flag_list_or_die(optarg,
342 bch2_sb_fields, "superblock field");
353 char *dev = arg_pop();
355 die("please supply a device");
357 die("too many arguments");
359 struct bch_opts opts = bch2_opts_empty();
361 opt_set(opts, noexcl, true);
362 opt_set(opts, nochanges, true);
364 struct bch_sb_handle sb;
365 int ret = bch2_read_super(dev, &opts, &sb);
367 die("Error opening %s: %s", dev, strerror(-ret));
369 bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
370 bch2_free_super(&sb);