]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdctl.c
mdctl-v0.2
[thirdparty/mdadm.git] / mdctl.c
1 /*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
30 #include "mdctl.h"
31 #include "md_p.h"
32
33 int main(int argc, char *argv[])
34 {
35 char mode = '\0';
36 int opt;
37 char *help_text;
38 char *c;
39 int rv;
40 int i;
41
42 int chunk = 0;
43 int level = -10;
44 int layout = -1;
45 int raiddisks = 0;
46 int sparedisks = 0;
47 int uuid[4];
48 int uuidset = 0;
49 char *configfile = NULL;
50 int scan = 0;
51 char devmode = 0;
52 int runstop = 0;
53 int readonly = 0;
54 char *mddev = NULL;
55 char *subdev[MD_SB_DISKS];
56 int devmodes[MD_SB_DISKS];
57 int subdevs = 0;
58 int verbose = 0;
59 int force = 0;
60
61 int mdfd = -1;
62
63 while ((opt=getopt_long(argc, argv,
64 short_options, long_options,
65 NULL)) != -1) {
66
67 switch(opt) {
68 case '@': /* just incase they say --manage */
69 case 'A':
70 case 'B':
71 case 'C':
72 case 'D':
73 case 'E':
74 /* setting mode - only once */
75 if (mode) {
76 fprintf(stderr, "mdctl: --%s/-%c not allowed, mode already set to %s\n",
77 long_options[opt-'A'+1].name,
78 long_options[opt-'A'+1].val,
79 long_options[mode-'A'+1].name);
80 exit(2);
81 }
82 mode = opt;
83 continue;
84
85 case 'h':
86 help_text = Help;
87 switch (mode) {
88 case 'C': help_text = Help_create; break;
89 case 'B': help_text = Help_build; break;
90 case 'A': help_text = Help_assemble; break;
91 }
92 fputs(help_text,stderr);
93 exit(0);
94
95 case 'V':
96 fputs(Version, stderr);
97 exit(0);
98
99 case 'v': verbose = 1;
100 continue;
101
102 case 1: /* an undecorated option - must be a device name.
103 * The first device is the "md" device unless scan
104 * has been set or mode is Examine or Detail
105 */
106 if (mddev == NULL && !scan && mode != 'E' && mode != 'D')
107 mddev = optarg;
108 else {
109 if (subdevs +1 >= MD_SB_DISKS) {
110 fprintf(stderr, "mdctl: too many devices at %s - current limit -s %d\n",
111 optarg, MD_SB_DISKS);
112 exit(2);
113 }
114 subdev[subdevs] = optarg;
115 devmodes[subdevs] = devmode;
116 subdevs++;
117 }
118 continue;
119
120 case ':':
121 case '?':
122 fputs(Usage, stderr);
123 exit(2);
124 default:
125 /* force mode setting - @==manage if nothing else */
126 if (!mode) mode = '@';
127 }
128
129 /* We've got a mode, and opt is now something else which
130 * could depend on the mode */
131 #define O(a,b) ((a<<8)|b)
132 switch (O(mode,opt)) {
133 case O('C','c'):
134 case O('B','c'): /* chunk or rounding */
135 if (chunk) {
136 fprintf(stderr, "mdctl: chunk/rounding may only be specified once. "
137 "Second value is %s.\n", optarg);
138 exit(2);
139 }
140 chunk = strtol(optarg, &c, 10);
141 if (!optarg[0] || *c) {
142 fprintf(stderr, "mdctl: invalid chunk/rounding value: %s\n",
143 optarg);
144 exit(2);
145 }
146 continue;
147
148 case O('C','l'):
149 case O('B','l'): /* set raid level*/
150 if (level != -10) {
151 fprintf(stderr, "mdctl: raid level may only be set once. "
152 "Second value is %s.\n", optarg);
153 exit(2);
154 }
155 if (strcmp(optarg,"linear")==0)
156 level = -1;
157 else if (strlen(optarg)==1 && strchr("01245", optarg[0]))
158 level = optarg[0]-'0';
159 else {
160 fprintf(stderr, "mdctl: invalid raid level: %s\n",
161 optarg);
162 exit(2);
163 }
164 if (level > 0 && mode == 'B') {
165 fprintf(stderr, "mdctl: Raid level %s not permitted with --build.\n",
166 optarg);
167 exit(2);
168 }
169 if (layout >=0 && level < 4) {
170 fprintf(stderr, "mdctl: raid level %s is incompatible with layout setting\n",
171 optarg);
172 exit(2);
173 }
174 if (sparedisks > 0 && level < 1) {
175 fprintf(stderr, "mdctl: raid level %s is incompatible with spare-disks setting.\n",
176 optarg);
177 exit(2);
178 }
179 continue;
180
181 case O('C','p'): /* raid5 layout */
182 if (layout >= 0) {
183 fprintf(stderr,"mdctl: layout may only be sent once. "
184 "Second value was %s\n", optarg);
185 exit(2);
186 }
187 if (level > -10 && level < 4) {
188 fprintf(stderr,"mdctl: layout is incompatible with raid levels below 4.\n");
189 exit(2);
190 }
191 if (strcmp(optarg, "left-symmetric")==0 || strcmp(optarg,"ls")==0)
192 layout = ALGORITHM_LEFT_SYMMETRIC;
193 else if (strcmp(optarg, "left-asymmetric")==0 || strcmp(optarg,"la")==0)
194 layout = ALGORITHM_LEFT_ASYMMETRIC;
195 else if (strcmp(optarg, "right-symmetric")==0 || strcmp(optarg,"rs")==0)
196 layout = ALGORITHM_RIGHT_SYMMETRIC;
197 else if (strcmp(optarg, "right-asymmetric")==0 || strcmp(optarg,"ra")==0)
198 layout = ALGORITHM_RIGHT_ASYMMETRIC;
199 else {
200 fprintf(stderr,"mdctl: %s is not a valid layout value\n",
201 optarg);
202 exit(2);
203 }
204 continue;
205
206 case O('C','n'):
207 case O('B','n'): /* number of raid disks */
208 if (raiddisks) {
209 fprintf(stderr, "mdctl: raid-disks set twice: %d and %s\n",
210 raiddisks, optarg);
211 exit(2);
212 }
213 raiddisks = strtol(optarg, &c, 10);
214 if (!optarg[0] || *c || raiddisks<=0 || raiddisks > MD_SB_DISKS) {
215 fprintf(stderr, "mdctl: invalid number of raid disks: %s\n",
216 optarg);
217 exit(2);
218 }
219 continue;
220
221 case O('C','x'): /* number of spare (eXtra) discs */
222 if (sparedisks) {
223 fprintf(stderr,"mdctl: spare-disks set twice: %d and %s\n",
224 sparedisks, optarg);
225 exit(2);
226 }
227 if (level > -10 && level < 1) {
228 fprintf(stderr, "mdctl: spare-disks setting is incompatible with raid level %d\n",
229 level);
230 exit(2);
231 }
232 sparedisks = strtol(optarg, &c, 10);
233 if (!optarg[0] || *c || sparedisks < 0 || sparedisks > MD_SB_DISKS - raiddisks) {
234 fprintf(stderr, "mdctl: invalid number of spare disks: %s\n",
235 optarg);
236 exit(2);
237 }
238 continue;
239
240 /* now for the Assemble options */
241 case O('A','f'): /* force assembly */
242 force = 1;
243 continue;
244 case O('A','u'): /* uuid of array */
245 if (uuidset) {
246 fprintf(stderr, "mdctl: uuid cannot bet set twice. "
247 "Second value %s.\n", optarg);
248 exit(2);
249 }
250 if (parse_uuid(optarg, uuid))
251 uuidset = 1;
252 else {
253 fprintf(stderr,"mdctl: Bad uuid: %s\n", optarg);
254 exit(2);
255 }
256 continue;
257
258 case O('A','c'): /* config file */
259 if (configfile) {
260 fprintf(stderr, "mdctl: configfile cannot be set twice. "
261 "Second value is %s.\n", optarg);
262 exit(2);
263 }
264 configfile = optarg;
265 /* FIXME possibly check that config file exists. Even parse it */
266 continue;
267 case O('A','s'): /* scan */
268 scan = 1;
269 continue;
270
271 /* now the general management options. Some are applicable
272 * to other modes. None have arguments.
273 */
274 case O('@','a'):
275 case O('C','a'):
276 case O('B','a'):
277 case O('A','a'): /* add a drive */
278 devmode = 'a';
279 continue;
280 case O('@','r'): /* remove a drive */
281 devmode = 'r';
282 continue;
283 case O('@','f'): /* set faulty */
284 case O('C','f'):
285 devmode = 'f';
286 continue;
287 case O('@','R'):
288 case O('A','R'):
289 case O('B','R'):
290 case O('C','R'): /* Run the array */
291 if (runstop < 0) {
292 fprintf(stderr, "mdctl: Cannot both Stop and Run an array\n");
293 exit(2);
294 }
295 runstop = 1;
296 continue;
297 case O('@','S'):
298 if (runstop > 0) {
299 fprintf(stderr, "mdctl: Cannot both Run and Stop an array\n");
300 exit(2);
301 }
302 runstop = -1;
303 continue;
304
305 case O('@','o'):
306 if (readonly < 0) {
307 fprintf(stderr, "mdctl: Cannot have both readonly and readwrite\n");
308 exit(2);
309 }
310 readonly = 1;
311 continue;
312 case O('@','w'):
313 if (readonly > 0) {
314 fprintf(stderr, "mkdctl: Cannot have both readwrite and readonly.\n");
315 exit(2);
316 }
317 readonly = -1;
318 continue;
319 }
320 /* We have now processed all the valid options. Anything else is
321 * an error
322 */
323 fprintf(stderr, "mdctl: option %c not valid in mode %c\n",
324 opt, mode);
325 exit(2);
326
327 }
328
329 if (!mode) {
330 fputs(Usage, stderr);
331 exit(2);
332 }
333 /* Ok, got the option parsing out of the way
334 * hopefully it's mostly right but there might be some stuff
335 * missing
336 *
337 * That is mosty checked in ther per-mode stuff but...
338 *
339 * There must be an mddev unless D or E or (A and scan)
340 * If there is one, we open it.
341 */
342 if (mode !='D' && mode !='E' && ! (mode =='A' && scan)) {
343 if (!mddev) {
344 fprintf(stderr, "mdctl: an md device must be given in this mode\n");
345 exit(2);
346 }
347 mdfd = open(mddev, O_RDWR, 0);
348 if (mdfd < 0) {
349 fprintf(stderr,"mdctl: error opening %s: %s\n",
350 mddev, strerror(errno));
351 exit(1);
352 }
353 if (md_get_version(mdfd) <= 0) {
354 fprintf(stderr, "mdctl: %s does not appear to be an md device\n",
355 mddev);
356 close(mdfd);
357 exit(1);
358 }
359 }
360
361 rv =0;
362 switch(mode) {
363 case '@':/* Management */
364 /* readonly, add/remove, readwrite, runstop */
365 if (readonly>1)
366 rv = Manage_ro(mddev, mdfd, readonly);
367 if (!rv && subdevs)
368 rv = Manage_subdevs(mddev, mdfd, subdevs, subdev, devmodes);
369 if (!rv && readonly < 1)
370 rv = Manage_ro(mddev, mdfd, readonly);
371 if (!rv && runstop)
372 rv = Manage_runstop(mddev, mdfd, runstop);
373 break;
374 case 'A': /* Assemble */
375 rv = Assemble(mddev, mdfd, uuid, uuidset, configfile, scan, subdevs, subdev, readonly, runstop, verbose, force);
376 break;
377 case 'B': /* Build */
378 rv = Build(mddev, mdfd, chunk, level, raiddisks, subdevs,subdev);
379 break;
380 case 'C': /* Create */
381 rv = Create(mddev, mdfd, chunk, level, layout, raiddisks, sparedisks,
382 subdevs,subdev,runstop);
383 break;
384 case 'D': /* Detail */
385 for (i=0; i<subdevs; i++)
386 rv |= Detail(subdev[i]);
387 break;
388 case 'E': /* Examine */
389 for (i=0; i<subdevs; i++)
390 rv |= Examine(subdev[i]);
391 break;
392 }
393 exit(rv);
394 }
395