File: raidstart.c

package info (click to toggle)
raidtools2 0.90.990824-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 688 kB
  • ctags: 468
  • sloc: ansic: 2,631; makefile: 143; sh: 114
file content (228 lines) | stat: -rw-r--r-- 5,654 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228

/*
   raidstart.c : Multiple Devices tools for Linux
	       Copyright (C) 1998 Erik Troan
	       <ewt@redhat.com>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.
   
   You should have received a copy of the GNU General Public License
   (for example /usr/src/linux/COPYING); if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
*/

#include "common.h"
#include "parser.h"
#include "raidlib.h"
#include "popt.h"
#include "version.h"

#include <asm/page.h>
#include <string.h>
#include <sys/sysmacros.h>

#define RAIDADD_VERSION "v0.3d"


void usage(unsigned char *arg, enum raidFunc func)
{
	if ((func == raidhotremove) ||
			(func == raidhotadd) || (func == raidsetfaulty)) {
		printf("usage: %s [--all] [--configfile] [--help] [--version] [-achv] </dev/md?> <device>\n", arg);
		return;
	}
	printf("usage: %s [--all] [--configfile] [--help] [--version] [-achv] </dev/md?>*\n", arg);
}

int main (int argc, char *argv[]) {
    int i, all=0, readonly=0, readwrite=0, exit_status=0, version = 0, help = 0;
    char *namestart=argv[0];
    md_cfg_entry_t *p;
    char ** args;
    enum raidFunc func;
    FILE * fp;
    poptContext optCon;
    char * configFile = RAID_CONFIG;
    struct poptOption optionsTable[] = {
	{ "all", 'a', 0, &all, 0 },
	{ "configfile", 'c', POPT_ARG_STRING, &configFile, 0 },
	{ "readonly", 'r', 0, &readonly, 0 },
	{ "readwrite", 'w', 0, &readwrite, 0 },
	{ "help", 'h', 0, &help, 0 },
	{ "version", 'V', 0, &version, 0 },
	{ NULL, 0, 0, NULL, 0 }
    } ;

    namestart = strrchr(argv[0], '/');
    if (!namestart)
	namestart = argv[0];
    else
	namestart++;

    if (!strcmp (namestart, "raidstart"))
	func = raidstart;
    else if (!strcmp (namestart, "raidstop"))
	func = raidstop;
    else if (!strcmp (namestart, "raidhotadd"))
	func = raidhotadd;
    else if (!strcmp (namestart, "raidhotremove"))
	func = raidhotremove;
    else if (!strcmp (namestart, "raidsetfaulty"))
	func = raidsetfaulty;
    else {
	fprintf (stderr, "Unknown command %s\n", argv[0]);
	usage(namestart, raidstart);
	return (EXIT_FAILURE);
    }

    optCon = poptGetContext(namestart, argc, argv, optionsTable, 0);
    if ((i = poptGetNextOpt(optCon)) < -1) {
	fprintf(stderr, "%s: %s\n", 
		poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		poptStrerror(i));
	usage(namestart, func);
	return EXIT_FAILURE;
    }

    if (help) {
	usage(namestart, func);
	return(EXIT_USAGE);
    } else if (version) {
	printf ("%s " RAIDADD_VERSION " compiled for md " MD_VERSION "\n",
		argv[0]);
	return (EXIT_VERSION);
    }

    fp = fopen(configFile, "r");
    if (fp == NULL) {
        fprintf(stderr, "Couldn't open %s -- %s\n", configFile, strerror(errno));
        exit(EXIT_FAILURE);
    }

    if (prepare_raidlib())
         return EXIT_FAILURE;

    if (readwrite) {
        int fd;
        if (func != raidstart) {
	   fprintf(stderr, "Can do --readwrite only with raidstart!\n");
	   return EXIT_FAILURE;
        }
	args = poptGetArgs(optCon);
	if (!args) {
	    fprintf(stderr, "nothing to do!\n");
            usage(namestart, func);
	    exit(EXIT_FAILURE);
	}

	fd = open_or_die(*args);

        if (do_raidstart_rw (fd, *args))
	    exit(EXIT_FAILURE);
        exit(0);
    }

    if (readonly) {
         if (func != raidstop) {
	    fprintf(stderr, "Can do --readonly only with raidstop!\n");
	    return EXIT_FAILURE;
         }
         func = raidstop_ro;
    }

    /*
     * stop is special, we want to get it done
     * without parsing the config
     */
    if ((func == raidstop) || (func == raidstop_ro)) {
        int fd;

	args = poptGetArgs(optCon);
	if (!args) {
	    fprintf(stderr, "nothing to do!\n");
            usage(namestart, func);
	    exit(EXIT_FAILURE);
	}

	fd = open_or_die(*args);

        if (do_raidstop (fd, *args, func == raidstop ? 0:1))
	    exit(EXIT_FAILURE);
        exit(0);
    }

    /*
     * we want to fast-handle hot-remove and hot-add too.
     */
    if ((func == raidhotremove) ||
			(func == raidhotadd) || (func == raidsetfaulty)) {
        int md_fd;
	char * md_name;

	md_name = poptGetArg(optCon);
	if (!md_name) {
	    fprintf(stderr, "nothing to do!\n");
            usage(namestart, func);
	    exit(EXIT_FAILURE);
	}

	
	md_fd = open_or_die(md_name);

	args = poptGetArgs(optCon);
	if (!args) {
	    fprintf(stderr, "no target disk specified!\n");
            usage(namestart, func);
	    exit(EXIT_FAILURE);
	}

	if (func == raidhotremove) {
	        if (do_raidhotremove (md_fd, *args, md_name))
		    exit(EXIT_FAILURE);
	} else if (func == raidhotadd) {
	        if (do_raidhotadd (md_fd, *args, md_name))
		    exit(EXIT_FAILURE);
	} else if (func == raidsetfaulty) {
	        if (do_raidsetfaulty (md_fd, *args, md_name))
		    exit(EXIT_FAILURE);
	}
        exit(0);
    }


    if (parse_config(fp))
	exit(EXIT_FAILURE);

    if (all) {
	for (p = cfg_head; p; p = p->next)
	    exit_status += handleOneConfig(func, p);
    } else {
	args = poptGetArgs(optCon);
	if (!args) {
	    fprintf(stderr, "nothing to do!\n");
            usage(namestart, func);
	    return EXIT_FAILURE;
	}

	while (*args) {
	    for (p = cfg_head; p; p = p->next) {
		if (strcmp(p->md_name, *args)) continue;
		exit_status += handleOneConfig(func, p);
		break;
	    }
	    if (!p) {
		fprintf(stderr, "device %s is not described in config file\n", *args);
		exit_status++;
	    }
	    args++;
	}
    }

    poptFreeContext(optCon);

    return (exit_status);
}