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-independant 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;
220 case 'K': if (!mode
) newmode
= MISC
; break;
222 if (mode
&& newmode
== mode
) {
223 /* everybody happy ! */
224 } else if (mode
&& newmode
!= mode
) {
226 fprintf(stderr
, Name
": ");
227 if (option_index
>= 0)
228 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
230 fprintf(stderr
, "-%c", opt
);
231 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
232 map_num(modes
, newmode
),
233 map_num(modes
, mode
));
235 } else if (!mode
&& newmode
) {
238 /* special case of -c --help */
240 ( strncmp(optarg
, "--h", 3)==0 ||
241 strncmp(optarg
, "-h", 2)==0)) {
242 fputs(Help_config
, stdout
);
246 /* If first option is a device, don't force the mode yet */
248 if (devs_found
== 0) {
249 dv
= malloc(sizeof(*dv
));
251 fprintf(stderr
, Name
": malloc failed\n");
254 dv
->devname
= optarg
;
255 dv
->disposition
= devmode
;
256 dv
->writemostly
= writemostly
;
261 devlistend
= &dv
->next
;
266 /* No mode yet, and this is the second device ... */
267 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
270 if (option_index
>= 0)
271 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
273 fprintf(stderr
, Name
": -%c", opt
);
274 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
278 /* if we just set the mode, then done */
292 /* an undecorated option - must be a device name.
294 if (devs_found
> 0 && mode
== '@' && !devmode
) {
295 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
298 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
299 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
302 dv
= malloc(sizeof(*dv
));
304 fprintf(stderr
, Name
": malloc failed\n");
307 dv
->devname
= optarg
;
308 dv
->disposition
= devmode
;
309 dv
->writemostly
= writemostly
;
313 devlistend
= &dv
->next
;
319 /* We've got a mode, and opt is now something else which
320 * could depend on the mode */
321 #define O(a,b) ((a<<8)|b)
322 switch (O(mode
,opt
)) {
324 case O(BUILD
,'c'): /* chunk or rounding */
326 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
327 "Second value is %s.\n", optarg
);
330 chunk
= strtol(optarg
, &c
, 10);
331 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
332 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
338 case O(ASSEMBLE
,AutoHomeHost
):
339 auto_update_home
= 1;
341 case O(INCREMENTAL
, 'e'):
343 case O(ASSEMBLE
,'e'):
344 case O(MISC
,'e'): /* set metadata (superblock) information */
346 fprintf(stderr
, Name
": metadata information already given\n");
349 for(i
=0; !ss
&& superlist
[i
]; i
++)
350 ss
= superlist
[i
]->match_metadata_desc(optarg
);
353 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
356 max_disks
= ss
->max_devs
;
362 /* set write-mostly for following devices */
367 /* clear write-mostly for following devices */
373 case O(CREATE
,'z'): /* size */
375 fprintf(stderr
, Name
": size may only be specified once. "
376 "Second value is %s.\n", optarg
);
379 if (strcmp(optarg
, "max")==0)
382 size
= strtoll(optarg
, &c
, 10);
383 if (!optarg
[0] || *c
|| size
< 4) {
384 fprintf(stderr
, Name
": invalid size: %s\n",
391 case O(GROW
,'l'): /* hack - needed to understand layout */
393 case O(BUILD
,'l'): /* set raid level*/
394 if (level
!= UnSet
) {
395 fprintf(stderr
, Name
": raid level may only be set once. "
396 "Second value is %s.\n", optarg
);
399 level
= map_name(pers
, optarg
);
400 if (level
== UnSet
) {
401 fprintf(stderr
, Name
": invalid raid level: %s\n",
405 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
406 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
410 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
411 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
418 case O(CREATE
,'p'): /* raid5 layout */
419 case O(BUILD
,'p'): /* faulty layout */
420 case O(GROW
, 'p'): /* faulty reconfig */
421 if (layout
!= UnSet
) {
422 fprintf(stderr
,Name
": layout may only be sent once. "
423 "Second value was %s\n", optarg
);
428 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
429 map_num(pers
, level
));
432 fprintf(stderr
, Name
": raid level must be given before layout.\n");
437 layout
= map_name(r5layout
, optarg
);
439 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
446 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
447 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
448 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
451 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
454 if (optarg
[0] == 'n')
455 layout
= 256 + copies
;
456 else if (optarg
[0] == 'o')
457 layout
= 0x10000 + (copies
<<8) + 1;
459 layout
= 1 + (copies
<<8);
466 int ln
= strcspn(optarg
, "0123456789");
467 char *m
= strdup(optarg
);
470 mode
= map_name(faultylayout
, m
);
472 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
476 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
481 case O(CREATE
,AssumeClean
):
482 case O(BUILD
,AssumeClean
): /* assume clean */
488 case O(BUILD
,'n'): /* number of raid disks */
490 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
494 raiddisks
= strtol(optarg
, &c
, 10);
495 if (!optarg
[0] || *c
|| raiddisks
<=0) {
496 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
500 ident
.raid_disks
= raiddisks
;
503 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
505 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
509 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
510 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
514 sparedisks
= strtol(optarg
, &c
, 10);
515 if (!optarg
[0] || *c
|| sparedisks
< 0) {
516 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
524 case O(INCREMENTAL
,'a'):
525 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
526 autof
= parse_auto(optarg
, "--auto flag", 0);
529 case O(CREATE
,Symlinks
):
530 case O(BUILD
,Symlinks
):
531 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
535 case O(BUILD
,'f'): /* force honouring '-n 1' */
536 case O(GROW
,'f'): /* ditto */
537 case O(CREATE
,'f'): /* force honouring of device list */
538 case O(ASSEMBLE
,'f'): /* force assembly */
539 case O(MISC
,'f'): /* force zero */
543 /* now for the Assemble options */
544 case O(CREATE
,'u'): /* uuid of array */
545 case O(ASSEMBLE
,'u'): /* uuid of array */
546 if (ident
.uuid_set
) {
547 fprintf(stderr
, Name
": uuid cannot be set twice. "
548 "Second value %s.\n", optarg
);
551 if (parse_uuid(optarg
, ident
.uuid
))
554 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
560 case O(ASSEMBLE
,'N'):
562 fprintf(stderr
, Name
": name cannot be set twice. "
563 "Second value %s.\n", optarg
);
566 if (strlen(optarg
) > 32) {
567 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
571 strcpy(ident
.name
, optarg
);
574 case O(ASSEMBLE
,'m'): /* super-minor for array */
575 if (ident
.super_minor
!= UnSet
) {
576 fprintf(stderr
, Name
": super-minor cannot be set twice. "
577 "Second value: %s.\n", optarg
);
580 if (strcmp(optarg
, "dev")==0)
581 ident
.super_minor
= -2;
583 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
584 if (!optarg
[0] || *cp
) {
585 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
591 case O(ASSEMBLE
,'U'): /* update the superblock */
593 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
598 if (strcmp(update
, "sparc2.2")==0)
600 if (strcmp(update
, "super-minor") == 0)
602 if (strcmp(update
, "summaries")==0)
604 if (strcmp(update
, "resync")==0)
606 if (strcmp(update
, "uuid")==0)
608 if (strcmp(update
, "name")==0)
610 if (strcmp(update
, "homehost")==0)
612 if (strcmp(update
, "devicesize")==0)
614 if (strcmp(update
, "byteorder")==0) {
616 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
619 for(i
=0; !ss
&& superlist
[i
]; i
++)
620 ss
= superlist
[i
]->match_metadata_desc("0.swap");
622 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
628 if (strcmp(update
,"?") == 0 ||
629 strcmp(update
, "help") == 0) {
631 fprintf(outf
, Name
": ");
635 Name
": '--update=%s' is invalid. ",
638 fprintf(outf
, "Valid --update options are:\n"
639 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
640 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
641 exit(outf
== stdout
? 0 : 2);
643 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
644 runstop
= -1; /* --stop isn't allowed for --assemble,
645 * so we overload slightly */
648 case O(ASSEMBLE
,'c'): /* config file */
649 case O(INCREMENTAL
, 'c'):
653 fprintf(stderr
, Name
": configfile cannot be set twice. "
654 "Second value is %s.\n", optarg
);
658 set_conffile(configfile
);
659 /* FIXME possibly check that config file exists. Even parse it */
661 case O(ASSEMBLE
,'s'): /* scan */
664 case O(INCREMENTAL
,'s'):
668 case O(MONITOR
,'m'): /* mail address */
670 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
676 case O(MONITOR
,'p'): /* alert program */
678 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
684 case O(MONITOR
,'d'): /* delay in seconds */
686 case O(BUILD
,'d'): /* delay for bitmap updates */
689 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
692 delay
= strtol(optarg
, &c
, 10);
693 if (!optarg
[0] || *c
|| delay
<1) {
694 fprintf(stderr
, Name
": invalid delay: %s\n",
700 case O(MONITOR
,'f'): /* daemonise */
703 case O(MONITOR
,'i'): /* pid */
705 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
710 case O(MONITOR
,'1'): /* oneshot */
713 case O(MONITOR
,'t'): /* test */
716 case O(MONITOR
,'y'): /* log messages to syslog */
717 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
721 /* now the general management options. Some are applicable
722 * to other modes. None have arguments.
725 case O(MANAGE
,'a'): /* add a drive */
729 case O(MANAGE
,ReAdd
):
733 case O(MANAGE
,'r'): /* remove a drive */
736 case O(MANAGE
,'f'): /* set faulty */
739 case O(INCREMENTAL
,'R'):
741 case O(ASSEMBLE
,'R'):
743 case O(CREATE
,'R'): /* Run the array */
745 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
752 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
768 case O(MISC
, Waitclean
):
769 if (devmode
&& devmode
!= opt
&&
770 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
771 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
772 devmode
=='E'?opt
:devmode
);
781 case O(MISC
, Sparc22
):
782 if (devmode
!= 'E') {
783 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
789 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
791 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
794 if (strcmp(optarg
, "internal")==0) {
795 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
798 bitmap_fd
= open(optarg
, O_RDWR
);
799 if (!*optarg
|| bitmap_fd
< 0) {
800 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
803 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
806 case O(ASSEMBLE
, BackupFile
):
807 case O(GROW
, BackupFile
):
808 /* Specify a file into which grow might place a backup,
809 * or from which assemble might recover a backup
812 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
815 backup_file
= optarg
;
819 case O(CREATE
,'b'): /* here we create the bitmap */
820 if (strcmp(optarg
, "none") == 0) {
821 fprintf(stderr
, Name
": '--bitmap none' only"
822 " support for --grow\n");
827 if (strcmp(optarg
, "internal")== 0 ||
828 strcmp(optarg
, "none")== 0 ||
829 strchr(optarg
, '/') != NULL
) {
830 bitmap_file
= optarg
;
834 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
837 case O(GROW
,BitmapChunk
):
838 case O(BUILD
,BitmapChunk
):
839 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
840 bitmap_chunk
= strtol(optarg
, &c
, 10);
841 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
842 bitmap_chunk
& (bitmap_chunk
- 1)) {
843 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
847 /* convert K to B, chunk of 0K means 512B */
848 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
851 case O(GROW
, WriteBehind
):
852 case O(BUILD
, WriteBehind
):
853 case O(CREATE
, WriteBehind
): /* write-behind mode */
854 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
856 write_behind
= strtol(optarg
, &c
, 10);
857 if (write_behind
< 0 || *c
||
858 write_behind
> 16383) {
859 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
865 case O(INCREMENTAL
, 'r'):
869 /* We have now processed all the valid options. Anything else is
872 if (option_index
> 0)
873 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
874 long_options
[option_index
].name
,
875 map_num(modes
, mode
));
877 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
878 opt
, map_num(modes
, mode
));
884 char *help_text
= Help
;
886 help_text
= OptionHelp
;
889 case ASSEMBLE
: help_text
= Help_assemble
; break;
890 case BUILD
: help_text
= Help_build
; break;
891 case CREATE
: help_text
= Help_create
; break;
892 case MANAGE
: help_text
= Help_manage
; break;
893 case MISC
: help_text
= Help_misc
; break;
894 case MONITOR
: help_text
= Help_monitor
; break;
895 case GROW
: help_text
= Help_grow
; break;
896 case INCREMENTAL
:help_text
= Help_incr
; break;
898 fputs(help_text
,stdout
);
902 if (!mode
&& devs_found
) {
905 if (devlist
->disposition
== 0)
906 devlist
->disposition
= devmode
;
909 fputs(Usage
, stderr
);
914 struct createinfo
*ci
= conf_get_create_info();
916 if (strcasecmp(symlinks
, "yes") == 0)
918 else if (strcasecmp(symlinks
, "no") == 0)
921 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
925 /* Ok, got the option parsing out of the way
926 * hopefully it's mostly right but there might be some stuff
929 * That is mosty checked in the per-mode stuff but...
931 * For @,B,C and A without -s, the first device listed must be an md device
932 * we check that here and open it.
935 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
936 (mode
== ASSEMBLE
&& ! scan
)) {
937 if (devs_found
< 1) {
938 fprintf(stderr
, Name
": an md device must be given in this mode\n");
941 if ((int)ident
.super_minor
== -2 && autof
) {
942 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
945 if (mode
== MANAGE
|| mode
== GROW
)
946 mdfd
= open_mddev(devlist
->devname
, 1);
948 mdfd
= create_mddev(devlist
->devname
, autof
);
951 if ((int)ident
.super_minor
== -2) {
954 ident
.super_minor
= minor(stb
.st_rdev
);
959 if (raiddisks
> max_disks
) {
960 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
964 if (raiddisks
== 1 && !force
&& level
!= -5) {
965 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
966 " a mistake. If you really mean it you will need to specify --force before\n"
967 " setting the number of drives.\n");
972 if ( sparedisks
> max_disks
- raiddisks
) {
973 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
979 if (homehost
== NULL
)
980 homehost
= conf_get_homehost();
981 if (homehost
&& strcmp(homehost
, "<system>")==0) {
982 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
983 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
984 homehost
= sys_hostname
;
991 /* readonly, add/remove, readwrite, runstop */
993 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
994 if (!rv
&& devs_found
>1)
995 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
996 devlist
->next
, verbose
-quiet
);
997 if (!rv
&& readonly
< 0)
998 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1000 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1003 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1004 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1005 /* Only a device has been given, so get details from config file */
1006 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1007 if (array_ident
== NULL
) {
1008 fprintf(stderr
, Name
": %s not identified in config file.\n",
1013 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
,
1015 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1019 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
,
1020 devlist
->next
, backup_file
,
1021 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1022 else if (devs_found
>0) {
1023 if (update
&& devs_found
> 1) {
1024 fprintf(stderr
, Name
": can only update a single array at a time\n");
1027 if (backup_file
&& devs_found
> 1) {
1028 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1031 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1032 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1033 if (array_ident
== NULL
) {
1034 fprintf(stderr
, Name
": %s not identified in config file.\n",
1039 mdfd
= create_mddev(dv
->devname
,
1040 array_ident
->autof
?array_ident
->autof
: autof
);
1045 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
,
1047 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1051 mddev_ident_t array_list
= conf_get_ident(NULL
);
1052 mddev_dev_t devlist
= conf_get_devs();
1054 if (devlist
== NULL
) {
1055 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1059 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1063 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1066 for (; array_list
; array_list
= array_list
->next
) {
1067 mdu_array_info_t array
;
1068 mdfd
= create_mddev(array_list
->devname
,
1069 array_list
->autof
? array_list
->autof
: autof
);
1074 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
1075 /* already assembled, skip */
1078 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
1081 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1087 /* Maybe we can auto-assemble something.
1088 * Repeatedly call Assemble in auto-assmble mode
1093 ident
.autof
= autof
;
1095 mddev_dev_t devlist
= conf_get_devs();
1098 rv2
= Assemble(ss
, NULL
, -1,
1101 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1107 /* found something so even though assembly failed we
1108 * want to avoid auto-updates
1110 auto_update_home
= 0;
1112 /* Incase there are stacked devices, we need to go around again */
1114 if (cnt
== 0 && auto_update_home
&& homehost
) {
1115 /* Nothing found, maybe we need to bootstrap homehost info */
1119 rv2
= Assemble(ss
, NULL
, -1,
1122 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1128 /* Incase there are stacked devices, we need to go around again */
1131 if (cnt
== 0 && rv
== 0) {
1132 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1136 } else if (cnt
== 0 && rv
== 0) {
1137 fprintf(stderr
, Name
": No arrays found in config file\n");
1143 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1144 if (write_behind
&& !bitmap_file
) {
1145 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1149 if (raiddisks
== 0) {
1150 fprintf(stderr
, Name
": no raid-devices specified.\n");
1156 if (strcmp(bitmap_file
, "internal")==0) {
1157 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1162 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
,
1163 raiddisks
, devlist
->next
, assume_clean
,
1164 bitmap_file
, bitmap_chunk
, write_behind
, delay
, verbose
-quiet
);
1167 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1168 if (write_behind
&& !bitmap_file
) {
1169 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1173 if (raiddisks
== 0) {
1174 fprintf(stderr
, Name
": no raid-devices specified.\n");
1179 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1180 raiddisks
, sparedisks
, ident
.name
, homehost
,
1181 ident
.uuid_set
? ident
.uuid
: NULL
,
1182 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1183 bitmap_file
, bitmap_chunk
, write_behind
, delay
);
1186 if (devmode
== 'E') {
1187 if (devlist
== NULL
&& !scan
) {
1188 fprintf(stderr
, Name
": No devices to examine\n");
1191 if (devlist
== NULL
)
1192 devlist
= conf_get_devs();
1193 if (devlist
== NULL
) {
1194 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1197 if (brief
&& verbose
)
1199 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1201 SparcAdjust
, ss
, homehost
);
1203 if (devlist
== NULL
) {
1204 if (devmode
=='D' && scan
) {
1205 /* apply --detail to all devices in /proc/mdstat */
1206 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1207 struct mdstat_ent
*e
;
1208 for (e
=ms
; e
; e
=e
->next
) {
1209 char *name
= get_md_name(e
->devnum
);
1212 fprintf(stderr
, Name
": cannot find device file for %s\n",
1216 rv
|= Detail(name
, verbose
>1?0:verbose
+1,
1217 export
, test
, homehost
);
1220 } else if (devmode
== 'S' && scan
) {
1221 /* apply --stop to all devices in /proc/mdstat */
1222 /* Due to possible stacking of devices, repeat until
1223 * nothing more can be stopped
1225 int progress
=1, err
;
1228 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1229 struct mdstat_ent
*e
;
1231 if (!progress
) last
= 1;
1232 progress
= 0; err
= 0;
1233 for (e
=ms
; e
; e
=e
->next
) {
1234 char *name
= get_md_name(e
->devnum
);
1237 fprintf(stderr
, Name
": cannot find device file for %s\n",
1241 mdfd
= open_mddev(name
, 1);
1243 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1252 } while (!last
&& err
);
1255 fprintf(stderr
, Name
": No devices given.\n");
1259 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1260 switch(dv
->disposition
) {
1262 rv
|= Detail(dv
->devname
,
1264 export
, test
, homehost
);
1266 case 'K': /* Zero superblock */
1267 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1270 rv
|= Query(dv
->devname
); continue;
1272 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1274 rv
|= Wait(dv
->devname
); continue;
1276 rv
|= WaitClean(dv
->devname
, verbose
-quiet
); continue;
1278 mdfd
= open_mddev(dv
->devname
, 1);
1280 switch(dv
->disposition
) {
1282 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1284 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1286 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1288 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1297 if (!devlist
&& !scan
) {
1298 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1302 if (pidfile
&& !daemonise
) {
1303 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1307 rv
= Monitor(devlist
, mailaddr
, program
,
1308 delay
?delay
:60, daemonise
, scan
, oneshot
,
1309 dosyslog
, test
, pidfile
);
1313 if (devs_found
> 1) {
1316 if (size
>= 0 || raiddisks
) {
1317 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1321 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1322 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1326 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1327 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1330 } else if (layout
!= UnSet
)
1331 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1332 else if (size
>= 0 || raiddisks
)
1333 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1334 size
, level
, layout
, chunk
, raiddisks
);
1335 else if (bitmap_file
) {
1336 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1337 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1338 bitmap_chunk
, delay
, write_behind
, force
);
1340 fprintf(stderr
, Name
": no changes to --grow\n");
1348 fprintf(stderr
, Name
1349 ": --incremental --scan meaningless without --run.\n");
1352 rv
= IncrementalScan(verbose
);
1355 if (!rebuild_map
&& !scan
) {
1356 fprintf(stderr
, Name
1357 ": --incremental requires a device.\n");
1362 if (devlist
->next
) {
1363 fprintf(stderr
, Name
1364 ": --incremental can only handle one device.\n");
1368 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1369 ss
, homehost
, autof
);