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 main(int argc
, char *argv
[])
49 char *configfile
= NULL
;
55 char *subdev
[MD_SB_DISKS
];
56 int devmodes
[MD_SB_DISKS
];
63 while ((opt
=getopt_long(argc
, argv
,
64 short_options
, long_options
,
68 case '@': /* just incase they say --manage */
74 /* setting mode - only once */
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
);
88 case 'C': help_text
= Help_create
; break;
89 case 'B': help_text
= Help_build
; break;
90 case 'A': help_text
= Help_assemble
; break;
92 fputs(help_text
,stderr
);
96 fputs(Version
, stderr
);
99 case 'v': verbose
= 1;
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
106 if (mddev
== NULL
&& !scan
&& mode
!= 'E' && mode
!= 'D')
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
);
114 subdev
[subdevs
] = optarg
;
115 devmodes
[subdevs
] = devmode
;
122 fputs(Usage
, stderr
);
125 /* force mode setting - @==manage if nothing else */
126 if (!mode
) mode
= '@';
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
)) {
134 case O('B','c'): /* chunk or rounding */
136 fprintf(stderr
, "mdctl: chunk/rounding may only be specified once. "
137 "Second value is %s.\n", optarg
);
140 chunk
= strtol(optarg
, &c
, 10);
141 if (!optarg
[0] || *c
) {
142 fprintf(stderr
, "mdctl: invalid chunk/rounding value: %s\n",
149 case O('B','l'): /* set raid level*/
151 fprintf(stderr
, "mdctl: raid level may only be set once. "
152 "Second value is %s.\n", optarg
);
155 if (strcmp(optarg
,"linear")==0)
157 else if (strlen(optarg
)==1 && strchr("01245", optarg
[0]))
158 level
= optarg
[0]-'0';
160 fprintf(stderr
, "mdctl: invalid raid level: %s\n",
164 if (level
> 0 && mode
== 'B') {
165 fprintf(stderr
, "mdctl: Raid level %s not permitted with --build.\n",
169 if (layout
>=0 && level
< 4) {
170 fprintf(stderr
, "mdctl: raid level %s is incompatible with layout setting\n",
174 if (sparedisks
> 0 && level
< 1) {
175 fprintf(stderr
, "mdctl: raid level %s is incompatible with spare-disks setting.\n",
181 case O('C','p'): /* raid5 layout */
183 fprintf(stderr
,"mdctl: layout may only be sent once. "
184 "Second value was %s\n", optarg
);
187 if (level
> -10 && level
< 4) {
188 fprintf(stderr
,"mdctl: layout is incompatible with raid levels below 4.\n");
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
;
200 fprintf(stderr
,"mdctl: %s is not a valid layout value\n",
207 case O('B','n'): /* number of raid disks */
209 fprintf(stderr
, "mdctl: raid-disks set twice: %d and %s\n",
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",
221 case O('C','x'): /* number of spare (eXtra) discs */
223 fprintf(stderr
,"mdctl: spare-disks set twice: %d and %s\n",
227 if (level
> -10 && level
< 1) {
228 fprintf(stderr
, "mdctl: spare-disks setting is incompatible with raid level %d\n",
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",
240 /* now for the Assemble options */
241 case O('A','f'): /* force assembly */
244 case O('A','u'): /* uuid of array */
246 fprintf(stderr
, "mdctl: uuid cannot bet set twice. "
247 "Second value %s.\n", optarg
);
250 if (parse_uuid(optarg
, uuid
))
253 fprintf(stderr
,"mdctl: Bad uuid: %s\n", optarg
);
258 case O('A','c'): /* config file */
260 fprintf(stderr
, "mdctl: configfile cannot be set twice. "
261 "Second value is %s.\n", optarg
);
265 /* FIXME possibly check that config file exists. Even parse it */
267 case O('A','s'): /* scan */
271 /* now the general management options. Some are applicable
272 * to other modes. None have arguments.
277 case O('A','a'): /* add a drive */
280 case O('@','r'): /* remove a drive */
283 case O('@','f'): /* set faulty */
290 case O('C','R'): /* Run the array */
292 fprintf(stderr
, "mdctl: Cannot both Stop and Run an array\n");
299 fprintf(stderr
, "mdctl: Cannot both Run and Stop an array\n");
307 fprintf(stderr
, "mdctl: Cannot have both readonly and readwrite\n");
314 fprintf(stderr
, "mkdctl: Cannot have both readwrite and readonly.\n");
320 /* We have now processed all the valid options. Anything else is
323 fprintf(stderr
, "mdctl: option %c not valid in mode %c\n",
330 fputs(Usage
, stderr
);
333 /* Ok, got the option parsing out of the way
334 * hopefully it's mostly right but there might be some stuff
337 * That is mosty checked in ther per-mode stuff but...
339 * There must be an mddev unless D or E or (A and scan)
340 * If there is one, we open it.
342 if (mode
!='D' && mode
!='E' && ! (mode
=='A' && scan
)) {
344 fprintf(stderr
, "mdctl: an md device must be given in this mode\n");
347 mdfd
= open(mddev
, O_RDWR
, 0);
349 fprintf(stderr
,"mdctl: error opening %s: %s\n",
350 mddev
, strerror(errno
));
353 if (md_get_version(mdfd
) <= 0) {
354 fprintf(stderr
, "mdctl: %s does not appear to be an md device\n",
363 case '@':/* Management */
364 /* readonly, add/remove, readwrite, runstop */
366 rv
= Manage_ro(mddev
, mdfd
, readonly
);
368 rv
= Manage_subdevs(mddev
, mdfd
, subdevs
, subdev
, devmodes
);
369 if (!rv
&& readonly
< 1)
370 rv
= Manage_ro(mddev
, mdfd
, readonly
);
372 rv
= Manage_runstop(mddev
, mdfd
, runstop
);
374 case 'A': /* Assemble */
375 rv
= Assemble(mddev
, mdfd
, uuid
, uuidset
, configfile
, scan
, subdevs
, subdev
, readonly
, runstop
, verbose
, force
);
377 case 'B': /* Build */
378 rv
= Build(mddev
, mdfd
, chunk
, level
, raiddisks
, subdevs
,subdev
);
380 case 'C': /* Create */
381 rv
= Create(mddev
, mdfd
, chunk
, level
, layout
, raiddisks
, sparedisks
,
382 subdevs
,subdev
,runstop
);
384 case 'D': /* Detail */
385 for (i
=0; i
<subdevs
; i
++)
386 rv
|= Detail(subdev
[i
]);
388 case 'E': /* Examine */
389 for (i
=0; i
<subdevs
; i
++)
390 rv
|= Examine(subdev
[i
]);