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', label, required_argument) \
37 x('U', uuid, required_argument) \
38 x(0, fs_size, required_argument) \
39 x(0, bucket_size, required_argument) \
40 x('g', group, required_argument) \
41 x(0, discard, no_argument) \
42 x(0, data_allowed, required_argument) \
43 x(0, durability, required_argument) \
44 x('f', force, no_argument) \
45 x('q', quiet, no_argument) \
46 x('h', help, no_argument)
48 static void usage(void)
50 puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
51 "Usage: bcachefs format [OPTION]... <devices>\n"
55 bch2_opts_usage(OPT_FORMAT);
58 " --replicas=# Sets both data and metadata replicas\n"
59 " --encrypted Enable whole filesystem encryption (chacha20/poly1305)\n"
60 " --no_passphrase Don't encrypt master encryption key\n"
61 " -L, --label=label\n"
64 "Device specific options:");
66 bch2_opts_usage(OPT_DEVICE);
68 puts(" -g, --group=label Disk group\n"
71 " -q, --quiet Only print errors\n"
72 " -h, --help Display this help and exit\n"
74 "Device specific options must come before corresponding devices, e.g.\n"
75 " bcachefs format --group cache /dev/sdb /dev/sdc\n"
77 "Report bugs to <linux-bcache@vger.kernel.org>");
82 #define x(shortopt, longopt, arg) O_##longopt,
87 #define x(shortopt, longopt, arg) { \
93 static const struct option format_opts[] = {
99 u64 read_flag_list_or_die(char *opt, const char * const list[],
102 u64 v = bch2_read_flag_list(opt, list);
104 die("Bad %s %s", msg, opt);
109 int cmd_format(int argc, char *argv[])
111 darray(struct dev_opts) devices;
112 struct format_opts opts = format_opts_default();
113 struct dev_opts dev_opts = dev_opts_default(), *dev;
114 bool force = false, no_passphrase = false, quiet = false;
118 darray_init(devices);
120 struct bch_opt_strs fs_opt_strs =
121 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
122 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
124 while ((opt = getopt_long(argc, argv,
130 if (kstrtouint(optarg, 10, &v) ||
132 v > BCH_REPLICAS_MAX)
133 die("invalid replicas");
135 opt_set(fs_opts, metadata_replicas, v);
136 opt_set(fs_opts, data_replicas, v);
139 opts.encrypted = true;
141 case O_no_passphrase:
142 no_passphrase = true;
150 if (uuid_parse(optarg, opts.uuid.b))
158 if (bch2_strtoull_h(optarg, &dev_opts.size))
159 die("invalid filesystem size");
164 dev_opts.bucket_size =
165 hatoi_validate(optarg, "bucket size");
169 dev_opts.group = optarg;
172 dev_opts.discard = true;
175 dev_opts.data_allowed =
176 read_flag_list_or_die(optarg,
177 bch2_data_types, "data type");
180 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
181 dev_opts.durability > BCH_REPLICAS_MAX)
182 die("invalid durability");
185 dev_opts.path = optarg;
186 darray_append(devices, dev_opts);
203 if (darray_empty(devices))
204 die("Please supply a device");
206 if (opts.encrypted && !no_passphrase)
207 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
209 darray_foreach(dev, devices)
210 dev->fd = open_for_format(dev->path, force);
213 bch2_format(fs_opt_strs,
216 devices.item, darray_size(devices));
219 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
222 if (opts.passphrase) {
223 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
224 free(opts.passphrase);
227 darray_free(devices);
232 static void show_super_usage(void)
234 puts("bcachefs show-super \n"
235 "Usage: bcachefs show-super [OPTION].. device\n"
238 " -f, --fields=(fields) list of sections to print\n"
239 " -l, --layout print superblock layout\n"
240 " -h, --help display this help and exit\n"
241 "Report bugs to <linux-bcache@vger.kernel.org>");
245 int cmd_show_super(int argc, char *argv[])
247 static const struct option longopts[] = {
248 { "fields", 1, NULL, 'f' },
249 { "layout", 0, NULL, 'l' },
250 { "help", 0, NULL, 'h' },
253 unsigned fields = 1 << BCH_SB_FIELD_members;
254 bool print_layout = false;
257 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
260 fields = !strcmp(optarg, "all")
262 : read_flag_list_or_die(optarg,
263 bch2_sb_fields, "superblock field");
274 char *dev = arg_pop();
276 die("please supply a device");
278 die("too many arguments");
280 struct bch_opts opts = bch2_opts_empty();
282 opt_set(opts, noexcl, true);
283 opt_set(opts, nochanges, true);
285 struct bch_sb_handle sb;
286 int ret = bch2_read_super(dev, &opts, &sb);
288 die("Error opening %s: %s", dev, strerror(-ret));
290 bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
291 bch2_free_super(&sb);