]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdctl.c
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
[])
50 char *configfile
= NULL
;
56 char *subdev
[MD_SB_DISKS
];
57 int devmodes
[MD_SB_DISKS
];
64 while ((opt
=getopt_long(argc
, argv
,
65 short_options
, long_options
,
69 case '@': /* just incase they say --manage */
75 /* setting mode - only once */
77 fprintf(stderr
, Name
": --%s/-%c not allowed, mode already set to %s\n",
78 long_options
[opt
-'A'+1].name
,
79 long_options
[opt
-'A'+1].val
,
80 long_options
[mode
-'A'+1].name
);
89 case 'C': help_text
= Help_create
; break;
90 case 'B': help_text
= Help_build
; break;
91 case 'A': help_text
= Help_assemble
; break;
93 fputs(help_text
,stderr
);
97 fputs(Version
, stderr
);
100 case 'v': verbose
= 1;
103 case 1: /* an undecorated option - must be a device name.
104 * The first device is the "md" device unless scan
105 * has been set or mode is Examine or Detail
107 if (mddev
== NULL
&& !scan
&& mode
!= 'E' && mode
!= 'D')
110 if (subdevs
+1 >= MD_SB_DISKS
) {
111 fprintf(stderr
, Name
": too many devices at %s - current limit -s %d\n",
112 optarg
, MD_SB_DISKS
);
115 subdev
[subdevs
] = optarg
;
116 devmodes
[subdevs
] = devmode
;
123 fputs(Usage
, stderr
);
126 /* force mode setting - @==manage if nothing else */
127 if (!mode
) mode
= '@';
130 /* We've got a mode, and opt is now something else which
131 * could depend on the mode */
132 #define O(a,b) ((a<<8)|b)
133 switch (O(mode
,opt
)) {
135 case O('B','c'): /* chunk or rounding */
137 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
138 "Second value is %s.\n", optarg
);
141 chunk
= strtol(optarg
, &c
, 10);
142 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
143 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
149 case O('c','z'): /* size */
151 fprintf(stderr
, Name
": size may only be specified once. "
152 "Second value is %s.\n", optarg
);
155 size
= strtol(optarg
, &c
, 10);
156 if (!optarg
[0] || *c
|| size
< 4) {
157 fprintf(stderr
, Name
": invalid size: %s\n",
164 case O('B','l'): /* set raid level*/
166 fprintf(stderr
, Name
": raid level may only be set once. "
167 "Second value is %s.\n", optarg
);
170 level
= map_name(pers
, optarg
);
172 fprintf(stderr
, Name
": invalid raid level: %s\n",
176 if (level
> 0 && mode
== 'B') {
177 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
181 if (sparedisks
> 0 && level
< 1) {
182 fprintf(stderr
, Name
": raid level %s is incompatible with spare-disks setting.\n",
188 case O('C','p'): /* raid5 layout */
190 fprintf(stderr
,Name
": layout may only be sent once. "
191 "Second value was %s\n", optarg
);
196 fprintf(stderr
, Name
": layout now meaningful for %s arrays.\n",
197 map_num(pers
, level
));
200 fprintf(stderr
, Name
": raid level must be given before layout.\n");
204 layout
= map_name(r5layout
, optarg
);
206 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
215 case O('B','n'): /* number of raid disks */
217 fprintf(stderr
, Name
": raid-disks set twice: %d and %s\n",
221 raiddisks
= strtol(optarg
, &c
, 10);
222 if (!optarg
[0] || *c
|| raiddisks
<=0 || raiddisks
> MD_SB_DISKS
) {
223 fprintf(stderr
, Name
": invalid number of raid disks: %s\n",
229 case O('C','x'): /* number of spare (eXtra) discs */
231 fprintf(stderr
,Name
": spare-disks set twice: %d and %s\n",
235 if (level
> -10 && level
< 1) {
236 fprintf(stderr
, Name
": spare-disks setting is incompatible with raid level %d\n",
240 sparedisks
= strtol(optarg
, &c
, 10);
241 if (!optarg
[0] || *c
|| sparedisks
< 0 || sparedisks
> MD_SB_DISKS
- raiddisks
) {
242 fprintf(stderr
, Name
": invalid number of spare disks: %s\n",
248 /* now for the Assemble options */
249 case O('A','f'): /* force assembly */
252 case O('A','u'): /* uuid of array */
254 fprintf(stderr
, Name
": uuid cannot bet set twice. "
255 "Second value %s.\n", optarg
);
258 if (parse_uuid(optarg
, uuid
))
261 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
266 case O('A','c'): /* config file */
268 fprintf(stderr
, Name
": configfile cannot be set twice. "
269 "Second value is %s.\n", optarg
);
273 /* FIXME possibly check that config file exists. Even parse it */
275 case O('A','s'): /* scan */
279 /* now the general management options. Some are applicable
280 * to other modes. None have arguments.
285 case O('A','a'): /* add a drive */
288 case O('@','r'): /* remove a drive */
291 case O('@','f'): /* set faulty */
298 case O('C','R'): /* Run the array */
300 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
307 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
315 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
322 fprintf(stderr
, "mkdctl: Cannot have both readwrite and readonly.\n");
328 /* We have now processed all the valid options. Anything else is
331 fprintf(stderr
, Name
": option %c not valid in mode %c\n",
338 fputs(Usage
, stderr
);
341 /* Ok, got the option parsing out of the way
342 * hopefully it's mostly right but there might be some stuff
345 * That is mosty checked in ther per-mode stuff but...
347 * There must be an mddev unless D or E or (A and scan)
348 * If there is one, we open it.
351 if (mode
!='D' && mode
!='E' && ! (mode
=='A' && scan
)) {
353 fprintf(stderr
, Name
": an md device must be given in this mode\n");
356 mdfd
= open(mddev
, O_RDWR
, 0);
358 fprintf(stderr
,Name
": error opening %s: %s\n",
359 mddev
, strerror(errno
));
362 if (md_get_version(mdfd
) <= 0) {
363 fprintf(stderr
, Name
": %s does not appear to be an md device\n",
373 case '@':/* Management */
374 /* readonly, add/remove, readwrite, runstop */
376 rv
= Manage_ro(mddev
, mdfd
, readonly
);
378 rv
= Manage_subdevs(mddev
, mdfd
, subdevs
, subdev
, devmodes
);
379 if (!rv
&& readonly
< 0)
380 rv
= Manage_ro(mddev
, mdfd
, readonly
);
382 rv
= Manage_runstop(mddev
, mdfd
, runstop
);
384 case 'A': /* Assemble */
385 rv
= Assemble(mddev
, mdfd
, uuid
, uuidset
, configfile
, scan
, subdevs
, subdev
, readonly
, runstop
, verbose
, force
);
387 case 'B': /* Build */
388 rv
= Build(mddev
, mdfd
, chunk
, level
, raiddisks
, subdevs
,subdev
);
390 case 'C': /* Create */
391 rv
= Create(mddev
, mdfd
, chunk
, level
, layout
, size
, raiddisks
, sparedisks
,
392 subdevs
,subdev
,runstop
, verbose
);
394 case 'D': /* Detail */
395 for (i
=0; i
<subdevs
; i
++)
396 rv
|= Detail(subdev
[i
]);
398 case 'E': /* Examine */
399 for (i
=0; i
<subdevs
; i
++)
400 rv
|= Examine(subdev
[i
]);