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