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 dev->fd = open_for_format(dev->path, force);
237 bch2_format(fs_opt_strs,
240 devices.data, devices.nr);
241 bch2_opt_strs_free(&fs_opt_strs);
244 struct printbuf buf = PRINTBUF;
246 buf.human_readable_units = true;
248 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members);
249 printf("%s", buf.buf);
255 if (opts.passphrase) {
256 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
257 free(opts.passphrase);
260 darray_exit(&devices);
263 struct bch_opts mount_opts = bch2_opts_empty();
266 opt_set(mount_opts, verbose, verbose);
269 * Start the filesystem once, to allocate the journal and create
270 * the root directory:
272 struct bch_fs *c = bch2_fs_open(device_paths.data,
276 die("error opening %s: %s", device_paths.data[0],
277 bch2_err_str(PTR_ERR(c)));
282 darray_exit(&device_paths);
287 static void show_super_usage(void)
289 puts("bcachefs show-super \n"
290 "Usage: bcachefs show-super [OPTION].. device\n"
293 " -f, --fields=(fields) list of sections to print\n"
294 " -l, --layout print superblock layout\n"
295 " -h, --help display this help and exit\n"
296 "Report bugs to <linux-bcachefs@vger.kernel.org>");
300 int cmd_show_super(int argc, char *argv[])
302 static const struct option longopts[] = {
303 { "fields", 1, NULL, 'f' },
304 { "layout", 0, NULL, 'l' },
305 { "help", 0, NULL, 'h' },
308 unsigned fields = 1 << BCH_SB_FIELD_members;
309 bool print_layout = false;
312 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
315 fields = !strcmp(optarg, "all")
317 : read_flag_list_or_die(optarg,
318 bch2_sb_fields, "superblock field");
329 char *dev = arg_pop();
331 die("please supply a device");
333 die("too many arguments");
335 struct bch_opts opts = bch2_opts_empty();
337 opt_set(opts, noexcl, true);
338 opt_set(opts, nochanges, true);
340 struct bch_sb_handle sb;
341 int ret = bch2_read_super(dev, &opts, &sb);
343 die("Error opening %s: %s", dev, bch2_err_str(ret));
345 struct printbuf buf = PRINTBUF;
347 buf.human_readable_units = true;
349 bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
350 printf("%s", buf.buf);
352 bch2_free_super(&sb);