hammer2 - Add media dump command, improve help output
[dragonfly.git] / sbin / hammer2 / main.c
CommitLineData
2910a90c
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include "hammer2.h"
37
38static void usage(int code);
39
5ba65e34 40int DebugOpt;
bd878fc9 41int VerboseOpt;
5ba65e34
MD
42int NormalExit = 1; /* if set to 0 main() has to pthread_exit() */
43
2910a90c
MD
44int
45main(int ac, char **av)
46{
5ba65e34 47 const char *sel_path = NULL;
2910a90c 48 const char *uuid_str = NULL;
62efe6ec 49 const char *arg;
2910a90c
MD
50 int pfs_type = HAMMER2_PFSTYPE_NONE;
51 int quick_opt = 0;
52 int all_opt = 0;
53 int ecode = 0;
54 int ch;
55
62efe6ec
MD
56 srandomdev();
57
2910a90c
MD
58 /*
59 * Core options
60 */
bd878fc9 61 while ((ch = getopt(ac, av, "adqs:t:u:v")) != -1) {
2910a90c
MD
62 switch(ch) {
63 case 'a':
64 all_opt = 1;
65 break;
bd878fc9
MD
66 case 'd':
67 DebugOpt = 1;
68 break;
2910a90c
MD
69 case 'q':
70 /*
71 * Quick mode - do not block verifying certain
72 * operations such as (connect).
73 */
74 quick_opt = 1;
75 break;
76 case 's':
77 sel_path = optarg;
78 break;
79 case 't':
80 /*
81 * set node type for mkpfs
82 */
83 if (strcasecmp(optarg, "ADMIN") == 0) {
84 pfs_type = HAMMER2_PFSTYPE_ADMIN;
85 } else if (strcasecmp(optarg, "CACHE") == 0) {
86 pfs_type = HAMMER2_PFSTYPE_CACHE;
87 } else if (strcasecmp(optarg, "COPY") == 0) {
88 pfs_type = HAMMER2_PFSTYPE_COPY;
89 } else if (strcasecmp(optarg, "SLAVE") == 0) {
90 pfs_type = HAMMER2_PFSTYPE_SLAVE;
91 } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) {
92 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
93 } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) {
94 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
95 } else if (strcasecmp(optarg, "MASTER") == 0) {
96 pfs_type = HAMMER2_PFSTYPE_MASTER;
97 } else {
98 fprintf(stderr, "-t: Unrecognized node type\n");
99 usage(1);
100 }
101 break;
102 case 'u':
103 /*
104 * set uuid for mkpfs, else one will be generated
105 * (required for all except the MASTER node_type)
106 */
107 uuid_str = optarg;
108 break;
bd878fc9
MD
109 case 'v':
110 ++VerboseOpt;
5ba65e34 111 break;
2910a90c
MD
112 default:
113 fprintf(stderr, "Unknown option: %c\n", ch);
114 usage(1);
115 /* not reached */
116 break;
117 }
118 }
119
120 /*
121 * Adjust, then process the command
122 */
123 ac -= optind;
124 av += optind;
125 if (ac < 1) {
126 fprintf(stderr, "Missing command\n");
127 usage(1);
128 /* not reached */
129 }
130
131 if (strcmp(av[0], "connect") == 0) {
132 /*
133 * Add cluster connection
134 */
135 if (ac < 2) {
136 fprintf(stderr, "connect: missing argument\n");
137 usage(1);
138 }
139 ecode = cmd_remote_connect(sel_path, av[1]);
140 } else if (strcmp(av[0], "disconnect") == 0) {
141 /*
142 * Remove cluster connection
143 */
144 if (ac < 2) {
145 fprintf(stderr, "disconnect: missing argument\n");
146 usage(1);
147 }
148 ecode = cmd_remote_disconnect(sel_path, av[1]);
149 } else if (strcmp(av[0], "status") == 0) {
150 /*
151 * Get status of PFS and its connections (-a for all PFSs)
152 */
153 ecode = cmd_remote_status(sel_path, all_opt);
bd878fc9 154 } else if (strcmp(av[0], "pfs-list") == 0) {
ae183399
MD
155 /*
156 * List all PFSs
157 */
158 ecode = cmd_pfs_list(sel_path);
bd878fc9 159 } else if (strcmp(av[0], "pfs-create") == 0) {
2910a90c
MD
160 /*
161 * Create new PFS using pfs_type
162 */
ae183399 163 if (ac < 2) {
bd878fc9 164 fprintf(stderr, "pfs-create: requires name\n");
ae183399
MD
165 usage(1);
166 }
167 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
bd878fc9 168 } else if (strcmp(av[0], "pfs-delete") == 0) {
ae183399
MD
169 /*
170 * Delete a PFS by name
171 */
172 if (ac < 2) {
bd878fc9 173 fprintf(stderr, "pfs-delete: requires name\n");
ae183399
MD
174 usage(1);
175 }
176 ecode = cmd_pfs_delete(sel_path, av[1]);
2910a90c
MD
177 } else if (strcmp(av[0], "snapshot") == 0) {
178 /*
bd878fc9 179 * Create snapshot with optional pfs-type and optional
2910a90c
MD
180 * label override.
181 */
62efe6ec 182 } else if (strcmp(av[0], "service") == 0) {
2910a90c 183 /*
62efe6ec
MD
184 * Start the service daemon. This daemon accepts
185 * connections from local and remote clients, handles
186 * the security handshake, and manages the core messaging
187 * protocol.
2910a90c 188 */
62efe6ec 189 ecode = cmd_service();
9ab15106
MD
190 } else if (strcmp(av[0], "leaf") == 0) {
191 /*
192 * Start the management daemon for a specific PFS.
193 *
194 * This will typically connect to the local master node
195 * daemon, register the PFS, and then pass its side of
196 * the socket descriptor to the kernel HAMMER2 VFS via an
197 * ioctl(). The process and/or thread context remains in the
198 * kernel until the PFS is unmounted or the connection is
199 * lost, then returns from the ioctl.
200 *
201 * It is possible to connect directly to a remote master node
202 * instead of the local master node in situations where
203 * encryption is not desired or no local master node is
204 * desired. This is not recommended because it represents
205 * a single point of failure for the PFS's communications.
206 *
207 * Direct kernel<->kernel communication between HAMMER2 VFSs
208 * is theoretically possible for directly-connected
209 * registrations (i.e. where the spanning tree is degenerate),
210 * but not recommended. We specifically try to reduce the
211 * complexity of the HAMMER2 VFS kernel code.
212 */
213 ecode = cmd_leaf(sel_path);
bd878fc9 214 } else if (strcmp(av[0], "shell") == 0) {
9ab15106
MD
215 /*
216 * Connect to the command line monitor in the hammer2 master
217 * node for the machine using HAMMER2_DBG_SHELL messages.
218 */
bd878fc9 219 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
62efe6ec
MD
220 } else if (strcmp(av[0], "rsainit") == 0) {
221 /*
222 * Initialize a RSA keypair. If no target directory is
223 * specified we default to "/etc/hammer2".
224 */
225 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
226 ecode = cmd_rsainit(arg);
227 } else if (strcmp(av[0], "rsaenc") == 0) {
228 /*
229 * Encrypt the input symmetrically by running it through
230 * the specified public and/or private key files.
231 *
232 * If no key files are specified data is encoded using
233 * "/etc/hammer2/rsa.pub".
234 *
235 * WARNING: no padding is added, data stream must contain
236 * random padding for this to be secure.
237 *
238 * Used for debugging only
239 */
240 if (ac == 1) {
241 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
242 ecode = cmd_rsaenc(&rsapath, 1);
243 } else {
244 ecode = cmd_rsaenc((const char **)&av[1], ac - 1);
245 }
246 } else if (strcmp(av[0], "rsadec") == 0) {
247 /*
248 * Decrypt the input symmetrically by running it through
249 * the specified public and/or private key files.
250 *
251 * If no key files are specified data is decoded using
252 * "/etc/hammer2/rsa.prv".
253 *
254 * WARNING: no padding is added, data stream must contain
255 * random padding for this to be secure.
256 *
257 * Used for debugging only
258 */
259 if (ac == 1) {
260 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
261 ecode = cmd_rsadec(&rsapath, 1);
262 } else {
263 ecode = cmd_rsadec((const char **)&av[1], ac - 1);
264 }
bd878fc9
MD
265 } else if (strcmp(av[0], "show") == 0) {
266 /*
267 * Raw dump of filesystem. Use -v to check all crc's, and
268 * -vv to dump bulk file data.
269 */
270 if (ac != 2) {
271 fprintf(stderr, "show: requires device path\n");
272 usage(1);
273 } else {
274 cmd_show(av[1]);
275 }
2910a90c
MD
276 } else {
277 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
278 usage(1);
279 }
5ba65e34
MD
280
281 /*
282 * In DebugMode we may wind up starting several pthreads in the
283 * original process, in which case we have to let them run and
284 * not actually exit.
285 */
286 if (NormalExit) {
287 return (ecode);
288 } else {
289 pthread_exit(NULL);
290 _exit(2); /* NOT REACHED */
291 }
2910a90c
MD
292}
293
294static
295void
296usage(int code)
297{
298 fprintf(stderr,
299 "hammer2 [-s path] command...\n"
300 " -s path Select filesystem\n"
bd878fc9
MD
301 " -t type PFS type for pfs-create\n"
302 " -u uuid uuid for pfs-create\n"
303 "\n"
304 " connect <target> Add cluster link\n"
305 " disconnect <target> Del cluster link\n"
306 " status Report cluster status\n"
307 " pfs-list List PFSs\n"
308 " pfs-create <label> Create a PFS\n"
309 " pfs-delete <label> Destroy a PFS\n"
310 " snapshot Snapshot a PFS\n"
311 " service Start service daemon\n"
312 " leaf Start pfs leaf daemon\n"
313 " shell [<host>] Connect to debug shell\n"
314 " rsainit Initialize rsa fields\n"
315 " show devpath Raw hammer2 media dump\n"
2910a90c
MD
316 );
317 exit(code);
318}