2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
19 #include <sys/types.h>
23 #include <uuid/uuid.h>
25 #include "ccan/darray/darray.h"
28 #include "libbcache.h"
31 /* Open a block device, do magic blkid stuff: */
32 static int open_for_format(const char *dev, bool force)
35 const char *fs_type = NULL, *fs_label = NULL;
36 size_t fs_type_len, fs_label_len;
39 if ((fd = open(dev, O_RDWR|O_EXCL)) == -1)
40 die("Can't open dev %s: %s\n", dev, strerror(errno));
45 if (!(pr = blkid_new_probe()))
47 if (blkid_probe_set_device(pr, fd, 0, 0))
49 if (blkid_probe_enable_partitions(pr, true))
51 if (blkid_do_fullprobe(pr) < 0)
54 blkid_probe_lookup_value(pr, "TYPE", &fs_type, &fs_type_len);
55 blkid_probe_lookup_value(pr, "LABEL", &fs_label, &fs_label_len);
59 printf("%s contains a %s filesystem labelled '%s'\n",
60 dev, fs_type, fs_label);
62 printf("%s contains a %s filesystem\n",
72 static void usage(void)
74 puts("bcache format - create a new bcache filesystem on one or more devices\n"
75 "Usage: bcache format [OPTION]... <devices>\n"
79 " --btree_node=size Btree node size, default 256k\n"
80 " --metadata_checksum_type=(none|crc32c|crc64)\n"
81 " --data_checksum_type=(none|crc32c|crc64)\n"
82 " --compression_type=(none|lz4|gzip)\n"
84 " --error_action=(continue|readonly|panic)\n"
85 " Action to take on filesystem error\n"
86 " -l, --label=label\n"
90 "Device specific options:\n"
91 " --fs_size=size Size of filesystem on device\n"
92 " --bucket=size bucket size\n"
93 " --discard Enable discards\n"
94 " -t, --tier=# tier of subsequent devices\n"
96 " -h, --help display this help and exit\n"
98 "Device specific options must come before corresponding devices, e.g.\n"
99 " bcache format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
101 "Report bugs to <linux-bcache@vger.kernel.org>");
106 OPT('b', block_size, required_argument) \
107 OPT(0, btree_node_size, required_argument) \
108 OPT(0, metadata_checksum_type, required_argument) \
109 OPT(0, data_checksum_type, required_argument) \
110 OPT(0, compression_type, required_argument) \
111 OPT(0, encrypted, no_argument) \
112 OPT('e', error_action, required_argument) \
113 OPT('L', label, required_argument) \
114 OPT('U', uuid, required_argument) \
115 OPT('f', force, no_argument) \
116 OPT(0, fs_size, required_argument) \
117 OPT(0, bucket_size, required_argument) \
118 OPT('t', tier, required_argument) \
119 OPT(0, discard, no_argument) \
120 OPT('h', help, no_argument)
124 #define OPT(shortopt, longopt, has_arg) Opt_##longopt,
129 static const struct option format_opts[] = {
130 #define OPT(shortopt, longopt, has_arg) { \
131 #longopt, has_arg, NULL, Opt_##longopt \
138 int cmd_format(int argc, char *argv[])
140 darray(struct dev_opts) devices;
141 struct dev_opts *dev;
142 unsigned block_size = 0;
143 unsigned btree_node_size = 0;
144 unsigned meta_csum_type = BCH_CSUM_CRC32C;
145 unsigned data_csum_type = BCH_CSUM_CRC32C;
146 unsigned compression_type = BCH_COMPRESSION_NONE;
147 bool encrypted = false;
148 unsigned on_error_action = BCH_ON_ERROR_RO;
153 /* Device specific options: */
154 u64 filesystem_size = 0;
155 unsigned bucket_size = 0;
157 bool discard = false;
158 char *passphrase = NULL;
161 darray_init(devices);
164 while ((opt = getopt_long(argc, argv,
171 block_size = hatoi_validate(optarg,
174 case Opt_btree_node_size:
175 btree_node_size = hatoi_validate(optarg,
178 case Opt_metadata_checksum_type:
179 meta_csum_type = read_string_list_or_die(optarg,
180 csum_types, "checksum type");
182 case Opt_data_checksum_type:
183 data_csum_type = read_string_list_or_die(optarg,
184 csum_types, "checksum type");
186 case Opt_compression_type:
187 compression_type = read_string_list_or_die(optarg,
188 compression_types, "compression type");
193 case Opt_error_action:
195 on_error_action = read_string_list_or_die(optarg,
196 error_actions, "error action");
200 label = strdup(optarg);
204 if (uuid_parse(optarg, uuid.b))
212 filesystem_size = hatoi(optarg) >> 9;
214 case Opt_bucket_size:
215 bucket_size = hatoi_validate(optarg, "bucket size");
219 tier = strtoul_or_die(optarg, CACHE_TIERS, "tier");
225 darray_append(devices, (struct dev_opts) {
226 .path = strdup(optarg),
227 .size = filesystem_size,
228 .bucket_size = bucket_size,
239 if (!darray_size(devices))
240 die("Please supply a device");
242 if (uuid_is_null(uuid.b))
243 uuid_generate(uuid.b);
248 passphrase = read_passphrase("Enter passphrase: ");
249 pass2 = read_passphrase("Enter same passphrase again: ");
251 if (strcmp(passphrase, pass2)) {
252 memzero_explicit(passphrase, strlen(passphrase));
253 memzero_explicit(pass2, strlen(pass2));
254 die("Passphrases do not match");
257 memzero_explicit(pass2, strlen(pass2));
261 darray_foreach(dev, devices)
262 dev->fd = open_for_format(dev->path, force);
264 bcache_format(devices.item, darray_size(devices),
278 memzero_explicit(passphrase, strlen(passphrase));