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
[])
49 long long array_size
= -1;
53 int max_disks
= MD_SB_DISKS
; /* just a default */
55 struct mddev_ident_s ident
;
56 char *configfile
= NULL
;
65 char *bitmap_file
= NULL
;
66 char *backup_file
= NULL
;
67 int bitmap_chunk
= UnSet
;
69 mddev_dev_t devlist
= NULL
;
70 mddev_dev_t
*devlistend
= & devlist
;
80 char *symlinks
= NULL
;
81 /* autof indicates whether and how to create device node.
82 * bottom 3 bits are style. Rest (when shifted) are number of parts
84 * 1 - don't create (no)
85 * 2 - if is_standard, then create (yes)
86 * 3 - create as 'md' - reject is_standard mdp (md)
87 * 4 - create as 'mdp' - reject is_standard md (mdp)
88 * 5 - default to md if not is_standard (md in config file)
89 * 6 - default to mdp if not is_standard (part, or mdp in config file)
93 char *homehost
= NULL
;
94 char sys_hostname
[256];
95 int require_homehost
= 1;
96 char *mailaddr
= NULL
;
100 char *pidfile
= NULL
;
102 struct supertype
*ss
= NULL
;
105 char *shortopt
= short_options
;
108 int auto_update_home
= 0;
115 srandom(time(0) ^ getpid());
119 ident
.raid_disks
= UnSet
;
120 ident
.super_minor
= UnSet
;
122 ident
.spare_group
= NULL
;
125 ident
.bitmap_fd
= -1;
126 ident
.bitmap_file
= NULL
;
128 ident
.container
= NULL
;
131 while ((option_index
= -1) ,
132 (opt
=getopt_long(argc
, argv
,
133 shortopt
, long_options
,
134 &option_index
)) != -1) {
136 /* firstly, some mode-independent options */
139 if (option_index
> 0 &&
140 strcmp(long_options
[option_index
].name
, "help-options")==0)
147 fputs(Version
, stderr
);
157 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
158 break; /* b means bitmap */
161 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
170 if (strcasecmp(optarg
, "<ignore>") == 0)
171 require_homehost
= 0;
178 fputs(Usage
, stderr
);
181 /* second, figure out the mode.
182 * Some options force the mode. Others
183 * set the mode if it isn't already
187 case '@': /* just incase they say --manage */
189 shortopt
= short_bitmap_options
;
194 case ReAdd
: /* re-add */
197 shortopt
= short_bitmap_options
;
201 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
202 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
203 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
204 case 'F': newmode
= MONITOR
;break;
205 case 'G': newmode
= GROW
;
206 shortopt
= short_bitmap_options
;
208 case 'I': newmode
= INCREMENTAL
;
209 shortopt
= short_bitmap_auto_options
; break;
211 newmode
= AUTODETECT
; break;
217 case 'Q': newmode
= MISC
; break;
225 case 'K': if (!mode
) newmode
= MISC
; break;
227 if (mode
&& newmode
== mode
) {
228 /* everybody happy ! */
229 } else if (mode
&& newmode
!= mode
) {
231 fprintf(stderr
, Name
": ");
232 if (option_index
>= 0)
233 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
235 fprintf(stderr
, "-%c", opt
);
236 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
237 map_num(modes
, newmode
),
238 map_num(modes
, mode
));
240 } else if (!mode
&& newmode
) {
243 /* special case of -c --help */
245 ( strncmp(optarg
, "--h", 3)==0 ||
246 strncmp(optarg
, "-h", 2)==0)) {
247 fputs(Help_config
, stdout
);
251 /* If first option is a device, don't force the mode yet */
253 if (devs_found
== 0) {
254 dv
= malloc(sizeof(*dv
));
256 fprintf(stderr
, Name
": malloc failed\n");
259 dv
->devname
= optarg
;
260 dv
->disposition
= devmode
;
261 dv
->writemostly
= writemostly
;
267 devlistend
= &dv
->next
;
272 /* No mode yet, and this is the second device ... */
273 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
276 if (option_index
>= 0)
277 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
279 fprintf(stderr
, Name
": -%c", opt
);
280 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
284 /* if we just set the mode, then done */
298 /* an undecorated option - must be a device name.
300 if (devs_found
> 0 && mode
== '@' && !devmode
) {
301 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
304 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
305 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
308 dv
= malloc(sizeof(*dv
));
310 fprintf(stderr
, Name
": malloc failed\n");
313 dv
->devname
= optarg
;
314 dv
->disposition
= devmode
;
315 dv
->writemostly
= writemostly
;
321 devlistend
= &dv
->next
;
327 /* We've got a mode, and opt is now something else which
328 * could depend on the mode */
329 #define O(a,b) ((a<<8)|b)
330 switch (O(mode
,opt
)) {
332 case O(BUILD
,'c'): /* chunk or rounding */
334 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
335 "Second value is %s.\n", optarg
);
338 chunk
= strtol(optarg
, &c
, 10);
339 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
340 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
346 case O(ASSEMBLE
,AutoHomeHost
):
347 auto_update_home
= 1;
349 case O(INCREMENTAL
, 'e'):
351 case O(ASSEMBLE
,'e'):
352 case O(MISC
,'e'): /* set metadata (superblock) information */
354 fprintf(stderr
, Name
": metadata information already given\n");
357 for(i
=0; !ss
&& superlist
[i
]; i
++)
358 ss
= superlist
[i
]->match_metadata_desc(optarg
);
361 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
364 max_disks
= ss
->max_devs
;
370 /* set write-mostly for following devices */
375 /* clear write-mostly for following devices */
382 case O(BUILD
,'z'): /* size */
384 fprintf(stderr
, Name
": size may only be specified once. "
385 "Second value is %s.\n", optarg
);
388 if (strcmp(optarg
, "max")==0)
391 size
= strtoll(optarg
, &c
, 10);
392 if (!optarg
[0] || *c
|| size
< 4) {
393 fprintf(stderr
, Name
": invalid size: %s\n",
400 case O(GROW
,'Z'): /* array size */
401 if (array_size
>= 0) {
402 fprintf(stderr
, Name
": array-size may only be specified once. "
403 "Second value is %s.\n", optarg
);
406 if (strcmp(optarg
, "max") == 0)
409 array_size
= parse_size(optarg
);
410 if (array_size
<= 0) {
411 fprintf(stderr
, Name
": invalid array size: %s\n",
418 case O(GROW
,'l'): /* hack - needed to understand layout */
420 case O(BUILD
,'l'): /* set raid level*/
421 if (level
!= UnSet
) {
422 fprintf(stderr
, Name
": raid level may only be set once. "
423 "Second value is %s.\n", optarg
);
426 level
= map_name(pers
, optarg
);
427 if (level
== UnSet
) {
428 fprintf(stderr
, Name
": invalid raid level: %s\n",
432 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
433 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
437 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
438 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
445 case O(CREATE
,'p'): /* raid5 layout */
446 case O(BUILD
,'p'): /* faulty layout */
447 case O(GROW
, 'p'): /* faulty reconfig */
448 if (layout
!= UnSet
) {
449 fprintf(stderr
,Name
": layout may only be sent once. "
450 "Second value was %s\n", optarg
);
455 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
456 map_num(pers
, level
));
459 fprintf(stderr
, Name
": raid level must be given before layout.\n");
463 layout
= map_name(r5layout
, optarg
);
465 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
471 layout
= map_name(r6layout
, optarg
);
473 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
480 layout
= parse_layout_10(optarg
);
482 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
489 layout
= parse_layout_faulty(optarg
);
491 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
499 case O(CREATE
,AssumeClean
):
500 case O(BUILD
,AssumeClean
): /* assume clean */
506 case O(BUILD
,'n'): /* number of raid disks */
508 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
512 raiddisks
= strtol(optarg
, &c
, 10);
513 if (!optarg
[0] || *c
|| raiddisks
<=0) {
514 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
518 ident
.raid_disks
= raiddisks
;
521 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
523 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
527 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
528 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
532 sparedisks
= strtol(optarg
, &c
, 10);
533 if (!optarg
[0] || *c
|| sparedisks
< 0) {
534 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
542 case O(INCREMENTAL
,'a'):
543 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
544 autof
= parse_auto(optarg
, "--auto flag", 0);
547 case O(CREATE
,Symlinks
):
548 case O(BUILD
,Symlinks
):
549 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
553 case O(BUILD
,'f'): /* force honouring '-n 1' */
554 case O(GROW
,'f'): /* ditto */
555 case O(CREATE
,'f'): /* force honouring of device list */
556 case O(ASSEMBLE
,'f'): /* force assembly */
557 case O(MISC
,'f'): /* force zero */
561 /* now for the Assemble options */
562 case O(CREATE
,'u'): /* uuid of array */
563 case O(ASSEMBLE
,'u'): /* uuid of array */
564 if (ident
.uuid_set
) {
565 fprintf(stderr
, Name
": uuid cannot be set twice. "
566 "Second value %s.\n", optarg
);
569 if (parse_uuid(optarg
, ident
.uuid
))
572 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
578 case O(ASSEMBLE
,'N'):
580 fprintf(stderr
, Name
": name cannot be set twice. "
581 "Second value %s.\n", optarg
);
584 if (strlen(optarg
) > 32) {
585 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
589 strcpy(ident
.name
, optarg
);
592 case O(ASSEMBLE
,'m'): /* super-minor for array */
593 if (ident
.super_minor
!= UnSet
) {
594 fprintf(stderr
, Name
": super-minor cannot be set twice. "
595 "Second value: %s.\n", optarg
);
598 if (strcmp(optarg
, "dev")==0)
599 ident
.super_minor
= -2;
601 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
602 if (!optarg
[0] || *cp
) {
603 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
609 case O(ASSEMBLE
,'U'): /* update the superblock */
611 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
616 if (strcmp(update
, "sparc2.2")==0)
618 if (strcmp(update
, "super-minor") == 0)
620 if (strcmp(update
, "summaries")==0)
622 if (strcmp(update
, "resync")==0)
624 if (strcmp(update
, "uuid")==0)
626 if (strcmp(update
, "name")==0)
628 if (strcmp(update
, "homehost")==0)
630 if (strcmp(update
, "devicesize")==0)
632 if (strcmp(update
, "byteorder")==0) {
634 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
637 for(i
=0; !ss
&& superlist
[i
]; i
++)
638 ss
= superlist
[i
]->match_metadata_desc("0.swap");
640 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
646 if (strcmp(update
,"?") == 0 ||
647 strcmp(update
, "help") == 0) {
649 fprintf(outf
, Name
": ");
653 Name
": '--update=%s' is invalid. ",
656 fprintf(outf
, "Valid --update options are:\n"
657 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
658 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
659 exit(outf
== stdout
? 0 : 2);
661 case O(INCREMENTAL
,NoDegraded
):
662 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
663 runstop
= -1; /* --stop isn't allowed for --assemble,
664 * so we overload slightly */
667 case O(ASSEMBLE
,'c'): /* config file */
668 case O(INCREMENTAL
, 'c'):
672 fprintf(stderr
, Name
": configfile cannot be set twice. "
673 "Second value is %s.\n", optarg
);
677 set_conffile(configfile
);
678 /* FIXME possibly check that config file exists. Even parse it */
680 case O(ASSEMBLE
,'s'): /* scan */
683 case O(INCREMENTAL
,'s'):
687 case O(MONITOR
,'m'): /* mail address */
689 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
695 case O(MONITOR
,'p'): /* alert program */
697 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
703 case O(MONITOR
,'d'): /* delay in seconds */
705 case O(BUILD
,'d'): /* delay for bitmap updates */
708 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
711 delay
= strtol(optarg
, &c
, 10);
712 if (!optarg
[0] || *c
|| delay
<1) {
713 fprintf(stderr
, Name
": invalid delay: %s\n",
719 case O(MONITOR
,'f'): /* daemonise */
722 case O(MONITOR
,'i'): /* pid */
724 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
729 case O(MONITOR
,'1'): /* oneshot */
732 case O(MONITOR
,'t'): /* test */
735 case O(MONITOR
,'y'): /* log messages to syslog */
736 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
740 /* now the general management options. Some are applicable
741 * to other modes. None have arguments.
744 case O(MANAGE
,'a'): /* add a drive */
748 case O(MANAGE
,ReAdd
):
752 case O(MANAGE
,'r'): /* remove a drive */
755 case O(MANAGE
,'f'): /* set faulty */
758 case O(INCREMENTAL
,'R'):
760 case O(ASSEMBLE
,'R'):
762 case O(CREATE
,'R'): /* Run the array */
764 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
771 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
787 case O(MISC
, Waitclean
):
788 case O(MISC
, DetailPlatform
):
789 if (devmode
&& devmode
!= opt
&&
790 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
791 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
792 devmode
=='E'?opt
:devmode
);
801 case O(MISC
, Sparc22
):
802 if (devmode
!= 'E') {
803 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
809 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
811 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
814 if (strcmp(optarg
, "internal")==0) {
815 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
818 bitmap_fd
= open(optarg
, O_RDWR
);
819 if (!*optarg
|| bitmap_fd
< 0) {
820 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
823 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
826 case O(ASSEMBLE
, BackupFile
):
827 case O(GROW
, BackupFile
):
828 /* Specify a file into which grow might place a backup,
829 * or from which assemble might recover a backup
832 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
835 backup_file
= optarg
;
839 case O(CREATE
,'b'): /* here we create the bitmap */
840 if (strcmp(optarg
, "none") == 0) {
841 fprintf(stderr
, Name
": '--bitmap none' only"
842 " support for --grow\n");
847 if (strcmp(optarg
, "internal")== 0 ||
848 strcmp(optarg
, "none")== 0 ||
849 strchr(optarg
, '/') != NULL
) {
850 bitmap_file
= optarg
;
854 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
857 case O(GROW
,BitmapChunk
):
858 case O(BUILD
,BitmapChunk
):
859 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
860 bitmap_chunk
= strtol(optarg
, &c
, 10);
861 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
862 bitmap_chunk
& (bitmap_chunk
- 1)) {
863 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
867 /* convert K to B, chunk of 0K means 512B */
868 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
871 case O(GROW
, WriteBehind
):
872 case O(BUILD
, WriteBehind
):
873 case O(CREATE
, WriteBehind
): /* write-behind mode */
874 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
876 write_behind
= strtol(optarg
, &c
, 10);
877 if (write_behind
< 0 || *c
||
878 write_behind
> 16383) {
879 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
885 case O(INCREMENTAL
, 'r'):
889 /* We have now processed all the valid options. Anything else is
892 if (option_index
> 0)
893 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
894 long_options
[option_index
].name
,
895 map_num(modes
, mode
));
897 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
898 opt
, map_num(modes
, mode
));
904 char *help_text
= Help
;
906 help_text
= OptionHelp
;
909 case ASSEMBLE
: help_text
= Help_assemble
; break;
910 case BUILD
: help_text
= Help_build
; break;
911 case CREATE
: help_text
= Help_create
; break;
912 case MANAGE
: help_text
= Help_manage
; break;
913 case MISC
: help_text
= Help_misc
; break;
914 case MONITOR
: help_text
= Help_monitor
; break;
915 case GROW
: help_text
= Help_grow
; break;
916 case INCREMENTAL
:help_text
= Help_incr
; break;
918 fputs(help_text
,stdout
);
922 if (!mode
&& devs_found
) {
925 if (devlist
->disposition
== 0)
926 devlist
->disposition
= devmode
;
929 fputs(Usage
, stderr
);
934 struct createinfo
*ci
= conf_get_create_info();
936 if (strcasecmp(symlinks
, "yes") == 0)
938 else if (strcasecmp(symlinks
, "no") == 0)
941 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
945 /* Ok, got the option parsing out of the way
946 * hopefully it's mostly right but there might be some stuff
949 * That is mosty checked in the per-mode stuff but...
951 * For @,B,C and A without -s, the first device listed must be an md device
952 * we check that here and open it.
955 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
956 (mode
== ASSEMBLE
&& ! scan
)) {
957 if (devs_found
< 1) {
958 fprintf(stderr
, Name
": an md device must be given in this mode\n");
961 if ((int)ident
.super_minor
== -2 && autof
) {
962 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
965 if (mode
== MANAGE
|| mode
== GROW
) {
966 mdfd
= open_mddev(devlist
->devname
, 1);
970 /* non-existent device is OK */
971 mdfd
= open_mddev(devlist
->devname
, 0);
973 fprintf(stderr
, Name
": device %s exists but is not an "
974 "md array.\n", devlist
->devname
);
977 if ((int)ident
.super_minor
== -2) {
980 fprintf(stderr
, Name
": --super-minor=dev given, and "
981 "listed device %s doesn't exist.\n",
986 ident
.super_minor
= minor(stb
.st_rdev
);
988 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
989 /* We don't really want this open yet, we just might
990 * have wanted to check some things
998 if (raiddisks
> max_disks
) {
999 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1003 if (raiddisks
== 1 && !force
&& level
!= -5) {
1004 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1005 " a mistake. If you really mean it you will need to specify --force before\n"
1006 " setting the number of drives.\n");
1011 if ( sparedisks
> max_disks
- raiddisks
) {
1012 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1018 if (homehost
== NULL
)
1019 homehost
= conf_get_homehost(&require_homehost
);
1020 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1021 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1022 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1023 homehost
= sys_hostname
;
1027 ident
.autof
= autof
;
1032 /* readonly, add/remove, readwrite, runstop */
1034 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1035 if (!rv
&& devs_found
>1)
1036 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1037 devlist
->next
, verbose
-quiet
);
1038 if (!rv
&& readonly
< 0)
1039 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1041 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1044 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1045 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1046 /* Only a device has been given, so get details from config file */
1047 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1048 if (array_ident
== NULL
) {
1049 fprintf(stderr
, Name
": %s not identified in config file.\n",
1055 if (array_ident
->autof
== 0)
1056 array_ident
->autof
= autof
;
1057 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1059 readonly
, runstop
, update
,
1060 homehost
, require_homehost
,
1061 verbose
-quiet
, force
);
1064 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1065 devlist
->next
, backup_file
,
1066 readonly
, runstop
, update
,
1067 homehost
, require_homehost
,
1068 verbose
-quiet
, force
);
1069 else if (devs_found
>0) {
1070 if (update
&& devs_found
> 1) {
1071 fprintf(stderr
, Name
": can only update a single array at a time\n");
1074 if (backup_file
&& devs_found
> 1) {
1075 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1078 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1079 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1080 if (array_ident
== NULL
) {
1081 fprintf(stderr
, Name
": %s not identified in config file.\n",
1086 if (array_ident
->autof
== 0)
1087 array_ident
->autof
= autof
;
1088 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1090 readonly
, runstop
, update
,
1091 homehost
, require_homehost
,
1092 verbose
-quiet
, force
);
1095 mddev_ident_t array_list
= conf_get_ident(NULL
);
1096 mddev_dev_t devlist
= conf_get_devs();
1098 if (devlist
== NULL
) {
1099 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1103 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1107 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1110 for (; array_list
; array_list
= array_list
->next
) {
1111 if (array_list
->devname
&&
1112 strcasecmp(array_list
->devname
, "<ignore>") == 0)
1114 if (array_list
->autof
== 0)
1115 array_list
->autof
= autof
;
1117 rv
|= Assemble(ss
, array_list
->devname
,
1120 readonly
, runstop
, NULL
,
1121 homehost
, require_homehost
,
1122 verbose
-quiet
, force
);
1125 if (homehost
&& cnt
== 0) {
1126 /* Maybe we can auto-assemble something.
1127 * Repeatedly call Assemble in auto-assemble mode
1132 ident
.autof
= autof
;
1134 mddev_dev_t devlist
= conf_get_devs();
1137 rv2
= Assemble(ss
, NULL
,
1140 readonly
, runstop
, NULL
,
1141 homehost
, require_homehost
,
1142 verbose
-quiet
, force
);
1148 /* found something so even though assembly failed we
1149 * want to avoid auto-updates
1151 auto_update_home
= 0;
1153 /* Incase there are stacked devices, we need to go around again */
1155 if (cnt
== 0 && auto_update_home
&& homehost
) {
1156 /* Nothing found, maybe we need to bootstrap homehost info */
1160 rv2
= Assemble(ss
, NULL
,
1163 readonly
, runstop
, "homehost",
1164 homehost
, require_homehost
,
1165 verbose
-quiet
, force
);
1171 /* Incase there are stacked devices, we need to go around again */
1174 if (cnt
== 0 && rv
== 0) {
1175 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1179 } else if (cnt
== 0 && rv
== 0) {
1180 fprintf(stderr
, Name
": No arrays found in config file\n");
1186 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1187 if (write_behind
&& !bitmap_file
) {
1188 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1192 if (raiddisks
== 0) {
1193 fprintf(stderr
, Name
": no raid-devices specified.\n");
1199 if (strcmp(bitmap_file
, "internal")==0) {
1200 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1205 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1206 raiddisks
, devlist
->next
, assume_clean
,
1207 bitmap_file
, bitmap_chunk
, write_behind
,
1208 delay
, verbose
-quiet
, autof
, size
);
1211 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1212 if (write_behind
&& !bitmap_file
) {
1213 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1217 if (raiddisks
== 0) {
1218 fprintf(stderr
, Name
": no raid-devices specified.\n");
1223 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1224 raiddisks
, sparedisks
, ident
.name
, homehost
,
1225 ident
.uuid_set
? ident
.uuid
: NULL
,
1226 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1227 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1230 if (devmode
== 'E') {
1231 if (devlist
== NULL
&& !scan
) {
1232 fprintf(stderr
, Name
": No devices to examine\n");
1235 if (devlist
== NULL
)
1236 devlist
= conf_get_devs();
1237 if (devlist
== NULL
) {
1238 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1241 if (brief
&& verbose
)
1243 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1245 SparcAdjust
, ss
, homehost
);
1246 } else if (devmode
== DetailPlatform
) {
1247 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1249 if (devlist
== NULL
) {
1250 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1251 /* apply --detail or --wait-clean to
1252 * all devices in /proc/mdstat
1254 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1255 struct mdstat_ent
*e
;
1256 struct map_ent
*map
= NULL
;
1257 int v
= verbose
>1?0:verbose
+1;
1259 for (e
=ms
; e
; e
=e
->next
) {
1262 me
= map_by_devnum(&map
, e
->devnum
);
1264 && strcmp(me
->path
, "/unknown") != 0)
1267 name
= get_md_name(e
->devnum
);
1270 fprintf(stderr
, Name
": cannot find device file for %s\n",
1275 rv
|= Detail(name
, v
,
1279 rv
|= WaitClean(name
, v
);
1283 } else if (devmode
== 'S' && scan
) {
1284 /* apply --stop to all devices in /proc/mdstat */
1285 /* Due to possible stacking of devices, repeat until
1286 * nothing more can be stopped
1288 int progress
=1, err
;
1291 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1292 struct mdstat_ent
*e
;
1294 if (!progress
) last
= 1;
1295 progress
= 0; err
= 0;
1296 for (e
=ms
; e
; e
=e
->next
) {
1297 char *name
= get_md_name(e
->devnum
);
1300 fprintf(stderr
, Name
": cannot find device file for %s\n",
1304 mdfd
= open_mddev(name
, 1);
1306 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1316 } while (!last
&& err
);
1319 fprintf(stderr
, Name
": No devices given.\n");
1323 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1324 switch(dv
->disposition
) {
1326 rv
|= Detail(dv
->devname
,
1328 export
, test
, homehost
);
1330 case 'K': /* Zero superblock */
1331 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1334 rv
|= Query(dv
->devname
); continue;
1336 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1338 rv
|= Wait(dv
->devname
); continue;
1340 rv
|= WaitClean(dv
->devname
, verbose
-quiet
); continue;
1342 mdfd
= open_mddev(dv
->devname
, 1);
1344 switch(dv
->disposition
) {
1346 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1348 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1350 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1352 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1361 if (!devlist
&& !scan
) {
1362 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1366 if (pidfile
&& !daemonise
) {
1367 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1371 rv
= Monitor(devlist
, mailaddr
, program
,
1372 delay
?delay
:60, daemonise
, scan
, oneshot
,
1373 dosyslog
, test
, pidfile
);
1377 if (array_size
>= 0) {
1378 /* alway impose array size first, independent of
1383 sysfs_init(&sra
, mdfd
, 0);
1384 if (array_size
== 0)
1385 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1387 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1390 fprintf(stderr
, Name
": --array-size setting"
1391 " is too large.\n");
1393 fprintf(stderr
, Name
": current kernel does"
1394 " not support setting --array-size\n");
1399 if (devs_found
> 1) {
1402 if (size
>= 0 || raiddisks
) {
1403 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1407 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1408 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1412 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1413 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1416 } else if (layout
!= UnSet
)
1417 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1418 else if (size
>= 0 || raiddisks
)
1419 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1420 size
, level
, layout
, chunk
, raiddisks
);
1421 else if (bitmap_file
) {
1422 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1423 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1424 bitmap_chunk
, delay
, write_behind
, force
);
1425 } else if (array_size
< 0)
1426 fprintf(stderr
, Name
": no changes to --grow\n");
1434 fprintf(stderr
, Name
1435 ": --incremental --scan meaningless without --run.\n");
1438 rv
= IncrementalScan(verbose
);
1441 if (!rebuild_map
&& !scan
) {
1442 fprintf(stderr
, Name
1443 ": --incremental requires a device.\n");
1448 if (devlist
->next
) {
1449 fprintf(stderr
, Name
1450 ": --incremental can only handle one device.\n");
1454 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1455 ss
, homehost
, require_homehost
, autof
);