2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
18 #include <sys/types.h>
21 #include <uuid/uuid.h>
24 #include "libbcachefs.h"
26 #include "libbcachefs/darray.h"
27 #include "libbcachefs/errcode.h"
28 #include "libbcachefs/opts.h"
29 #include "libbcachefs/super-io.h"
30 #include "libbcachefs/util.h"
33 x(0, replicas, required_argument) \
34 x(0, encrypted, no_argument) \
35 x(0, no_passphrase, no_argument) \
36 x('L', fs_label, required_argument) \
37 x('U', uuid, required_argument) \
38 x(0, fs_size, required_argument) \
39 x(0, superblock_size, required_argument) \
40 x(0, bucket_size, required_argument) \
41 x('l', label, required_argument) \
42 x(0, discard, no_argument) \
43 x(0, data_allowed, required_argument) \
44 x(0, durability, required_argument) \
45 x(0, version, required_argument) \
46 x(0, no_initialize, no_argument) \
47 x('f', force, no_argument) \
48 x('q', quiet, no_argument) \
49 x('v', verbose, no_argument) \
50 x('h', help, no_argument)
52 static void usage(void)
54 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
55 "Usage: bcachefs format [OPTION]... <devices>\n"
59 bch2_opts_usage(OPT_FORMAT);
62 " --replicas=# Sets both data and metadata replicas\n"
63 " --encrypted Enable whole filesystem encryption (chacha20/poly1305)\n"
64 " --no_passphrase Don't encrypt master encryption key\n"
65 " -L, --fs_label=label\n"
67 " --superblock_size=size\n"
69 "Device specific options:");
71 bch2_opts_usage(OPT_DEVICE);
73 puts(" -l, --label=label Disk label\n"
76 " -q, --quiet Only print errors\n"
77 " -v, --verbose Verbose filesystem initialization\n"
78 " -h, --help Display this help and exit\n"
80 "Device specific options must come before corresponding devices, e.g.\n"
81 " bcachefs format --label cache /dev/sdb /dev/sdc\n"
83 "Report bugs to <linux-bcachefs@vger.kernel.org>");
88 #define x(shortopt, longopt, arg) O_##longopt,
93 #define x(shortopt, longopt, arg) { \
99 static const struct option format_opts[] = {
105 u64 read_flag_list_or_die(char *opt, const char * const list[],
108 u64 v = bch2_read_flag_list(opt, list);
110 die("Bad %s %s", msg, opt);
115 int cmd_format(int argc, char *argv[])
117 DARRAY(struct dev_opts) devices = { 0 };
118 DARRAY(char *) device_paths = { 0 };
119 struct format_opts opts = format_opts_default();
120 struct dev_opts dev_opts = dev_opts_default(), *dev;
121 bool force = false, no_passphrase = false, quiet = false, initialize = true, verbose = false;
125 struct bch_opt_strs fs_opt_strs =
126 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
127 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
129 while ((opt = getopt_long(argc, argv,
135 if (kstrtouint(optarg, 10, &v) ||
137 v > BCH_REPLICAS_MAX)
138 die("invalid replicas");
140 opt_set(fs_opts, metadata_replicas, v);
141 opt_set(fs_opts, data_replicas, v);
144 opts.encrypted = true;
146 case O_no_passphrase:
147 no_passphrase = true;
155 if (uuid_parse(optarg, opts.uuid.b))
163 if (bch2_strtoull_h(optarg, &dev_opts.size))
164 die("invalid filesystem size");
166 case O_superblock_size:
167 if (bch2_strtouint_h(optarg, &opts.superblock_size))
168 die("invalid filesystem size");
170 opts.superblock_size >>= 9;
173 if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
174 die("bad bucket_size %s", optarg);
178 dev_opts.label = optarg;
181 dev_opts.discard = true;
184 dev_opts.data_allowed =
185 read_flag_list_or_die(optarg,
186 bch2_data_types, "data type");
189 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
190 dev_opts.durability > BCH_REPLICAS_MAX)
191 die("invalid durability");
194 if (kstrtouint(optarg, 10, &opts.version))
195 die("invalid version");
197 case O_no_initialize:
201 darray_push(&device_paths, optarg);
202 dev_opts.path = optarg;
203 darray_push(&devices, dev_opts);
222 if (opts.version != bcachefs_metadata_version_current)
226 die("Please supply a device");
228 if (opts.encrypted && !no_passphrase) {
229 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
233 darray_for_each(devices, dev) {
234 int ret = open_for_format(dev, force);
236 die("Error opening %s: %s", dev_opts.path, strerror(-ret));
240 bch2_format(fs_opt_strs,
243 devices.data, devices.nr);
244 bch2_opt_strs_free(&fs_opt_strs);
247 struct printbuf buf = PRINTBUF;
249 buf.human_readable_units = true;
251 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members_v2);
252 printf("%s", buf.buf);
258 if (opts.passphrase) {
259 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
260 free(opts.passphrase);
263 darray_exit(&devices);
266 struct bch_opts mount_opts = bch2_opts_empty();
269 opt_set(mount_opts, verbose, verbose);
272 * Start the filesystem once, to allocate the journal and create
273 * the root directory:
275 struct bch_fs *c = bch2_fs_open(device_paths.data,
279 die("error opening %s: %s", device_paths.data[0],
280 bch2_err_str(PTR_ERR(c)));
285 darray_exit(&device_paths);
290 static void show_super_usage(void)
292 puts("bcachefs show-super \n"
293 "Usage: bcachefs show-super [OPTION].. device\n"
296 " -f, --fields=(fields) list of sections to print\n"
297 " -l, --layout print superblock layout\n"
298 " -h, --help display this help and exit\n"
299 "Report bugs to <linux-bcachefs@vger.kernel.org>");
303 int cmd_show_super(int argc, char *argv[])
305 static const struct option longopts[] = {
306 { "fields", 1, NULL, 'f' },
307 { "layout", 0, NULL, 'l' },
308 { "help", 0, NULL, 'h' },
312 bool print_layout = false;
313 bool print_default_fields = true;
316 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
319 fields = !strcmp(optarg, "all")
321 : read_flag_list_or_die(optarg,
322 bch2_sb_fields, "superblock field");
323 print_default_fields = false;
334 char *dev = arg_pop();
336 die("please supply a device");
338 die("too many arguments");
340 struct bch_opts opts = bch2_opts_empty();
342 opt_set(opts, noexcl, true);
343 opt_set(opts, nochanges, true);
345 struct bch_sb_handle sb;
346 int ret = bch2_read_super(dev, &opts, &sb);
348 die("Error opening %s: %s", dev, bch2_err_str(ret));
350 if (print_default_fields)
351 fields = bch2_sb_get_members_v2(sb.sb)
352 ? 1 << BCH_SB_FIELD_members_v2
353 : 1 << BCH_SB_FIELD_members_v1;
355 struct printbuf buf = PRINTBUF;
357 buf.human_readable_units = true;
359 bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
360 printf("%s", buf.buf);
362 bch2_free_super(&sb);