mfiutil.c revision 237259
1186681Sed/*-
2186681Sed * Copyright (c) 2008, 2009 Yahoo!, Inc.
3186681Sed * All rights reserved.
4186681Sed *
5186681Sed * Redistribution and use in source and binary forms, with or without
6186681Sed * modification, are permitted provided that the following conditions
7186681Sed * are met:
8186681Sed * 1. Redistributions of source code must retain the above copyright
9186681Sed *    notice, this list of conditions and the following disclaimer.
10186681Sed * 2. Redistributions in binary form must reproduce the above copyright
11186681Sed *    notice, this list of conditions and the following disclaimer in the
12186681Sed *    documentation and/or other materials provided with the distribution.
13186681Sed * 3. The names of the authors may not be used to endorse or promote
14186681Sed *    products derived from this software without specific prior written
15186681Sed *    permission.
16186681Sed *
17186681Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18186681Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19186681Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20186681Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21186681Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22186681Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23186681Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24186681Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25186681Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26186681Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27186681Sed * SUCH DAMAGE.
28186681Sed *
29186681Sed * $FreeBSD: head/usr.sbin/mfiutil/mfiutil.c 237259 2012-06-19 06:18:37Z eadler $
30186681Sed */
31186681Sed
32186681Sed#include <sys/errno.h>
33186681Sed#include <err.h>
34186681Sed#include <stdio.h>
35186681Sed#include <stdlib.h>
36187367Sed#include <string.h>
37187367Sed#include <unistd.h>
38187367Sed#include "mfiutil.h"
39187367Sed
40187367SedSET_DECLARE(MFI_DATASET(top), struct mfiutil_command);
41186681Sed
42186681SedMFI_TABLE(top, start);
43187367SedMFI_TABLE(top, stop);
44187367SedMFI_TABLE(top, abort);
45187367Sed
46186681Sedint mfi_unit;
47186681Sed
48186681Sedu_int mfi_opts;
49186681Sed
50186681Sedstatic void
51186681Sedusage(void)
52186681Sed{
53186681Sed
54186681Sed	fprintf(stderr, "usage: mfiutil [-de] [-u unit] <command> ...\n\n");
55186681Sed	fprintf(stderr, "Commands include:\n");
56186681Sed	fprintf(stderr, "    version\n");
57186681Sed	fprintf(stderr, "    show adapter              - display controller information\n");
58186681Sed	fprintf(stderr, "    show battery              - display battery information\n");
59186681Sed	fprintf(stderr, "    show config               - display RAID configuration\n");
60186681Sed	fprintf(stderr, "    show drives               - list physical drives\n");
61186681Sed	fprintf(stderr, "    show events               - display event log\n");
62186681Sed	fprintf(stderr, "    show firmware             - list firmware images\n");
63186681Sed	fprintf(stderr, "    show logstate             - display event log sequence numbers\n");
64186681Sed	fprintf(stderr, "    show volumes              - list logical volumes\n");
65186681Sed	fprintf(stderr, "    show patrol               - display patrol read status\n");
66186681Sed	fprintf(stderr, "    show progress             - display status of active operations\n");
67186681Sed	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
68186681Sed	fprintf(stderr, "    good <drive>              - mark a bad physical drive as good\n");
69186681Sed	fprintf(stderr, "    rebuild <drive>           - mark failed drive ready for rebuild\n");
70186681Sed	fprintf(stderr, "    drive progress <drive>    - display status of active operations\n");
71186681Sed	fprintf(stderr, "    drive clear <drive> <start|stop> - clear a drive with all 0x00\n");
72186681Sed	fprintf(stderr, "    start rebuild <drive>\n");
73186681Sed	fprintf(stderr, "    abort rebuild <drive>\n");
74186681Sed	fprintf(stderr, "    locate <drive> <on|off>   - toggle drive LED\n");
75186681Sed	fprintf(stderr, "    cache <volume> [command [setting]]\n");
76186681Sed	fprintf(stderr, "    name <volume> <name>\n");
77186681Sed	fprintf(stderr, "    volume progress <volume>  - display status of active operations\n");
78186681Sed	fprintf(stderr, "    clear                     - clear volume configuration\n");
79186681Sed	fprintf(stderr, "    create <type> [-v] <drive>[,<drive>[,...]] [<drive>[,<drive>[,...]]\n");
80186681Sed	fprintf(stderr, "    delete <volume>\n");
81186681Sed	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
82186681Sed	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
83186681Sed	fprintf(stderr, "    patrol <disable|auto|manual> [interval [start]]\n");
84186681Sed	fprintf(stderr, "    start patrol              - start a patrol read\n");
85186681Sed	fprintf(stderr, "    stop patrol               - stop a patrol read\n");
86186681Sed	fprintf(stderr, "    flash <firmware>\n");
87186681Sed#ifdef DEBUG
88186681Sed	fprintf(stderr, "    debug                     - debug 'show config'\n");
89186681Sed	fprintf(stderr, "    dump                      - display 'saved' config\n");
90186681Sed#endif
91186681Sed	exit(1);
92186681Sed}
93186681Sed
94186681Sedstatic int
95186681Sedversion(int ac, char **av)
96186681Sed{
97186681Sed
98186681Sed	printf("mfiutil version 1.0.14");
99186681Sed#ifdef DEBUG
100186681Sed	printf(" (DEBUG)");
101186681Sed#endif
102186681Sed	printf("\n");
103186681Sed	return (0);
104186681Sed}
105186681SedMFI_COMMAND(top, version, version);
106186681Sed
107186681Sedint
108186681Sedmain(int ac, char **av)
109186681Sed{
110186681Sed	struct mfiutil_command **cmd;
111186681Sed	int ch;
112186681Sed
113186681Sed	while ((ch = getopt(ac, av, "deu:")) != -1) {
114186681Sed		switch (ch) {
115186681Sed		case 'd':
116186681Sed			mfi_opts |= MFI_DNAME_DEVICE_ID;
117186681Sed			break;
118186681Sed		case 'e':
119186681Sed			mfi_opts |= MFI_DNAME_ES;
120186681Sed			break;
121186681Sed		case 'u':
122186681Sed			mfi_unit = atoi(optarg);
123186681Sed			break;
124186681Sed		case '?':
125186681Sed			usage();
126186681Sed		}
127186681Sed	}
128186681Sed
129186681Sed	av += optind;
130186681Sed	ac -= optind;
131186681Sed
132186681Sed	/* getopt() eats av[0], so we can't use mfi_table_handler() directly. */
133186681Sed	if (ac == 0)
134186681Sed		usage();
135186681Sed
136186681Sed	SET_FOREACH(cmd, MFI_DATASET(top)) {
137186681Sed		if (strcmp((*cmd)->name, av[0]) == 0) {
138186681Sed			if ((*cmd)->handler(ac, av))
139186681Sed				return (1);
140186681Sed			else
141186681Sed				return (0);
142186681Sed		}
143186681Sed	}
144186681Sed	warnx("Unknown command %s.", av[0]);
145186681Sed	return (1);
146186681Sed}
147186681Sed