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
38 static void usage(int code);
43 int NormalExit = 1; /* if set to 0 main() has to pthread_exit() */
46 main(int ac, char **av)
48 const char *sel_path = NULL;
49 const char *uuid_str = NULL;
51 int pfs_type = HAMMER2_PFSTYPE_NONE;
57 signal(SIGPIPE, SIG_IGN);
63 while ((ch = getopt(ac, av, "adqs:t:u:v")) != -1) {
76 * set node type for mkpfs
78 if (strcasecmp(optarg, "ADMIN") == 0) {
79 pfs_type = HAMMER2_PFSTYPE_ADMIN;
80 } else if (strcasecmp(optarg, "CACHE") == 0) {
81 pfs_type = HAMMER2_PFSTYPE_CACHE;
82 } else if (strcasecmp(optarg, "COPY") == 0) {
83 pfs_type = HAMMER2_PFSTYPE_COPY;
84 } else if (strcasecmp(optarg, "SLAVE") == 0) {
85 pfs_type = HAMMER2_PFSTYPE_SLAVE;
86 } else if (strcasecmp(optarg, "SOFT_SLAVE") == 0) {
87 pfs_type = HAMMER2_PFSTYPE_SOFT_SLAVE;
88 } else if (strcasecmp(optarg, "SOFT_MASTER") == 0) {
89 pfs_type = HAMMER2_PFSTYPE_SOFT_MASTER;
90 } else if (strcasecmp(optarg, "MASTER") == 0) {
91 pfs_type = HAMMER2_PFSTYPE_MASTER;
93 fprintf(stderr, "-t: Unrecognized node type\n");
99 * set uuid for mkpfs, else one will be generated
100 * (required for all except the MASTER node_type)
117 fprintf(stderr, "Unknown option: %c\n", ch);
125 * Adjust, then process the command
130 fprintf(stderr, "Missing command\n");
135 if (strcmp(av[0], "connect") == 0) {
137 * Add cluster connection
140 fprintf(stderr, "connect: missing argument\n");
143 ecode = cmd_remote_connect(sel_path, av[1]);
144 } else if (strcmp(av[0], "debugspan") == 0) {
146 * Debug connection to the target hammer2 service and run
147 * the CONN/SPAN protocol.
150 fprintf(stderr, "debugspan: requires hostname\n");
153 ecode = cmd_debugspan(av[1]);
154 } else if (strcmp(av[0], "disconnect") == 0) {
156 * Remove cluster connection
159 fprintf(stderr, "disconnect: missing argument\n");
162 ecode = cmd_remote_disconnect(sel_path, av[1]);
163 } else if (strcmp(av[0], "status") == 0) {
165 * Get status of PFS and its connections (-a for all PFSs)
167 ecode = cmd_remote_status(sel_path, all_opt);
168 } else if (strcmp(av[0], "pfs-clid") == 0) {
170 * Print cluster id (uuid) for specific PFS
173 fprintf(stderr, "pfs-clid: requires name\n");
176 ecode = cmd_pfs_getid(sel_path, av[1], 0);
177 } else if (strcmp(av[0], "pfs-fsid") == 0) {
179 * Print private id (uuid) for specific PFS
182 fprintf(stderr, "pfs-fsid: requires name\n");
185 ecode = cmd_pfs_getid(sel_path, av[1], 1);
186 } else if (strcmp(av[0], "pfs-list") == 0) {
190 ecode = cmd_pfs_list(sel_path);
191 } else if (strcmp(av[0], "pfs-create") == 0) {
193 * Create new PFS using pfs_type
196 fprintf(stderr, "pfs-create: requires name\n");
199 ecode = cmd_pfs_create(sel_path, av[1], pfs_type, uuid_str);
200 } else if (strcmp(av[0], "pfs-delete") == 0) {
202 * Delete a PFS by name
205 fprintf(stderr, "pfs-delete: requires name\n");
208 ecode = cmd_pfs_delete(sel_path, av[1]);
209 } else if (strcmp(av[0], "snapshot") == 0) {
211 * Create snapshot with optional pfs-type and optional
215 fprintf(stderr, "pfs-snapshot: too many arguments\n");
219 ecode = cmd_pfs_snapshot(sel_path, NULL);
221 ecode = cmd_pfs_snapshot(sel_path, av[1]);
222 } else if (strcmp(av[0], "service") == 0) {
224 * Start the service daemon. This daemon accepts
225 * connections from local and remote clients, handles
226 * the security handshake, and manages the core messaging
229 ecode = cmd_service();
230 } else if (strcmp(av[0], "stat") == 0) {
231 ecode = cmd_stat(ac - 1, (const char **)(void *)&av[1]);
232 } else if (strcmp(av[0], "leaf") == 0) {
234 * Start the management daemon for a specific PFS.
236 * This will typically connect to the local master node
237 * daemon, register the PFS, and then pass its side of
238 * the socket descriptor to the kernel HAMMER2 VFS via an
239 * ioctl(). The process and/or thread context remains in the
240 * kernel until the PFS is unmounted or the connection is
241 * lost, then returns from the ioctl.
243 * It is possible to connect directly to a remote master node
244 * instead of the local master node in situations where
245 * encryption is not desired or no local master node is
246 * desired. This is not recommended because it represents
247 * a single point of failure for the PFS's communications.
249 * Direct kernel<->kernel communication between HAMMER2 VFSs
250 * is theoretically possible for directly-connected
251 * registrations (i.e. where the spanning tree is degenerate),
252 * but not recommended. We specifically try to reduce the
253 * complexity of the HAMMER2 VFS kernel code.
255 ecode = cmd_leaf(sel_path);
256 } else if (strcmp(av[0], "shell") == 0) {
258 * Connect to the command line monitor in the hammer2 master
259 * node for the machine using HAMMER2_DBG_SHELL messages.
261 ecode = cmd_shell((ac < 2) ? NULL : av[1]);
262 } else if (strcmp(av[0], "rsainit") == 0) {
264 * Initialize a RSA keypair. If no target directory is
265 * specified we default to "/etc/hammer2".
267 arg = (ac < 2) ? HAMMER2_DEFAULT_DIR : av[1];
268 ecode = cmd_rsainit(arg);
269 } else if (strcmp(av[0], "rsaenc") == 0) {
271 * Encrypt the input symmetrically by running it through
272 * the specified public and/or private key files.
274 * If no key files are specified data is encoded using
275 * "/etc/hammer2/rsa.pub".
277 * WARNING: no padding is added, data stream must contain
278 * random padding for this to be secure.
280 * Used for debugging only
283 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.pub";
284 ecode = cmd_rsaenc(&rsapath, 1);
286 ecode = cmd_rsaenc((const char **)(void *)&av[1],
289 } else if (strcmp(av[0], "rsadec") == 0) {
291 * Decrypt the input symmetrically by running it through
292 * the specified public and/or private key files.
294 * If no key files are specified data is decoded using
295 * "/etc/hammer2/rsa.prv".
297 * WARNING: no padding is added, data stream must contain
298 * random padding for this to be secure.
300 * Used for debugging only
303 const char *rsapath = HAMMER2_DEFAULT_DIR "/rsa.prv";
304 ecode = cmd_rsadec(&rsapath, 1);
306 ecode = cmd_rsadec((const char **)(void *)&av[1],
309 } else if (strcmp(av[0], "show") == 0) {
311 * Raw dump of filesystem. Use -v to check all crc's, and
312 * -vv to dump bulk file data.
315 fprintf(stderr, "show: requires device path\n");
321 fprintf(stderr, "Unrecognized command: %s\n", av[0]);
326 * In DebugMode we may wind up starting several pthreads in the
327 * original process, in which case we have to let them run and
334 _exit(2); /* NOT REACHED */
343 "hammer2 [-s path] command...\n"
344 " -s path Select filesystem\n"
345 " -t type PFS type for pfs-create\n"
346 " -u uuid uuid for pfs-create\n"
348 " connect <target> Add cluster link\n"
349 " disconnect <target> Del cluster link\n"
350 " status Report cluster status\n"
351 " pfs-list List PFSs\n"
352 " pfs-clid <label> Print cluster id for specific PFS\n"
353 " pfs-fsid <label> Print private id for specific PFS\n"
354 " pfs-create <label> Create a PFS\n"
355 " pfs-delete <label> Destroy a PFS\n"
356 " snapshot Snapshot a PFS\n"
357 " service Start service daemon\n"
358 " stat [<path>] Return inode quota & config\n"
359 " leaf Start pfs leaf daemon\n"
360 " shell [<host>] Connect to debug shell\n"
361 " debugspan <target> Connect to target, run CONN/SPAN\n"
362 " rsainit Initialize rsa fields\n"
363 " show devpath Raw hammer2 media dump\n"