xref: /freebsd/usr.sbin/mptutil/mptutil.c (revision aa0a1e58)
1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$FreeBSD$");
33 
34 #include <sys/param.h>
35 #include <sys/errno.h>
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include "mptutil.h"
42 
43 SET_DECLARE(MPT_DATASET(top), struct mptutil_command);
44 
45 int mpt_unit;
46 
47 static void
48 usage(void)
49 {
50 
51 	fprintf(stderr, "usage: mptutil [-u unit] <command> ...\n\n");
52 	fprintf(stderr, "Commands include:\n");
53 	fprintf(stderr, "    version\n");
54 	fprintf(stderr, "    show adapter              - display controller information\n");
55 	fprintf(stderr, "    show config               - display RAID configuration\n");
56 	fprintf(stderr, "    show drives               - list physical drives\n");
57 	fprintf(stderr, "    show events               - display event log\n");
58 	fprintf(stderr, "    show volumes              - list logical volumes\n");
59 	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
60 	fprintf(stderr, "    online <drive>            - bring an offline physical drive online\n");
61 	fprintf(stderr, "    offline <drive>           - mark a physical drive offline\n");
62 	fprintf(stderr, "    name <volume> <name>\n");
63 	fprintf(stderr, "    volume status <volume>    - display volume status\n");
64 	fprintf(stderr, "    volume cache <volume> <enable|disable>\n");
65 	fprintf(stderr, "                              - Enable or disable the volume drive caches\n");
66 	fprintf(stderr, "    clear                     - clear volume configuration\n");
67 	fprintf(stderr, "    create <type> [-vq] [-s stripe] <drive>[,<drive>[,...]]\n");
68 	fprintf(stderr, "    delete <volume>\n");
69 	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
70 	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
71 #ifdef DEBUG
72 	fprintf(stderr, "    pd create <drive>         - create RAID physdisk\n");
73 	fprintf(stderr, "    pd delete <drive>         - delete RAID physdisk\n");
74 	fprintf(stderr, "    debug                     - debug 'show config'\n");
75 #endif
76 	exit(1);
77 }
78 
79 static int
80 version(int ac, char **av)
81 {
82 
83 	printf("mptutil version 1.0.3");
84 #ifdef DEBUG
85 	printf(" (DEBUG)");
86 #endif
87 	printf("\n");
88 	return (0);
89 }
90 MPT_COMMAND(top, version, version);
91 
92 int
93 main(int ac, char **av)
94 {
95 	struct mptutil_command **cmd;
96 	int ch;
97 
98 	while ((ch = getopt(ac, av, "u:")) != -1) {
99 		switch (ch) {
100 		case 'u':
101 			mpt_unit = atoi(optarg);
102 			break;
103 		case '?':
104 			usage();
105 		}
106 	}
107 
108 	av += optind;
109 	ac -= optind;
110 
111 	/* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
112 	if (ac == 0)
113 		usage();
114 
115 	SET_FOREACH(cmd, MPT_DATASET(top)) {
116 		if (strcmp((*cmd)->name, av[0]) == 0) {
117 			if ((*cmd)->handler(ac, av))
118 				return (1);
119 			else
120 				return (0);
121 		}
122 	}
123 	warnx("Unknown command %s.", av[0]);
124 	return (1);
125 }
126