]> git.sesse.net Git - bcachefs-tools-debian/blob - bcachefs.c
bcachefs: make usage output more consistent
[bcachefs-tools-debian] / bcachefs.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
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <errno.h>
13 #include <inttypes.h>
14 #include <limits.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <stdbool.h>
18 #include <stdint.h>
19 #include <string.h>
20 #include <sys/ioctl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "cmds.h"
25
26 static void usage(void)
27 {
28         puts("bcachefs - tool for managing bcachefs filesystems\n"
29              "usage: bcachefs <command> [<args>]\n"
30              "\n"
31              "Superblock commands:\n"
32              "  format               Format a new filesystem\n"
33              "  show-super           Dump superblock information to stdout\n"
34              "\n"
35              "Repair:\n"
36              "  fsck                 Check an existing filesystem for errors\n"
37              "\n"
38              "Startup/shutdown, assembly of multi device filesystems:\n"
39              "  assemble             Assemble an existing multi device filesystem\n"
40              "  incremental          Incrementally assemble an existing multi device filesystem\n"
41              "  run                  Start a partially assembled filesystem\n"
42              "  stop                   Stop a running filesystem\n"
43              "\n"
44              "Commands for managing a running filesystem:\n"
45              "  fs show              Show various information about a filesystem\n"
46              "  fs set               Modify filesystem options\n"
47              "\n"
48              "Commands for managing devices within a running filesystem:\n"
49              "  device add           Add a new device to an existing filesystem\n"
50              "  device remove        Remove a device from an existing filesystem\n"
51              "  device online        Re-add an existing member to a filesystem\n"
52              "  device offline       Take a device offline, without removing it\n"
53              "  device evacuate      Migrate data off of a specific device\n"
54              "  device set-state     Mark a device as failed\n"
55              "\n"
56              "Encryption:\n"
57              "  unlock               Unlock an encrypted filesystem prior to running/mounting\n"
58              "  set-passphrase       Change passphrase on an existing (unmounted) filesystem\n"
59              "  remove-passphrase    Remove passphrase on an existing (unmounted) filesystem\n"
60              "\n"
61              "Migrate:\n"
62              "  migrate              Migrate an existing filesystem to bcachefs, in place\n"
63              "  migrate-superblock   Add default superblock, after bcachefs migrate\n"
64              "\n"
65              "Debug:\n"
66              "These commands work on offline, unmounted filesystems\n"
67              "  dump                 Dump filesystem metadata to a qcow2 image\n"
68              "  list                 List filesystem metadata in textual form\n");
69 }
70
71 static char *full_cmd;
72
73 static char *pop_cmd(int *argc, char *argv[])
74 {
75         if (*argc < 2) {
76                 printf("%s: missing command\n", argv[0]);
77                 usage();
78                 exit(EXIT_FAILURE);
79         }
80
81         char *cmd = argv[1];
82         memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
83         (*argc)--;
84
85         full_cmd = mprintf("%s %s", full_cmd, cmd);
86         return cmd;
87 }
88
89 static int fs_cmds(int argc, char *argv[])
90 {
91         char *cmd = pop_cmd(&argc, argv);
92
93         if (!strcmp(cmd, "show"))
94                 return cmd_fs_show(argc, argv);
95         if (!strcmp(cmd, "set"))
96                 return cmd_fs_set(argc, argv);
97
98         usage();
99         return 0;
100 }
101
102 static int device_cmds(int argc, char *argv[])
103 {
104         char *cmd = pop_cmd(&argc, argv);
105
106         if (!strcmp(cmd, "add"))
107                 return cmd_device_add(argc, argv);
108         if (!strcmp(cmd, "remove"))
109                 return cmd_device_remove(argc, argv);
110         if (!strcmp(cmd, "online"))
111                 return cmd_device_online(argc, argv);
112         if (!strcmp(cmd, "offline"))
113                 return cmd_device_offline(argc, argv);
114         if (!strcmp(cmd, "evacuate"))
115                 return cmd_device_offline(argc, argv);
116         if (!strcmp(cmd, "set-state"))
117                 return cmd_device_set_state(argc, argv);
118
119         usage();
120         return 0;
121 }
122
123 int main(int argc, char *argv[])
124 {
125         full_cmd = argv[0];
126
127         setvbuf(stdout, NULL, _IOLBF, 0);
128
129         char *cmd = pop_cmd(&argc, argv);
130
131         if (!strcmp(cmd, "format"))
132                 return cmd_format(argc, argv);
133         if (!strcmp(cmd, "show-super"))
134                 return cmd_show_super(argc, argv);
135
136         if (!strcmp(cmd, "fsck"))
137                 return cmd_fsck(argc, argv);
138
139         if (!strcmp(cmd, "assemble"))
140                 return cmd_assemble(argc, argv);
141         if (!strcmp(cmd, "incremental"))
142                 return cmd_incremental(argc, argv);
143         if (!strcmp(cmd, "run"))
144                 return cmd_run(argc, argv);
145         if (!strcmp(cmd, "stop"))
146                 return cmd_stop(argc, argv);
147
148         if (!strcmp(cmd, "fs"))
149                 return fs_cmds(argc, argv);
150
151         if (!strcmp(cmd, "device"))
152                 return device_cmds(argc, argv);
153
154         if (!strcmp(cmd, "unlock"))
155                 return cmd_unlock(argc, argv);
156         if (!strcmp(cmd, "set-passphrase"))
157                 return cmd_set_passphrase(argc, argv);
158         if (!strcmp(cmd, "remove-passphrase"))
159                 return cmd_remove_passphrase(argc, argv);
160
161         if (!strcmp(cmd, "migrate"))
162                 return cmd_migrate(argc, argv);
163         if (!strcmp(cmd, "migrate-superblock"))
164                 return cmd_migrate_superblock(argc, argv);
165
166         if (!strcmp(cmd, "dump"))
167                 return cmd_dump(argc, argv);
168         if (!strcmp(cmd, "list"))
169                 return cmd_list(argc, argv);
170
171         printf("Unknown command %s\n", cmd);
172         usage();
173         exit(EXIT_FAILURE);
174 }