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
;
79 char *symlinks
= NULL
;
80 /* autof indicates whether and how to create device node.
81 * bottom 3 bits are style. Rest (when shifted) are number of parts
83 * 1 - don't create (no)
84 * 2 - if is_standard, then create (yes)
85 * 3 - create as 'md' - reject is_standard mdp (md)
86 * 4 - create as 'mdp' - reject is_standard md (mdp)
87 * 5 - default to md if not is_standard (md in config file)
88 * 6 - default to mdp if not is_standard (part, or mdp in config file)
92 char *homehost
= NULL
;
93 char sys_hostname
[256];
94 char *mailaddr
= NULL
;
100 struct supertype
*ss
= NULL
;
103 char *shortopt
= short_options
;
106 int auto_update_home
= 0;
114 srandom(time(0) ^ getpid());
118 ident
.raid_disks
= UnSet
;
119 ident
.super_minor
= UnSet
;
121 ident
.spare_group
= NULL
;
124 ident
.bitmap_fd
= -1;
125 ident
.bitmap_file
= NULL
;
127 ident
.container
= NULL
;
130 while ((option_index
= -1) ,
131 (opt
=getopt_long(argc
, argv
,
132 shortopt
, long_options
,
133 &option_index
)) != -1) {
135 /* firstly, some mode-independent options */
138 if (option_index
> 0 &&
139 strcmp(long_options
[option_index
].name
, "help-options")==0)
146 fputs(Version
, stderr
);
156 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
157 break; /* b means bitmap */
160 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
174 fputs(Usage
, stderr
);
177 /* second, figure out the mode.
178 * Some options force the mode. Others
179 * set the mode if it isn't already
183 case '@': /* just incase they say --manage */
185 shortopt
= short_bitmap_options
;
190 case ReAdd
: /* re-add */
193 shortopt
= short_bitmap_options
;
197 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
198 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
199 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
200 case 'F': newmode
= MONITOR
;break;
201 case 'G': newmode
= GROW
;
202 shortopt
= short_bitmap_options
;
204 case 'I': newmode
= INCREMENTAL
;
205 shortopt
= short_bitmap_auto_options
; break;
207 newmode
= AUTODETECT
; break;
213 case 'Q': newmode
= MISC
; break;
221 case 'K': if (!mode
) newmode
= MISC
; break;
223 if (mode
&& newmode
== mode
) {
224 /* everybody happy ! */
225 } else if (mode
&& newmode
!= mode
) {
227 fprintf(stderr
, Name
": ");
228 if (option_index
>= 0)
229 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
231 fprintf(stderr
, "-%c", opt
);
232 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
233 map_num(modes
, newmode
),
234 map_num(modes
, mode
));
236 } else if (!mode
&& newmode
) {
239 /* special case of -c --help */
241 ( strncmp(optarg
, "--h", 3)==0 ||
242 strncmp(optarg
, "-h", 2)==0)) {
243 fputs(Help_config
, stdout
);
247 /* If first option is a device, don't force the mode yet */
249 if (devs_found
== 0) {
250 dv
= malloc(sizeof(*dv
));
252 fprintf(stderr
, Name
": malloc failed\n");
255 dv
->devname
= optarg
;
256 dv
->disposition
= devmode
;
257 dv
->writemostly
= writemostly
;
263 devlistend
= &dv
->next
;
268 /* No mode yet, and this is the second device ... */
269 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
272 if (option_index
>= 0)
273 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
275 fprintf(stderr
, Name
": -%c", opt
);
276 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
280 /* if we just set the mode, then done */
294 /* an undecorated option - must be a device name.
296 if (devs_found
> 0 && mode
== '@' && !devmode
) {
297 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
300 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
301 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
304 dv
= malloc(sizeof(*dv
));
306 fprintf(stderr
, Name
": malloc failed\n");
309 dv
->devname
= optarg
;
310 dv
->disposition
= devmode
;
311 dv
->writemostly
= writemostly
;
317 devlistend
= &dv
->next
;
323 /* We've got a mode, and opt is now something else which
324 * could depend on the mode */
325 #define O(a,b) ((a<<8)|b)
326 switch (O(mode
,opt
)) {
328 case O(BUILD
,'c'): /* chunk or rounding */
330 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
331 "Second value is %s.\n", optarg
);
334 chunk
= strtol(optarg
, &c
, 10);
335 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
336 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
342 case O(ASSEMBLE
,AutoHomeHost
):
343 auto_update_home
= 1;
345 case O(INCREMENTAL
, 'e'):
347 case O(ASSEMBLE
,'e'):
348 case O(MISC
,'e'): /* set metadata (superblock) information */
350 fprintf(stderr
, Name
": metadata information already given\n");
353 for(i
=0; !ss
&& superlist
[i
]; i
++)
354 ss
= superlist
[i
]->match_metadata_desc(optarg
);
357 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
360 max_disks
= ss
->max_devs
;
366 /* set write-mostly for following devices */
371 /* clear write-mostly for following devices */
377 case O(CREATE
,'z'): /* size */
379 fprintf(stderr
, Name
": size may only be specified once. "
380 "Second value is %s.\n", optarg
);
383 if (strcmp(optarg
, "max")==0)
386 size
= strtoll(optarg
, &c
, 10);
387 if (!optarg
[0] || *c
|| size
< 4) {
388 fprintf(stderr
, Name
": invalid size: %s\n",
395 case O(GROW
,'l'): /* hack - needed to understand layout */
397 case O(BUILD
,'l'): /* set raid level*/
398 if (level
!= UnSet
) {
399 fprintf(stderr
, Name
": raid level may only be set once. "
400 "Second value is %s.\n", optarg
);
403 level
= map_name(pers
, optarg
);
404 if (level
== UnSet
) {
405 fprintf(stderr
, Name
": invalid raid level: %s\n",
409 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
410 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
414 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
415 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
422 case O(CREATE
,'p'): /* raid5 layout */
423 case O(BUILD
,'p'): /* faulty layout */
424 case O(GROW
, 'p'): /* faulty reconfig */
425 if (layout
!= UnSet
) {
426 fprintf(stderr
,Name
": layout may only be sent once. "
427 "Second value was %s\n", optarg
);
432 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
433 map_num(pers
, level
));
436 fprintf(stderr
, Name
": raid level must be given before layout.\n");
440 layout
= map_name(r5layout
, optarg
);
442 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
448 layout
= map_name(r6layout
, optarg
);
450 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
457 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
458 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
459 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
462 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
465 if (optarg
[0] == 'n')
466 layout
= 256 + copies
;
467 else if (optarg
[0] == 'o')
468 layout
= 0x10000 + (copies
<<8) + 1;
470 layout
= 1 + (copies
<<8);
477 int ln
= strcspn(optarg
, "0123456789");
478 char *m
= strdup(optarg
);
481 mode
= map_name(faultylayout
, m
);
483 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
487 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
492 case O(CREATE
,AssumeClean
):
493 case O(BUILD
,AssumeClean
): /* assume clean */
499 case O(BUILD
,'n'): /* number of raid disks */
501 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
505 raiddisks
= strtol(optarg
, &c
, 10);
506 if (!optarg
[0] || *c
|| raiddisks
<=0) {
507 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
511 ident
.raid_disks
= raiddisks
;
514 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
516 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
520 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
521 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
525 sparedisks
= strtol(optarg
, &c
, 10);
526 if (!optarg
[0] || *c
|| sparedisks
< 0) {
527 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
535 case O(INCREMENTAL
,'a'):
536 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
537 autof
= parse_auto(optarg
, "--auto flag", 0);
540 case O(CREATE
,Symlinks
):
541 case O(BUILD
,Symlinks
):
542 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
546 case O(BUILD
,'f'): /* force honouring '-n 1' */
547 case O(GROW
,'f'): /* ditto */
548 case O(CREATE
,'f'): /* force honouring of device list */
549 case O(ASSEMBLE
,'f'): /* force assembly */
550 case O(MISC
,'f'): /* force zero */
554 /* now for the Assemble options */
555 case O(CREATE
,'u'): /* uuid of array */
556 case O(ASSEMBLE
,'u'): /* uuid of array */
557 if (ident
.uuid_set
) {
558 fprintf(stderr
, Name
": uuid cannot be set twice. "
559 "Second value %s.\n", optarg
);
562 if (parse_uuid(optarg
, ident
.uuid
))
565 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
571 case O(ASSEMBLE
,'N'):
573 fprintf(stderr
, Name
": name cannot be set twice. "
574 "Second value %s.\n", optarg
);
577 if (strlen(optarg
) > 32) {
578 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
582 strcpy(ident
.name
, optarg
);
585 case O(ASSEMBLE
,'m'): /* super-minor for array */
586 if (ident
.super_minor
!= UnSet
) {
587 fprintf(stderr
, Name
": super-minor cannot be set twice. "
588 "Second value: %s.\n", optarg
);
591 if (strcmp(optarg
, "dev")==0)
592 ident
.super_minor
= -2;
594 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
595 if (!optarg
[0] || *cp
) {
596 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
602 case O(ASSEMBLE
,'U'): /* update the superblock */
604 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
609 if (strcmp(update
, "sparc2.2")==0)
611 if (strcmp(update
, "super-minor") == 0)
613 if (strcmp(update
, "summaries")==0)
615 if (strcmp(update
, "resync")==0)
617 if (strcmp(update
, "uuid")==0)
619 if (strcmp(update
, "name")==0)
621 if (strcmp(update
, "homehost")==0)
623 if (strcmp(update
, "devicesize")==0)
625 if (strcmp(update
, "byteorder")==0) {
627 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
630 for(i
=0; !ss
&& superlist
[i
]; i
++)
631 ss
= superlist
[i
]->match_metadata_desc("0.swap");
633 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
639 if (strcmp(update
,"?") == 0 ||
640 strcmp(update
, "help") == 0) {
642 fprintf(outf
, Name
": ");
646 Name
": '--update=%s' is invalid. ",
649 fprintf(outf
, "Valid --update options are:\n"
650 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
651 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
652 exit(outf
== stdout
? 0 : 2);
654 case O(INCREMENTAL
,NoDegraded
):
655 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
656 runstop
= -1; /* --stop isn't allowed for --assemble,
657 * so we overload slightly */
660 case O(ASSEMBLE
,'c'): /* config file */
661 case O(INCREMENTAL
, 'c'):
665 fprintf(stderr
, Name
": configfile cannot be set twice. "
666 "Second value is %s.\n", optarg
);
670 set_conffile(configfile
);
671 /* FIXME possibly check that config file exists. Even parse it */
673 case O(ASSEMBLE
,'s'): /* scan */
676 case O(INCREMENTAL
,'s'):
680 case O(MONITOR
,'m'): /* mail address */
682 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
688 case O(MONITOR
,'p'): /* alert program */
690 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
696 case O(MONITOR
,'d'): /* delay in seconds */
698 case O(BUILD
,'d'): /* delay for bitmap updates */
701 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
704 delay
= strtol(optarg
, &c
, 10);
705 if (!optarg
[0] || *c
|| delay
<1) {
706 fprintf(stderr
, Name
": invalid delay: %s\n",
712 case O(MONITOR
,'f'): /* daemonise */
715 case O(MONITOR
,'i'): /* pid */
717 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
722 case O(MONITOR
,'1'): /* oneshot */
725 case O(MONITOR
,'t'): /* test */
728 case O(MONITOR
,'y'): /* log messages to syslog */
729 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
733 /* now the general management options. Some are applicable
734 * to other modes. None have arguments.
737 case O(MANAGE
,'a'): /* add a drive */
741 case O(MANAGE
,ReAdd
):
745 case O(MANAGE
,'r'): /* remove a drive */
748 case O(MANAGE
,'f'): /* set faulty */
751 case O(INCREMENTAL
,'R'):
753 case O(ASSEMBLE
,'R'):
755 case O(CREATE
,'R'): /* Run the array */
757 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
764 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
780 case O(MISC
, Waitclean
):
781 case O(MISC
, DetailPlatform
):
782 if (devmode
&& devmode
!= opt
&&
783 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
784 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
785 devmode
=='E'?opt
:devmode
);
794 case O(MISC
, Sparc22
):
795 if (devmode
!= 'E') {
796 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
802 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
804 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
807 if (strcmp(optarg
, "internal")==0) {
808 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
811 bitmap_fd
= open(optarg
, O_RDWR
);
812 if (!*optarg
|| bitmap_fd
< 0) {
813 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
816 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
819 case O(ASSEMBLE
, BackupFile
):
820 case O(GROW
, BackupFile
):
821 /* Specify a file into which grow might place a backup,
822 * or from which assemble might recover a backup
825 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
828 backup_file
= optarg
;
832 case O(CREATE
,'b'): /* here we create the bitmap */
833 if (strcmp(optarg
, "none") == 0) {
834 fprintf(stderr
, Name
": '--bitmap none' only"
835 " support for --grow\n");
840 if (strcmp(optarg
, "internal")== 0 ||
841 strcmp(optarg
, "none")== 0 ||
842 strchr(optarg
, '/') != NULL
) {
843 bitmap_file
= optarg
;
847 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
850 case O(GROW
,BitmapChunk
):
851 case O(BUILD
,BitmapChunk
):
852 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
853 bitmap_chunk
= strtol(optarg
, &c
, 10);
854 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
855 bitmap_chunk
& (bitmap_chunk
- 1)) {
856 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
860 /* convert K to B, chunk of 0K means 512B */
861 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
864 case O(GROW
, WriteBehind
):
865 case O(BUILD
, WriteBehind
):
866 case O(CREATE
, WriteBehind
): /* write-behind mode */
867 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
869 write_behind
= strtol(optarg
, &c
, 10);
870 if (write_behind
< 0 || *c
||
871 write_behind
> 16383) {
872 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
878 case O(INCREMENTAL
, 'r'):
882 /* We have now processed all the valid options. Anything else is
885 if (option_index
> 0)
886 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
887 long_options
[option_index
].name
,
888 map_num(modes
, mode
));
890 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
891 opt
, map_num(modes
, mode
));
897 char *help_text
= Help
;
899 help_text
= OptionHelp
;
902 case ASSEMBLE
: help_text
= Help_assemble
; break;
903 case BUILD
: help_text
= Help_build
; break;
904 case CREATE
: help_text
= Help_create
; break;
905 case MANAGE
: help_text
= Help_manage
; break;
906 case MISC
: help_text
= Help_misc
; break;
907 case MONITOR
: help_text
= Help_monitor
; break;
908 case GROW
: help_text
= Help_grow
; break;
909 case INCREMENTAL
:help_text
= Help_incr
; break;
911 fputs(help_text
,stdout
);
915 if (!mode
&& devs_found
) {
918 if (devlist
->disposition
== 0)
919 devlist
->disposition
= devmode
;
922 fputs(Usage
, stderr
);
927 struct createinfo
*ci
= conf_get_create_info();
929 if (strcasecmp(symlinks
, "yes") == 0)
931 else if (strcasecmp(symlinks
, "no") == 0)
934 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
938 /* Ok, got the option parsing out of the way
939 * hopefully it's mostly right but there might be some stuff
942 * That is mosty checked in the per-mode stuff but...
944 * For @,B,C and A without -s, the first device listed must be an md device
945 * we check that here and open it.
948 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
949 (mode
== ASSEMBLE
&& ! scan
)) {
950 if (devs_found
< 1) {
951 fprintf(stderr
, Name
": an md device must be given in this mode\n");
954 if ((int)ident
.super_minor
== -2 && autof
) {
955 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
958 if (mode
== MANAGE
|| mode
== GROW
) {
959 mdfd
= open_mddev(devlist
->devname
, 1);
963 /* non-existent device is OK */
964 mdfd
= open_mddev(devlist
->devname
, 0);
966 fprintf(stderr
, Name
": device %s exists but is not an "
967 "md array.\n", devlist
->devname
);
970 if ((int)ident
.super_minor
== -2) {
973 fprintf(stderr
, Name
": --super-minor=dev given, and "
974 "listed device %s doesn't exist.\n",
979 ident
.super_minor
= minor(stb
.st_rdev
);
981 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
982 /* We don't really want this open yet, we just might
983 * have wanted to check some things
991 if (raiddisks
> max_disks
) {
992 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
996 if (raiddisks
== 1 && !force
&& level
!= -5) {
997 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
998 " a mistake. If you really mean it you will need to specify --force before\n"
999 " setting the number of drives.\n");
1004 if ( sparedisks
> max_disks
- raiddisks
) {
1005 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1011 if (homehost
== NULL
)
1012 homehost
= conf_get_homehost();
1013 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1014 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1015 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1016 homehost
= sys_hostname
;
1020 ident
.autof
= autof
;
1025 /* readonly, add/remove, readwrite, runstop */
1027 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1028 if (!rv
&& devs_found
>1)
1029 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1030 devlist
->next
, verbose
-quiet
);
1031 if (!rv
&& readonly
< 0)
1032 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1034 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1037 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1038 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1039 /* Only a device has been given, so get details from config file */
1040 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1041 if (array_ident
== NULL
) {
1042 fprintf(stderr
, Name
": %s not identified in config file.\n",
1048 if (array_ident
->autof
== 0)
1049 array_ident
->autof
= autof
;
1050 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1052 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1055 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1056 devlist
->next
, backup_file
,
1057 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1058 else if (devs_found
>0) {
1059 if (update
&& devs_found
> 1) {
1060 fprintf(stderr
, Name
": can only update a single array at a time\n");
1063 if (backup_file
&& devs_found
> 1) {
1064 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1067 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1068 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1069 if (array_ident
== NULL
) {
1070 fprintf(stderr
, Name
": %s not identified in config file.\n",
1075 if (array_ident
->autof
== 0)
1076 array_ident
->autof
= autof
;
1077 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1079 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1082 mddev_ident_t array_list
= conf_get_ident(NULL
);
1083 mddev_dev_t devlist
= conf_get_devs();
1085 if (devlist
== NULL
) {
1086 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1090 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1094 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1097 for (; array_list
; array_list
= array_list
->next
) {
1098 if (array_list
->autof
== 0)
1099 array_list
->autof
= autof
;
1101 rv
|= Assemble(ss
, array_list
->devname
,
1104 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1107 if (homehost
&& cnt
== 0) {
1108 /* Maybe we can auto-assemble something.
1109 * Repeatedly call Assemble in auto-assemble mode
1114 ident
.autof
= autof
;
1116 mddev_dev_t devlist
= conf_get_devs();
1119 rv2
= Assemble(ss
, NULL
,
1122 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1128 /* found something so even though assembly failed we
1129 * want to avoid auto-updates
1131 auto_update_home
= 0;
1133 /* Incase there are stacked devices, we need to go around again */
1135 if (cnt
== 0 && auto_update_home
&& homehost
) {
1136 /* Nothing found, maybe we need to bootstrap homehost info */
1140 rv2
= Assemble(ss
, NULL
,
1143 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1149 /* Incase there are stacked devices, we need to go around again */
1152 if (cnt
== 0 && rv
== 0) {
1153 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1157 } else if (cnt
== 0 && rv
== 0) {
1158 fprintf(stderr
, Name
": No arrays found in config file\n");
1164 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1165 if (write_behind
&& !bitmap_file
) {
1166 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1170 if (raiddisks
== 0) {
1171 fprintf(stderr
, Name
": no raid-devices specified.\n");
1177 if (strcmp(bitmap_file
, "internal")==0) {
1178 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1183 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1184 raiddisks
, devlist
->next
, assume_clean
,
1185 bitmap_file
, bitmap_chunk
, write_behind
,
1186 delay
, verbose
-quiet
, autof
);
1189 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1190 if (write_behind
&& !bitmap_file
) {
1191 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1195 if (raiddisks
== 0) {
1196 fprintf(stderr
, Name
": no raid-devices specified.\n");
1201 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1202 raiddisks
, sparedisks
, ident
.name
, homehost
,
1203 ident
.uuid_set
? ident
.uuid
: NULL
,
1204 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1205 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1208 if (devmode
== 'E') {
1209 if (devlist
== NULL
&& !scan
) {
1210 fprintf(stderr
, Name
": No devices to examine\n");
1213 if (devlist
== NULL
)
1214 devlist
= conf_get_devs();
1215 if (devlist
== NULL
) {
1216 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1219 if (brief
&& verbose
)
1221 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1223 SparcAdjust
, ss
, homehost
);
1224 } else if (devmode
== DetailPlatform
) {
1225 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1227 if (devlist
== NULL
) {
1228 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1229 /* apply --detail or --wait-clean to
1230 * all devices in /proc/mdstat
1232 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1233 struct mdstat_ent
*e
;
1234 struct map_ent
*map
= NULL
;
1235 int v
= verbose
>1?0:verbose
+1;
1237 for (e
=ms
; e
; e
=e
->next
) {
1240 me
= map_by_devnum(&map
, e
->devnum
);
1244 name
= get_md_name(e
->devnum
);
1247 fprintf(stderr
, Name
": cannot find device file for %s\n",
1252 rv
|= Detail(name
, v
,
1256 rv
|= WaitClean(name
, v
);
1260 } else if (devmode
== 'S' && scan
) {
1261 /* apply --stop to all devices in /proc/mdstat */
1262 /* Due to possible stacking of devices, repeat until
1263 * nothing more can be stopped
1265 int progress
=1, err
;
1268 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1269 struct mdstat_ent
*e
;
1271 if (!progress
) last
= 1;
1272 progress
= 0; err
= 0;
1273 for (e
=ms
; e
; e
=e
->next
) {
1274 char *name
= get_md_name(e
->devnum
);
1277 fprintf(stderr
, Name
": cannot find device file for %s\n",
1281 mdfd
= open_mddev(name
, 1);
1283 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1293 } while (!last
&& err
);
1296 fprintf(stderr
, Name
": No devices given.\n");
1300 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1301 switch(dv
->disposition
) {
1303 rv
|= Detail(dv
->devname
,
1305 export
, test
, homehost
);
1307 case 'K': /* Zero superblock */
1308 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1311 rv
|= Query(dv
->devname
); continue;
1313 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1315 rv
|= Wait(dv
->devname
); continue;
1317 rv
|= WaitClean(dv
->devname
, verbose
-quiet
); continue;
1319 mdfd
= open_mddev(dv
->devname
, 1);
1321 switch(dv
->disposition
) {
1323 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1325 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1327 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1329 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1338 if (!devlist
&& !scan
) {
1339 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1343 if (pidfile
&& !daemonise
) {
1344 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1348 rv
= Monitor(devlist
, mailaddr
, program
,
1349 delay
?delay
:60, daemonise
, scan
, oneshot
,
1350 dosyslog
, test
, pidfile
);
1354 if (devs_found
> 1) {
1357 if (size
>= 0 || raiddisks
) {
1358 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1362 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1363 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1367 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1368 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1371 } else if (layout
!= UnSet
)
1372 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1373 else if (size
>= 0 || raiddisks
)
1374 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1375 size
, level
, layout
, chunk
, raiddisks
);
1376 else if (bitmap_file
) {
1377 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1378 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1379 bitmap_chunk
, delay
, write_behind
, force
);
1381 fprintf(stderr
, Name
": no changes to --grow\n");
1389 fprintf(stderr
, Name
1390 ": --incremental --scan meaningless without --run.\n");
1393 rv
= IncrementalScan(verbose
);
1396 if (!rebuild_map
&& !scan
) {
1397 fprintf(stderr
, Name
1398 ": --incremental requires a device.\n");
1403 if (devlist
->next
) {
1404 fprintf(stderr
, Name
1405 ": --incremental can only handle one device.\n");
1409 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1410 ss
, homehost
, autof
);