2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
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
29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
30 * Paul Clements, SteelEye Technology, Inc.
38 int main(int argc
, char *argv
[])
52 int max_disks
= MD_SB_DISKS
; /* just a default */
54 struct mddev_ident_s ident
;
55 char *configfile
= NULL
;
64 char *bitmap_file
= NULL
;
65 char *backup_file
= NULL
;
66 int bitmap_chunk
= UnSet
;
68 mddev_dev_t devlist
= NULL
;
69 mddev_dev_t
*devlistend
= & devlist
;
78 char *symlinks
= NULL
;
79 /* autof indicates whether and how to create device node.
80 * bottom 3 bits are style. Rest (when shifted) are number of parts
82 * 1 - don't create (no)
83 * 2 - if is_standard, then create (yes)
84 * 3 - create as 'md' - reject is_standard mdp (md)
85 * 4 - create as 'mdp' - reject is_standard md (mdp)
86 * 5 - default to md if not is_standard (md in config file)
87 * 6 - default to mdp if not is_standard (part, or mdp in config file)
91 char *homehost
= NULL
;
92 char sys_hostname
[256];
93 char *mailaddr
= NULL
;
99 struct supertype
*ss
= NULL
;
102 char *shortopt
= short_options
;
104 int auto_update_home
= 0;
111 srandom(time(0) ^ getpid());
115 ident
.raid_disks
= UnSet
;
116 ident
.super_minor
= UnSet
;
118 ident
.spare_group
= NULL
;
121 ident
.bitmap_fd
= -1;
122 ident
.bitmap_file
= NULL
;
125 while ((option_index
= -1) ,
126 (opt
=getopt_long(argc
, argv
,
127 shortopt
, long_options
,
128 &option_index
)) != -1) {
130 /* firstly, some mode-independant options */
133 if (option_index
> 0 &&
134 strcmp(long_options
[option_index
].name
, "help-options")==0)
141 fputs(Version
, stderr
);
151 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
152 break; /* b means bitmap */
155 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
166 fputs(Usage
, stderr
);
169 /* second, figure out the mode.
170 * Some options force the mode. Others
171 * set the mode if it isn't already
175 case '@': /* just incase they say --manage */
177 shortopt
= short_bitmap_auto_options
;
182 case ReAdd
: /* re-add */
185 shortopt
= short_bitmap_auto_options
;
189 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
190 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
191 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
192 case 'F': newmode
= MONITOR
;break;
193 case 'G': newmode
= GROW
; shortopt
= short_bitmap_auto_options
; break;
199 case 'Q': newmode
= MISC
; break;
204 case 'K': if (!mode
) newmode
= MISC
; break;
206 if (mode
&& newmode
== mode
) {
207 /* everybody happy ! */
208 } else if (mode
&& newmode
!= mode
) {
210 fprintf(stderr
, Name
": ");
211 if (option_index
>= 0)
212 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
214 fprintf(stderr
, "-%c", opt
);
215 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
216 map_num(modes
, newmode
),
217 map_num(modes
, mode
));
219 } else if (!mode
&& newmode
) {
222 /* special case of -c --help */
224 ( strncmp(optarg
, "--h", 3)==0 ||
225 strncmp(optarg
, "-h", 2)==0)) {
226 fputs(Help_config
, stderr
);
230 /* If first option is a device, don't force the mode yet */
232 if (devs_found
== 0) {
233 dv
= malloc(sizeof(*dv
));
235 fprintf(stderr
, Name
": malloc failed\n");
238 dv
->devname
= optarg
;
239 dv
->disposition
= devmode
;
240 dv
->writemostly
= writemostly
;
245 devlistend
= &dv
->next
;
250 /* No mode yet, and this is the second device ... */
251 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
254 if (option_index
>= 0)
255 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
257 fprintf(stderr
, Name
": -%c", opt
);
258 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
262 /* if we just set the mode, then done */
274 /* an undecorated option - must be a device name.
276 if (devs_found
> 0 && mode
== '@' && !devmode
) {
277 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
280 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
281 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
284 dv
= malloc(sizeof(*dv
));
286 fprintf(stderr
, Name
": malloc failed\n");
289 dv
->devname
= optarg
;
290 dv
->disposition
= devmode
;
291 dv
->writemostly
= writemostly
;
295 devlistend
= &dv
->next
;
301 /* We've got a mode, and opt is now something else which
302 * could depend on the mode */
303 #define O(a,b) ((a<<8)|b)
304 switch (O(mode
,opt
)) {
306 case O(BUILD
,'c'): /* chunk or rounding */
308 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
309 "Second value is %s.\n", optarg
);
312 chunk
= strtol(optarg
, &c
, 10);
313 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
314 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
320 case O(ASSEMBLE
,AutoHomeHost
):
321 auto_update_home
= 1;
324 case O(ASSEMBLE
,'e'):
325 case O(MISC
,'e'): /* set metadata (superblock) information */
327 fprintf(stderr
, Name
": metadata information already given\n");
330 for(i
=0; !ss
&& superlist
[i
]; i
++)
331 ss
= superlist
[i
]->match_metadata_desc(optarg
);
334 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
337 max_disks
= ss
->max_devs
;
343 /* set write-mostly for following devices */
348 case O(CREATE
,'z'): /* size */
350 fprintf(stderr
, Name
": size may only be specified once. "
351 "Second value is %s.\n", optarg
);
354 if (strcmp(optarg
, "max")==0)
357 size
= strtoll(optarg
, &c
, 10);
358 if (!optarg
[0] || *c
|| size
< 4) {
359 fprintf(stderr
, Name
": invalid size: %s\n",
366 case O(GROW
,'l'): /* hack - needed to understand layout */
368 case O(BUILD
,'l'): /* set raid level*/
369 if (level
!= UnSet
) {
370 fprintf(stderr
, Name
": raid level may only be set once. "
371 "Second value is %s.\n", optarg
);
374 level
= map_name(pers
, optarg
);
375 if (level
== UnSet
) {
376 fprintf(stderr
, Name
": invalid raid level: %s\n",
380 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
381 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
385 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
386 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
393 case O(CREATE
,'p'): /* raid5 layout */
394 case O(BUILD
,'p'): /* faulty layout */
395 case O(GROW
, 'p'): /* faulty reconfig */
396 if (layout
!= UnSet
) {
397 fprintf(stderr
,Name
": layout may only be sent once. "
398 "Second value was %s\n", optarg
);
403 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
404 map_num(pers
, level
));
407 fprintf(stderr
, Name
": raid level must be given before layout.\n");
412 layout
= map_name(r5layout
, optarg
);
414 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
421 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
422 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
423 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
426 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
429 if (optarg
[0] == 'n')
430 layout
= 256 + copies
;
431 else if (optarg
[0] == 'o')
432 layout
= 0x10000 + (copies
<<8) + 1;
434 layout
= 1 + (copies
<<8);
441 int ln
= strcspn(optarg
, "0123456789");
442 char *m
= strdup(optarg
);
445 mode
= map_name(faultylayout
, m
);
447 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
451 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
456 case O(CREATE
,AssumeClean
):
457 case O(BUILD
,AssumeClean
): /* assume clean */
463 case O(BUILD
,'n'): /* number of raid disks */
465 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
469 raiddisks
= strtol(optarg
, &c
, 10);
470 if (!optarg
[0] || *c
|| raiddisks
<=0) {
471 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
475 ident
.raid_disks
= raiddisks
;
478 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
480 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
484 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
485 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
489 sparedisks
= strtol(optarg
, &c
, 10);
490 if (!optarg
[0] || *c
|| sparedisks
< 0) {
491 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
499 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
500 autof
= parse_auto(optarg
, "--auto flag", 0);
503 case O(CREATE
,Symlinks
):
504 case O(BUILD
,Symlinks
):
505 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
509 case O(BUILD
,'f'): /* force honouring '-n 1' */
510 case O(GROW
,'f'): /* ditto */
511 case O(CREATE
,'f'): /* force honouring of device list */
512 case O(ASSEMBLE
,'f'): /* force assembly */
513 case O(MISC
,'f'): /* force zero */
517 /* now for the Assemble options */
518 case O(ASSEMBLE
,'u'): /* uuid of array */
519 if (ident
.uuid_set
) {
520 fprintf(stderr
, Name
": uuid cannot be set twice. "
521 "Second value %s.\n", optarg
);
524 if (parse_uuid(optarg
, ident
.uuid
))
527 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
533 case O(ASSEMBLE
,'N'):
535 fprintf(stderr
, Name
": name cannot be set twice. "
536 "Second value %s.\n", optarg
);
539 if (strlen(optarg
) > 32) {
540 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
544 strcpy(ident
.name
, optarg
);
547 case O(ASSEMBLE
,'m'): /* super-minor for array */
548 if (ident
.super_minor
!= UnSet
) {
549 fprintf(stderr
, Name
": super-minor cannot be set twice. "
550 "Second value: %s.\n", optarg
);
553 if (strcmp(optarg
, "dev")==0)
554 ident
.super_minor
= -2;
556 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
557 if (!optarg
[0] || *cp
) {
558 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
564 case O(ASSEMBLE
,'U'): /* update the superblock */
566 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
571 if (strcmp(update
, "sparc2.2")==0)
573 if (strcmp(update
, "super-minor") == 0)
575 if (strcmp(update
, "summaries")==0)
577 if (strcmp(update
, "resync")==0)
579 if (strcmp(update
, "uuid")==0)
581 if (strcmp(update
, "name")==0)
583 if (strcmp(update
, "homehost")==0)
585 if (strcmp(update
, "byteorder")==0) {
587 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
590 for(i
=0; !ss
&& superlist
[i
]; i
++)
591 ss
= superlist
[i
]->match_metadata_desc("0.swap");
593 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
599 if (strcmp(update
,"?") == 0 || strcmp(update
, "help") == 0)
600 fprintf(stderr
, Name
": ");
602 fprintf(stderr
, Name
": '--update=%s' is invalid. ", update
);
603 fprintf(stderr
, "Valid --update options are:\n"
604 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
605 " 'summaries', 'homehost', 'byteorder'.\n");
608 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
609 runstop
= -1; /* --stop isn't allowed for --assemble, so we overload slightly */
612 case O(ASSEMBLE
,'c'): /* config file */
616 fprintf(stderr
, Name
": configfile cannot be set twice. "
617 "Second value is %s.\n", optarg
);
621 set_conffile(configfile
);
622 /* FIXME possibly check that config file exists. Even parse it */
624 case O(ASSEMBLE
,'s'): /* scan */
630 case O(MONITOR
,'m'): /* mail address */
632 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
638 case O(MONITOR
,'p'): /* alert program */
640 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
646 case O(MONITOR
,'d'): /* delay in seconds */
648 case O(BUILD
,'d'): /* delay for bitmap updates */
651 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
654 delay
= strtol(optarg
, &c
, 10);
655 if (!optarg
[0] || *c
|| delay
<1) {
656 fprintf(stderr
, Name
": invalid delay: %s\n",
662 case O(MONITOR
,'f'): /* daemonise */
665 case O(MONITOR
,'i'): /* pid */
667 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
672 case O(MONITOR
,'1'): /* oneshot */
675 case O(MONITOR
,'t'): /* test */
678 case O(MONITOR
,'y'): /* log messages to syslog */
679 openlog("mdadm", 0, SYSLOG_FACILITY
);
683 /* now the general management options. Some are applicable
684 * to other modes. None have arguments.
687 case O(MANAGE
,'a'): /* add a drive */
691 case O(MANAGE
,ReAdd
):
695 case O(MANAGE
,'r'): /* remove a drive */
698 case O(MANAGE
,'f'): /* set faulty */
702 case O(ASSEMBLE
,'R'):
704 case O(CREATE
,'R'): /* Run the array */
706 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
713 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
721 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
728 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
743 if (devmode
&& devmode
!= opt
&&
744 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
745 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
746 devmode
=='E'?opt
:devmode
);
755 case O(MISC
, Sparc22
):
756 if (devmode
!= 'E') {
757 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
763 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
765 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
768 if (strcmp(optarg
, "internal")==0) {
769 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
772 bitmap_fd
= open(optarg
, O_RDWR
);
773 if (!*optarg
|| bitmap_fd
< 0) {
774 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
777 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
780 case O(ASSEMBLE
, BackupFile
):
781 case O(GROW
, BackupFile
):
782 /* Specify a file into which grow might place a backup,
783 * or from which assemble might recover a backup
786 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
789 backup_file
= optarg
;
794 case O(CREATE
,'b'): /* here we create the bitmap */
795 if (strcmp(optarg
, "internal")== 0 ||
796 strcmp(optarg
, "none")== 0 ||
797 strchr(optarg
, '/') != NULL
) {
798 bitmap_file
= optarg
;
802 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
805 case O(GROW
,BitmapChunk
):
806 case O(BUILD
,BitmapChunk
):
807 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
808 bitmap_chunk
= strtol(optarg
, &c
, 10);
809 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
810 bitmap_chunk
& (bitmap_chunk
- 1)) {
811 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
815 /* convert K to B, chunk of 0K means 512B */
816 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
819 case O(BUILD
, WriteBehind
):
820 case O(CREATE
, WriteBehind
): /* write-behind mode */
821 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
823 write_behind
= strtol(optarg
, &c
, 10);
824 if (write_behind
< 0 || *c
||
825 write_behind
> 16383) {
826 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
832 /* We have now processed all the valid options. Anything else is
835 if (option_index
> 0)
836 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
837 long_options
[option_index
].name
,
838 map_num(modes
, mode
));
840 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
841 opt
, map_num(modes
, mode
));
847 char *help_text
= Help
;
849 help_text
= OptionHelp
;
852 case ASSEMBLE
: help_text
= Help_assemble
; break;
853 case BUILD
: help_text
= Help_build
; break;
854 case CREATE
: help_text
= Help_create
; break;
855 case MANAGE
: help_text
= Help_manage
; break;
856 case MISC
: help_text
= Help_misc
; break;
857 case MONITOR
: help_text
= Help_monitor
; break;
858 case GROW
: help_text
= Help_grow
; break;
860 fputs(help_text
,stderr
);
864 if (!mode
&& devs_found
) {
867 if (devlist
->disposition
== 0)
868 devlist
->disposition
= devmode
;
871 fputs(Usage
, stderr
);
876 struct createinfo
*ci
= conf_get_create_info();
878 if (strcasecmp(symlinks
, "yes") == 0)
880 else if (strcasecmp(symlinks
, "no") == 0)
883 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
887 /* Ok, got the option parsing out of the way
888 * hopefully it's mostly right but there might be some stuff
891 * That is mosty checked in the per-mode stuff but...
893 * For @,B,C and A without -s, the first device listed must be an md device
894 * we check that here and open it.
897 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
898 (mode
== ASSEMBLE
&& ! scan
)) {
899 if (devs_found
< 1) {
900 fprintf(stderr
, Name
": an md device must be given in this mode\n");
903 if ((int)ident
.super_minor
== -2 && autof
) {
904 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
907 if (mode
== MANAGE
|| mode
== GROW
)
908 autof
=1; /* Don't create */
909 mdfd
= open_mddev(devlist
->devname
, autof
);
912 if ((int)ident
.super_minor
== -2) {
915 ident
.super_minor
= minor(stb
.st_rdev
);
920 if (raiddisks
> max_disks
) {
921 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
925 if (raiddisks
== 1 && !force
&& level
!= -5) {
926 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
927 " a mistake. If you really mean it you will need to specify --force before\n"
928 " setting the number of drives.\n");
933 if ( sparedisks
> max_disks
- raiddisks
) {
934 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
940 if (homehost
== NULL
)
941 homehost
= conf_get_homehost();
942 if (homehost
&& strcmp(homehost
, "<system>")==0) {
943 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
944 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
945 homehost
= sys_hostname
;
952 /* readonly, add/remove, readwrite, runstop */
954 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
955 if (!rv
&& devs_found
>1)
956 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
957 devlist
->next
, verbose
-quiet
);
958 if (!rv
&& readonly
< 0)
959 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
961 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
964 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
965 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
966 /* Only a device has been given, so get details from config file */
967 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
968 if (array_ident
== NULL
) {
969 fprintf(stderr
, Name
": %s not identified in config file.\n",
973 mdfd
= open_mddev(devlist
->devname
,
974 array_ident
->autof
? array_ident
->autof
: autof
);
978 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
,
980 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
985 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
,
986 devlist
->next
, backup_file
,
987 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
988 else if (devs_found
>0) {
989 if (update
&& devs_found
> 1) {
990 fprintf(stderr
, Name
": can only update a single array at a time\n");
993 if (backup_file
&& devs_found
> 1) {
994 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
997 for (dv
= devlist
; dv
; dv
=dv
->next
) {
998 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
999 if (array_ident
== NULL
) {
1000 fprintf(stderr
, Name
": %s not identified in config file.\n",
1005 mdfd
= open_mddev(dv
->devname
,
1006 array_ident
->autof
?array_ident
->autof
: autof
);
1011 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
,
1013 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1017 mddev_ident_t array_list
= conf_get_ident(NULL
);
1018 mddev_dev_t devlist
= conf_get_devs();
1020 if (devlist
== NULL
) {
1021 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1025 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1029 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1032 for (; array_list
; array_list
= array_list
->next
) {
1033 mdu_array_info_t array
;
1034 mdfd
= open_mddev(array_list
->devname
,
1035 array_list
->autof
? array_list
->autof
: autof
);
1040 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
1041 /* already assembled, skip */
1044 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
1047 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1053 /* Maybe we can auto-assemble something.
1054 * Repeatedly call Assemble in auto-assmble mode
1059 ident
.autof
= autof
;
1063 rv2
= Assemble(ss
, NULL
, -1,
1066 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1072 /* found something so even though assembly failed we
1073 * want to avoid auto-updates
1075 auto_update_home
= 0;
1077 /* Incase there are stacked devices, we need to go around again */
1079 if (cnt
== 0 && auto_update_home
&& homehost
) {
1080 /* Nothing found, maybe we need to bootstrap homehost info */
1084 rv2
= Assemble(ss
, NULL
, -1,
1087 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1093 /* Incase there are stacked devices, we need to go around again */
1096 if (cnt
== 0 && rv
== 0) {
1097 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1100 } else if (cnt
== 0 && rv
== 0) {
1101 fprintf(stderr
, Name
": No arrays found in config file\n");
1107 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1108 if (write_behind
&& !bitmap_file
) {
1109 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1115 if (strcmp(bitmap_file
, "internal")==0) {
1116 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1121 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
,
1122 raiddisks
, devlist
->next
, assume_clean
,
1123 bitmap_file
, bitmap_chunk
, write_behind
, delay
, verbose
-quiet
);
1126 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1127 if (write_behind
&& !bitmap_file
) {
1128 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1133 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1134 raiddisks
, sparedisks
, ident
.name
, homehost
,
1135 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1136 bitmap_file
, bitmap_chunk
, write_behind
, delay
);
1140 if (devmode
== 'E') {
1141 if (devlist
== NULL
&& !scan
) {
1142 fprintf(stderr
, Name
": No devices to examine\n");
1145 if (devlist
== NULL
)
1146 devlist
= conf_get_devs();
1147 if (devlist
== NULL
) {
1148 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1151 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
, scan
, SparcAdjust
, ss
, homehost
);
1153 if (devlist
== NULL
) {
1154 if (devmode
=='D' && scan
) {
1155 /* apply --detail to all devices in /proc/mdstat */
1156 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1157 struct mdstat_ent
*e
;
1158 for (e
=ms
; e
; e
=e
->next
) {
1159 char *name
= get_md_name(e
->devnum
);
1162 fprintf(stderr
, Name
": cannot find device file for %s\n",
1166 rv
|= Detail(name
, verbose
>1?0:verbose
+1, test
, homehost
);
1169 } else if (devmode
== 'S' && scan
) {
1170 /* apply --stop to all devices in /proc/mdstat */
1171 /* Due to possible stacking of devices, repeat until
1172 * nothing more can be stopped
1174 int progress
=1, err
;
1177 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1178 struct mdstat_ent
*e
;
1180 if (!progress
) last
= 1;
1181 progress
= 0; err
= 0;
1182 for (e
=ms
; e
; e
=e
->next
) {
1183 char *name
= get_md_name(e
->devnum
);
1186 fprintf(stderr
, Name
": cannot find device file for %s\n",
1190 mdfd
= open_mddev(name
, 1);
1192 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1201 } while (!last
&& err
);
1204 fprintf(stderr
, Name
": No devices given.\n");
1208 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1209 switch(dv
->disposition
) {
1211 rv
|= Detail(dv
->devname
, brief
?1+verbose
:0, test
, homehost
); continue;
1212 case 'K': /* Zero superblock */
1213 rv
|= Kill(dv
->devname
, force
, quiet
); continue;
1215 rv
|= Query(dv
->devname
); continue;
1217 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1219 mdfd
= open_mddev(dv
->devname
, 1);
1221 switch(dv
->disposition
) {
1223 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1225 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1227 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1229 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1238 if (!devlist
&& !scan
) {
1239 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1243 if (pidfile
&& !daemonise
) {
1244 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1248 rv
= Monitor(devlist
, mailaddr
, program
,
1249 delay
?delay
:60, daemonise
, scan
, oneshot
,
1250 dosyslog
, test
, pidfile
);
1254 if (devs_found
> 1) {
1257 if (size
>= 0 || raiddisks
) {
1258 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1262 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1263 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1267 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1268 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1271 } else if (layout
!= UnSet
)
1272 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1273 else if (size
>= 0 || raiddisks
)
1274 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1275 size
, level
, layout
, chunk
, raiddisks
);
1276 else if (bitmap_file
) {
1277 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1278 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1279 bitmap_chunk
, delay
, write_behind
, force
);
1281 fprintf(stderr
, Name
": no changes to --grow\n");