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
[])
44 long long array_size
= -1;
47 char *layout_str
= NULL
;
49 int max_disks
= MD_SB_DISKS
; /* just a default */
51 struct mddev_ident_s ident
;
52 char *configfile
= NULL
;
61 char *bitmap_file
= NULL
;
62 char *backup_file
= NULL
;
63 int bitmap_chunk
= UnSet
;
65 mddev_dev_t devlist
= NULL
;
66 mddev_dev_t
*devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 char *homehost
= NULL
;
90 char sys_hostname
[256];
91 int require_homehost
= 1;
92 char *mailaddr
= NULL
;
99 struct supertype
*ss
= NULL
;
102 char *shortopt
= short_options
;
105 int auto_update_home
= 0;
112 srandom(time(0) ^ getpid());
116 ident
.raid_disks
= UnSet
;
117 ident
.super_minor
= UnSet
;
119 ident
.spare_group
= NULL
;
122 ident
.bitmap_fd
= -1;
123 ident
.bitmap_file
= NULL
;
125 ident
.container
= NULL
;
128 while ((option_index
= -1) ,
129 (opt
=getopt_long(argc
, argv
,
130 shortopt
, long_options
,
131 &option_index
)) != -1) {
133 /* firstly, some mode-independent options */
136 if (option_index
> 0 &&
137 strcmp(long_options
[option_index
].name
, "help-options")==0)
144 fputs(Version
, stderr
);
154 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
155 break; /* b means bitmap */
158 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
167 if (strcasecmp(optarg
, "<ignore>") == 0)
168 require_homehost
= 0;
175 fputs(Usage
, stderr
);
178 /* second, figure out the mode.
179 * Some options force the mode. Others
180 * set the mode if it isn't already
184 case '@': /* just incase they say --manage */
186 shortopt
= short_bitmap_options
;
191 case ReAdd
: /* re-add */
194 shortopt
= short_bitmap_options
;
198 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
199 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
200 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
201 case 'F': newmode
= MONITOR
;break;
202 case 'G': newmode
= GROW
;
203 shortopt
= short_bitmap_options
;
205 case 'I': newmode
= INCREMENTAL
;
206 shortopt
= short_bitmap_auto_options
; break;
208 newmode
= AUTODETECT
; break;
214 case 'Q': newmode
= MISC
; break;
222 case 'K': if (!mode
) newmode
= MISC
; break;
224 if (mode
&& newmode
== mode
) {
225 /* everybody happy ! */
226 } else if (mode
&& newmode
!= mode
) {
228 fprintf(stderr
, Name
": ");
229 if (option_index
>= 0)
230 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
232 fprintf(stderr
, "-%c", opt
);
233 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
234 map_num(modes
, newmode
),
235 map_num(modes
, mode
));
237 } else if (!mode
&& newmode
) {
240 /* special case of -c --help */
242 ( strncmp(optarg
, "--h", 3)==0 ||
243 strncmp(optarg
, "-h", 2)==0)) {
244 fputs(Help_config
, stdout
);
248 /* If first option is a device, don't force the mode yet */
250 if (devs_found
== 0) {
251 dv
= malloc(sizeof(*dv
));
253 fprintf(stderr
, Name
": malloc failed\n");
256 dv
->devname
= optarg
;
257 dv
->disposition
= devmode
;
258 dv
->writemostly
= writemostly
;
264 devlistend
= &dv
->next
;
269 /* No mode yet, and this is the second device ... */
270 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
273 if (option_index
>= 0)
274 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
276 fprintf(stderr
, Name
": -%c", opt
);
277 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
281 /* if we just set the mode, then done */
295 /* an undecorated option - must be a device name.
297 if (devs_found
> 0 && mode
== '@' && !devmode
) {
298 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
301 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
302 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
305 dv
= malloc(sizeof(*dv
));
307 fprintf(stderr
, Name
": malloc failed\n");
310 dv
->devname
= optarg
;
311 dv
->disposition
= devmode
;
312 dv
->writemostly
= writemostly
;
318 devlistend
= &dv
->next
;
324 /* We've got a mode, and opt is now something else which
325 * could depend on the mode */
326 #define O(a,b) ((a<<8)|b)
327 switch (O(mode
,opt
)) {
330 case O(BUILD
,'c'): /* chunk or rounding */
332 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
333 "Second value is %s.\n", optarg
);
336 chunk
= strtol(optarg
, &c
, 10);
337 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
338 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
345 case O(ASSEMBLE
,AutoHomeHost
):
346 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
= parse_size(optarg
);
393 fprintf(stderr
, Name
": invalid size: %s\n",
397 /* convert sectors to K */
402 case O(GROW
,'Z'): /* array size */
403 if (array_size
>= 0) {
404 fprintf(stderr
, Name
": array-size may only be specified once. "
405 "Second value is %s.\n", optarg
);
408 if (strcmp(optarg
, "max") == 0)
411 array_size
= parse_size(optarg
);
412 if (array_size
<= 0) {
413 fprintf(stderr
, Name
": invalid array size: %s\n",
422 case O(BUILD
,'l'): /* set raid level*/
423 if (level
!= UnSet
) {
424 fprintf(stderr
, Name
": raid level may only be set once. "
425 "Second value is %s.\n", optarg
);
428 level
= map_name(pers
, optarg
);
429 if (level
== UnSet
) {
430 fprintf(stderr
, Name
": invalid raid level: %s\n",
434 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
435 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
438 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
442 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
443 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
450 case O(GROW
, 'p'): /* new layout */
452 fprintf(stderr
,Name
": layout may only be sent once. "
453 "Second value was %s\n", optarg
);
457 /* 'Grow' will parse the value */
460 case O(CREATE
,'p'): /* raid5 layout */
461 case O(BUILD
,'p'): /* faulty layout */
462 if (layout
!= UnSet
) {
463 fprintf(stderr
,Name
": layout may only be sent once. "
464 "Second value was %s\n", optarg
);
469 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
470 map_num(pers
, level
));
473 fprintf(stderr
, Name
": raid level must be given before layout.\n");
477 layout
= map_name(r5layout
, optarg
);
479 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
485 layout
= map_name(r6layout
, optarg
);
487 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
494 layout
= parse_layout_10(optarg
);
496 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
504 layout
= parse_layout_faulty(optarg
);
506 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
514 case O(CREATE
,AssumeClean
):
515 case O(BUILD
,AssumeClean
): /* assume clean */
521 case O(BUILD
,'n'): /* number of raid disks */
523 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
527 raiddisks
= strtol(optarg
, &c
, 10);
528 if (!optarg
[0] || *c
|| raiddisks
<=0) {
529 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
533 ident
.raid_disks
= raiddisks
;
536 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
538 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
542 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
543 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
547 sparedisks
= strtol(optarg
, &c
, 10);
548 if (!optarg
[0] || *c
|| sparedisks
< 0) {
549 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
557 case O(INCREMENTAL
,'a'):
558 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
559 autof
= parse_auto(optarg
, "--auto flag", 0);
562 case O(CREATE
,Symlinks
):
563 case O(BUILD
,Symlinks
):
564 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
568 case O(BUILD
,'f'): /* force honouring '-n 1' */
569 case O(GROW
,'f'): /* ditto */
570 case O(CREATE
,'f'): /* force honouring of device list */
571 case O(ASSEMBLE
,'f'): /* force assembly */
572 case O(MISC
,'f'): /* force zero */
576 /* now for the Assemble options */
577 case O(CREATE
,'u'): /* uuid of array */
578 case O(ASSEMBLE
,'u'): /* uuid of array */
579 if (ident
.uuid_set
) {
580 fprintf(stderr
, Name
": uuid cannot be set twice. "
581 "Second value %s.\n", optarg
);
584 if (parse_uuid(optarg
, ident
.uuid
))
587 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
593 case O(ASSEMBLE
,'N'):
595 fprintf(stderr
, Name
": name cannot be set twice. "
596 "Second value %s.\n", optarg
);
599 if (strlen(optarg
) > 32) {
600 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
604 strcpy(ident
.name
, optarg
);
607 case O(ASSEMBLE
,'m'): /* super-minor for array */
608 if (ident
.super_minor
!= UnSet
) {
609 fprintf(stderr
, Name
": super-minor cannot be set twice. "
610 "Second value: %s.\n", optarg
);
613 if (strcmp(optarg
, "dev")==0)
614 ident
.super_minor
= -2;
616 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
617 if (!optarg
[0] || *cp
) {
618 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
624 case O(ASSEMBLE
,'U'): /* update the superblock */
626 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
631 if (strcmp(update
, "sparc2.2")==0)
633 if (strcmp(update
, "super-minor") == 0)
635 if (strcmp(update
, "summaries")==0)
637 if (strcmp(update
, "resync")==0)
639 if (strcmp(update
, "uuid")==0)
641 if (strcmp(update
, "name")==0)
643 if (strcmp(update
, "homehost")==0)
645 if (strcmp(update
, "devicesize")==0)
647 if (strcmp(update
, "byteorder")==0) {
649 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
652 for(i
=0; !ss
&& superlist
[i
]; i
++)
653 ss
= superlist
[i
]->match_metadata_desc("0.swap");
655 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
661 if (strcmp(update
,"?") == 0 ||
662 strcmp(update
, "help") == 0) {
664 fprintf(outf
, Name
": ");
668 Name
": '--update=%s' is invalid. ",
671 fprintf(outf
, "Valid --update options are:\n"
672 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
673 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
674 exit(outf
== stdout
? 0 : 2);
676 case O(INCREMENTAL
,NoDegraded
):
677 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
678 runstop
= -1; /* --stop isn't allowed for --assemble,
679 * so we overload slightly */
682 case O(ASSEMBLE
,'c'): /* config file */
683 case O(INCREMENTAL
, 'c'):
687 fprintf(stderr
, Name
": configfile cannot be set twice. "
688 "Second value is %s.\n", optarg
);
692 set_conffile(configfile
);
693 /* FIXME possibly check that config file exists. Even parse it */
695 case O(ASSEMBLE
,'s'): /* scan */
698 case O(INCREMENTAL
,'s'):
702 case O(MONITOR
,'m'): /* mail address */
704 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
710 case O(MONITOR
,'p'): /* alert program */
712 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
718 case O(MONITOR
,'r'): /* rebuild increments */
719 increments
= atoi(optarg
);
720 if (increments
>99 || increments
<1) {
721 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
726 case O(MONITOR
,'d'): /* delay in seconds */
728 case O(BUILD
,'d'): /* delay for bitmap updates */
731 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
734 delay
= strtol(optarg
, &c
, 10);
735 if (!optarg
[0] || *c
|| delay
<1) {
736 fprintf(stderr
, Name
": invalid delay: %s\n",
742 case O(MONITOR
,'f'): /* daemonise */
745 case O(MONITOR
,'i'): /* pid */
747 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
752 case O(MONITOR
,'1'): /* oneshot */
755 case O(MONITOR
,'t'): /* test */
758 case O(MONITOR
,'y'): /* log messages to syslog */
759 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
763 /* now the general management options. Some are applicable
764 * to other modes. None have arguments.
767 case O(MANAGE
,'a'): /* add a drive */
771 case O(MANAGE
,ReAdd
):
775 case O(MANAGE
,'r'): /* remove a drive */
778 case O(MANAGE
,'f'): /* set faulty */
781 case O(INCREMENTAL
,'R'):
783 case O(ASSEMBLE
,'R'):
785 case O(CREATE
,'R'): /* Run the array */
787 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
794 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
810 case O(MISC
, Waitclean
):
811 case O(MISC
, DetailPlatform
):
812 if (devmode
&& devmode
!= opt
&&
813 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
814 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
815 devmode
=='E'?opt
:devmode
);
824 case O(MISC
, Sparc22
):
825 if (devmode
!= 'E') {
826 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
832 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
834 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
837 if (strcmp(optarg
, "internal")==0) {
838 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
841 bitmap_fd
= open(optarg
, O_RDWR
);
842 if (!*optarg
|| bitmap_fd
< 0) {
843 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
846 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
849 case O(ASSEMBLE
, BackupFile
):
850 case O(GROW
, BackupFile
):
851 /* Specify a file into which grow might place a backup,
852 * or from which assemble might recover a backup
855 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
858 backup_file
= optarg
;
862 case O(CREATE
,'b'): /* here we create the bitmap */
863 if (strcmp(optarg
, "none") == 0) {
864 fprintf(stderr
, Name
": '--bitmap none' only"
865 " support for --grow\n");
870 if (strcmp(optarg
, "internal")== 0 ||
871 strcmp(optarg
, "none")== 0 ||
872 strchr(optarg
, '/') != NULL
) {
873 bitmap_file
= optarg
;
877 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
880 case O(GROW
,BitmapChunk
):
881 case O(BUILD
,BitmapChunk
):
882 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
883 bitmap_chunk
= strtol(optarg
, &c
, 10);
884 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
885 bitmap_chunk
& (bitmap_chunk
- 1)) {
886 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
890 /* convert K to B, chunk of 0K means 512B */
891 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
894 case O(GROW
, WriteBehind
):
895 case O(BUILD
, WriteBehind
):
896 case O(CREATE
, WriteBehind
): /* write-behind mode */
897 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
899 write_behind
= strtol(optarg
, &c
, 10);
900 if (write_behind
< 0 || *c
||
901 write_behind
> 16383) {
902 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
908 case O(INCREMENTAL
, 'r'):
912 /* We have now processed all the valid options. Anything else is
915 if (option_index
> 0)
916 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
917 long_options
[option_index
].name
,
918 map_num(modes
, mode
));
920 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
921 opt
, map_num(modes
, mode
));
927 char *help_text
= Help
;
929 help_text
= OptionHelp
;
932 case ASSEMBLE
: help_text
= Help_assemble
; break;
933 case BUILD
: help_text
= Help_build
; break;
934 case CREATE
: help_text
= Help_create
; break;
935 case MANAGE
: help_text
= Help_manage
; break;
936 case MISC
: help_text
= Help_misc
; break;
937 case MONITOR
: help_text
= Help_monitor
; break;
938 case GROW
: help_text
= Help_grow
; break;
939 case INCREMENTAL
:help_text
= Help_incr
; break;
941 fputs(help_text
,stdout
);
945 if (!mode
&& devs_found
) {
948 if (devlist
->disposition
== 0)
949 devlist
->disposition
= devmode
;
952 fputs(Usage
, stderr
);
957 struct createinfo
*ci
= conf_get_create_info();
959 if (strcasecmp(symlinks
, "yes") == 0)
961 else if (strcasecmp(symlinks
, "no") == 0)
964 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
968 /* Ok, got the option parsing out of the way
969 * hopefully it's mostly right but there might be some stuff
972 * That is mosty checked in the per-mode stuff but...
974 * For @,B,C and A without -s, the first device listed must be an md device
975 * we check that here and open it.
978 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
979 (mode
== ASSEMBLE
&& ! scan
)) {
980 if (devs_found
< 1) {
981 fprintf(stderr
, Name
": an md device must be given in this mode\n");
984 if ((int)ident
.super_minor
== -2 && autof
) {
985 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
988 if (mode
== MANAGE
|| mode
== GROW
) {
989 mdfd
= open_mddev(devlist
->devname
, 1);
993 /* non-existent device is OK */
994 mdfd
= open_mddev(devlist
->devname
, 0);
996 fprintf(stderr
, Name
": device %s exists but is not an "
997 "md array.\n", devlist
->devname
);
1000 if ((int)ident
.super_minor
== -2) {
1003 fprintf(stderr
, Name
": --super-minor=dev given, and "
1004 "listed device %s doesn't exist.\n",
1009 ident
.super_minor
= minor(stb
.st_rdev
);
1011 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1012 /* We don't really want this open yet, we just might
1013 * have wanted to check some things
1021 if (raiddisks
> max_disks
) {
1022 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1026 if (raiddisks
== 1 && !force
&& level
!= -5) {
1027 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1028 " a mistake. If you really mean it you will need to specify --force before\n"
1029 " setting the number of drives.\n");
1034 if ( sparedisks
> max_disks
- raiddisks
) {
1035 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1041 if (homehost
== NULL
)
1042 homehost
= conf_get_homehost(&require_homehost
);
1043 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1044 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1045 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1046 homehost
= sys_hostname
;
1050 ident
.autof
= autof
;
1055 /* readonly, add/remove, readwrite, runstop */
1057 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1058 if (!rv
&& devs_found
>1)
1059 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1060 devlist
->next
, verbose
-quiet
);
1061 if (!rv
&& readonly
< 0)
1062 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1064 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1067 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1068 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1069 /* Only a device has been given, so get details from config file */
1070 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1071 if (array_ident
== NULL
) {
1072 fprintf(stderr
, Name
": %s not identified in config file.\n",
1078 if (array_ident
->autof
== 0)
1079 array_ident
->autof
= autof
;
1080 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1082 readonly
, runstop
, update
,
1083 homehost
, require_homehost
,
1084 verbose
-quiet
, force
);
1087 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1088 devlist
->next
, backup_file
,
1089 readonly
, runstop
, update
,
1090 homehost
, require_homehost
,
1091 verbose
-quiet
, force
);
1092 else if (devs_found
>0) {
1093 if (update
&& devs_found
> 1) {
1094 fprintf(stderr
, Name
": can only update a single array at a time\n");
1097 if (backup_file
&& devs_found
> 1) {
1098 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1101 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1102 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1103 if (array_ident
== NULL
) {
1104 fprintf(stderr
, Name
": %s not identified in config file.\n",
1109 if (array_ident
->autof
== 0)
1110 array_ident
->autof
= autof
;
1111 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1113 readonly
, runstop
, update
,
1114 homehost
, require_homehost
,
1115 verbose
-quiet
, force
);
1118 mddev_ident_t array_list
= conf_get_ident(NULL
);
1119 mddev_dev_t devlist
= conf_get_devs();
1121 if (devlist
== NULL
) {
1122 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1126 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1130 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1133 for (; array_list
; array_list
= array_list
->next
) {
1134 if (array_list
->devname
&&
1135 strcasecmp(array_list
->devname
, "<ignore>") == 0)
1137 if (array_list
->autof
== 0)
1138 array_list
->autof
= autof
;
1140 rv
|= Assemble(ss
, array_list
->devname
,
1143 readonly
, runstop
, NULL
,
1144 homehost
, require_homehost
,
1145 verbose
-quiet
, force
);
1148 if (homehost
&& cnt
== 0) {
1149 /* Maybe we can auto-assemble something.
1150 * Repeatedly call Assemble in auto-assemble mode
1155 ident
.autof
= autof
;
1157 mddev_dev_t devlist
= conf_get_devs();
1160 rv2
= Assemble(ss
, NULL
,
1163 readonly
, runstop
, NULL
,
1164 homehost
, require_homehost
,
1165 verbose
-quiet
, force
);
1171 /* found something so even though assembly failed we
1172 * want to avoid auto-updates
1174 auto_update_home
= 0;
1176 /* Incase there are stacked devices, we need to go around again */
1179 if (cnt
== 0 && auto_update_home
&& homehost
) {
1180 /* Nothing found, maybe we need to bootstrap homehost info */
1184 rv2
= Assemble(ss
, NULL
,
1187 readonly
, runstop
, "homehost",
1188 homehost
, require_homehost
,
1189 verbose
-quiet
, force
);
1195 /* Incase there are stacked devices, we need to go around again */
1199 if (cnt
== 0 && rv
== 0) {
1200 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1204 } else if (cnt
== 0 && rv
== 0) {
1205 fprintf(stderr
, Name
": No arrays found in config file\n");
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");
1224 if (strcmp(bitmap_file
, "internal")==0) {
1225 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1230 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1231 raiddisks
, devlist
->next
, assume_clean
,
1232 bitmap_file
, bitmap_chunk
, write_behind
,
1233 delay
, verbose
-quiet
, autof
, size
);
1236 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1237 if (write_behind
&& !bitmap_file
) {
1238 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1242 if (raiddisks
== 0) {
1243 fprintf(stderr
, Name
": no raid-devices specified.\n");
1248 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1249 raiddisks
, sparedisks
, ident
.name
, homehost
,
1250 ident
.uuid_set
? ident
.uuid
: NULL
,
1251 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1252 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1255 if (devmode
== 'E') {
1256 if (devlist
== NULL
&& !scan
) {
1257 fprintf(stderr
, Name
": No devices to examine\n");
1260 if (devlist
== NULL
)
1261 devlist
= conf_get_devs();
1262 if (devlist
== NULL
) {
1263 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1266 if (brief
&& verbose
)
1268 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1270 SparcAdjust
, ss
, homehost
);
1271 } else if (devmode
== DetailPlatform
) {
1272 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1274 if (devlist
== NULL
) {
1275 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1276 /* apply --detail or --wait-clean to
1277 * all devices in /proc/mdstat
1279 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1280 struct mdstat_ent
*e
;
1281 struct map_ent
*map
= NULL
;
1283 int v
= verbose
>1?0:verbose
+1;
1285 for (members
= 0; members
<= 1; members
++) {
1286 for (e
=ms
; e
; e
=e
->next
) {
1289 int member
= e
->metadata_version
&&
1290 strncmp(e
->metadata_version
,
1291 "external:/", 10) == 0;
1292 if (members
!= member
)
1294 me
= map_by_devnum(&map
, e
->devnum
);
1296 && strcmp(me
->path
, "/unknown") != 0)
1299 name
= get_md_name(e
->devnum
);
1302 fprintf(stderr
, Name
": cannot find device file for %s\n",
1307 rv
|= Detail(name
, v
,
1311 rv
|= WaitClean(name
, -1, v
);
1316 } else if (devmode
== 'S' && scan
) {
1317 /* apply --stop to all devices in /proc/mdstat */
1318 /* Due to possible stacking of devices, repeat until
1319 * nothing more can be stopped
1321 int progress
=1, err
;
1324 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1325 struct mdstat_ent
*e
;
1327 if (!progress
) last
= 1;
1328 progress
= 0; err
= 0;
1329 for (e
=ms
; e
; e
=e
->next
) {
1330 char *name
= get_md_name(e
->devnum
);
1333 fprintf(stderr
, Name
": cannot find device file for %s\n",
1337 mdfd
= open_mddev(name
, 1);
1339 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1349 } while (!last
&& err
);
1352 fprintf(stderr
, Name
": No devices given.\n");
1356 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1357 switch(dv
->disposition
) {
1359 rv
|= Detail(dv
->devname
,
1361 export
, test
, homehost
);
1363 case 'K': /* Zero superblock */
1365 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1369 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1371 } while ((rv
& 2) == 0);
1376 rv
|= Query(dv
->devname
); continue;
1378 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1380 rv
|= Wait(dv
->devname
); continue;
1382 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1384 mdfd
= open_mddev(dv
->devname
, 1);
1386 switch(dv
->disposition
) {
1388 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1390 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1392 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1394 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1403 if (!devlist
&& !scan
) {
1404 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1408 if (pidfile
&& !daemonise
) {
1409 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1414 if (get_linux_version() > 20616)
1415 /* mdstat responds to poll */
1420 rv
= Monitor(devlist
, mailaddr
, program
,
1421 delay
?delay
:60, daemonise
, scan
, oneshot
,
1422 dosyslog
, test
, pidfile
, increments
);
1426 if (array_size
>= 0) {
1427 /* alway impose array size first, independent of
1429 * Do not allow level or raid_disks changes at the
1430 * same time as that can be irreversibly destructive.
1434 if (raiddisks
|| level
!= UnSet
) {
1435 fprintf(stderr
, Name
": cannot change array size in same operation "
1436 "as changing raiddisks or level.\n"
1437 " Change size first, then check that data is still intact.\n");
1441 sysfs_init(&sra
, mdfd
, 0);
1442 if (array_size
== 0)
1443 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1445 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1448 fprintf(stderr
, Name
": --array-size setting"
1449 " is too large.\n");
1451 fprintf(stderr
, Name
": current kernel does"
1452 " not support setting --array-size\n");
1457 if (devs_found
> 1) {
1460 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1461 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1465 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1466 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1470 } else if (bitmap_file
) {
1471 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1472 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1477 delay
= DEFAULT_BITMAP_DELAY
;
1478 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1479 bitmap_chunk
, delay
, write_behind
, force
);
1480 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1481 || chunk
!= 0 || level
!= UnSet
) {
1482 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1483 size
, level
, layout_str
, chunk
, raiddisks
);
1484 } else if (array_size
< 0)
1485 fprintf(stderr
, Name
": no changes to --grow\n");
1493 fprintf(stderr
, Name
1494 ": --incremental --scan meaningless without --run.\n");
1497 rv
= IncrementalScan(verbose
);
1500 if (!rebuild_map
&& !scan
) {
1501 fprintf(stderr
, Name
1502 ": --incremental requires a device.\n");
1507 if (devlist
->next
) {
1508 fprintf(stderr
, Name
1509 ": --incremental can only handle one device.\n");
1513 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1514 ss
, homehost
, require_homehost
, autof
);