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 t("Device specific options must come before corresponding devices, e.g.") \
61 t(" bcachefs format --group cache /dev/sdb --tier 1 /dev/sdc") \
63 x('q', quiet, NULL, "Only print errors") \
64 x('h', help, NULL, "Display this help and exit")
66 static void usage(void)
68 #define t(text) puts(text "\n")
69 #define x(shortopt, longopt, arg, help) do { \
74 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
75 "Usage: bcachefs format [OPTION]... <devices>\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"
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"
101 " -q, --quiet Only print errors\n"
102 " -h, --help Display this help and exit\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"
107 "Report bugs to <linux-bcache@vger.kernel.org>");
113 #define x(shortopt, longopt, arg, help) O_##longopt,
119 static const struct option format_opts[] = {
121 #define x(shortopt, longopt, arg, help) { \
123 .has_arg = arg ? required_argument : no_argument, \
125 .val = O_##longopt, \
133 u64 read_flag_list_or_die(char *opt, const char * const list[],
136 u64 v = bch2_read_flag_list(opt, list);
138 die("Bad %s %s", msg, opt);
143 int cmd_format(int argc, char *argv[])
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;
151 darray_init(devices);
153 while ((opt = getopt_long(argc, argv,
161 hatoi_validate(optarg, "block size");
163 case O_btree_node_size:
164 opts.btree_node_size =
165 hatoi_validate(optarg, "btree node size");
167 case O_metadata_checksum_type:
168 opts.meta_csum_type =
169 read_string_list_or_die(optarg,
170 bch2_csum_types, "checksum type");
172 case O_data_checksum_type:
173 opts.data_csum_type =
174 read_string_list_or_die(optarg,
175 bch2_csum_types, "checksum type");
177 case O_compression_type:
178 opts.compression_type =
179 read_string_list_or_die(optarg,
180 bch2_compression_types,
183 case O_background_compression_type:
184 opts.background_compression_type =
185 read_string_list_or_die(optarg,
186 bch2_compression_types,
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");
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");
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;
208 case O_foreground_target:
209 opts.foreground_target = strdup(optarg);
211 case O_background_target:
212 opts.background_target = strdup(optarg);
214 case O_promote_target:
215 opts.promote_target = strdup(optarg);
218 opts.encrypted = true;
220 case O_no_passphrase:
221 no_passphrase = true;
225 opts.on_error_action =
226 read_string_list_or_die(optarg,
227 bch2_error_actions, "error action");
231 opts.label = strdup(optarg);
235 if (uuid_parse(optarg, opts.uuid.b))
243 if (bch2_strtoull_h(optarg, &dev_opts.size))
244 die("invalid filesystem size");
249 dev_opts.bucket_size =
250 hatoi_validate(optarg, "bucket size");
254 dev_opts.group = strdup(optarg);
257 dev_opts.discard = true;
260 dev_opts.data_allowed =
261 read_flag_list_or_die(optarg,
262 bch2_data_types, "data type");
265 dev_opts.path = strdup(optarg);
266 darray_append(devices, dev_opts);
280 if (!darray_size(devices))
281 die("Please supply a device");
283 if (opts.encrypted && !no_passphrase)
284 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
286 darray_foreach(dev, devices)
287 dev->fd = open_for_format(dev->path, force);
290 bch2_format(opts, devices.item, darray_size(devices));
293 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
296 if (opts.passphrase) {
297 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
298 free(opts.passphrase);
304 static void show_super_usage(void)
306 puts("bcachefs show-super \n"
307 "Usage: bcachefs show-super [OPTION].. device\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>");
317 int cmd_show_super(int argc, char *argv[])
319 static const struct option longopts[] = {
320 { "fields", 1, NULL, 'f' },
321 { "layout", 0, NULL, 'l' },
322 { "help", 0, NULL, 'h' },
325 unsigned fields = 1 << BCH_SB_FIELD_members;
326 bool print_layout = false;
329 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
332 fields = !strcmp(optarg, "all")
334 : read_flag_list_or_die(optarg,
335 bch2_sb_fields, "superblock field");
346 char *dev = arg_pop();
348 die("please supply a device");
350 die("too many arguments");
352 struct bch_opts opts = bch2_opts_empty();
354 opt_set(opts, noexcl, true);
355 opt_set(opts, nochanges, true);
357 struct bch_sb_handle sb;
358 int ret = bch2_read_super(dev, &opts, &sb);
360 die("Error opening %s: %s", dev, strerror(-ret));
362 bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
363 bch2_free_super(&sb);