2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
33 int main(int argc
, char *argv
[])
47 int max_disks
= MD_SB_DISKS
; /* just a default */
49 struct mddev_ident_s ident
;
50 char *configfile
= NULL
;
59 char *bitmap_file
= NULL
;
60 char *backup_file
= NULL
;
61 int bitmap_chunk
= UnSet
;
63 mddev_dev_t devlist
= NULL
;
64 mddev_dev_t
*devlistend
= & devlist
;
74 char *symlinks
= NULL
;
75 /* autof indicates whether and how to create device node.
76 * bottom 3 bits are style. Rest (when shifted) are number of parts
78 * 1 - don't create (no)
79 * 2 - if is_standard, then create (yes)
80 * 3 - create as 'md' - reject is_standard mdp (md)
81 * 4 - create as 'mdp' - reject is_standard md (mdp)
82 * 5 - default to md if not is_standard (md in config file)
83 * 6 - default to mdp if not is_standard (part, or mdp in config file)
87 char *homehost
= NULL
;
88 char sys_hostname
[256];
89 int require_homehost
= 1;
90 char *mailaddr
= NULL
;
96 struct supertype
*ss
= NULL
;
99 char *shortopt
= short_options
;
102 int auto_update_home
= 0;
110 srandom(time(0) ^ getpid());
114 ident
.raid_disks
= UnSet
;
115 ident
.super_minor
= UnSet
;
117 ident
.spare_group
= NULL
;
120 ident
.bitmap_fd
= -1;
121 ident
.bitmap_file
= NULL
;
123 ident
.container
= NULL
;
126 while ((option_index
= -1) ,
127 (opt
=getopt_long(argc
, argv
,
128 shortopt
, long_options
,
129 &option_index
)) != -1) {
131 /* firstly, some mode-independent options */
134 if (option_index
> 0 &&
135 strcmp(long_options
[option_index
].name
, "help-options")==0)
142 fputs(Version
, stderr
);
152 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
153 break; /* b means bitmap */
156 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
165 if (strcasecmp(optarg
, "<ignore>") == 0)
166 require_homehost
= 0;
173 fputs(Usage
, stderr
);
176 /* second, figure out the mode.
177 * Some options force the mode. Others
178 * set the mode if it isn't already
182 case '@': /* just incase they say --manage */
184 shortopt
= short_bitmap_options
;
189 case ReAdd
: /* re-add */
192 shortopt
= short_bitmap_options
;
196 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
197 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
198 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
199 case 'F': newmode
= MONITOR
;break;
200 case 'G': newmode
= GROW
;
201 shortopt
= short_bitmap_options
;
203 case 'I': newmode
= INCREMENTAL
;
204 shortopt
= short_bitmap_auto_options
; break;
206 newmode
= AUTODETECT
; break;
212 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
;
262 devlistend
= &dv
->next
;
267 /* No mode yet, and this is the second device ... */
268 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
271 if (option_index
>= 0)
272 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
274 fprintf(stderr
, Name
": -%c", opt
);
275 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
279 /* if we just set the mode, then done */
293 /* an undecorated option - must be a device name.
295 if (devs_found
> 0 && mode
== '@' && !devmode
) {
296 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
299 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
300 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
303 dv
= malloc(sizeof(*dv
));
305 fprintf(stderr
, Name
": malloc failed\n");
308 dv
->devname
= optarg
;
309 dv
->disposition
= devmode
;
310 dv
->writemostly
= writemostly
;
316 devlistend
= &dv
->next
;
322 /* We've got a mode, and opt is now something else which
323 * could depend on the mode */
324 #define O(a,b) ((a<<8)|b)
325 switch (O(mode
,opt
)) {
327 case O(BUILD
,'c'): /* chunk or rounding */
329 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
330 "Second value is %s.\n", optarg
);
333 chunk
= strtol(optarg
, &c
, 10);
334 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
335 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
342 case O(ASSEMBLE
,AutoHomeHost
):
343 auto_update_home
= 1;
346 case O(INCREMENTAL
, 'e'):
348 case O(ASSEMBLE
,'e'):
349 case O(MISC
,'e'): /* set metadata (superblock) information */
351 fprintf(stderr
, Name
": metadata information already given\n");
354 for(i
=0; !ss
&& superlist
[i
]; i
++)
355 ss
= superlist
[i
]->match_metadata_desc(optarg
);
358 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
361 max_disks
= ss
->max_devs
;
367 /* set write-mostly for following devices */
372 /* clear write-mostly for following devices */
379 case O(BUILD
,'z'): /* size */
381 fprintf(stderr
, Name
": size may only be specified once. "
382 "Second value is %s.\n", optarg
);
385 if (strcmp(optarg
, "max")==0)
388 size
= strtoll(optarg
, &c
, 10);
389 if (!optarg
[0] || *c
|| size
< 4) {
390 fprintf(stderr
, Name
": invalid size: %s\n",
397 case O(GROW
,'l'): /* hack - needed to understand layout */
399 case O(BUILD
,'l'): /* set raid level*/
400 if (level
!= UnSet
) {
401 fprintf(stderr
, Name
": raid level may only be set once. "
402 "Second value is %s.\n", optarg
);
405 level
= map_name(pers
, optarg
);
406 if (level
== UnSet
) {
407 fprintf(stderr
, Name
": invalid raid level: %s\n",
411 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
412 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
415 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
419 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
420 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
427 case O(CREATE
,'p'): /* raid5 layout */
428 case O(BUILD
,'p'): /* faulty layout */
429 case O(GROW
, 'p'): /* faulty reconfig */
430 if (layout
!= UnSet
) {
431 fprintf(stderr
,Name
": layout may only be sent once. "
432 "Second value was %s\n", optarg
);
437 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
438 map_num(pers
, level
));
441 fprintf(stderr
, Name
": raid level must be given before layout.\n");
445 layout
= map_name(r5layout
, optarg
);
447 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
453 layout
= map_name(r6layout
, optarg
);
455 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
462 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
463 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
464 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
467 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
470 if (optarg
[0] == 'n')
471 layout
= 256 + copies
;
472 else if (optarg
[0] == 'o')
473 layout
= 0x10000 + (copies
<<8) + 1;
475 layout
= 1 + (copies
<<8);
482 int ln
= strcspn(optarg
, "0123456789");
483 char *m
= strdup(optarg
);
486 mode
= map_name(faultylayout
, m
);
488 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
492 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
497 case O(CREATE
,AssumeClean
):
498 case O(BUILD
,AssumeClean
): /* assume clean */
504 case O(BUILD
,'n'): /* number of raid disks */
506 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
510 raiddisks
= strtol(optarg
, &c
, 10);
511 if (!optarg
[0] || *c
|| raiddisks
<=0) {
512 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
516 ident
.raid_disks
= raiddisks
;
519 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
521 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
525 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
526 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
530 sparedisks
= strtol(optarg
, &c
, 10);
531 if (!optarg
[0] || *c
|| sparedisks
< 0) {
532 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
540 case O(INCREMENTAL
,'a'):
541 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
542 autof
= parse_auto(optarg
, "--auto flag", 0);
545 case O(CREATE
,Symlinks
):
546 case O(BUILD
,Symlinks
):
547 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
551 case O(BUILD
,'f'): /* force honouring '-n 1' */
552 case O(GROW
,'f'): /* ditto */
553 case O(CREATE
,'f'): /* force honouring of device list */
554 case O(ASSEMBLE
,'f'): /* force assembly */
555 case O(MISC
,'f'): /* force zero */
559 /* now for the Assemble options */
560 case O(CREATE
,'u'): /* uuid of array */
561 case O(ASSEMBLE
,'u'): /* uuid of array */
562 if (ident
.uuid_set
) {
563 fprintf(stderr
, Name
": uuid cannot be set twice. "
564 "Second value %s.\n", optarg
);
567 if (parse_uuid(optarg
, ident
.uuid
))
570 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
576 case O(ASSEMBLE
,'N'):
578 fprintf(stderr
, Name
": name cannot be set twice. "
579 "Second value %s.\n", optarg
);
582 if (strlen(optarg
) > 32) {
583 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
587 strcpy(ident
.name
, optarg
);
590 case O(ASSEMBLE
,'m'): /* super-minor for array */
591 if (ident
.super_minor
!= UnSet
) {
592 fprintf(stderr
, Name
": super-minor cannot be set twice. "
593 "Second value: %s.\n", optarg
);
596 if (strcmp(optarg
, "dev")==0)
597 ident
.super_minor
= -2;
599 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
600 if (!optarg
[0] || *cp
) {
601 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
607 case O(ASSEMBLE
,'U'): /* update the superblock */
609 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
614 if (strcmp(update
, "sparc2.2")==0)
616 if (strcmp(update
, "super-minor") == 0)
618 if (strcmp(update
, "summaries")==0)
620 if (strcmp(update
, "resync")==0)
622 if (strcmp(update
, "uuid")==0)
624 if (strcmp(update
, "name")==0)
626 if (strcmp(update
, "homehost")==0)
628 if (strcmp(update
, "devicesize")==0)
630 if (strcmp(update
, "byteorder")==0) {
632 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
635 for(i
=0; !ss
&& superlist
[i
]; i
++)
636 ss
= superlist
[i
]->match_metadata_desc("0.swap");
638 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
644 if (strcmp(update
,"?") == 0 ||
645 strcmp(update
, "help") == 0) {
647 fprintf(outf
, Name
": ");
651 Name
": '--update=%s' is invalid. ",
654 fprintf(outf
, "Valid --update options are:\n"
655 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
656 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
657 exit(outf
== stdout
? 0 : 2);
659 case O(INCREMENTAL
,NoDegraded
):
660 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
661 runstop
= -1; /* --stop isn't allowed for --assemble,
662 * so we overload slightly */
665 case O(ASSEMBLE
,'c'): /* config file */
666 case O(INCREMENTAL
, 'c'):
670 fprintf(stderr
, Name
": configfile cannot be set twice. "
671 "Second value is %s.\n", optarg
);
675 set_conffile(configfile
);
676 /* FIXME possibly check that config file exists. Even parse it */
678 case O(ASSEMBLE
,'s'): /* scan */
681 case O(INCREMENTAL
,'s'):
685 case O(MONITOR
,'m'): /* mail address */
687 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
693 case O(MONITOR
,'p'): /* alert program */
695 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
701 case O(MONITOR
,'d'): /* delay in seconds */
703 case O(BUILD
,'d'): /* delay for bitmap updates */
706 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
709 delay
= strtol(optarg
, &c
, 10);
710 if (!optarg
[0] || *c
|| delay
<1) {
711 fprintf(stderr
, Name
": invalid delay: %s\n",
717 case O(MONITOR
,'f'): /* daemonise */
720 case O(MONITOR
,'i'): /* pid */
722 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
727 case O(MONITOR
,'1'): /* oneshot */
730 case O(MONITOR
,'t'): /* test */
733 case O(MONITOR
,'y'): /* log messages to syslog */
734 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
738 /* now the general management options. Some are applicable
739 * to other modes. None have arguments.
742 case O(MANAGE
,'a'): /* add a drive */
746 case O(MANAGE
,ReAdd
):
750 case O(MANAGE
,'r'): /* remove a drive */
753 case O(MANAGE
,'f'): /* set faulty */
756 case O(INCREMENTAL
,'R'):
758 case O(ASSEMBLE
,'R'):
760 case O(CREATE
,'R'): /* Run the array */
762 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
769 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
785 case O(MISC
, Waitclean
):
786 case O(MISC
, DetailPlatform
):
787 if (devmode
&& devmode
!= opt
&&
788 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
789 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
790 devmode
=='E'?opt
:devmode
);
799 case O(MISC
, Sparc22
):
800 if (devmode
!= 'E') {
801 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
807 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
809 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
812 if (strcmp(optarg
, "internal")==0) {
813 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
816 bitmap_fd
= open(optarg
, O_RDWR
);
817 if (!*optarg
|| bitmap_fd
< 0) {
818 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
821 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
824 case O(ASSEMBLE
, BackupFile
):
825 case O(GROW
, BackupFile
):
826 /* Specify a file into which grow might place a backup,
827 * or from which assemble might recover a backup
830 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
833 backup_file
= optarg
;
837 case O(CREATE
,'b'): /* here we create the bitmap */
838 if (strcmp(optarg
, "none") == 0) {
839 fprintf(stderr
, Name
": '--bitmap none' only"
840 " support for --grow\n");
845 if (strcmp(optarg
, "internal")== 0 ||
846 strcmp(optarg
, "none")== 0 ||
847 strchr(optarg
, '/') != NULL
) {
848 bitmap_file
= optarg
;
852 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
855 case O(GROW
,BitmapChunk
):
856 case O(BUILD
,BitmapChunk
):
857 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
858 bitmap_chunk
= strtol(optarg
, &c
, 10);
859 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
860 bitmap_chunk
& (bitmap_chunk
- 1)) {
861 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
865 /* convert K to B, chunk of 0K means 512B */
866 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
869 case O(GROW
, WriteBehind
):
870 case O(BUILD
, WriteBehind
):
871 case O(CREATE
, WriteBehind
): /* write-behind mode */
872 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
874 write_behind
= strtol(optarg
, &c
, 10);
875 if (write_behind
< 0 || *c
||
876 write_behind
> 16383) {
877 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
883 case O(INCREMENTAL
, 'r'):
887 /* We have now processed all the valid options. Anything else is
890 if (option_index
> 0)
891 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
892 long_options
[option_index
].name
,
893 map_num(modes
, mode
));
895 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
896 opt
, map_num(modes
, mode
));
902 char *help_text
= Help
;
904 help_text
= OptionHelp
;
907 case ASSEMBLE
: help_text
= Help_assemble
; break;
908 case BUILD
: help_text
= Help_build
; break;
909 case CREATE
: help_text
= Help_create
; break;
910 case MANAGE
: help_text
= Help_manage
; break;
911 case MISC
: help_text
= Help_misc
; break;
912 case MONITOR
: help_text
= Help_monitor
; break;
913 case GROW
: help_text
= Help_grow
; break;
914 case INCREMENTAL
:help_text
= Help_incr
; break;
916 fputs(help_text
,stdout
);
920 if (!mode
&& devs_found
) {
923 if (devlist
->disposition
== 0)
924 devlist
->disposition
= devmode
;
927 fputs(Usage
, stderr
);
932 struct createinfo
*ci
= conf_get_create_info();
934 if (strcasecmp(symlinks
, "yes") == 0)
936 else if (strcasecmp(symlinks
, "no") == 0)
939 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
943 /* Ok, got the option parsing out of the way
944 * hopefully it's mostly right but there might be some stuff
947 * That is mosty checked in the per-mode stuff but...
949 * For @,B,C and A without -s, the first device listed must be an md device
950 * we check that here and open it.
953 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
954 (mode
== ASSEMBLE
&& ! scan
)) {
955 if (devs_found
< 1) {
956 fprintf(stderr
, Name
": an md device must be given in this mode\n");
959 if ((int)ident
.super_minor
== -2 && autof
) {
960 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
963 if (mode
== MANAGE
|| mode
== GROW
) {
964 mdfd
= open_mddev(devlist
->devname
, 1);
968 /* non-existent device is OK */
969 mdfd
= open_mddev(devlist
->devname
, 0);
971 fprintf(stderr
, Name
": device %s exists but is not an "
972 "md array.\n", devlist
->devname
);
975 if ((int)ident
.super_minor
== -2) {
978 fprintf(stderr
, Name
": --super-minor=dev given, and "
979 "listed device %s doesn't exist.\n",
984 ident
.super_minor
= minor(stb
.st_rdev
);
986 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
987 /* We don't really want this open yet, we just might
988 * have wanted to check some things
996 if (raiddisks
> max_disks
) {
997 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1001 if (raiddisks
== 1 && !force
&& level
!= -5) {
1002 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1003 " a mistake. If you really mean it you will need to specify --force before\n"
1004 " setting the number of drives.\n");
1009 if ( sparedisks
> max_disks
- raiddisks
) {
1010 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1016 if (homehost
== NULL
)
1017 homehost
= conf_get_homehost(&require_homehost
);
1018 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1019 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1020 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1021 homehost
= sys_hostname
;
1025 ident
.autof
= autof
;
1030 /* readonly, add/remove, readwrite, runstop */
1032 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1033 if (!rv
&& devs_found
>1)
1034 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1035 devlist
->next
, verbose
-quiet
);
1036 if (!rv
&& readonly
< 0)
1037 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1039 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1042 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1043 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1044 /* Only a device has been given, so get details from config file */
1045 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1046 if (array_ident
== NULL
) {
1047 fprintf(stderr
, Name
": %s not identified in config file.\n",
1053 if (array_ident
->autof
== 0)
1054 array_ident
->autof
= autof
;
1055 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1057 readonly
, runstop
, update
,
1058 homehost
, require_homehost
,
1059 verbose
-quiet
, force
);
1062 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1063 devlist
->next
, backup_file
,
1064 readonly
, runstop
, update
,
1065 homehost
, require_homehost
,
1066 verbose
-quiet
, force
);
1067 else if (devs_found
>0) {
1068 if (update
&& devs_found
> 1) {
1069 fprintf(stderr
, Name
": can only update a single array at a time\n");
1072 if (backup_file
&& devs_found
> 1) {
1073 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1076 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1077 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1078 if (array_ident
== NULL
) {
1079 fprintf(stderr
, Name
": %s not identified in config file.\n",
1084 if (array_ident
->autof
== 0)
1085 array_ident
->autof
= autof
;
1086 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1088 readonly
, runstop
, update
,
1089 homehost
, require_homehost
,
1090 verbose
-quiet
, force
);
1093 mddev_ident_t array_list
= conf_get_ident(NULL
);
1094 mddev_dev_t devlist
= conf_get_devs();
1096 if (devlist
== NULL
) {
1097 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1101 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1105 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1108 for (; array_list
; array_list
= array_list
->next
) {
1109 if (array_list
->devname
&&
1110 strcasecmp(array_list
->devname
, "<ignore>") == 0)
1112 if (array_list
->autof
== 0)
1113 array_list
->autof
= autof
;
1115 rv
|= Assemble(ss
, array_list
->devname
,
1118 readonly
, runstop
, NULL
,
1119 homehost
, require_homehost
,
1120 verbose
-quiet
, force
);
1123 if (homehost
&& cnt
== 0) {
1124 /* Maybe we can auto-assemble something.
1125 * Repeatedly call Assemble in auto-assemble mode
1130 ident
.autof
= autof
;
1132 mddev_dev_t devlist
= conf_get_devs();
1135 rv2
= Assemble(ss
, NULL
,
1138 readonly
, runstop
, NULL
,
1139 homehost
, require_homehost
,
1140 verbose
-quiet
, force
);
1146 /* found something so even though assembly failed we
1147 * want to avoid auto-updates
1149 auto_update_home
= 0;
1151 /* Incase there are stacked devices, we need to go around again */
1154 if (cnt
== 0 && auto_update_home
&& homehost
) {
1155 /* Nothing found, maybe we need to bootstrap homehost info */
1159 rv2
= Assemble(ss
, NULL
,
1162 readonly
, runstop
, "homehost",
1163 homehost
, require_homehost
,
1164 verbose
-quiet
, force
);
1170 /* 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");
1372 if (get_linux_version() > 20616)
1373 /* mdstat responds to poll */
1378 rv
= Monitor(devlist
, mailaddr
, program
,
1379 delay
?delay
:60, daemonise
, scan
, oneshot
,
1380 dosyslog
, test
, pidfile
);
1384 if (devs_found
> 1) {
1387 if (size
>= 0 || raiddisks
) {
1388 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1392 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1393 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1397 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1398 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1401 } else if (layout
!= UnSet
)
1402 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1403 else if (size
>= 0 || raiddisks
)
1404 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1405 size
, level
, layout
, chunk
, raiddisks
);
1406 else if (bitmap_file
) {
1407 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1408 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1409 bitmap_chunk
, delay
, write_behind
, force
);
1411 fprintf(stderr
, Name
": no changes to --grow\n");
1419 fprintf(stderr
, Name
1420 ": --incremental --scan meaningless without --run.\n");
1423 rv
= IncrementalScan(verbose
);
1426 if (!rebuild_map
&& !scan
) {
1427 fprintf(stderr
, Name
1428 ": --incremental requires a device.\n");
1433 if (devlist
->next
) {
1434 fprintf(stderr
, Name
1435 ": --incremental can only handle one device.\n");
1439 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1440 ss
, homehost
, require_homehost
, autof
);