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;
106 char *subarray
= NULL
;
113 srandom(time(0) ^ getpid());
117 ident
.raid_disks
= UnSet
;
118 ident
.super_minor
= UnSet
;
120 ident
.spare_group
= NULL
;
123 ident
.bitmap_fd
= -1;
124 ident
.bitmap_file
= NULL
;
126 ident
.container
= NULL
;
129 while ((option_index
= -1) ,
130 (opt
=getopt_long(argc
, argv
,
131 shortopt
, long_options
,
132 &option_index
)) != -1) {
134 /* firstly, some mode-independent options */
137 if (option_index
> 0 &&
138 strcmp(long_options
[option_index
].name
, "help-options")==0)
145 fputs(Version
, stderr
);
155 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
156 mode
== INCREMENTAL
|| mode
== MANAGE
)
157 break; /* b means bitmap */
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;
221 if (opt
== KillSubarray
)
223 case 'K': if (!mode
) newmode
= MISC
; break;
225 if (mode
&& newmode
== mode
) {
226 /* everybody happy ! */
227 } else if (mode
&& newmode
!= mode
) {
229 fprintf(stderr
, Name
": ");
230 if (option_index
>= 0)
231 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
233 fprintf(stderr
, "-%c", opt
);
234 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
235 map_num(modes
, newmode
),
236 map_num(modes
, mode
));
238 } else if (!mode
&& newmode
) {
241 /* special case of -c --help */
243 ( strncmp(optarg
, "--h", 3)==0 ||
244 strncmp(optarg
, "-h", 2)==0)) {
245 fputs(Help_config
, stdout
);
249 /* If first option is a device, don't force the mode yet */
251 if (devs_found
== 0) {
252 dv
= malloc(sizeof(*dv
));
254 fprintf(stderr
, Name
": malloc failed\n");
257 dv
->devname
= optarg
;
258 dv
->disposition
= devmode
;
259 dv
->writemostly
= writemostly
;
265 devlistend
= &dv
->next
;
270 /* No mode yet, and this is the second device ... */
271 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
272 " (%s) is listed\n", optarg
);
275 if (option_index
>= 0)
276 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
278 fprintf(stderr
, Name
": -%c", opt
);
279 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
283 /* if we just set the mode, then done */
297 /* an undecorated option - must be a device name.
299 if (devs_found
> 0 && mode
== '@' && !devmode
) {
300 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
303 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
304 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
307 dv
= malloc(sizeof(*dv
));
309 fprintf(stderr
, Name
": malloc failed\n");
312 dv
->devname
= optarg
;
313 dv
->disposition
= devmode
;
314 dv
->writemostly
= writemostly
;
320 devlistend
= &dv
->next
;
326 /* We've got a mode, and opt is now something else which
327 * could depend on the mode */
328 #define O(a,b) ((a<<8)|b)
329 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",
347 case O(ASSEMBLE
,AutoHomeHost
):
348 auto_update_home
= 1;
351 case O(INCREMENTAL
, 'e'):
353 case O(ASSEMBLE
,'e'):
354 case O(MISC
,'e'): /* set metadata (superblock) information */
356 fprintf(stderr
, Name
": metadata information already given\n");
359 for(i
=0; !ss
&& superlist
[i
]; i
++)
360 ss
= superlist
[i
]->match_metadata_desc(optarg
);
363 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
366 max_disks
= ss
->max_devs
;
372 /* set write-mostly for following devices */
377 /* clear write-mostly for following devices */
384 case O(BUILD
,'z'): /* size */
386 fprintf(stderr
, Name
": size may only be specified once. "
387 "Second value is %s.\n", optarg
);
390 if (strcmp(optarg
, "max")==0)
393 size
= parse_size(optarg
);
395 fprintf(stderr
, Name
": invalid size: %s\n",
399 /* convert sectors to K */
404 case O(GROW
,'Z'): /* array size */
405 if (array_size
>= 0) {
406 fprintf(stderr
, Name
": array-size may only be specified once. "
407 "Second value is %s.\n", optarg
);
410 if (strcmp(optarg
, "max") == 0)
413 array_size
= parse_size(optarg
);
414 if (array_size
<= 0) {
415 fprintf(stderr
, Name
": invalid array size: %s\n",
424 case O(BUILD
,'l'): /* set raid level*/
425 if (level
!= UnSet
) {
426 fprintf(stderr
, Name
": raid level may only be set once. "
427 "Second value is %s.\n", optarg
);
430 level
= map_name(pers
, optarg
);
431 if (level
== UnSet
) {
432 fprintf(stderr
, Name
": invalid raid level: %s\n",
436 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
437 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
440 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
444 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
445 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
452 case O(GROW
, 'p'): /* new layout */
454 fprintf(stderr
,Name
": layout may only be sent once. "
455 "Second value was %s\n", optarg
);
459 /* 'Grow' will parse the value */
462 case O(CREATE
,'p'): /* raid5 layout */
463 case O(BUILD
,'p'): /* faulty layout */
464 if (layout
!= UnSet
) {
465 fprintf(stderr
,Name
": layout may only be sent once. "
466 "Second value was %s\n", optarg
);
471 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
472 map_num(pers
, level
));
475 fprintf(stderr
, Name
": raid level must be given before layout.\n");
479 layout
= map_name(r5layout
, optarg
);
481 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
487 layout
= map_name(r6layout
, optarg
);
489 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
496 layout
= parse_layout_10(optarg
);
498 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
506 layout
= parse_layout_faulty(optarg
);
508 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
516 case O(CREATE
,AssumeClean
):
517 case O(BUILD
,AssumeClean
): /* assume clean */
523 case O(BUILD
,'n'): /* number of raid disks */
525 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
529 raiddisks
= strtol(optarg
, &c
, 10);
530 if (!optarg
[0] || *c
|| raiddisks
<=0) {
531 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
535 ident
.raid_disks
= raiddisks
;
538 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
540 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
544 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
545 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
549 sparedisks
= strtol(optarg
, &c
, 10);
550 if (!optarg
[0] || *c
|| sparedisks
< 0) {
551 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
559 case O(INCREMENTAL
,'a'):
560 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
561 autof
= parse_auto(optarg
, "--auto flag", 0);
564 case O(CREATE
,Symlinks
):
565 case O(BUILD
,Symlinks
):
566 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
570 case O(BUILD
,'f'): /* force honouring '-n 1' */
571 case O(GROW
,'f'): /* ditto */
572 case O(CREATE
,'f'): /* force honouring of device list */
573 case O(ASSEMBLE
,'f'): /* force assembly */
574 case O(MISC
,'f'): /* force zero */
578 /* now for the Assemble options */
579 case O(CREATE
,'u'): /* uuid of array */
580 case O(ASSEMBLE
,'u'): /* uuid of array */
581 if (ident
.uuid_set
) {
582 fprintf(stderr
, Name
": uuid cannot be set twice. "
583 "Second value %s.\n", optarg
);
586 if (parse_uuid(optarg
, ident
.uuid
))
589 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
595 case O(ASSEMBLE
,'N'):
597 fprintf(stderr
, Name
": name cannot be set twice. "
598 "Second value %s.\n", optarg
);
601 if (strlen(optarg
) > 32) {
602 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
606 strcpy(ident
.name
, optarg
);
609 case O(ASSEMBLE
,'m'): /* super-minor for array */
610 if (ident
.super_minor
!= UnSet
) {
611 fprintf(stderr
, Name
": super-minor cannot be set twice. "
612 "Second value: %s.\n", optarg
);
615 if (strcmp(optarg
, "dev")==0)
616 ident
.super_minor
= -2;
618 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
619 if (!optarg
[0] || *cp
) {
620 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
626 case O(ASSEMBLE
,'U'): /* update the superblock */
628 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
633 if (strcmp(update
, "sparc2.2")==0)
635 if (strcmp(update
, "super-minor") == 0)
637 if (strcmp(update
, "summaries")==0)
639 if (strcmp(update
, "resync")==0)
641 if (strcmp(update
, "uuid")==0)
643 if (strcmp(update
, "name")==0)
645 if (strcmp(update
, "homehost")==0)
647 if (strcmp(update
, "devicesize")==0)
649 if (strcmp(update
, "byteorder")==0) {
651 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
654 for(i
=0; !ss
&& superlist
[i
]; i
++)
655 ss
= superlist
[i
]->match_metadata_desc("0.swap");
657 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
663 if (strcmp(update
,"?") == 0 ||
664 strcmp(update
, "help") == 0) {
666 fprintf(outf
, Name
": ");
670 Name
": '--update=%s' is invalid. ",
673 fprintf(outf
, "Valid --update options are:\n"
674 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
675 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
676 exit(outf
== stdout
? 0 : 2);
678 case O(INCREMENTAL
,NoDegraded
):
679 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
680 runstop
= -1; /* --stop isn't allowed for --assemble,
681 * so we overload slightly */
684 case O(ASSEMBLE
,'c'): /* config file */
685 case O(INCREMENTAL
, 'c'):
689 fprintf(stderr
, Name
": configfile cannot be set twice. "
690 "Second value is %s.\n", optarg
);
694 set_conffile(configfile
);
695 /* FIXME possibly check that config file exists. Even parse it */
697 case O(ASSEMBLE
,'s'): /* scan */
700 case O(INCREMENTAL
,'s'):
704 case O(MONITOR
,'m'): /* mail address */
706 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
712 case O(MONITOR
,'p'): /* alert program */
714 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
720 case O(MONITOR
,'r'): /* rebuild increments */
721 increments
= atoi(optarg
);
722 if (increments
>99 || increments
<1) {
723 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
728 case O(MONITOR
,'d'): /* delay in seconds */
730 case O(BUILD
,'d'): /* delay for bitmap updates */
733 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
736 delay
= strtol(optarg
, &c
, 10);
737 if (!optarg
[0] || *c
|| delay
<1) {
738 fprintf(stderr
, Name
": invalid delay: %s\n",
744 case O(MONITOR
,'f'): /* daemonise */
747 case O(MONITOR
,'i'): /* pid */
749 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
754 case O(MONITOR
,'1'): /* oneshot */
757 case O(MONITOR
,'t'): /* test */
760 case O(MONITOR
,'y'): /* log messages to syslog */
761 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
765 /* now the general management options. Some are applicable
766 * to other modes. None have arguments.
769 case O(MANAGE
,'a'): /* add a drive */
773 case O(MANAGE
,ReAdd
):
777 case O(MANAGE
,'r'): /* remove a drive */
780 case O(MANAGE
,'f'): /* set faulty */
783 case O(INCREMENTAL
,'R'):
785 case O(ASSEMBLE
,'R'):
787 case O(CREATE
,'R'): /* Run the array */
789 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
796 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
812 case O(MISC
, Waitclean
):
813 case O(MISC
, DetailPlatform
):
814 case O(MISC
, KillSubarray
):
815 if (devmode
&& devmode
!= opt
&&
816 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
817 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
818 devmode
=='E'?opt
:devmode
);
827 case O(MISC
, Sparc22
):
828 if (devmode
!= 'E') {
829 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
835 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
837 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
840 if (strcmp(optarg
, "internal")==0) {
841 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
844 bitmap_fd
= open(optarg
, O_RDWR
);
845 if (!*optarg
|| bitmap_fd
< 0) {
846 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
849 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
852 case O(ASSEMBLE
, BackupFile
):
853 case O(GROW
, BackupFile
):
854 /* Specify a file into which grow might place a backup,
855 * or from which assemble might recover a backup
858 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
861 backup_file
= optarg
;
865 case O(CREATE
,'b'): /* here we create the bitmap */
866 if (strcmp(optarg
, "none") == 0) {
867 fprintf(stderr
, Name
": '--bitmap none' only"
868 " support for --grow\n");
873 if (strcmp(optarg
, "internal")== 0 ||
874 strcmp(optarg
, "none")== 0 ||
875 strchr(optarg
, '/') != NULL
) {
876 bitmap_file
= optarg
;
880 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
881 " not '%s'\n", optarg
);
884 case O(GROW
,BitmapChunk
):
885 case O(BUILD
,BitmapChunk
):
886 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
887 bitmap_chunk
= strtol(optarg
, &c
, 10);
888 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
889 bitmap_chunk
& (bitmap_chunk
- 1)) {
890 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
894 /* convert K to B, chunk of 0K means 512B */
895 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
898 case O(GROW
, WriteBehind
):
899 case O(BUILD
, WriteBehind
):
900 case O(CREATE
, WriteBehind
): /* write-behind mode */
901 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
903 write_behind
= strtol(optarg
, &c
, 10);
904 if (write_behind
< 0 || *c
||
905 write_behind
> 16383) {
906 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
912 case O(INCREMENTAL
, 'r'):
916 /* We have now processed all the valid options. Anything else is
919 if (option_index
> 0)
920 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
921 long_options
[option_index
].name
,
922 map_num(modes
, mode
));
924 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
925 opt
, map_num(modes
, mode
));
931 char *help_text
= Help
;
933 help_text
= OptionHelp
;
936 case ASSEMBLE
: help_text
= Help_assemble
; break;
937 case BUILD
: help_text
= Help_build
; break;
938 case CREATE
: help_text
= Help_create
; break;
939 case MANAGE
: help_text
= Help_manage
; break;
940 case MISC
: help_text
= Help_misc
; break;
941 case MONITOR
: help_text
= Help_monitor
; break;
942 case GROW
: help_text
= Help_grow
; break;
943 case INCREMENTAL
:help_text
= Help_incr
; break;
945 fputs(help_text
,stdout
);
949 if (!mode
&& devs_found
) {
952 if (devlist
->disposition
== 0)
953 devlist
->disposition
= devmode
;
956 fputs(Usage
, stderr
);
961 struct createinfo
*ci
= conf_get_create_info();
963 if (strcasecmp(symlinks
, "yes") == 0)
965 else if (strcasecmp(symlinks
, "no") == 0)
968 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
972 /* Ok, got the option parsing out of the way
973 * hopefully it's mostly right but there might be some stuff
976 * That is mosty checked in the per-mode stuff but...
978 * For @,B,C and A without -s, the first device listed must be an md device
979 * we check that here and open it.
982 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
983 (mode
== ASSEMBLE
&& ! scan
)) {
984 if (devs_found
< 1) {
985 fprintf(stderr
, Name
": an md device must be given in this mode\n");
988 if ((int)ident
.super_minor
== -2 && autof
) {
989 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
992 if (mode
== MANAGE
|| mode
== GROW
) {
993 mdfd
= open_mddev(devlist
->devname
, 1);
997 /* non-existent device is OK */
998 mdfd
= open_mddev(devlist
->devname
, 0);
1000 fprintf(stderr
, Name
": device %s exists but is not an "
1001 "md array.\n", devlist
->devname
);
1004 if ((int)ident
.super_minor
== -2) {
1007 fprintf(stderr
, Name
": --super-minor=dev given, and "
1008 "listed device %s doesn't exist.\n",
1013 ident
.super_minor
= minor(stb
.st_rdev
);
1015 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1016 /* We don't really want this open yet, we just might
1017 * have wanted to check some things
1025 if (raiddisks
> max_disks
) {
1026 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1030 if (raiddisks
== 1 && !force
&& level
!= -5) {
1031 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1032 " a mistake. If you really mean it you will need to specify --force before\n"
1033 " setting the number of drives.\n");
1038 if ( sparedisks
> max_disks
- raiddisks
) {
1039 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1045 if (homehost
== NULL
)
1046 homehost
= conf_get_homehost(&require_homehost
);
1047 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1048 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1049 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1050 homehost
= sys_hostname
;
1054 if ((mode
!= MISC
|| devmode
!= 'E') &&
1056 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1060 ident
.autof
= autof
;
1065 /* readonly, add/remove, readwrite, runstop */
1067 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1068 if (!rv
&& devs_found
>1)
1069 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1070 devlist
->next
, verbose
-quiet
);
1071 if (!rv
&& readonly
< 0)
1072 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1074 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1077 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1078 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1079 /* Only a device has been given, so get details from config file */
1080 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1081 if (array_ident
== NULL
) {
1082 fprintf(stderr
, Name
": %s not identified in config file.\n",
1088 if (array_ident
->autof
== 0)
1089 array_ident
->autof
= autof
;
1090 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1092 readonly
, runstop
, update
,
1093 homehost
, require_homehost
,
1094 verbose
-quiet
, force
);
1097 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1098 devlist
->next
, backup_file
,
1099 readonly
, runstop
, update
,
1100 homehost
, require_homehost
,
1101 verbose
-quiet
, force
);
1102 else if (devs_found
>0) {
1103 if (update
&& devs_found
> 1) {
1104 fprintf(stderr
, Name
": can only update a single array at a time\n");
1107 if (backup_file
&& devs_found
> 1) {
1108 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1111 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1112 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1113 if (array_ident
== NULL
) {
1114 fprintf(stderr
, Name
": %s not identified in config file.\n",
1119 if (array_ident
->autof
== 0)
1120 array_ident
->autof
= autof
;
1121 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1123 readonly
, runstop
, update
,
1124 homehost
, require_homehost
,
1125 verbose
-quiet
, force
);
1128 mddev_ident_t a
, array_list
= conf_get_ident(NULL
);
1129 mddev_dev_t devlist
= conf_get_devs();
1131 int failures
, successes
;
1132 if (devlist
== NULL
) {
1133 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1137 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1141 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1144 for (a
= array_list
; a
; a
= a
->next
) {
1153 for (a
= array_list
; a
; a
= a
->next
) {
1158 strcasecmp(a
->devname
, "<ignore>") == 0)
1161 r
= Assemble(ss
, a
->devname
,
1164 readonly
, runstop
, NULL
,
1165 homehost
, require_homehost
,
1166 verbose
-quiet
, force
);
1175 } while (failures
&& successes
);
1176 if (homehost
&& cnt
== 0) {
1177 /* Maybe we can auto-assemble something.
1178 * Repeatedly call Assemble in auto-assemble mode
1183 ident
.autof
= autof
;
1185 mddev_dev_t devlist
= conf_get_devs();
1188 rv2
= Assemble(ss
, NULL
,
1191 readonly
, runstop
, NULL
,
1192 homehost
, require_homehost
,
1193 verbose
-quiet
, force
);
1199 /* found something so even though assembly failed we
1200 * want to avoid auto-updates
1202 auto_update_home
= 0;
1204 /* Incase there are stacked devices, we need to go around again */
1207 if (cnt
== 0 && auto_update_home
&& homehost
) {
1208 /* Nothing found, maybe we need to bootstrap homehost info */
1212 rv2
= Assemble(ss
, NULL
,
1215 readonly
, runstop
, "homehost",
1216 homehost
, require_homehost
,
1217 verbose
-quiet
, force
);
1223 /* Incase there are stacked devices, we need to go around again */
1227 if (cnt
== 0 && rv
== 0) {
1228 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1232 } else if (cnt
== 0 && rv
== 0) {
1233 fprintf(stderr
, Name
": No arrays found in config file\n");
1239 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1240 if (write_behind
&& !bitmap_file
) {
1241 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1245 if (raiddisks
== 0) {
1246 fprintf(stderr
, Name
": no raid-devices specified.\n");
1252 if (strcmp(bitmap_file
, "internal")==0) {
1253 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1258 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1259 raiddisks
, devlist
->next
, assume_clean
,
1260 bitmap_file
, bitmap_chunk
, write_behind
,
1261 delay
, verbose
-quiet
, autof
, size
);
1264 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1265 if (write_behind
&& !bitmap_file
) {
1266 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1270 if (raiddisks
== 0) {
1271 fprintf(stderr
, Name
": no raid-devices specified.\n");
1276 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1277 raiddisks
, sparedisks
, ident
.name
, homehost
,
1278 ident
.uuid_set
? ident
.uuid
: NULL
,
1279 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1280 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1283 if (devmode
== 'E') {
1284 if (devlist
== NULL
&& !scan
) {
1285 fprintf(stderr
, Name
": No devices to examine\n");
1288 if (devlist
== NULL
)
1289 devlist
= conf_get_devs();
1290 if (devlist
== NULL
) {
1291 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1294 if (brief
&& verbose
)
1296 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1298 SparcAdjust
, ss
, homehost
);
1299 } else if (devmode
== DetailPlatform
) {
1300 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1302 if (devlist
== NULL
) {
1303 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1304 /* apply --detail or --wait-clean to
1305 * all devices in /proc/mdstat
1307 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1308 struct mdstat_ent
*e
;
1309 struct map_ent
*map
= NULL
;
1311 int v
= verbose
>1?0:verbose
+1;
1313 for (members
= 0; members
<= 1; members
++) {
1314 for (e
=ms
; e
; e
=e
->next
) {
1317 int member
= e
->metadata_version
&&
1318 strncmp(e
->metadata_version
,
1319 "external:/", 10) == 0;
1320 if (members
!= member
)
1322 me
= map_by_devnum(&map
, e
->devnum
);
1324 && strcmp(me
->path
, "/unknown") != 0)
1327 name
= get_md_name(e
->devnum
);
1330 fprintf(stderr
, Name
": cannot find device file for %s\n",
1335 rv
|= Detail(name
, v
,
1339 rv
|= WaitClean(name
, -1, v
);
1344 } else if (devmode
== 'S' && scan
) {
1345 /* apply --stop to all devices in /proc/mdstat */
1346 /* Due to possible stacking of devices, repeat until
1347 * nothing more can be stopped
1349 int progress
=1, err
;
1352 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1353 struct mdstat_ent
*e
;
1355 if (!progress
) last
= 1;
1356 progress
= 0; err
= 0;
1357 for (e
=ms
; e
; e
=e
->next
) {
1358 char *name
= get_md_name(e
->devnum
);
1361 fprintf(stderr
, Name
": cannot find device file for %s\n",
1365 mdfd
= open_mddev(name
, 1);
1367 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1377 } while (!last
&& err
);
1380 fprintf(stderr
, Name
": No devices given.\n");
1384 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1385 switch(dv
->disposition
) {
1387 rv
|= Detail(dv
->devname
,
1389 export
, test
, homehost
);
1391 case 'K': /* Zero superblock */
1393 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1397 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1404 rv
|= Query(dv
->devname
); continue;
1406 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1408 rv
|= Wait(dv
->devname
); continue;
1410 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1412 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1415 mdfd
= open_mddev(dv
->devname
, 1);
1417 switch(dv
->disposition
) {
1419 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1421 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1423 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1425 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1434 if (!devlist
&& !scan
) {
1435 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1439 if (pidfile
&& !daemonise
) {
1440 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1445 if (get_linux_version() > 20616)
1446 /* mdstat responds to poll */
1451 rv
= Monitor(devlist
, mailaddr
, program
,
1452 delay
?delay
:60, daemonise
, scan
, oneshot
,
1453 dosyslog
, test
, pidfile
, increments
);
1457 if (array_size
>= 0) {
1458 /* alway impose array size first, independent of
1460 * Do not allow level or raid_disks changes at the
1461 * same time as that can be irreversibly destructive.
1465 if (raiddisks
|| level
!= UnSet
) {
1466 fprintf(stderr
, Name
": cannot change array size in same operation "
1467 "as changing raiddisks or level.\n"
1468 " Change size first, then check that data is still intact.\n");
1472 sysfs_init(&sra
, mdfd
, 0);
1473 if (array_size
== 0)
1474 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1476 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1479 fprintf(stderr
, Name
": --array-size setting"
1480 " is too large.\n");
1482 fprintf(stderr
, Name
": current kernel does"
1483 " not support setting --array-size\n");
1488 if (devs_found
> 1) {
1491 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1492 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1496 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1497 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1501 } else if (bitmap_file
) {
1502 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1503 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1508 delay
= DEFAULT_BITMAP_DELAY
;
1509 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1510 bitmap_chunk
, delay
, write_behind
, force
);
1511 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1512 || chunk
!= 0 || level
!= UnSet
) {
1513 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1514 size
, level
, layout_str
, chunk
, raiddisks
);
1515 } else if (array_size
< 0)
1516 fprintf(stderr
, Name
": no changes to --grow\n");
1524 fprintf(stderr
, Name
1525 ": --incremental --scan meaningless without --run.\n");
1528 rv
= IncrementalScan(verbose
);
1531 if (!rebuild_map
&& !scan
) {
1532 fprintf(stderr
, Name
1533 ": --incremental requires a device.\n");
1538 if (devlist
->next
) {
1539 fprintf(stderr
, Name
1540 ": --incremental can only handle one device.\n");
1544 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1545 ss
, homehost
, require_homehost
, autof
);