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 mode
== INCREMENTAL
|| mode
== MANAGE
)
156 break; /* b means bitmap */
164 if (strcasecmp(optarg
, "<ignore>") == 0)
165 require_homehost
= 0;
172 fputs(Usage
, stderr
);
175 /* second, figure out the mode.
176 * Some options force the mode. Others
177 * set the mode if it isn't already
181 case '@': /* just incase they say --manage */
183 shortopt
= short_bitmap_options
;
188 case ReAdd
: /* re-add */
191 shortopt
= short_bitmap_options
;
195 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
196 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
197 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
198 case 'F': newmode
= MONITOR
;break;
199 case 'G': newmode
= GROW
;
200 shortopt
= short_bitmap_options
;
202 case 'I': newmode
= INCREMENTAL
;
203 shortopt
= short_bitmap_auto_options
; break;
205 newmode
= AUTODETECT
; break;
211 case 'Q': newmode
= MISC
; break;
219 case 'K': if (!mode
) newmode
= MISC
; break;
221 if (mode
&& newmode
== mode
) {
222 /* everybody happy ! */
223 } else if (mode
&& newmode
!= mode
) {
225 fprintf(stderr
, Name
": ");
226 if (option_index
>= 0)
227 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
229 fprintf(stderr
, "-%c", opt
);
230 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
231 map_num(modes
, newmode
),
232 map_num(modes
, mode
));
234 } else if (!mode
&& newmode
) {
237 /* special case of -c --help */
239 ( strncmp(optarg
, "--h", 3)==0 ||
240 strncmp(optarg
, "-h", 2)==0)) {
241 fputs(Help_config
, stdout
);
245 /* If first option is a device, don't force the mode yet */
247 if (devs_found
== 0) {
248 dv
= malloc(sizeof(*dv
));
250 fprintf(stderr
, Name
": malloc failed\n");
253 dv
->devname
= optarg
;
254 dv
->disposition
= devmode
;
255 dv
->writemostly
= writemostly
;
261 devlistend
= &dv
->next
;
266 /* No mode yet, and this is the second device ... */
267 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
268 " (%s) is listed\n", optarg
);
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
)) {
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
= parse_size(optarg
);
391 fprintf(stderr
, Name
": invalid size: %s\n",
395 /* convert sectors to K */
400 case O(GROW
,'Z'): /* array size */
401 if (array_size
>= 0) {
402 fprintf(stderr
, Name
": array-size may only be specified once. "
403 "Second value is %s.\n", optarg
);
406 if (strcmp(optarg
, "max") == 0)
409 array_size
= parse_size(optarg
);
410 if (array_size
<= 0) {
411 fprintf(stderr
, Name
": invalid array size: %s\n",
420 case O(BUILD
,'l'): /* set raid level*/
421 if (level
!= UnSet
) {
422 fprintf(stderr
, Name
": raid level may only be set once. "
423 "Second value is %s.\n", optarg
);
426 level
= map_name(pers
, optarg
);
427 if (level
== UnSet
) {
428 fprintf(stderr
, Name
": invalid raid level: %s\n",
432 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
433 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
436 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
440 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
441 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
448 case O(GROW
, 'p'): /* new layout */
450 fprintf(stderr
,Name
": layout may only be sent once. "
451 "Second value was %s\n", optarg
);
455 /* 'Grow' will parse the value */
458 case O(CREATE
,'p'): /* raid5 layout */
459 case O(BUILD
,'p'): /* faulty layout */
460 if (layout
!= UnSet
) {
461 fprintf(stderr
,Name
": layout may only be sent once. "
462 "Second value was %s\n", optarg
);
467 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
468 map_num(pers
, level
));
471 fprintf(stderr
, Name
": raid level must be given before layout.\n");
475 layout
= map_name(r5layout
, optarg
);
477 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
483 layout
= map_name(r6layout
, optarg
);
485 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
492 layout
= parse_layout_10(optarg
);
494 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
502 layout
= parse_layout_faulty(optarg
);
504 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
512 case O(CREATE
,AssumeClean
):
513 case O(BUILD
,AssumeClean
): /* assume clean */
519 case O(BUILD
,'n'): /* number of raid disks */
521 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
525 raiddisks
= strtol(optarg
, &c
, 10);
526 if (!optarg
[0] || *c
|| raiddisks
<=0) {
527 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
531 ident
.raid_disks
= raiddisks
;
534 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
536 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
540 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
541 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
545 sparedisks
= strtol(optarg
, &c
, 10);
546 if (!optarg
[0] || *c
|| sparedisks
< 0) {
547 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
555 case O(INCREMENTAL
,'a'):
556 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
557 autof
= parse_auto(optarg
, "--auto flag", 0);
560 case O(CREATE
,Symlinks
):
561 case O(BUILD
,Symlinks
):
562 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
566 case O(BUILD
,'f'): /* force honouring '-n 1' */
567 case O(GROW
,'f'): /* ditto */
568 case O(CREATE
,'f'): /* force honouring of device list */
569 case O(ASSEMBLE
,'f'): /* force assembly */
570 case O(MISC
,'f'): /* force zero */
574 /* now for the Assemble options */
575 case O(CREATE
,'u'): /* uuid of array */
576 case O(ASSEMBLE
,'u'): /* uuid of array */
577 if (ident
.uuid_set
) {
578 fprintf(stderr
, Name
": uuid cannot be set twice. "
579 "Second value %s.\n", optarg
);
582 if (parse_uuid(optarg
, ident
.uuid
))
585 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
591 case O(ASSEMBLE
,'N'):
593 fprintf(stderr
, Name
": name cannot be set twice. "
594 "Second value %s.\n", optarg
);
597 if (strlen(optarg
) > 32) {
598 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
602 strcpy(ident
.name
, optarg
);
605 case O(ASSEMBLE
,'m'): /* super-minor for array */
606 if (ident
.super_minor
!= UnSet
) {
607 fprintf(stderr
, Name
": super-minor cannot be set twice. "
608 "Second value: %s.\n", optarg
);
611 if (strcmp(optarg
, "dev")==0)
612 ident
.super_minor
= -2;
614 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
615 if (!optarg
[0] || *cp
) {
616 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
622 case O(ASSEMBLE
,'U'): /* update the superblock */
624 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
629 if (strcmp(update
, "sparc2.2")==0)
631 if (strcmp(update
, "super-minor") == 0)
633 if (strcmp(update
, "summaries")==0)
635 if (strcmp(update
, "resync")==0)
637 if (strcmp(update
, "uuid")==0)
639 if (strcmp(update
, "name")==0)
641 if (strcmp(update
, "homehost")==0)
643 if (strcmp(update
, "devicesize")==0)
645 if (strcmp(update
, "byteorder")==0) {
647 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
650 for(i
=0; !ss
&& superlist
[i
]; i
++)
651 ss
= superlist
[i
]->match_metadata_desc("0.swap");
653 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
659 if (strcmp(update
,"?") == 0 ||
660 strcmp(update
, "help") == 0) {
662 fprintf(outf
, Name
": ");
666 Name
": '--update=%s' is invalid. ",
669 fprintf(outf
, "Valid --update options are:\n"
670 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
671 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
672 exit(outf
== stdout
? 0 : 2);
674 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
675 runstop
= -1; /* --stop isn't allowed for --assemble,
676 * so we overload slightly */
679 case O(ASSEMBLE
,'c'): /* config file */
680 case O(INCREMENTAL
, 'c'):
684 fprintf(stderr
, Name
": configfile cannot be set twice. "
685 "Second value is %s.\n", optarg
);
689 set_conffile(configfile
);
690 /* FIXME possibly check that config file exists. Even parse it */
692 case O(ASSEMBLE
,'s'): /* scan */
695 case O(INCREMENTAL
,'s'):
699 case O(MONITOR
,'m'): /* mail address */
701 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
707 case O(MONITOR
,'p'): /* alert program */
709 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
715 case O(MONITOR
,'r'): /* rebuild increments */
716 increments
= atoi(optarg
);
717 if (increments
>99 || increments
<1) {
718 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
723 case O(MONITOR
,'d'): /* delay in seconds */
725 case O(BUILD
,'d'): /* delay for bitmap updates */
728 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
731 delay
= strtol(optarg
, &c
, 10);
732 if (!optarg
[0] || *c
|| delay
<1) {
733 fprintf(stderr
, Name
": invalid delay: %s\n",
739 case O(MONITOR
,'f'): /* daemonise */
742 case O(MONITOR
,'i'): /* pid */
744 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
749 case O(MONITOR
,'1'): /* oneshot */
752 case O(MONITOR
,'t'): /* test */
755 case O(MONITOR
,'y'): /* log messages to syslog */
756 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
760 /* now the general management options. Some are applicable
761 * to other modes. None have arguments.
764 case O(MANAGE
,'a'): /* add a drive */
768 case O(MANAGE
,ReAdd
):
772 case O(MANAGE
,'r'): /* remove a drive */
775 case O(MANAGE
,'f'): /* set faulty */
776 case O(INCREMENTAL
,'f'): /* r for incremental is taken, use f
777 * even though we will both fail and
778 * remove the device */
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"
878 " not '%s'\n", optarg
);
881 case O(GROW
,BitmapChunk
):
882 case O(BUILD
,BitmapChunk
):
883 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
884 bitmap_chunk
= strtol(optarg
, &c
, 10);
885 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
886 bitmap_chunk
& (bitmap_chunk
- 1)) {
887 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
891 /* convert K to B, chunk of 0K means 512B */
892 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
895 case O(GROW
, WriteBehind
):
896 case O(BUILD
, WriteBehind
):
897 case O(CREATE
, WriteBehind
): /* write-behind mode */
898 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
900 write_behind
= strtol(optarg
, &c
, 10);
901 if (write_behind
< 0 || *c
||
902 write_behind
> 16383) {
903 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
909 case O(INCREMENTAL
, 'r'):
913 /* We have now processed all the valid options. Anything else is
916 if (option_index
> 0)
917 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
918 long_options
[option_index
].name
,
919 map_num(modes
, mode
));
921 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
922 opt
, map_num(modes
, mode
));
928 char *help_text
= Help
;
930 help_text
= OptionHelp
;
933 case ASSEMBLE
: help_text
= Help_assemble
; break;
934 case BUILD
: help_text
= Help_build
; break;
935 case CREATE
: help_text
= Help_create
; break;
936 case MANAGE
: help_text
= Help_manage
; break;
937 case MISC
: help_text
= Help_misc
; break;
938 case MONITOR
: help_text
= Help_monitor
; break;
939 case GROW
: help_text
= Help_grow
; break;
940 case INCREMENTAL
:help_text
= Help_incr
; break;
942 fputs(help_text
,stdout
);
946 if (!mode
&& devs_found
) {
949 if (devlist
->disposition
== 0)
950 devlist
->disposition
= devmode
;
953 fputs(Usage
, stderr
);
958 struct createinfo
*ci
= conf_get_create_info();
960 if (strcasecmp(symlinks
, "yes") == 0)
962 else if (strcasecmp(symlinks
, "no") == 0)
965 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
969 /* Ok, got the option parsing out of the way
970 * hopefully it's mostly right but there might be some stuff
973 * That is mosty checked in the per-mode stuff but...
975 * For @,B,C and A without -s, the first device listed must be an md device
976 * we check that here and open it.
979 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
980 (mode
== ASSEMBLE
&& ! scan
)) {
981 if (devs_found
< 1) {
982 fprintf(stderr
, Name
": an md device must be given in this mode\n");
985 if ((int)ident
.super_minor
== -2 && autof
) {
986 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
989 if (mode
== MANAGE
|| mode
== GROW
) {
990 mdfd
= open_mddev(devlist
->devname
, 1);
994 /* non-existent device is OK */
995 mdfd
= open_mddev(devlist
->devname
, 0);
997 fprintf(stderr
, Name
": device %s exists but is not an "
998 "md array.\n", devlist
->devname
);
1001 if ((int)ident
.super_minor
== -2) {
1004 fprintf(stderr
, Name
": --super-minor=dev given, and "
1005 "listed device %s doesn't exist.\n",
1010 ident
.super_minor
= minor(stb
.st_rdev
);
1012 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1013 /* We don't really want this open yet, we just might
1014 * have wanted to check some things
1022 if (raiddisks
> max_disks
) {
1023 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1027 if (raiddisks
== 1 && !force
&& level
!= -5) {
1028 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1029 " a mistake. If you really mean it you will need to specify --force before\n"
1030 " setting the number of drives.\n");
1035 if ( sparedisks
> max_disks
- raiddisks
) {
1036 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1042 if (homehost
== NULL
)
1043 homehost
= conf_get_homehost(&require_homehost
);
1044 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1045 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1046 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1047 homehost
= sys_hostname
;
1051 if ((mode
!= MISC
|| devmode
!= 'E') &&
1053 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1057 ident
.autof
= autof
;
1062 /* readonly, add/remove, readwrite, runstop */
1064 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1065 if (!rv
&& devs_found
>1)
1066 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1067 devlist
->next
, verbose
-quiet
);
1068 if (!rv
&& readonly
< 0)
1069 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1071 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1074 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1075 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1076 /* Only a device has been given, so get details from config file */
1077 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1078 if (array_ident
== NULL
) {
1079 fprintf(stderr
, Name
": %s not identified in config file.\n",
1085 if (array_ident
->autof
== 0)
1086 array_ident
->autof
= autof
;
1087 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1089 readonly
, runstop
, update
,
1090 homehost
, require_homehost
,
1091 verbose
-quiet
, force
);
1094 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1095 devlist
->next
, backup_file
,
1096 readonly
, runstop
, update
,
1097 homehost
, require_homehost
,
1098 verbose
-quiet
, force
);
1099 else if (devs_found
>0) {
1100 if (update
&& devs_found
> 1) {
1101 fprintf(stderr
, Name
": can only update a single array at a time\n");
1104 if (backup_file
&& devs_found
> 1) {
1105 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1108 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1109 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1110 if (array_ident
== NULL
) {
1111 fprintf(stderr
, Name
": %s not identified in config file.\n",
1116 if (array_ident
->autof
== 0)
1117 array_ident
->autof
= autof
;
1118 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1120 readonly
, runstop
, update
,
1121 homehost
, require_homehost
,
1122 verbose
-quiet
, force
);
1125 mddev_ident_t a
, array_list
= conf_get_ident(NULL
);
1126 mddev_dev_t devlist
= conf_get_devs();
1128 int failures
, successes
;
1129 if (devlist
== NULL
) {
1130 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1134 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1138 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1141 for (a
= array_list
; a
; a
= a
->next
) {
1150 for (a
= array_list
; a
; a
= a
->next
) {
1155 strcasecmp(a
->devname
, "<ignore>") == 0)
1158 r
= Assemble(ss
, a
->devname
,
1161 readonly
, runstop
, NULL
,
1162 homehost
, require_homehost
,
1163 verbose
-quiet
, force
);
1172 } while (failures
&& successes
);
1173 if (homehost
&& cnt
== 0) {
1174 /* Maybe we can auto-assemble something.
1175 * Repeatedly call Assemble in auto-assemble mode
1180 ident
.autof
= autof
;
1182 mddev_dev_t devlist
= conf_get_devs();
1185 rv2
= Assemble(ss
, NULL
,
1188 readonly
, runstop
, NULL
,
1189 homehost
, require_homehost
,
1190 verbose
-quiet
, force
);
1196 /* found something so even though assembly failed we
1197 * want to avoid auto-updates
1199 auto_update_home
= 0;
1201 /* Incase there are stacked devices, we need to go around again */
1204 if (cnt
== 0 && auto_update_home
&& homehost
) {
1205 /* Nothing found, maybe we need to bootstrap homehost info */
1209 rv2
= Assemble(ss
, NULL
,
1212 readonly
, runstop
, "homehost",
1213 homehost
, require_homehost
,
1214 verbose
-quiet
, force
);
1220 /* Incase there are stacked devices, we need to go around again */
1224 if (cnt
== 0 && rv
== 0) {
1225 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1229 } else if (cnt
== 0 && rv
== 0) {
1230 fprintf(stderr
, Name
": No arrays found in config file\n");
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");
1249 if (strcmp(bitmap_file
, "internal")==0) {
1250 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1255 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1256 raiddisks
, devlist
->next
, assume_clean
,
1257 bitmap_file
, bitmap_chunk
, write_behind
,
1258 delay
, verbose
-quiet
, autof
, size
);
1261 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1262 if (write_behind
&& !bitmap_file
) {
1263 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1267 if (raiddisks
== 0) {
1268 fprintf(stderr
, Name
": no raid-devices specified.\n");
1273 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1274 raiddisks
, sparedisks
, ident
.name
, homehost
,
1275 ident
.uuid_set
? ident
.uuid
: NULL
,
1276 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1277 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1280 if (devmode
== 'E') {
1281 if (devlist
== NULL
&& !scan
) {
1282 fprintf(stderr
, Name
": No devices to examine\n");
1285 if (devlist
== NULL
)
1286 devlist
= conf_get_devs();
1287 if (devlist
== NULL
) {
1288 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1291 if (brief
&& verbose
)
1293 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1295 SparcAdjust
, ss
, homehost
);
1296 } else if (devmode
== DetailPlatform
) {
1297 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1299 if (devlist
== NULL
) {
1300 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1301 /* apply --detail or --wait-clean to
1302 * all devices in /proc/mdstat
1304 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1305 struct mdstat_ent
*e
;
1306 struct map_ent
*map
= NULL
;
1308 int v
= verbose
>1?0:verbose
+1;
1310 for (members
= 0; members
<= 1; members
++) {
1311 for (e
=ms
; e
; e
=e
->next
) {
1314 int member
= e
->metadata_version
&&
1315 strncmp(e
->metadata_version
,
1316 "external:/", 10) == 0;
1317 if (members
!= member
)
1319 me
= map_by_devnum(&map
, e
->devnum
);
1321 && strcmp(me
->path
, "/unknown") != 0)
1324 name
= get_md_name(e
->devnum
);
1327 fprintf(stderr
, Name
": cannot find device file for %s\n",
1332 rv
|= Detail(name
, v
,
1336 rv
|= WaitClean(name
, -1, v
);
1341 } else if (devmode
== 'S' && scan
) {
1342 /* apply --stop to all devices in /proc/mdstat */
1343 /* Due to possible stacking of devices, repeat until
1344 * nothing more can be stopped
1346 int progress
=1, err
;
1349 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1350 struct mdstat_ent
*e
;
1352 if (!progress
) last
= 1;
1353 progress
= 0; err
= 0;
1354 for (e
=ms
; e
; e
=e
->next
) {
1355 char *name
= get_md_name(e
->devnum
);
1358 fprintf(stderr
, Name
": cannot find device file for %s\n",
1362 mdfd
= open_mddev(name
, 1);
1364 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1374 } while (!last
&& err
);
1377 fprintf(stderr
, Name
": No devices given.\n");
1381 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1382 switch(dv
->disposition
) {
1384 rv
|= Detail(dv
->devname
,
1386 export
, test
, homehost
);
1388 case 'K': /* Zero superblock */
1390 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1394 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1401 rv
|= Query(dv
->devname
); continue;
1403 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1405 rv
|= Wait(dv
->devname
); continue;
1407 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1409 mdfd
= open_mddev(dv
->devname
, 1);
1411 switch(dv
->disposition
) {
1413 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1415 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1417 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1419 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1428 if (!devlist
&& !scan
) {
1429 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1433 if (pidfile
&& !daemonise
) {
1434 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1439 if (get_linux_version() > 20616)
1440 /* mdstat responds to poll */
1445 rv
= Monitor(devlist
, mailaddr
, program
,
1446 delay
?delay
:60, daemonise
, scan
, oneshot
,
1447 dosyslog
, test
, pidfile
, increments
);
1451 if (array_size
>= 0) {
1452 /* alway impose array size first, independent of
1454 * Do not allow level or raid_disks changes at the
1455 * same time as that can be irreversibly destructive.
1459 if (raiddisks
|| level
!= UnSet
) {
1460 fprintf(stderr
, Name
": cannot change array size in same operation "
1461 "as changing raiddisks or level.\n"
1462 " Change size first, then check that data is still intact.\n");
1466 sysfs_init(&sra
, mdfd
, 0);
1467 if (array_size
== 0)
1468 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1470 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1473 fprintf(stderr
, Name
": --array-size setting"
1474 " is too large.\n");
1476 fprintf(stderr
, Name
": current kernel does"
1477 " not support setting --array-size\n");
1482 if (devs_found
> 1) {
1485 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1486 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1490 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1491 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1495 } else if (bitmap_file
) {
1496 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1497 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1502 delay
= DEFAULT_BITMAP_DELAY
;
1503 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1504 bitmap_chunk
, delay
, write_behind
, force
);
1505 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1506 || chunk
!= 0 || level
!= UnSet
) {
1507 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1508 size
, level
, layout_str
, chunk
, raiddisks
);
1509 } else if (array_size
< 0)
1510 fprintf(stderr
, Name
": no changes to --grow\n");
1518 fprintf(stderr
, Name
1519 ": --incremental --scan meaningless without --run.\n");
1522 if (devmode
== 'f') {
1523 fprintf(stderr
, Name
1524 ": --incremental --scan --fail not supported.\n");
1527 rv
= IncrementalScan(verbose
);
1530 if (!rebuild_map
&& !scan
) {
1531 fprintf(stderr
, Name
1532 ": --incremental requires a device.\n");
1537 if (devlist
->next
) {
1538 fprintf(stderr
, Name
1539 ": --incremental can only handle one device.\n");
1543 if (devmode
== 'f') {
1544 rv
= IncrementalRemove(devlist
->devname
, verbose
-quiet
);
1547 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1548 ss
, homehost
, require_homehost
, autof
);