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>
23 #include "ccan/darray/darray.h"
26 #include "libbcachefs.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('h', help, no_argument)
51 static void usage(void)
53 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
54 "Usage: bcachefs format [OPTION]... <devices>\n"
58 bch2_opts_usage(OPT_FORMAT);
61 " --replicas=# Sets both data and metadata replicas\n"
62 " --encrypted Enable whole filesystem encryption (chacha20/poly1305)\n"
63 " --no_passphrase Don't encrypt master encryption key\n"
64 " -L, --fs_label=label\n"
66 " --superblock_size=size\n"
68 "Device specific options:");
70 bch2_opts_usage(OPT_DEVICE);
72 puts(" -l, --label=label Disk label\n"
75 " -q, --quiet Only print errors\n"
76 " -h, --help Display this help and exit\n"
78 "Device specific options must come before corresponding devices, e.g.\n"
79 " bcachefs format --label cache /dev/sdb /dev/sdc\n"
81 "Report bugs to <linux-bcache@vger.kernel.org>");
86 #define x(shortopt, longopt, arg) O_##longopt,
91 #define x(shortopt, longopt, arg) { \
97 static const struct option format_opts[] = {
103 u64 read_flag_list_or_die(char *opt, const char * const list[],
106 u64 v = bch2_read_flag_list(opt, list);
108 die("Bad %s %s", msg, opt);
113 int cmd_format(int argc, char *argv[])
115 darray(struct dev_opts) devices;
116 darray(char *) device_paths;
117 struct format_opts opts = format_opts_default();
118 struct dev_opts dev_opts = dev_opts_default(), *dev;
119 bool force = false, no_passphrase = false, quiet = false, initialize = true;
123 darray_init(devices);
124 darray_init(device_paths);
126 struct bch_opt_strs fs_opt_strs =
127 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
128 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
130 while ((opt = getopt_long(argc, argv,
136 if (kstrtouint(optarg, 10, &v) ||
138 v > BCH_REPLICAS_MAX)
139 die("invalid replicas");
141 opt_set(fs_opts, metadata_replicas, v);
142 opt_set(fs_opts, data_replicas, v);
145 opts.encrypted = true;
147 case O_no_passphrase:
148 no_passphrase = true;
156 if (uuid_parse(optarg, opts.uuid.b))
164 if (bch2_strtoull_h(optarg, &dev_opts.size))
165 die("invalid filesystem size");
167 case O_superblock_size:
168 if (bch2_strtouint_h(optarg, &opts.superblock_size))
169 die("invalid filesystem size");
171 opts.superblock_size >>= 9;
174 if (bch2_strtoull_h(optarg, &dev_opts.bucket_size))
175 die("bad bucket_size %s", optarg);
179 dev_opts.label = optarg;
182 dev_opts.discard = true;
185 dev_opts.data_allowed =
186 read_flag_list_or_die(optarg,
187 bch2_data_types, "data type");
190 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
191 dev_opts.durability > BCH_REPLICAS_MAX)
192 die("invalid durability");
195 if (kstrtouint(optarg, 10, &opts.version))
196 die("invalid version");
198 case O_no_initialize:
202 darray_append(device_paths, optarg);
203 dev_opts.path = optarg;
204 darray_append(devices, dev_opts);
221 if (darray_empty(devices))
222 die("Please supply a device");
224 if (opts.encrypted && !no_passphrase) {
225 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
229 darray_foreach(dev, devices)
230 dev->fd = open_for_format(dev->path, force);
233 bch2_format(fs_opt_strs,
236 devices.item, darray_size(devices));
237 bch2_opt_strs_free(&fs_opt_strs);
240 struct printbuf buf = PRINTBUF;
242 buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
244 bch2_sb_to_text(&buf, sb, false, 1 << BCH_SB_FIELD_members);
245 printf("%s", buf.buf);
251 if (opts.passphrase) {
252 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
253 free(opts.passphrase);
256 darray_free(devices);
259 struct bch_opts mount_opts = bch2_opts_empty();
262 opt_set(mount_opts, verbose, true);
265 * Start the filesystem once, to allocate the journal and create
266 * the root directory:
268 struct bch_fs *c = bch2_fs_open(device_paths.item,
269 darray_size(device_paths),
272 die("error opening %s: %s", device_paths.item[0],
273 strerror(-PTR_ERR(c)));
278 darray_free(device_paths);
283 static void show_super_usage(void)
285 puts("bcachefs show-super \n"
286 "Usage: bcachefs show-super [OPTION].. device\n"
289 " -f, --fields=(fields) list of sections to print\n"
290 " -l, --layout print superblock layout\n"
291 " -h, --help display this help and exit\n"
292 "Report bugs to <linux-bcache@vger.kernel.org>");
296 int cmd_show_super(int argc, char *argv[])
298 static const struct option longopts[] = {
299 { "fields", 1, NULL, 'f' },
300 { "layout", 0, NULL, 'l' },
301 { "help", 0, NULL, 'h' },
304 unsigned fields = 1 << BCH_SB_FIELD_members;
305 bool print_layout = false;
308 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
311 fields = !strcmp(optarg, "all")
313 : read_flag_list_or_die(optarg,
314 bch2_sb_fields, "superblock field");
325 char *dev = arg_pop();
327 die("please supply a device");
329 die("too many arguments");
331 struct bch_opts opts = bch2_opts_empty();
333 opt_set(opts, noexcl, true);
334 opt_set(opts, nochanges, true);
336 struct bch_sb_handle sb;
337 int ret = bch2_read_super(dev, &opts, &sb);
339 die("Error opening %s: %s", dev, strerror(-ret));
341 struct printbuf buf = PRINTBUF;
343 buf.units = PRINTBUF_UNITS_HUMAN_READABLE;
345 bch2_sb_to_text(&buf, sb.sb, print_layout, fields);
346 printf("%s", buf.buf);
348 bch2_free_super(&sb);