]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_format.c
readd -q (--quiet)
[bcachefs-tools-debian] / cmd_format.c
1 /*
2  * Authors: Kent Overstreet <kent.overstreet@gmail.com>
3  *          Gabriel de Perthuis <g2p.code@gmail.com>
4  *          Jacob Malevich <jam@datera.io>
5  *
6  * GPLv2
7  */
8 #include <ctype.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <stdbool.h>
13 #include <stdint.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 #include <uuid/uuid.h>
22
23 #include "ccan/darray/darray.h"
24
25 #include "cmds.h"
26 #include "libbcachefs.h"
27 #include "crypto.h"
28 #include "libbcachefs/opts.h"
29 #include "libbcachefs/super-io.h"
30 #include "libbcachefs/util.h"
31
32 #define OPTS                                            \
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)
47
48 static void usage(void)
49 {
50         puts("bcachefs format - create a new bcachefs filesystem on one or more devices\n"
51              "Usage: bcachefs format [OPTION]... <devices>\n"
52              "\n"
53              "Options:");
54
55         bch2_opts_usage(OPT_FORMAT);
56
57         puts(
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"
62              "  -U, --uuid=uuid\n"
63              "\n"
64              "Device specific options:");
65
66         bch2_opts_usage(OPT_DEVICE);
67
68         puts("  -g, --group=label           Disk group\n"
69              "\n"
70              "  -f, --force\n"
71              "  -q, --quiet                 Only print errors\n"
72              "  -h, --help                  Display this help and exit\n"
73              "\n"
74              "Device specific options must come before corresponding devices, e.g.\n"
75              "  bcachefs format --group cache /dev/sdb /dev/sdc\n"
76              "\n"
77              "Report bugs to <linux-bcache@vger.kernel.org>");
78 }
79
80 enum {
81         O_no_opt = 1,
82 #define x(shortopt, longopt, arg)       O_##longopt,
83         OPTS
84 #undef x
85 };
86
87 #define x(shortopt, longopt, arg) {                     \
88         .name           = #longopt,                     \
89         .has_arg        = arg,                          \
90         .flag           = NULL,                         \
91         .val            = O_##longopt,                  \
92 },
93 static const struct option format_opts[] = {
94         OPTS
95         { NULL }
96 };
97 #undef x
98
99 u64 read_flag_list_or_die(char *opt, const char * const list[],
100                           const char *msg)
101 {
102         u64 v = bch2_read_flag_list(opt, list);
103         if (v == (u64) -1)
104                 die("Bad %s %s", msg, opt);
105
106         return v;
107 }
108
109 int cmd_format(int argc, char *argv[])
110 {
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;
115         unsigned v;
116         int opt;
117
118         darray_init(devices);
119
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);
123
124         while ((opt = getopt_long(argc, argv,
125                                   "-L:U:fqh",
126                                   format_opts,
127                                   NULL)) != -1)
128                 switch (opt) {
129                 case O_replicas:
130                         if (kstrtouint(optarg, 10, &v) ||
131                             !v ||
132                             v > BCH_REPLICAS_MAX)
133                                 die("invalid replicas");
134
135                         opt_set(fs_opts, metadata_replicas, v);
136                         opt_set(fs_opts, data_replicas, v);
137                         break;
138                 case O_encrypted:
139                         opts.encrypted = true;
140                         break;
141                 case O_no_passphrase:
142                         no_passphrase = true;
143                         break;
144                 case O_label:
145                 case 'L':
146                         opts.label = optarg;
147                         break;
148                 case O_uuid:
149                 case 'U':
150                         if (uuid_parse(optarg, opts.uuid.b))
151                                 die("Bad uuid");
152                         break;
153                 case O_force:
154                 case 'f':
155                         force = true;
156                         break;
157                 case O_fs_size:
158                         if (bch2_strtoull_h(optarg, &dev_opts.size))
159                                 die("invalid filesystem size");
160
161                         dev_opts.size >>= 9;
162                         break;
163                 case O_bucket_size:
164                         dev_opts.bucket_size =
165                                 hatoi_validate(optarg, "bucket size");
166                         break;
167                 case O_group:
168                 case 'g':
169                         dev_opts.group = optarg;
170                         break;
171                 case O_discard:
172                         dev_opts.discard = true;
173                         break;
174                 case O_data_allowed:
175                         dev_opts.data_allowed =
176                                 read_flag_list_or_die(optarg,
177                                         bch2_data_types, "data type");
178                         break;
179                 case O_durability:
180                         if (kstrtouint(optarg, 10, &dev_opts.durability) ||
181                             dev_opts.durability > BCH_REPLICAS_MAX)
182                                 die("invalid durability");
183                         break;
184                 case O_no_opt:
185                         dev_opts.path = optarg;
186                         darray_append(devices, dev_opts);
187                         dev_opts.size = 0;
188                         break;
189                 case O_quiet:
190                 case 'q':
191                         quiet = true;
192                         break;
193                 case O_help:
194                 case 'h':
195                         usage();
196                         exit(EXIT_SUCCESS);
197                         break;
198                 case '?':
199                         die("unrecognized option %s", optarg);
200                         break;
201                 }
202
203         if (darray_empty(devices))
204                 die("Please supply a device");
205
206         if (opts.encrypted && !no_passphrase)
207                 opts.passphrase = read_passphrase_twice("Enter passphrase: ");
208
209         darray_foreach(dev, devices)
210                 dev->fd = open_for_format(dev->path, force);
211
212         struct bch_sb *sb =
213                 bch2_format(fs_opt_strs,
214                             fs_opts,
215                             opts,
216                             devices.item, darray_size(devices));
217
218         if (!quiet)
219                 bch2_sb_print(sb, false, 1 << BCH_SB_FIELD_members, HUMAN_READABLE);
220         free(sb);
221
222         if (opts.passphrase) {
223                 memzero_explicit(opts.passphrase, strlen(opts.passphrase));
224                 free(opts.passphrase);
225         }
226
227         darray_free(devices);
228
229         return 0;
230 }
231
232 static void show_super_usage(void)
233 {
234         puts("bcachefs show-super \n"
235              "Usage: bcachefs show-super [OPTION].. device\n"
236              "\n"
237              "Options:\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>");
242         exit(EXIT_SUCCESS);
243 }
244
245 int cmd_show_super(int argc, char *argv[])
246 {
247         static const struct option longopts[] = {
248                 { "fields",                     1, NULL, 'f' },
249                 { "layout",                     0, NULL, 'l' },
250                 { "help",                       0, NULL, 'h' },
251                 { NULL }
252         };
253         unsigned fields = 1 << BCH_SB_FIELD_members;
254         bool print_layout = false;
255         int opt;
256
257         while ((opt = getopt_long(argc, argv, "f:lh", longopts, NULL)) != -1)
258                 switch (opt) {
259                 case 'f':
260                         fields = !strcmp(optarg, "all")
261                                 ? ~0
262                                 : read_flag_list_or_die(optarg,
263                                         bch2_sb_fields, "superblock field");
264                         break;
265                 case 'l':
266                         print_layout = true;
267                         break;
268                 case 'h':
269                         show_super_usage();
270                         break;
271                 }
272         args_shift(optind);
273
274         char *dev = arg_pop();
275         if (!dev)
276                 die("please supply a device");
277         if (argc)
278                 die("too many arguments");
279
280         struct bch_opts opts = bch2_opts_empty();
281
282         opt_set(opts, noexcl,   true);
283         opt_set(opts, nochanges, true);
284
285         struct bch_sb_handle sb;
286         int ret = bch2_read_super(dev, &opts, &sb);
287         if (ret)
288                 die("Error opening %s: %s", dev, strerror(-ret));
289
290         bch2_sb_print(sb.sb, print_layout, fields, HUMAN_READABLE);
291         bch2_free_super(&sb);
292         return 0;
293 }