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 darray(char *) device_paths;
113 struct format_opts opts = format_opts_default();
114 struct dev_opts dev_opts = dev_opts_default(), *dev;
115 bool force = false, no_passphrase = false, quiet = false;
119 darray_init(devices);
120 darray_init(device_paths);
122 struct bch_opt_strs fs_opt_strs =
123 bch2_cmdline_opts_get(&argc, argv, OPT_FORMAT);
124 struct bch_opts fs_opts = bch2_parse_opts(fs_opt_strs);
126 while ((opt = getopt_long(argc, argv,
132 if (kstrtouint(optarg, 10, &v) ||
134 v > BCH_REPLICAS_MAX)
135 die("invalid replicas");
137 opt_set(fs_opts, metadata_replicas, v);
138 opt_set(fs_opts, data_replicas, v);
141 opts.encrypted = true;
143 case O_no_passphrase:
144 no_passphrase = true;
152 if (uuid_parse(optarg, opts.uuid.b))
160 if (bch2_strtoull_h(optarg, &dev_opts.size))
161 die("invalid filesystem size");
166 dev_opts.bucket_size =
167 hatoi_validate(optarg, "bucket size");
171 dev_opts.group = optarg;
174 dev_opts.discard = true;
177 dev_opts.data_allowed =
178 read_flag_list_or_die(optarg,
179 bch2_data_types, "data type");
182 if (kstrtouint(optarg, 10, &dev_opts.durability) ||
183 dev_opts.durability > BCH_REPLICAS_MAX)
184 die("invalid durability");
187 darray_append(device_paths, optarg);
188 dev_opts.path = optarg;
189 darray_append(devices, dev_opts);
206 if (darray_empty(devices))
207 die("Please supply a device");
209 if (opts.encrypted && !no_passphrase)
210 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
212 darray_foreach(dev, devices)
213 dev->fd = open_for_format(dev->path, force);
216 bch2_format(fs_opt_strs,
219 devices.item, darray_size(devices));
222 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
225 if (opts.passphrase) {
226 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
227 free(opts.passphrase);
230 darray_free(devices);
232 if (!opts.passphrase) {
234 * Start the filesystem once, to allocate the journal and create
235 * the root directory:
237 struct bch_fs *c = bch2_fs_open(device_paths.item,
238 darray_size(device_paths),
241 die("error opening %s: %s", device_paths.item,
242 strerror(-PTR_ERR(c)));
250 static void show_super_usage(void)
252 puts("bcachefs show-super \n"
253 "Usage: bcachefs show-super [OPTION].. device\n"
256 " -f, --fields=(fields) list of sections to print\n"
257 " -l, --layout print superblock layout\n"
258 " -h, --help display this help and exit\n"
259 "Report bugs to <linux-bcache@vger.kernel.org>");
263 int cmd_show_super(int argc, char *argv[])
265 static const struct option longopts[] = {
266 { "fields", 1, NULL, 'f' },
267 { "layout", 0, NULL, 'l' },
268 { "help", 0, NULL, 'h' },
271 unsigned fields = 1 << BCH_SB_FIELD_members;
272 bool print_layout = false;
275 while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
278 fields = !strcmp(optarg, "all")
280 : read_flag_list_or_die(optarg,
281 bch2_sb_fields, "superblock field");
292 char *dev = arg_pop();
294 die("please supply a device");
296 die("too many arguments");
298 struct bch_opts opts = bch2_opts_empty();
300 opt_set(opts, noexcl, true);
301 opt_set(opts, nochanges, true);
303 struct bch_sb_handle sb;
304 int ret = bch2_read_super(dev, &opts, &sb);
306 die("Error opening %s: %s", dev, strerror(-ret));
308 bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
309 bch2_free_super(&sb);