]> git.sesse.net Git - bcachefs-tools-debian/blob - bcachefs.c
Add commands for changing and removing passphrase
[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
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    Readd 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\n"
64              "                   Add default superblock, after bcachefs migrate\n"
65              "\n"
66              "Debug:\n"
67              "These commands work on offline, unmounted filesystems\n"
68              "  dump             Dump filesystem metadata to a qcow2 image\n"
69              "  list             List filesystem metadata in textual form\n");
70 }
71
72 static char *full_cmd;
73
74 static char *pop_cmd(int *argc, char *argv[])
75 {
76         if (*argc < 2) {
77                 printf("%s: missing command\n", argv[0]);
78                 usage();
79                 exit(EXIT_FAILURE);
80         }
81
82         char *cmd = argv[1];
83         memmove(&argv[1], &argv[2], *argc * sizeof(argv[0]));
84         (*argc)--;
85
86         full_cmd = mprintf("%s %s", full_cmd, cmd);
87         return cmd;
88 }
89
90 static int fs_cmds(int argc, char *argv[])
91 {
92         char *cmd = pop_cmd(&argc, argv);
93
94         if (!strcmp(cmd, "show"))
95                 return cmd_fs_show(argc, argv);
96         if (!strcmp(cmd, "set"))
97                 return cmd_fs_set(argc, argv);
98
99         usage();
100         return 0;
101 }
102
103 static int device_cmds(int argc, char *argv[])
104 {
105         char *cmd = pop_cmd(&argc, argv);
106
107         if (!strcmp(cmd, "add"))
108                 return cmd_device_add(argc, argv);
109         if (!strcmp(cmd, "remove"))
110                 return cmd_device_remove(argc, argv);
111         if (!strcmp(cmd, "online"))
112                 return cmd_device_online(argc, argv);
113         if (!strcmp(cmd, "offline"))
114                 return cmd_device_offline(argc, argv);
115         if (!strcmp(cmd, "evacuate"))
116                 return cmd_device_offline(argc, argv);
117         if (!strcmp(cmd, "set-state"))
118                 return cmd_device_set_state(argc, argv);
119
120         usage();
121         return 0;
122 }
123
124 int main(int argc, char *argv[])
125 {
126         full_cmd = argv[0];
127
128         setvbuf(stdout, NULL, _IOLBF, 0);
129
130         char *cmd = pop_cmd(&argc, argv);
131
132         if (!strcmp(cmd, "format"))
133                 return cmd_format(argc, argv);
134         if (!strcmp(cmd, "show-super"))
135                 return cmd_show_super(argc, argv);
136
137         if (!strcmp(cmd, "fsck"))
138                 return cmd_fsck(argc, argv);
139
140         if (!strcmp(cmd, "assemble"))
141                 return cmd_assemble(argc, argv);
142         if (!strcmp(cmd, "incremental"))
143                 return cmd_incremental(argc, argv);
144         if (!strcmp(cmd, "run"))
145                 return cmd_run(argc, argv);
146         if (!strcmp(cmd, "stop"))
147                 return cmd_stop(argc, argv);
148
149         if (!strcmp(cmd, "fs"))
150                 return fs_cmds(argc, argv);
151
152         if (!strcmp(cmd, "device"))
153                 return device_cmds(argc, argv);
154
155         if (!strcmp(cmd, "unlock"))
156                 return cmd_unlock(argc, argv);
157         if (!strcmp(cmd, "set-passphrase"))
158                 return cmd_set_passphrase(argc, argv);
159         if (!strcmp(cmd, "remove-passphrase"))
160                 return cmd_remove_passphrase(argc, argv);
161
162         if (!strcmp(cmd, "migrate"))
163                 return cmd_migrate(argc, argv);
164         if (!strcmp(cmd, "migrate-superblock"))
165                 return cmd_migrate_superblock(argc, argv);
166
167         if (!strcmp(cmd, "dump"))
168                 return cmd_dump(argc, argv);
169         if (!strcmp(cmd, "list"))
170                 return cmd_list(argc, argv);
171
172         printf("Unknown command %s\n", cmd);
173         usage();
174         exit(EXIT_FAILURE);
175 }