2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
41 int NormalExit = 1; /* if set to 0 main() has to pthread_exit() */
45 static void usage(int code);
48 main(int ac, char **av)
50 const char *sel_path = NULL;
51 const char *uuid_str = NULL;
53 int pfs_type = DMSG_PFSTYPE_NONE;
59 signal(SIGPIPE, SIG_IGN);
65 while ((ch = getopt(ac, av, "adfrqs:t:u:v")) != -1) {
84 * set node type for mkpfs
86 if (strcasecmp(optarg, "CACHE") == 0) {
87 pfs_type = HAMMER2_PFSTYPE_CACHE;
88 } else if (strcasecmp(optarg, "COPY") == 0) {
89 pfs_type = HAMMER2_PFSTYPE_COPY;
90 } else if (strcasecmp(optarg, "SLAVE") == 0) {
91 pfs_type = HAMMER2_PFSTYPE_SLAVE;
92 } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) {
93 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
94 } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) {
95 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
96 } else if (strcasecmp(optarg, "MASTER") == 0) {
97 pfs_type = HAMMER2_PFSTYPE_MASTER;
99 fprintf(stderr, "-t: Unrecognized node type\n");
105 * set uuid for mkpfs, else one will be generated
106 * (required for all except the MASTER node_type)
123 fprintf(stderr, "Unknown option: %c\n", ch);
131 * Adjust, then process the command
136 fprintf(stderr, "Missing command\n");
141 if (strcmp(av[0], "connect") == 0) {
143 * Add cluster connection
146 fprintf(stderr, "connect: missing argument\n");
149 ecode = cmd_remote_connect(sel_path, av[1]);
150 } else if (strcmp(av[0], "chaindump") == 0) {
152 ecode = cmd_chaindump(".");
154 ecode = cmd_chaindump(av[1]);
155 } else if (strcmp(av[0], "debugspan") == 0) {
157 * Debug connection to the target hammer2 service and run
158 * the CONN/SPAN protocol.
161 fprintf(stderr, "debugspan: requires hostname\n");
164 ecode = cmd_debugspan(av[1]);
165 } else if (strcmp(av[0], "disconnect") == 0) {
167 * Remove cluster connection
170 fprintf(stderr, "disconnect: missing argument\n");
173 ecode = cmd_remote_disconnect(sel_path, av[1]);
174 } else if (strcmp(av[0], "hash") == 0) {
175 ecode = cmd_hash(ac - 1, (const char **)(void *)&av[1]);
176 } else if (strcmp(av[0], "status") == 0) {
178 * Get status of PFS and its connections (-a for all PFSs)
180 ecode = cmd_remote_status(sel_path, all_opt);
181 } else if (strcmp(av[0], "pfs-clid") == 0) {
183 * Print cluster id (uuid) for specific PFS
186 fprintf(stderr, "pfs-clid: requires name\n");
189 ecode = cmd_pfs_getid(sel_path, av[1], 0);
190 } else if (strcmp(av[0], "pfs-fsid") == 0) {
192 * Print private id (uuid) for specific PFS
195 fprintf(stderr, "pfs-fsid: requires name\n");
198 ecode = cmd_pfs_getid(sel_path, av[1], 1);
199 } else if (strcmp(av[0], "pfs-list") == 0) {
203 ecode = cmd_pfs_list(sel_path);
204 } else if (strcmp(av[0], "pfs-create") == 0) {
206 * Create new PFS using pfs_type
209 fprintf(stderr, "pfs-create: requires name\n");
212 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
213 } else if (strcmp(av[0], "pfs-delete") == 0) {
215 * Delete a PFS by name
218 fprintf(stderr, "pfs-delete: requires name\n");
221 ecode = cmd_pfs_delete(sel_path, av[1]);
222 } else if (strcmp(av[0], "snapshot") == 0) {
224 * Create snapshot with optional pfs-type and optional
228 fprintf(stderr, "pfs-snapshot: too many arguments\n");
232 ecode = cmd_pfs_snapshot(sel_path, NULL);
234 ecode = cmd_pfs_snapshot(sel_path, av[1]);
235 } else if (strcmp(av[0], "service") == 0) {
237 * Start the service daemon. This daemon accepts
238 * connections from local and remote clients, handles
239 * the security handshake, and manages the core messaging
242 ecode = cmd_service();
243 } else if (strcmp(av[0], "stat") == 0) {
244 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]);
245 } else if (strcmp(av[0], "leaf") == 0) {
247 * Start the management daemon for a specific PFS.
249 * This will typically connect to the local master node
250 * daemon, register the PFS, and then pass its side of
251 * the socket descriptor to the kernel HAMMER2 VFS via an
252 * ioctl(). The process and/or thread context remains in the
253 * kernel until the PFS is unmounted or the connection is
254 * lost, then returns from the ioctl.
256 * It is possible to connect directly to a remote master node
257 * instead of the local master node in situations where
258 * encryption is not desired or no local master node is
259 * desired. This is not recommended because it represents
260 * a single point of failure for the PFS's communications.
262 * Direct kernel<->kernel communication between HAMMER2 VFSs
263 * is theoretically possible for directly-connected
264 * registrations (i.e. where the spanning tree is degenerate),
265 * but not recommended. We specifically try to reduce the
266 * complexity of the HAMMER2 VFS kernel code.
268 ecode = cmd_leaf(sel_path);
269 } else if (strcmp(av[0], "shell") == 0) {
271 * Connect to the command line monitor in the hammer2 master
272 * node for the machine using HAMMER2_DBG_SHELL messages.
274 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
275 } else if (strcmp(av[0], "rsainit") == 0) {
277 * Initialize a RSA keypair. If no target directory is
278 * specified we default to "/etc/hammer2".
280 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
281 ecode = cmd_rsainit(arg);
282 } else if (strcmp(av[0], "rsaenc") == 0) {
284 * Encrypt the input symmetrically by running it through
285 * the specified public and/or private key files.
287 * If no key files are specified data is encoded using
288 * "/etc/hammer2/rsa.pub".
290 * WARNING: no padding is added, data stream must contain
291 * random padding for this to be secure.
293 * Used for debugging only
296 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
297 ecode = cmd_rsaenc(&rsapath, 1);
299 ecode = cmd_rsaenc((const char **)(void *)&av[1],
302 } else if (strcmp(av[0], "rsadec") == 0) {
304 * Decrypt the input symmetrically by running it through
305 * the specified public and/or private key files.
307 * If no key files are specified data is decoded using
308 * "/etc/hammer2/rsa.prv".
310 * WARNING: no padding is added, data stream must contain
311 * random padding for this to be secure.
313 * Used for debugging only
316 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
317 ecode = cmd_rsadec(&rsapath, 1);
319 ecode = cmd_rsadec((const char **)(void *)&av[1],
322 } else if (strcmp(av[0], "show") == 0) {
324 * Raw dump of filesystem. Use -v to check all crc's, and
325 * -vv to dump bulk file data.
328 fprintf(stderr, "show: requires device path\n");
333 } else if (strcmp(av[0], "freemap") == 0) {
335 * Raw dump of freemap. Use -v to check all crc's, and
336 * -vv to dump bulk file data.
339 fprintf(stderr, "freemap: requires device path\n");
344 } else if (strcmp(av[0], "setcomp") == 0) {
347 * Missing compression method and at least one
351 "setcomp: requires compression method and"
352 "directory/file path\n");
356 * Multiple paths may be specified
358 ecode = cmd_setcomp(av[1], &av[2]);
360 } else if (strcmp(av[0], "printinode") == 0) {
363 "printinode: requires directory/file path\n");
369 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
374 * In DebugMode we may wind up starting several pthreads in the
375 * original process, in which case we have to let them run and
382 _exit(2); /* NOT REACHED */
391 "hammer2 [-s path] command...\n"
392 " -s path Select filesystem\n"
393 " -t type PFS type for pfs-create\n"
394 " -u uuid uuid for pfs-create\n"
398 " disconnect <target> "
401 "Print directory hash\n"
403 "Report cluster status\n"
407 "Print cluster id for specific PFS\n"
409 "Print private id for specific PFS\n"
410 " pfs-create <label> "
412 " pfs-delete <label> "
414 " snapshot [<label>] "
417 "Start service daemon\n"
419 "Return inode quota & config\n"
421 "Start pfs leaf daemon\n"
423 "Connect to debug shell\n"
424 " debugspan <target> "
425 "Connect to target, run CONN/SPAN\n"
427 "Initialize rsa fields\n"
429 "Raw hammer2 media dump\n"
431 "Raw hammer2 media dump\n"
432 " setcomp comp[:level] path "
433 "Set compression {none, autozero, lz4, zlib} & level\n"