2 * mdctl - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
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.
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.
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
22 * Email: <neilb@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
33 int open_mddev(char *dev
)
35 int mdfd
= open(dev
, O_RDWR
, 0);
37 fprintf(stderr
,Name
": error opening %s: %s\n",
38 dev
, strerror(errno
));
39 else if (md_get_version(mdfd
) <= 0) {
40 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
50 int main(int argc
, char *argv
[])
65 struct mddev_ident_s ident
;
66 char *configfile
= NULL
;
71 char *devs
[MD_SB_DISKS
+1];
72 int devmodes
[MD_SB_DISKS
+1];
77 char *mailaddr
= NULL
;
84 ident
.super_minor
= -1;
87 while ((opt
=getopt_long(argc
, argv
,
88 short_options
, long_options
,
92 case '@': /* just incase they say --manage */
99 /* setting mode - only once */
101 fprintf(stderr
, Name
": --%s/-%c not allowed, mode already set to %s\n",
102 long_options
[opt
-'A'+1].name
,
103 long_options
[opt
-'A'+1].val
,
104 long_options
[mode
-'A'+1].name
);
113 case 'C': help_text
= Help_create
; break;
114 case 'B': help_text
= Help_build
; break;
115 case 'A': help_text
= Help_assemble
; break;
117 fputs(help_text
,stderr
);
121 fputs(Version
, stderr
);
124 case 'v': verbose
= 1;
127 case 1: /* an undecorated option - must be a device name.
128 * Depending on mode, it could be that:
129 * All devices listed are "md" devices : --Detail, -As
130 * No devices are "md" devices : --Examine
131 * First device is "md", others are component: -A,-B,-C
133 if (devs_found
>= MD_SB_DISKS
+1) {
134 fprintf(stderr
, Name
": too many devices at %s - current limit -s %d\n",
135 optarg
, MD_SB_DISKS
+1);
138 devs
[devs_found
] = optarg
;
139 devmodes
[devs_found
] = devmode
;
145 fputs(Usage
, stderr
);
148 /* force mode setting - @==manage if nothing else */
149 if (!mode
) mode
= '@';
152 /* We've got a mode, and opt is now something else which
153 * could depend on the mode */
154 #define O(a,b) ((a<<8)|b)
155 switch (O(mode
,opt
)) {
157 case O('B','c'): /* chunk or rounding */
159 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
160 "Second value is %s.\n", optarg
);
163 chunk
= strtol(optarg
, &c
, 10);
164 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
165 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
171 case O('C','z'): /* size */
173 fprintf(stderr
, Name
": size may only be specified once. "
174 "Second value is %s.\n", optarg
);
177 size
= strtol(optarg
, &c
, 10);
178 if (!optarg
[0] || *c
|| size
< 4) {
179 fprintf(stderr
, Name
": invalid size: %s\n",
186 case O('B','l'): /* set raid level*/
188 fprintf(stderr
, Name
": raid level may only be set once. "
189 "Second value is %s.\n", optarg
);
192 level
= map_name(pers
, optarg
);
194 fprintf(stderr
, Name
": invalid raid level: %s\n",
198 if (level
> 0 && mode
== 'B') {
199 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
203 if (sparedisks
> 0 && level
< 1) {
204 fprintf(stderr
, Name
": raid level %s is incompatible with spare-disks setting.\n",
210 case O('C','p'): /* raid5 layout */
212 fprintf(stderr
,Name
": layout may only be sent once. "
213 "Second value was %s\n", optarg
);
218 fprintf(stderr
, Name
": layout now meaningful for %s arrays.\n",
219 map_num(pers
, level
));
222 fprintf(stderr
, Name
": raid level must be given before layout.\n");
226 layout
= map_name(r5layout
, optarg
);
228 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
237 case O('B','n'): /* number of raid disks */
239 fprintf(stderr
, Name
": raid-disks set twice: %d and %s\n",
243 raiddisks
= strtol(optarg
, &c
, 10);
244 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
245 fprintf(stderr
, Name
": invalid number of raid disks: %s\n",
251 case O('C','x'): /* number of spare (eXtra) discs */
253 fprintf(stderr
,Name
": spare-disks set twice: %d and %s\n",
257 if (level
> -10 && level
< 1) {
258 fprintf(stderr
, Name
": spare-disks setting is incompatible with raid level %d\n",
262 sparedisks
= strtol(optarg
, &c
, 10);
263 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
264 fprintf(stderr
, Name
": invalid number of spare disks: %s\n",
269 case O('C','f'): /* force honouring of device list */
273 /* now for the Assemble options */
274 case O('A','f'): /* force assembly */
277 case O('A','u'): /* uuid of array */
278 if (ident
.uuid_set
) {
279 fprintf(stderr
, Name
": uuid cannot bet set twice. "
280 "Second value %s.\n", optarg
);
283 if (parse_uuid(optarg
, ident
.uuid
))
286 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
291 case O('A','c'): /* config file */
294 fprintf(stderr
, Name
": configfile cannot be set twice. "
295 "Second value is %s.\n", optarg
);
299 /* FIXME possibly check that config file exists. Even parse it */
301 case O('A','s'): /* scan */
305 case O('F','m'): /* mail address */
307 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
313 case O('F','p'): /* alert program */
315 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
321 case O('F','d'): /* delay in seconds */
323 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
326 delay
= strtol(optarg
, &c
, 10);
327 if (!optarg
[0] || *c
|| delay
<1) {
328 fprintf(stderr
, Name
": invalid delay: %s\n",
336 /* now the general management options. Some are applicable
337 * to other modes. None have arguments.
342 case O('A','a'): /* add a drive */
345 case O('@','r'): /* remove a drive */
348 case O('@','f'): /* set faulty */
354 case O('C','R'): /* Run the array */
356 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
363 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
371 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
378 fprintf(stderr
, "mkdctl: Cannot have both readwrite and readonly.\n");
384 /* We have now processed all the valid options. Anything else is
387 fprintf(stderr
, Name
": option %c not valid in mode %c\n",
394 fputs(Usage
, stderr
);
397 /* Ok, got the option parsing out of the way
398 * hopefully it's mostly right but there might be some stuff
401 * That is mosty checked in ther per-mode stuff but...
403 * For @,B,C and A without -s, the first device listed must be an md device
404 * we check that here and open it.
407 if (mode
=='@' || mode
== 'B' || mode
== 'C' || (mode
== 'A' && ! scan
)) {
408 if (devs_found
< 1) {
409 fprintf(stderr
, Name
": an md device must be given in this mode\n");
412 mdfd
= open_mddev(devs
[0]);
420 case '@':/* Management */
421 /* readonly, add/remove, readwrite, runstop */
423 rv
= Manage_ro(devs
[0], mdfd
, readonly
);
424 if (!rv
&& devs_found
>1)
425 rv
= Manage_subdevs(devs
[0], mdfd
,
426 devs_found
-1, devs
+1, devmodes
+1);
427 if (!rv
&& readonly
< 0)
428 rv
= Manage_ro(devs
[0], mdfd
, readonly
);
430 rv
= Manage_runstop(devs
[0], mdfd
, runstop
);
432 case 'A': /* Assemble */
434 rv
= Assemble(devs
[0], mdfd
, &ident
, configfile
,
435 devs_found
-1, devs
+1,
436 readonly
, runstop
, verbose
, force
);
437 else if (devs_found
>0)
438 for (i
=0; i
<devs_found
; i
++) {
439 mddev_ident_t array_ident
= conf_get_ident(configfile
, devs
[i
]);
440 mdfd
= open_mddev(devs
[i
]);
445 if (array_ident
== NULL
) {
446 fprintf(stderr
, Name
": %s not identified in config file.\n",
451 rv
|= Assemble(devs
[i
], mdfd
, array_ident
, configfile
,
453 readonly
, runstop
, verbose
, force
);
456 mddev_ident_t array_list
= conf_get_ident(configfile
, NULL
);
458 fprintf(stderr
, Name
": No arrays found in config file\n");
461 for (; array_list
; array_list
= array_list
->next
) {
462 mdfd
= open_mddev(array_list
->devname
);
467 rv
|= Assemble(array_list
->devname
, mdfd
,
468 array_list
, configfile
,
470 readonly
, runstop
, verbose
, force
);
474 case 'B': /* Build */
475 rv
= Build(devs
[0], mdfd
, chunk
, level
, raiddisks
, devs_found
-1,devs
+1);
477 case 'C': /* Create */
478 rv
= Create(devs
[0], mdfd
, chunk
, level
, layout
, size
,
479 raiddisks
, sparedisks
,
480 devs_found
-1,devs
+1, runstop
, verbose
, force
);
482 case 'D': /* Detail */
483 for (i
=0; i
<devs_found
; i
++)
484 rv
|= Detail(devs
[i
]);
486 case 'E': /* Examine */
487 for (i
=0; i
<devs_found
; i
++)
488 rv
|= Examine(devs
[i
]);
490 case 'F': /* Follow */
491 rv
= Monitor(devs_found
, devs
, mailaddr
, program
,
492 delay
?delay
:60, configfile
);