2 * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3 * Gabriel de Perthuis <g2p.code@gmail.com>
4 * Jacob Malevich <jam@datera.io>
17 #include <sys/types.h>
21 #include <uuid/uuid.h>
23 #include "ccan/darray/darray.h"
25 #include "bcache-cmds.h"
26 #include "libbcache.h"
28 /* Open a block device, do magic blkid stuff: */
29 static int open_for_format(const char *dev, bool force)
32 const char *fs_type = NULL, *fs_label = NULL;
33 size_t fs_type_len, fs_label_len;
36 if ((fd = open(dev, O_RDWR|O_EXCL)) == -1)
37 die("Can't open dev %s: %s\n", dev, strerror(errno));
42 if (!(pr = blkid_new_probe()))
44 if (blkid_probe_set_device(pr, fd, 0, 0))
46 if (blkid_probe_enable_partitions(pr, true))
48 if (blkid_do_fullprobe(pr) < 0)
51 blkid_probe_lookup_value(pr, "TYPE", &fs_type, &fs_type_len);
52 blkid_probe_lookup_value(pr, "LABEL", &fs_label, &fs_label_len);
56 printf("%s contains a %s filesystem labelled '%s'\n",
57 dev, fs_type, fs_label);
59 printf("%s contains a %s filesystem\n",
69 static void usage(void)
71 puts("bcache format - create a new bcache filesystem on one or more devices\n"
72 "Usage: bcache format [OPTION]... <devices>\n"
76 " --btree_node=size Btree node size, default 256k\n"
77 " --metadata_checksum_type=(none|crc32c|crc64)\n"
78 " --data_checksum_type=(none|crc32c|crc64)\n"
79 " --compression_type=(none|lz4|gzip)\n"
80 " --error_action=(continue|readonly|panic)\n"
81 " Action to take on filesystem error\n"
82 " --max_journal_entry_size=size\n"
83 " -l, --label=label\n"
87 "Device specific options:\n"
88 " --fs_size=size Size of filesystem on device\n"
89 " --bucket=size bucket size\n"
90 " --discard Enable discards\n"
91 " -t, --tier=# tier of subsequent devices\n"
93 " -h, --help display this help and exit\n"
95 "Device specific options must come before corresponding devices, e.g.\n"
96 " bcache format --tier 0 /dev/sdb --tier 1 /dev/sdc\n"
98 "Report bugs to <linux-bcache@vger.kernel.org>");
103 OPT('b', block_size, required_argument) \
104 OPT(0, btree_node_size, required_argument) \
105 OPT(0, metadata_checksum_type, required_argument) \
106 OPT(0, data_checksum_type, required_argument) \
107 OPT(0, compression_type, required_argument) \
108 OPT('e', error_action, required_argument) \
109 OPT(0, max_journal_entry_size, required_argument) \
110 OPT('L', label, required_argument) \
111 OPT('U', uuid, required_argument) \
112 OPT('f', force, no_argument) \
113 OPT(0, fs_size, required_argument) \
114 OPT(0, bucket_size, required_argument) \
115 OPT('t', tier, required_argument) \
116 OPT(0, discard, no_argument) \
117 OPT('h', help, no_argument)
121 #define OPT(shortopt, longopt, has_arg) Opt_##longopt,
126 static const struct option format_opts[] = {
127 #define OPT(shortopt, longopt, has_arg) { \
128 #longopt, has_arg, NULL, Opt_##longopt \
135 int cmd_format(int argc, char *argv[])
137 darray(struct dev_opts) devices;
138 struct dev_opts *dev;
139 unsigned block_size = 0;
140 unsigned btree_node_size = 0;
141 unsigned meta_csum_type = BCH_CSUM_CRC32C;
142 unsigned data_csum_type = BCH_CSUM_CRC32C;
143 unsigned compression_type = BCH_COMPRESSION_NONE;
144 unsigned on_error_action = BCH_ON_ERROR_RO;
149 /* Device specific options: */
150 u64 filesystem_size = 0;
151 unsigned bucket_size = 0;
153 bool discard = false;
154 unsigned max_journal_entry_size = 0;
155 char *passphrase = NULL;
158 darray_init(devices);
161 while ((opt = getopt_long(argc, argv,
168 block_size = hatoi_validate(optarg,
171 case Opt_btree_node_size:
172 btree_node_size = hatoi_validate(optarg,
175 case Opt_metadata_checksum_type:
176 meta_csum_type = read_string_list_or_die(optarg,
177 csum_types, "checksum type");
179 case Opt_data_checksum_type:
180 data_csum_type = read_string_list_or_die(optarg,
181 csum_types, "checksum type");
183 case Opt_compression_type:
184 compression_type = read_string_list_or_die(optarg,
185 compression_types, "compression type");
187 case Opt_error_action:
189 on_error_action = read_string_list_or_die(optarg,
190 error_actions, "error action");
192 case Opt_max_journal_entry_size:
193 max_journal_entry_size = hatoi_validate(optarg,
194 "journal entry size");
198 label = strdup(optarg);
202 if (uuid_parse(optarg, uuid.b))
210 filesystem_size = hatoi(optarg) >> 9;
212 case Opt_bucket_size:
213 bucket_size = hatoi_validate(optarg, "bucket size");
217 tier = strtoul_or_die(optarg, CACHE_TIERS, "tier");
223 darray_append(devices, (struct dev_opts) {
224 .path = strdup(optarg),
225 .size = filesystem_size,
226 .bucket_size = bucket_size,
237 if (!darray_size(devices))
238 die("Please supply a device");
240 if (uuid_is_null(uuid.b))
241 uuid_generate(uuid.b);
243 darray_foreach(dev, devices)
244 dev->fd = open_for_format(dev->path, force);
246 bcache_format(devices.item, darray_size(devices),
255 max_journal_entry_size,
260 memzero_explicit(passphrase, strlen(passphrase));