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
;
97 struct supertype
*ss
= NULL
;
100 char *shortopt
= short_options
;
103 int auto_update_home
= 0;
111 srandom(time(0) ^ getpid());
115 ident
.raid_disks
= UnSet
;
116 ident
.super_minor
= UnSet
;
118 ident
.spare_group
= NULL
;
121 ident
.bitmap_fd
= -1;
122 ident
.bitmap_file
= NULL
;
124 ident
.container
= NULL
;
127 while ((option_index
= -1) ,
128 (opt
=getopt_long(argc
, argv
,
129 shortopt
, long_options
,
130 &option_index
)) != -1) {
132 /* firstly, some mode-independent options */
135 if (option_index
> 0 &&
136 strcmp(long_options
[option_index
].name
, "help-options")==0)
143 fputs(Version
, stderr
);
153 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
154 break; /* b means bitmap */
157 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
166 if (strcasecmp(optarg
, "<ignore>") == 0)
167 require_homehost
= 0;
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",
343 case O(ASSEMBLE
,AutoHomeHost
):
344 auto_update_home
= 1;
347 case O(INCREMENTAL
, 'e'):
349 case O(ASSEMBLE
,'e'):
350 case O(MISC
,'e'): /* set metadata (superblock) information */
352 fprintf(stderr
, Name
": metadata information already given\n");
355 for(i
=0; !ss
&& superlist
[i
]; i
++)
356 ss
= superlist
[i
]->match_metadata_desc(optarg
);
359 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
362 max_disks
= ss
->max_devs
;
368 /* set write-mostly for following devices */
373 /* clear write-mostly for following devices */
380 case O(BUILD
,'z'): /* size */
382 fprintf(stderr
, Name
": size may only be specified once. "
383 "Second value is %s.\n", optarg
);
386 if (strcmp(optarg
, "max")==0)
389 size
= strtoll(optarg
, &c
, 10);
390 if (!optarg
[0] || *c
|| size
< 4) {
391 fprintf(stderr
, Name
": invalid size: %s\n",
398 case O(GROW
,'l'): /* hack - needed to understand layout */
400 case O(BUILD
,'l'): /* set raid level*/
401 if (level
!= UnSet
) {
402 fprintf(stderr
, Name
": raid level may only be set once. "
403 "Second value is %s.\n", optarg
);
406 level
= map_name(pers
, optarg
);
407 if (level
== UnSet
) {
408 fprintf(stderr
, Name
": invalid raid level: %s\n",
412 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
413 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
416 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
420 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
421 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
428 case O(CREATE
,'p'): /* raid5 layout */
429 case O(BUILD
,'p'): /* faulty layout */
430 case O(GROW
, 'p'): /* faulty reconfig */
431 if (layout
!= UnSet
) {
432 fprintf(stderr
,Name
": layout may only be sent once. "
433 "Second value was %s\n", optarg
);
438 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
439 map_num(pers
, level
));
442 fprintf(stderr
, Name
": raid level must be given before layout.\n");
446 layout
= map_name(r5layout
, optarg
);
448 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
454 layout
= map_name(r6layout
, optarg
);
456 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
463 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
464 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
465 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
468 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
471 if (optarg
[0] == 'n')
472 layout
= 256 + copies
;
473 else if (optarg
[0] == 'o')
474 layout
= 0x10000 + (copies
<<8) + 1;
476 layout
= 1 + (copies
<<8);
483 int ln
= strcspn(optarg
, "0123456789");
484 char *m
= strdup(optarg
);
487 mode
= map_name(faultylayout
, m
);
489 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
493 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
498 case O(CREATE
,AssumeClean
):
499 case O(BUILD
,AssumeClean
): /* assume clean */
505 case O(BUILD
,'n'): /* number of raid disks */
507 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
511 raiddisks
= strtol(optarg
, &c
, 10);
512 if (!optarg
[0] || *c
|| raiddisks
<=0) {
513 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
517 ident
.raid_disks
= raiddisks
;
520 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
522 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
526 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
527 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
531 sparedisks
= strtol(optarg
, &c
, 10);
532 if (!optarg
[0] || *c
|| sparedisks
< 0) {
533 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
541 case O(INCREMENTAL
,'a'):
542 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
543 autof
= parse_auto(optarg
, "--auto flag", 0);
546 case O(CREATE
,Symlinks
):
547 case O(BUILD
,Symlinks
):
548 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
552 case O(BUILD
,'f'): /* force honouring '-n 1' */
553 case O(GROW
,'f'): /* ditto */
554 case O(CREATE
,'f'): /* force honouring of device list */
555 case O(ASSEMBLE
,'f'): /* force assembly */
556 case O(MISC
,'f'): /* force zero */
560 /* now for the Assemble options */
561 case O(CREATE
,'u'): /* uuid of array */
562 case O(ASSEMBLE
,'u'): /* uuid of array */
563 if (ident
.uuid_set
) {
564 fprintf(stderr
, Name
": uuid cannot be set twice. "
565 "Second value %s.\n", optarg
);
568 if (parse_uuid(optarg
, ident
.uuid
))
571 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
577 case O(ASSEMBLE
,'N'):
579 fprintf(stderr
, Name
": name cannot be set twice. "
580 "Second value %s.\n", optarg
);
583 if (strlen(optarg
) > 32) {
584 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
588 strcpy(ident
.name
, optarg
);
591 case O(ASSEMBLE
,'m'): /* super-minor for array */
592 if (ident
.super_minor
!= UnSet
) {
593 fprintf(stderr
, Name
": super-minor cannot be set twice. "
594 "Second value: %s.\n", optarg
);
597 if (strcmp(optarg
, "dev")==0)
598 ident
.super_minor
= -2;
600 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
601 if (!optarg
[0] || *cp
) {
602 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
608 case O(ASSEMBLE
,'U'): /* update the superblock */
610 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
615 if (strcmp(update
, "sparc2.2")==0)
617 if (strcmp(update
, "super-minor") == 0)
619 if (strcmp(update
, "summaries")==0)
621 if (strcmp(update
, "resync")==0)
623 if (strcmp(update
, "uuid")==0)
625 if (strcmp(update
, "name")==0)
627 if (strcmp(update
, "homehost")==0)
629 if (strcmp(update
, "devicesize")==0)
631 if (strcmp(update
, "byteorder")==0) {
633 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
636 for(i
=0; !ss
&& superlist
[i
]; i
++)
637 ss
= superlist
[i
]->match_metadata_desc("0.swap");
639 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
645 if (strcmp(update
,"?") == 0 ||
646 strcmp(update
, "help") == 0) {
648 fprintf(outf
, Name
": ");
652 Name
": '--update=%s' is invalid. ",
655 fprintf(outf
, "Valid --update options are:\n"
656 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
657 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
658 exit(outf
== stdout
? 0 : 2);
660 case O(INCREMENTAL
,NoDegraded
):
661 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
662 runstop
= -1; /* --stop isn't allowed for --assemble,
663 * so we overload slightly */
666 case O(ASSEMBLE
,'c'): /* config file */
667 case O(INCREMENTAL
, 'c'):
671 fprintf(stderr
, Name
": configfile cannot be set twice. "
672 "Second value is %s.\n", optarg
);
676 set_conffile(configfile
);
677 /* FIXME possibly check that config file exists. Even parse it */
679 case O(ASSEMBLE
,'s'): /* scan */
682 case O(INCREMENTAL
,'s'):
686 case O(MONITOR
,'m'): /* mail address */
688 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
694 case O(MONITOR
,'p'): /* alert program */
696 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
702 case O(MONITOR
,'r'): /* rebuild increments */
703 increments
= atoi(optarg
);
704 if (increments
>99 || increments
<1) {
705 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
710 case O(MONITOR
,'d'): /* delay in seconds */
712 case O(BUILD
,'d'): /* delay for bitmap updates */
715 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
718 delay
= strtol(optarg
, &c
, 10);
719 if (!optarg
[0] || *c
|| delay
<1) {
720 fprintf(stderr
, Name
": invalid delay: %s\n",
726 case O(MONITOR
,'f'): /* daemonise */
729 case O(MONITOR
,'i'): /* pid */
731 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
736 case O(MONITOR
,'1'): /* oneshot */
739 case O(MONITOR
,'t'): /* test */
742 case O(MONITOR
,'y'): /* log messages to syslog */
743 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
747 /* now the general management options. Some are applicable
748 * to other modes. None have arguments.
751 case O(MANAGE
,'a'): /* add a drive */
755 case O(MANAGE
,ReAdd
):
759 case O(MANAGE
,'r'): /* remove a drive */
762 case O(MANAGE
,'f'): /* set faulty */
765 case O(INCREMENTAL
,'R'):
767 case O(ASSEMBLE
,'R'):
769 case O(CREATE
,'R'): /* Run the array */
771 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
778 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
794 case O(MISC
, Waitclean
):
795 case O(MISC
, DetailPlatform
):
796 if (devmode
&& devmode
!= opt
&&
797 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
798 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
799 devmode
=='E'?opt
:devmode
);
808 case O(MISC
, Sparc22
):
809 if (devmode
!= 'E') {
810 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
816 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
818 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
821 if (strcmp(optarg
, "internal")==0) {
822 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
825 bitmap_fd
= open(optarg
, O_RDWR
);
826 if (!*optarg
|| bitmap_fd
< 0) {
827 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
830 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
833 case O(ASSEMBLE
, BackupFile
):
834 case O(GROW
, BackupFile
):
835 /* Specify a file into which grow might place a backup,
836 * or from which assemble might recover a backup
839 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
842 backup_file
= optarg
;
846 case O(CREATE
,'b'): /* here we create the bitmap */
847 if (strcmp(optarg
, "none") == 0) {
848 fprintf(stderr
, Name
": '--bitmap none' only"
849 " support for --grow\n");
854 if (strcmp(optarg
, "internal")== 0 ||
855 strcmp(optarg
, "none")== 0 ||
856 strchr(optarg
, '/') != NULL
) {
857 bitmap_file
= optarg
;
861 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
864 case O(GROW
,BitmapChunk
):
865 case O(BUILD
,BitmapChunk
):
866 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
867 bitmap_chunk
= strtol(optarg
, &c
, 10);
868 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
869 bitmap_chunk
& (bitmap_chunk
- 1)) {
870 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
874 /* convert K to B, chunk of 0K means 512B */
875 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
878 case O(GROW
, WriteBehind
):
879 case O(BUILD
, WriteBehind
):
880 case O(CREATE
, WriteBehind
): /* write-behind mode */
881 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
883 write_behind
= strtol(optarg
, &c
, 10);
884 if (write_behind
< 0 || *c
||
885 write_behind
> 16383) {
886 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
892 case O(INCREMENTAL
, 'r'):
896 /* We have now processed all the valid options. Anything else is
899 if (option_index
> 0)
900 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
901 long_options
[option_index
].name
,
902 map_num(modes
, mode
));
904 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
905 opt
, map_num(modes
, mode
));
911 char *help_text
= Help
;
913 help_text
= OptionHelp
;
916 case ASSEMBLE
: help_text
= Help_assemble
; break;
917 case BUILD
: help_text
= Help_build
; break;
918 case CREATE
: help_text
= Help_create
; break;
919 case MANAGE
: help_text
= Help_manage
; break;
920 case MISC
: help_text
= Help_misc
; break;
921 case MONITOR
: help_text
= Help_monitor
; break;
922 case GROW
: help_text
= Help_grow
; break;
923 case INCREMENTAL
:help_text
= Help_incr
; break;
925 fputs(help_text
,stdout
);
929 if (!mode
&& devs_found
) {
932 if (devlist
->disposition
== 0)
933 devlist
->disposition
= devmode
;
936 fputs(Usage
, stderr
);
941 struct createinfo
*ci
= conf_get_create_info();
943 if (strcasecmp(symlinks
, "yes") == 0)
945 else if (strcasecmp(symlinks
, "no") == 0)
948 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
952 /* Ok, got the option parsing out of the way
953 * hopefully it's mostly right but there might be some stuff
956 * That is mosty checked in the per-mode stuff but...
958 * For @,B,C and A without -s, the first device listed must be an md device
959 * we check that here and open it.
962 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
963 (mode
== ASSEMBLE
&& ! scan
)) {
964 if (devs_found
< 1) {
965 fprintf(stderr
, Name
": an md device must be given in this mode\n");
968 if ((int)ident
.super_minor
== -2 && autof
) {
969 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
972 if (mode
== MANAGE
|| mode
== GROW
) {
973 mdfd
= open_mddev(devlist
->devname
, 1);
977 /* non-existent device is OK */
978 mdfd
= open_mddev(devlist
->devname
, 0);
980 fprintf(stderr
, Name
": device %s exists but is not an "
981 "md array.\n", devlist
->devname
);
984 if ((int)ident
.super_minor
== -2) {
987 fprintf(stderr
, Name
": --super-minor=dev given, and "
988 "listed device %s doesn't exist.\n",
993 ident
.super_minor
= minor(stb
.st_rdev
);
995 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
996 /* We don't really want this open yet, we just might
997 * have wanted to check some things
1005 if (raiddisks
> max_disks
) {
1006 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1010 if (raiddisks
== 1 && !force
&& level
!= -5) {
1011 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1012 " a mistake. If you really mean it you will need to specify --force before\n"
1013 " setting the number of drives.\n");
1018 if ( sparedisks
> max_disks
- raiddisks
) {
1019 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1025 if (homehost
== NULL
)
1026 homehost
= conf_get_homehost(&require_homehost
);
1027 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1028 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1029 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1030 homehost
= sys_hostname
;
1034 ident
.autof
= autof
;
1039 /* readonly, add/remove, readwrite, runstop */
1041 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1042 if (!rv
&& devs_found
>1)
1043 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1044 devlist
->next
, verbose
-quiet
);
1045 if (!rv
&& readonly
< 0)
1046 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1048 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1051 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1052 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1053 /* Only a device has been given, so get details from config file */
1054 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1055 if (array_ident
== NULL
) {
1056 fprintf(stderr
, Name
": %s not identified in config file.\n",
1062 if (array_ident
->autof
== 0)
1063 array_ident
->autof
= autof
;
1064 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1066 readonly
, runstop
, update
,
1067 homehost
, require_homehost
,
1068 verbose
-quiet
, force
);
1071 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1072 devlist
->next
, backup_file
,
1073 readonly
, runstop
, update
,
1074 homehost
, require_homehost
,
1075 verbose
-quiet
, force
);
1076 else if (devs_found
>0) {
1077 if (update
&& devs_found
> 1) {
1078 fprintf(stderr
, Name
": can only update a single array at a time\n");
1081 if (backup_file
&& devs_found
> 1) {
1082 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1085 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1086 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1087 if (array_ident
== NULL
) {
1088 fprintf(stderr
, Name
": %s not identified in config file.\n",
1093 if (array_ident
->autof
== 0)
1094 array_ident
->autof
= autof
;
1095 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1097 readonly
, runstop
, update
,
1098 homehost
, require_homehost
,
1099 verbose
-quiet
, force
);
1102 mddev_ident_t array_list
= conf_get_ident(NULL
);
1103 mddev_dev_t devlist
= conf_get_devs();
1105 if (devlist
== NULL
) {
1106 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1110 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1114 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1117 for (; array_list
; array_list
= array_list
->next
) {
1118 if (array_list
->devname
&&
1119 strcasecmp(array_list
->devname
, "<ignore>") == 0)
1121 if (array_list
->autof
== 0)
1122 array_list
->autof
= autof
;
1124 rv
|= Assemble(ss
, array_list
->devname
,
1127 readonly
, runstop
, NULL
,
1128 homehost
, require_homehost
,
1129 verbose
-quiet
, force
);
1132 if (homehost
&& cnt
== 0) {
1133 /* Maybe we can auto-assemble something.
1134 * Repeatedly call Assemble in auto-assemble mode
1139 ident
.autof
= autof
;
1141 mddev_dev_t devlist
= conf_get_devs();
1144 rv2
= Assemble(ss
, NULL
,
1147 readonly
, runstop
, NULL
,
1148 homehost
, require_homehost
,
1149 verbose
-quiet
, force
);
1155 /* found something so even though assembly failed we
1156 * want to avoid auto-updates
1158 auto_update_home
= 0;
1160 /* Incase there are stacked devices, we need to go around again */
1163 if (cnt
== 0 && auto_update_home
&& homehost
) {
1164 /* Nothing found, maybe we need to bootstrap homehost info */
1168 rv2
= Assemble(ss
, NULL
,
1171 readonly
, runstop
, "homehost",
1172 homehost
, require_homehost
,
1173 verbose
-quiet
, force
);
1179 /* Incase there are stacked devices, we need to go around again */
1183 if (cnt
== 0 && rv
== 0) {
1184 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1188 } else if (cnt
== 0 && rv
== 0) {
1189 fprintf(stderr
, Name
": No arrays found in config file\n");
1195 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1196 if (write_behind
&& !bitmap_file
) {
1197 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1201 if (raiddisks
== 0) {
1202 fprintf(stderr
, Name
": no raid-devices specified.\n");
1208 if (strcmp(bitmap_file
, "internal")==0) {
1209 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1214 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1215 raiddisks
, devlist
->next
, assume_clean
,
1216 bitmap_file
, bitmap_chunk
, write_behind
,
1217 delay
, verbose
-quiet
, autof
, size
);
1220 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1221 if (write_behind
&& !bitmap_file
) {
1222 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1226 if (raiddisks
== 0) {
1227 fprintf(stderr
, Name
": no raid-devices specified.\n");
1232 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1233 raiddisks
, sparedisks
, ident
.name
, homehost
,
1234 ident
.uuid_set
? ident
.uuid
: NULL
,
1235 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1236 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1239 if (devmode
== 'E') {
1240 if (devlist
== NULL
&& !scan
) {
1241 fprintf(stderr
, Name
": No devices to examine\n");
1244 if (devlist
== NULL
)
1245 devlist
= conf_get_devs();
1246 if (devlist
== NULL
) {
1247 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1250 if (brief
&& verbose
)
1252 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1254 SparcAdjust
, ss
, homehost
);
1255 } else if (devmode
== DetailPlatform
) {
1256 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1258 if (devlist
== NULL
) {
1259 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1260 /* apply --detail or --wait-clean to
1261 * all devices in /proc/mdstat
1263 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1264 struct mdstat_ent
*e
;
1265 struct map_ent
*map
= NULL
;
1266 int v
= verbose
>1?0:verbose
+1;
1268 for (e
=ms
; e
; e
=e
->next
) {
1271 me
= map_by_devnum(&map
, e
->devnum
);
1273 && strcmp(me
->path
, "/unknown") != 0)
1276 name
= get_md_name(e
->devnum
);
1279 fprintf(stderr
, Name
": cannot find device file for %s\n",
1284 rv
|= Detail(name
, v
,
1288 rv
|= WaitClean(name
, -1, v
);
1292 } else if (devmode
== 'S' && scan
) {
1293 /* apply --stop to all devices in /proc/mdstat */
1294 /* Due to possible stacking of devices, repeat until
1295 * nothing more can be stopped
1297 int progress
=1, err
;
1300 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1301 struct mdstat_ent
*e
;
1303 if (!progress
) last
= 1;
1304 progress
= 0; err
= 0;
1305 for (e
=ms
; e
; e
=e
->next
) {
1306 char *name
= get_md_name(e
->devnum
);
1309 fprintf(stderr
, Name
": cannot find device file for %s\n",
1313 mdfd
= open_mddev(name
, 1);
1315 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1325 } while (!last
&& err
);
1328 fprintf(stderr
, Name
": No devices given.\n");
1332 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1333 switch(dv
->disposition
) {
1335 rv
|= Detail(dv
->devname
,
1337 export
, test
, homehost
);
1339 case 'K': /* Zero superblock */
1340 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1343 rv
|= Query(dv
->devname
); continue;
1345 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1347 rv
|= Wait(dv
->devname
); continue;
1349 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1351 mdfd
= open_mddev(dv
->devname
, 1);
1353 switch(dv
->disposition
) {
1355 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1357 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1359 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1361 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1370 if (!devlist
&& !scan
) {
1371 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1375 if (pidfile
&& !daemonise
) {
1376 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1381 if (get_linux_version() > 20616)
1382 /* mdstat responds to poll */
1387 rv
= Monitor(devlist
, mailaddr
, program
,
1388 delay
?delay
:60, daemonise
, scan
, oneshot
,
1389 dosyslog
, test
, pidfile
, increments
);
1393 if (devs_found
> 1) {
1396 if (size
>= 0 || raiddisks
) {
1397 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1401 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1402 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1406 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1407 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1410 } else if (layout
!= UnSet
)
1411 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1412 else if (size
>= 0 || raiddisks
)
1413 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1414 size
, level
, layout
, chunk
, raiddisks
);
1415 else if (bitmap_file
) {
1416 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1417 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1418 bitmap_chunk
, delay
, write_behind
, force
);
1420 fprintf(stderr
, Name
": no changes to --grow\n");
1428 fprintf(stderr
, Name
1429 ": --incremental --scan meaningless without --run.\n");
1432 rv
= IncrementalScan(verbose
);
1435 if (!rebuild_map
&& !scan
) {
1436 fprintf(stderr
, Name
1437 ": --incremental requires a device.\n");
1442 if (devlist
->next
) {
1443 fprintf(stderr
, Name
1444 ": --incremental can only handle one device.\n");
1448 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1449 ss
, homehost
, require_homehost
, autof
);