1196212Sscottl/*-
2196212Sscottl * Copyright (c) 2008 Yahoo!, Inc.
3196212Sscottl * All rights reserved.
4196212Sscottl * Written by: John Baldwin <jhb@FreeBSD.org>
5196212Sscottl *
6196212Sscottl * Redistribution and use in source and binary forms, with or without
7196212Sscottl * modification, are permitted provided that the following conditions
8196212Sscottl * are met:
9196212Sscottl * 1. Redistributions of source code must retain the above copyright
10196212Sscottl *    notice, this list of conditions and the following disclaimer.
11196212Sscottl * 2. Redistributions in binary form must reproduce the above copyright
12196212Sscottl *    notice, this list of conditions and the following disclaimer in the
13196212Sscottl *    documentation and/or other materials provided with the distribution.
14196212Sscottl * 3. Neither the name of the author nor the names of any co-contributors
15196212Sscottl *    may be used to endorse or promote products derived from this software
16196212Sscottl *    without specific prior written permission.
17196212Sscottl *
18196212Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19196212Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20196212Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21196212Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22196212Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23196212Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24196212Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25196212Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26196212Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27196212Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28196212Sscottl * SUCH DAMAGE.
29196212Sscottl */
30196212Sscottl
31196212Sscottl#include <sys/cdefs.h>
32196212Sscottl__RCSID("$FreeBSD: stable/11/usr.sbin/mptutil/mptutil.c 350442 2019-07-30 14:19:18Z emaste $");
33196212Sscottl
34196212Sscottl#include <sys/param.h>
35196212Sscottl#include <sys/errno.h>
36196212Sscottl#include <err.h>
37196212Sscottl#include <stdio.h>
38196212Sscottl#include <stdlib.h>
39196212Sscottl#include <string.h>
40196212Sscottl#include <unistd.h>
41196212Sscottl#include "mptutil.h"
42196212Sscottl
43196212SscottlSET_DECLARE(MPT_DATASET(top), struct mptutil_command);
44196212Sscottl
45196212Sscottlint mpt_unit;
46196212Sscottl
47196212Sscottlstatic void
48196212Sscottlusage(void)
49196212Sscottl{
50196212Sscottl
51196212Sscottl	fprintf(stderr, "usage: mptutil [-u unit] <command> ...\n\n");
52196212Sscottl	fprintf(stderr, "Commands include:\n");
53196212Sscottl	fprintf(stderr, "    version\n");
54196212Sscottl	fprintf(stderr, "    show adapter              - display controller information\n");
55196212Sscottl	fprintf(stderr, "    show config               - display RAID configuration\n");
56196212Sscottl	fprintf(stderr, "    show drives               - list physical drives\n");
57196212Sscottl	fprintf(stderr, "    show events               - display event log\n");
58196212Sscottl	fprintf(stderr, "    show volumes              - list logical volumes\n");
59196212Sscottl	fprintf(stderr, "    fail <drive>              - fail a physical drive\n");
60196212Sscottl	fprintf(stderr, "    online <drive>            - bring an offline physical drive online\n");
61196212Sscottl	fprintf(stderr, "    offline <drive>           - mark a physical drive offline\n");
62196212Sscottl	fprintf(stderr, "    name <volume> <name>\n");
63196212Sscottl	fprintf(stderr, "    volume status <volume>    - display volume status\n");
64196212Sscottl	fprintf(stderr, "    volume cache <volume> <enable|disable>\n");
65196212Sscottl	fprintf(stderr, "                              - Enable or disable the volume drive caches\n");
66196212Sscottl	fprintf(stderr, "    clear                     - clear volume configuration\n");
67196212Sscottl	fprintf(stderr, "    create <type> [-vq] [-s stripe] <drive>[,<drive>[,...]]\n");
68196212Sscottl	fprintf(stderr, "    delete <volume>\n");
69196212Sscottl	fprintf(stderr, "    add <drive> [volume]      - add a hot spare\n");
70196212Sscottl	fprintf(stderr, "    remove <drive>            - remove a hot spare\n");
71196212Sscottl#ifdef DEBUG
72196212Sscottl	fprintf(stderr, "    pd create <drive>         - create RAID physdisk\n");
73196212Sscottl	fprintf(stderr, "    pd delete <drive>         - delete RAID physdisk\n");
74196212Sscottl	fprintf(stderr, "    debug                     - debug 'show config'\n");
75196212Sscottl#endif
76196212Sscottl	exit(1);
77196212Sscottl}
78196212Sscottl
79196212Sscottlstatic int
80196212Sscottlversion(int ac, char **av)
81196212Sscottl{
82196212Sscottl
83196212Sscottl	printf("mptutil version 1.0.3");
84196212Sscottl#ifdef DEBUG
85196212Sscottl	printf(" (DEBUG)");
86196212Sscottl#endif
87196212Sscottl	printf("\n");
88196212Sscottl	return (0);
89196212Sscottl}
90196212SscottlMPT_COMMAND(top, version, version);
91196212Sscottl
92196212Sscottlint
93196212Sscottlmain(int ac, char **av)
94196212Sscottl{
95196212Sscottl	struct mptutil_command **cmd;
96196212Sscottl	int ch;
97196212Sscottl
98196212Sscottl	while ((ch = getopt(ac, av, "u:")) != -1) {
99196212Sscottl		switch (ch) {
100196212Sscottl		case 'u':
101196212Sscottl			mpt_unit = atoi(optarg);
102196212Sscottl			break;
103196212Sscottl		case '?':
104196212Sscottl			usage();
105196212Sscottl		}
106196212Sscottl	}
107196212Sscottl
108196212Sscottl	av += optind;
109196212Sscottl	ac -= optind;
110196212Sscottl
111196212Sscottl	/* getopt() eats av[0], so we can't use mpt_table_handler() directly. */
112196212Sscottl	if (ac == 0)
113196212Sscottl		usage();
114196212Sscottl
115350442Semaste#if BYTE_ORDER == BIG_ENDIAN
116350442Semaste	warnx("mptutil is known to be broken on big-endian architectures");
117350442Semaste#endif
118350442Semaste
119196212Sscottl	SET_FOREACH(cmd, MPT_DATASET(top)) {
120196212Sscottl		if (strcmp((*cmd)->name, av[0]) == 0) {
121213672Srandi			if ((*cmd)->handler(ac, av))
122213672Srandi				return (1);
123213672Srandi			else
124213672Srandi				return (0);
125196212Sscottl		}
126196212Sscottl	}
127196212Sscottl	warnx("Unknown command %s.", av[0]);
128213672Srandi	return (1);
129196212Sscottl}
130