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"
31 t("bcachefs format - create a new bcachefs filesystem on one or more devices") \
32 t("Usage: bcachefs format [OPTION]... <devices>") \
34 x('b', block_size, "size", NULL) \
35 x(0, btree_node_size, "size", "Default 256k") \
36 x(0, metadata_checksum_type, "(none|crc32c|crc64)", NULL) \
37 x(0, data_checksum_type, "(none|crc32c|crc64)", NULL) \
38 x(0, compression_type, "(none|lz4|gzip)", NULL) \
39 x(0, data_replicas, "#", NULL) \
40 x(0, metadata_replicas, "#", NULL) \
41 x(0, encrypted, NULL, "Enable whole filesystem encryption (chacha20/poly1305)")\
42 x(0, no_passphrase, NULL, "Don't encrypt master encryption key")\
43 x('e', error_action, "(continue|readonly|panic)", NULL) \
44 x('L', label, "label", NULL) \
45 x('U', uuid, "uuid", NULL) \
46 x('f', force, NULL, NULL) \
48 t("Device specific options:") \
49 x(0, fs_size, "size", "Size of filesystem on device")\
50 x(0, bucket_size, "size", "Bucket size") \
51 x('t', tier, "#", "Higher tier indicates slower devices")\
52 x(0, discard, NULL, NULL) \
53 x(0, data_allowed, "journal,btree,data", "Allowed types of data on this device")\
54 t("Device specific options must come before corresponding devices, e.g.") \
55 t(" bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc") \
57 x('q', quiet, NULL, "Only print errors") \
58 x('h', help, NULL, "Display this help and exit")
60 static void usage(void)
62 #define t(text) puts(text "\n")
63 #define x(shortopt, longopt, arg, help) do { \
68 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
69 "Usage: bcachefs format [OPTION]... <devices>\n"
73 " --btree_node=size Btree node size, default 256k\n"
74 " --metadata_checksum_type=(none|crc32c|crc64)\n"
75 " --data_checksum_type=(none|crc32c|crc64)\n"
76 " --compression_type=(none|lz4|gzip)\n"
77 " --data_replicas=# Number of data replicas\n"
78 " --metadata_replicas=# Number of metadata replicas\n"
79 " --encrypted Enable whole filesystem encryption (chacha20/poly1305)\n"
80 " --no_passphrase Don't encrypt master encryption key\n"
81 " --error_action=(continue|readonly|panic)\n"
82 " Action to take on filesystem error\n"
83 " -L, --label=label\n"
87 "Device specific options:\n"
88 " --fs_size=size Size of filesystem on device\n"
89 " --bucket=size Bucket size\n"
90 " --discard Enable discards\n"
91 " -t, --tier=# Higher tier (e.g. 1) indicates slower devices\n"
93 " -q, --quiet Only print errors\n"
94 " -h, --help Display this help and exit\n"
96 "Device specific options must come before corresponding devices, e.g.\n"
97 " bcachefs format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
99 "Report bugs to <linux-bcache@vger.kernel.org>");
105 #define x(shortopt, longopt, arg, help) O_##longopt,
111 static const struct option format_opts[] = {
113 #define x(shortopt, longopt, arg, help) { \
115 .has_arg = arg ? required_argument : no_argument, \
117 .val = O_##longopt, \
125 u64 read_flag_list_or_die(char *opt, const char * const list[],
128 u64 v = bch2_read_flag_list(opt, list);
130 die("Bad %s %s", msg, opt);
135 int cmd_format(int argc, char *argv[])
137 darray(struct dev_opts) devices;
138 struct format_opts opts = format_opts_default();
139 struct dev_opts dev_opts = dev_opts_default(), *dev;
140 bool force = false, no_passphrase = false, quiet = false;
143 darray_init(devices);
145 while ((opt = getopt_long(argc, argv,
153 hatoi_validate(optarg, "block size");
155 case O_btree_node_size:
156 opts.btree_node_size =
157 hatoi_validate(optarg, "btree node size");
159 case O_metadata_checksum_type:
160 opts.meta_csum_type =
161 read_string_list_or_die(optarg,
162 bch2_csum_types, "checksum type");
164 case O_data_checksum_type:
165 opts.data_csum_type =
166 read_string_list_or_die(optarg,
167 bch2_csum_types, "checksum type");
169 case O_compression_type:
170 opts.compression_type =
171 read_string_list_or_die(optarg,
172 bch2_compression_types,
175 case O_data_replicas:
176 if (kstrtouint(optarg, 10, &opts.data_replicas) ||
177 dev_opts.tier >= BCH_REPLICAS_MAX)
178 die("invalid replicas");
180 case O_metadata_replicas:
181 if (kstrtouint(optarg, 10, &opts.meta_replicas) ||
182 dev_opts.tier >= BCH_REPLICAS_MAX)
183 die("invalid replicas");
186 opts.encrypted = true;
188 case O_no_passphrase:
189 no_passphrase = true;
193 opts.on_error_action =
194 read_string_list_or_die(optarg,
195 bch2_error_actions, "error action");
199 opts.label = strdup(optarg);
203 if (uuid_parse(optarg, opts.uuid.b))
211 if (bch2_strtoull_h(optarg, &dev_opts.size))
212 die("invalid filesystem size");
217 dev_opts.bucket_size =
218 hatoi_validate(optarg, "bucket size");
222 if (kstrtouint(optarg, 10, &dev_opts.tier) ||
223 dev_opts.tier >= BCH_TIER_MAX)
227 dev_opts.discard = true;
230 dev_opts.data_allowed =
231 read_flag_list_or_die(optarg,
232 bch2_data_types, "data type");
235 dev_opts.path = strdup(optarg);
236 darray_append(devices, dev_opts);
250 if (!darray_size(devices))
251 die("Please supply a device");
253 if (opts.encrypted && !no_passphrase) {
254 opts.passphrase = read_passphrase("Enter passphrase: ");
256 if (isatty(STDIN_FILENO)) {
258 read_passphrase("Enter same passphrase again: ");
260 if (strcmp(opts.passphrase, pass2)) {
261 memzero_explicit(opts.passphrase,
262 strlen(opts.passphrase));
263 memzero_explicit(pass2, strlen(pass2));
264 die("Passphrases do not match");
267 memzero_explicit(pass2, strlen(pass2));
272 darray_foreach(dev, devices)
273 dev->fd = open_for_format(dev->path, force);
276 bch2_format(opts, devices.item, darray_size(devices));
279 bch2_super_print(sb, HUMAN_READABLE);
282 if (opts.passphrase) {
283 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
284 free(opts.passphrase);
290 int cmd_show_super(int argc, char *argv[])
295 die("please supply a single device");
297 sb = bch2_super_read(argv[1]);
298 bch2_super_print(sb, HUMAN_READABLE);