2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
29 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
30 * Paul Clements, SteelEye Technology, Inc.
38 int main(int argc
, char *argv
[])
52 int max_disks
= MD_SB_DISKS
; /* just a default */
54 struct mddev_ident_s ident
;
55 char *configfile
= NULL
;
64 char *bitmap_file
= NULL
;
65 char *backup_file
= NULL
;
66 int bitmap_chunk
= UnSet
;
68 mddev_dev_t devlist
= NULL
;
69 mddev_dev_t
*devlistend
= & devlist
;
79 char *symlinks
= NULL
;
80 /* autof indicates whether and how to create device node.
81 * bottom 3 bits are style. Rest (when shifted) are number of parts
83 * 1 - don't create (no)
84 * 2 - if is_standard, then create (yes)
85 * 3 - create as 'md' - reject is_standard mdp (md)
86 * 4 - create as 'mdp' - reject is_standard md (mdp)
87 * 5 - default to md if not is_standard (md in config file)
88 * 6 - default to mdp if not is_standard (part, or mdp in config file)
92 char *homehost
= NULL
;
93 char sys_hostname
[256];
94 char *mailaddr
= NULL
;
100 struct supertype
*ss
= NULL
;
103 char *shortopt
= short_options
;
106 int auto_update_home
= 0;
114 srandom(time(0) ^ getpid());
118 ident
.raid_disks
= UnSet
;
119 ident
.super_minor
= UnSet
;
121 ident
.spare_group
= NULL
;
124 ident
.bitmap_fd
= -1;
125 ident
.bitmap_file
= NULL
;
128 while ((option_index
= -1) ,
129 (opt
=getopt_long(argc
, argv
,
130 shortopt
, long_options
,
131 &option_index
)) != -1) {
133 /* firstly, some mode-independant 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");
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;
218 case 'K': if (!mode
) newmode
= MISC
; break;
220 if (mode
&& newmode
== mode
) {
221 /* everybody happy ! */
222 } else if (mode
&& newmode
!= mode
) {
224 fprintf(stderr
, Name
": ");
225 if (option_index
>= 0)
226 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
228 fprintf(stderr
, "-%c", opt
);
229 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
230 map_num(modes
, newmode
),
231 map_num(modes
, mode
));
233 } else if (!mode
&& newmode
) {
236 /* special case of -c --help */
238 ( strncmp(optarg
, "--h", 3)==0 ||
239 strncmp(optarg
, "-h", 2)==0)) {
240 fputs(Help_config
, stdout
);
244 /* If first option is a device, don't force the mode yet */
246 if (devs_found
== 0) {
247 dv
= malloc(sizeof(*dv
));
249 fprintf(stderr
, Name
": malloc failed\n");
252 dv
->devname
= optarg
;
253 dv
->disposition
= devmode
;
254 dv
->writemostly
= writemostly
;
259 devlistend
= &dv
->next
;
264 /* No mode yet, and this is the second device ... */
265 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
268 if (option_index
>= 0)
269 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
271 fprintf(stderr
, Name
": -%c", opt
);
272 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
276 /* if we just set the mode, then done */
290 /* an undecorated option - must be a device name.
292 if (devs_found
> 0 && mode
== '@' && !devmode
) {
293 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
296 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
297 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
300 dv
= malloc(sizeof(*dv
));
302 fprintf(stderr
, Name
": malloc failed\n");
305 dv
->devname
= optarg
;
306 dv
->disposition
= devmode
;
307 dv
->writemostly
= writemostly
;
311 devlistend
= &dv
->next
;
317 /* We've got a mode, and opt is now something else which
318 * could depend on the mode */
319 #define O(a,b) ((a<<8)|b)
320 switch (O(mode
,opt
)) {
322 case O(BUILD
,'c'): /* chunk or rounding */
324 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
325 "Second value is %s.\n", optarg
);
328 chunk
= strtol(optarg
, &c
, 10);
329 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
330 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
336 case O(ASSEMBLE
,AutoHomeHost
):
337 auto_update_home
= 1;
339 case O(INCREMENTAL
, 'e'):
341 case O(ASSEMBLE
,'e'):
342 case O(MISC
,'e'): /* set metadata (superblock) information */
344 fprintf(stderr
, Name
": metadata information already given\n");
347 for(i
=0; !ss
&& superlist
[i
]; i
++)
348 ss
= superlist
[i
]->match_metadata_desc(optarg
);
351 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
354 max_disks
= ss
->max_devs
;
360 /* set write-mostly for following devices */
365 /* clear write-mostly for following devices */
371 case O(CREATE
,'z'): /* size */
373 fprintf(stderr
, Name
": size may only be specified once. "
374 "Second value is %s.\n", optarg
);
377 if (strcmp(optarg
, "max")==0)
380 size
= strtoll(optarg
, &c
, 10);
381 if (!optarg
[0] || *c
|| size
< 4) {
382 fprintf(stderr
, Name
": invalid size: %s\n",
389 case O(GROW
,'l'): /* hack - needed to understand layout */
391 case O(BUILD
,'l'): /* set raid level*/
392 if (level
!= UnSet
) {
393 fprintf(stderr
, Name
": raid level may only be set once. "
394 "Second value is %s.\n", optarg
);
397 level
= map_name(pers
, optarg
);
398 if (level
== UnSet
) {
399 fprintf(stderr
, Name
": invalid raid level: %s\n",
403 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
404 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
408 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
409 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
416 case O(CREATE
,'p'): /* raid5 layout */
417 case O(BUILD
,'p'): /* faulty layout */
418 case O(GROW
, 'p'): /* faulty reconfig */
419 if (layout
!= UnSet
) {
420 fprintf(stderr
,Name
": layout may only be sent once. "
421 "Second value was %s\n", optarg
);
426 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
427 map_num(pers
, level
));
430 fprintf(stderr
, Name
": raid level must be given before layout.\n");
435 layout
= map_name(r5layout
, optarg
);
437 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
444 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
445 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
446 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
449 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
452 if (optarg
[0] == 'n')
453 layout
= 256 + copies
;
454 else if (optarg
[0] == 'o')
455 layout
= 0x10000 + (copies
<<8) + 1;
457 layout
= 1 + (copies
<<8);
464 int ln
= strcspn(optarg
, "0123456789");
465 char *m
= strdup(optarg
);
468 mode
= map_name(faultylayout
, m
);
470 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
474 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
479 case O(CREATE
,AssumeClean
):
480 case O(BUILD
,AssumeClean
): /* assume clean */
486 case O(BUILD
,'n'): /* number of raid disks */
488 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
492 raiddisks
= strtol(optarg
, &c
, 10);
493 if (!optarg
[0] || *c
|| raiddisks
<=0) {
494 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
498 ident
.raid_disks
= raiddisks
;
501 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
503 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
507 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
508 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
512 sparedisks
= strtol(optarg
, &c
, 10);
513 if (!optarg
[0] || *c
|| sparedisks
< 0) {
514 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
522 case O(INCREMENTAL
,'a'):
523 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
524 autof
= parse_auto(optarg
, "--auto flag", 0);
527 case O(CREATE
,Symlinks
):
528 case O(BUILD
,Symlinks
):
529 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
533 case O(BUILD
,'f'): /* force honouring '-n 1' */
534 case O(GROW
,'f'): /* ditto */
535 case O(CREATE
,'f'): /* force honouring of device list */
536 case O(ASSEMBLE
,'f'): /* force assembly */
537 case O(MISC
,'f'): /* force zero */
541 /* now for the Assemble options */
542 case O(CREATE
,'u'): /* uuid of array */
543 case O(ASSEMBLE
,'u'): /* uuid of array */
544 if (ident
.uuid_set
) {
545 fprintf(stderr
, Name
": uuid cannot be set twice. "
546 "Second value %s.\n", optarg
);
549 if (parse_uuid(optarg
, ident
.uuid
))
552 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
558 case O(ASSEMBLE
,'N'):
560 fprintf(stderr
, Name
": name cannot be set twice. "
561 "Second value %s.\n", optarg
);
564 if (strlen(optarg
) > 32) {
565 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
569 strcpy(ident
.name
, optarg
);
572 case O(ASSEMBLE
,'m'): /* super-minor for array */
573 if (ident
.super_minor
!= UnSet
) {
574 fprintf(stderr
, Name
": super-minor cannot be set twice. "
575 "Second value: %s.\n", optarg
);
578 if (strcmp(optarg
, "dev")==0)
579 ident
.super_minor
= -2;
581 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
582 if (!optarg
[0] || *cp
) {
583 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
589 case O(ASSEMBLE
,'U'): /* update the superblock */
591 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
596 if (strcmp(update
, "sparc2.2")==0)
598 if (strcmp(update
, "super-minor") == 0)
600 if (strcmp(update
, "summaries")==0)
602 if (strcmp(update
, "resync")==0)
604 if (strcmp(update
, "uuid")==0)
606 if (strcmp(update
, "name")==0)
608 if (strcmp(update
, "homehost")==0)
610 if (strcmp(update
, "devicesize")==0)
612 if (strcmp(update
, "byteorder")==0) {
614 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
617 for(i
=0; !ss
&& superlist
[i
]; i
++)
618 ss
= superlist
[i
]->match_metadata_desc("0.swap");
620 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
626 if (strcmp(update
,"?") == 0 ||
627 strcmp(update
, "help") == 0) {
629 fprintf(outf
, Name
": ");
633 Name
": '--update=%s' is invalid. ",
636 fprintf(outf
, "Valid --update options are:\n"
637 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
638 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
639 exit(outf
== stdout
? 0 : 2);
641 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
642 runstop
= -1; /* --stop isn't allowed for --assemble,
643 * so we overload slightly */
646 case O(ASSEMBLE
,'c'): /* config file */
647 case O(INCREMENTAL
, 'c'):
651 fprintf(stderr
, Name
": configfile cannot be set twice. "
652 "Second value is %s.\n", optarg
);
656 set_conffile(configfile
);
657 /* FIXME possibly check that config file exists. Even parse it */
659 case O(ASSEMBLE
,'s'): /* scan */
662 case O(INCREMENTAL
,'s'):
666 case O(MONITOR
,'m'): /* mail address */
668 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
674 case O(MONITOR
,'p'): /* alert program */
676 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
682 case O(MONITOR
,'d'): /* delay in seconds */
684 case O(BUILD
,'d'): /* delay for bitmap updates */
687 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
690 delay
= strtol(optarg
, &c
, 10);
691 if (!optarg
[0] || *c
|| delay
<1) {
692 fprintf(stderr
, Name
": invalid delay: %s\n",
698 case O(MONITOR
,'f'): /* daemonise */
701 case O(MONITOR
,'i'): /* pid */
703 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
708 case O(MONITOR
,'1'): /* oneshot */
711 case O(MONITOR
,'t'): /* test */
714 case O(MONITOR
,'y'): /* log messages to syslog */
715 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
719 /* now the general management options. Some are applicable
720 * to other modes. None have arguments.
723 case O(MANAGE
,'a'): /* add a drive */
727 case O(MANAGE
,ReAdd
):
731 case O(MANAGE
,'r'): /* remove a drive */
734 case O(MANAGE
,'f'): /* set faulty */
737 case O(INCREMENTAL
,'R'):
739 case O(ASSEMBLE
,'R'):
741 case O(CREATE
,'R'): /* Run the array */
743 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
750 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
766 case O(MISC
, Waitclean
):
767 if (devmode
&& devmode
!= opt
&&
768 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
769 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
770 devmode
=='E'?opt
:devmode
);
779 case O(MISC
, Sparc22
):
780 if (devmode
!= 'E') {
781 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
787 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
789 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
792 if (strcmp(optarg
, "internal")==0) {
793 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
796 bitmap_fd
= open(optarg
, O_RDWR
);
797 if (!*optarg
|| bitmap_fd
< 0) {
798 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
801 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
804 case O(ASSEMBLE
, BackupFile
):
805 case O(GROW
, BackupFile
):
806 /* Specify a file into which grow might place a backup,
807 * or from which assemble might recover a backup
810 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
813 backup_file
= optarg
;
817 case O(CREATE
,'b'): /* here we create the bitmap */
818 if (strcmp(optarg
, "none") == 0) {
819 fprintf(stderr
, Name
": '--bitmap none' only"
820 " support for --grow\n");
825 if (strcmp(optarg
, "internal")== 0 ||
826 strcmp(optarg
, "none")== 0 ||
827 strchr(optarg
, '/') != NULL
) {
828 bitmap_file
= optarg
;
832 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
835 case O(GROW
,BitmapChunk
):
836 case O(BUILD
,BitmapChunk
):
837 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
838 bitmap_chunk
= strtol(optarg
, &c
, 10);
839 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
840 bitmap_chunk
& (bitmap_chunk
- 1)) {
841 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
845 /* convert K to B, chunk of 0K means 512B */
846 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
849 case O(GROW
, WriteBehind
):
850 case O(BUILD
, WriteBehind
):
851 case O(CREATE
, WriteBehind
): /* write-behind mode */
852 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
854 write_behind
= strtol(optarg
, &c
, 10);
855 if (write_behind
< 0 || *c
||
856 write_behind
> 16383) {
857 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
863 case O(INCREMENTAL
, 'r'):
867 /* We have now processed all the valid options. Anything else is
870 if (option_index
> 0)
871 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
872 long_options
[option_index
].name
,
873 map_num(modes
, mode
));
875 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
876 opt
, map_num(modes
, mode
));
882 char *help_text
= Help
;
884 help_text
= OptionHelp
;
887 case ASSEMBLE
: help_text
= Help_assemble
; break;
888 case BUILD
: help_text
= Help_build
; break;
889 case CREATE
: help_text
= Help_create
; break;
890 case MANAGE
: help_text
= Help_manage
; break;
891 case MISC
: help_text
= Help_misc
; break;
892 case MONITOR
: help_text
= Help_monitor
; break;
893 case GROW
: help_text
= Help_grow
; break;
894 case INCREMENTAL
:help_text
= Help_incr
; break;
896 fputs(help_text
,stdout
);
900 if (!mode
&& devs_found
) {
903 if (devlist
->disposition
== 0)
904 devlist
->disposition
= devmode
;
907 fputs(Usage
, stderr
);
912 struct createinfo
*ci
= conf_get_create_info();
914 if (strcasecmp(symlinks
, "yes") == 0)
916 else if (strcasecmp(symlinks
, "no") == 0)
919 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
923 /* Ok, got the option parsing out of the way
924 * hopefully it's mostly right but there might be some stuff
927 * That is mosty checked in the per-mode stuff but...
929 * For @,B,C and A without -s, the first device listed must be an md device
930 * we check that here and open it.
933 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
934 (mode
== ASSEMBLE
&& ! scan
)) {
935 if (devs_found
< 1) {
936 fprintf(stderr
, Name
": an md device must be given in this mode\n");
939 if ((int)ident
.super_minor
== -2 && autof
) {
940 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
943 if (mode
== MANAGE
|| mode
== GROW
)
944 autof
=1; /* Don't create */
945 mdfd
= open_mddev(devlist
->devname
, autof
);
948 if ((int)ident
.super_minor
== -2) {
951 ident
.super_minor
= minor(stb
.st_rdev
);
956 if (raiddisks
> max_disks
) {
957 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
961 if (raiddisks
== 1 && !force
&& level
!= -5) {
962 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
963 " a mistake. If you really mean it you will need to specify --force before\n"
964 " setting the number of drives.\n");
969 if ( sparedisks
> max_disks
- raiddisks
) {
970 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
976 if (homehost
== NULL
)
977 homehost
= conf_get_homehost();
978 if (homehost
&& strcmp(homehost
, "<system>")==0) {
979 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
980 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
981 homehost
= sys_hostname
;
988 /* readonly, add/remove, readwrite, runstop */
990 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
991 if (!rv
&& devs_found
>1)
992 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
993 devlist
->next
, verbose
-quiet
);
994 if (!rv
&& readonly
< 0)
995 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
997 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1000 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1001 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1002 /* Only a device has been given, so get details from config file */
1003 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1004 if (array_ident
== NULL
) {
1005 fprintf(stderr
, Name
": %s not identified in config file.\n",
1009 mdfd
= open_mddev(devlist
->devname
,
1010 array_ident
->autof
? array_ident
->autof
: autof
);
1014 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
,
1016 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1021 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
,
1022 devlist
->next
, backup_file
,
1023 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1024 else if (devs_found
>0) {
1025 if (update
&& devs_found
> 1) {
1026 fprintf(stderr
, Name
": can only update a single array at a time\n");
1029 if (backup_file
&& devs_found
> 1) {
1030 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1033 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1034 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1035 if (array_ident
== NULL
) {
1036 fprintf(stderr
, Name
": %s not identified in config file.\n",
1041 mdfd
= open_mddev(dv
->devname
,
1042 array_ident
->autof
?array_ident
->autof
: autof
);
1047 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
,
1049 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1053 mddev_ident_t array_list
= conf_get_ident(NULL
);
1054 mddev_dev_t devlist
= conf_get_devs();
1056 if (devlist
== NULL
) {
1057 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1061 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1065 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1068 for (; array_list
; array_list
= array_list
->next
) {
1069 mdu_array_info_t array
;
1070 mdfd
= open_mddev(array_list
->devname
,
1071 array_list
->autof
? array_list
->autof
: autof
);
1076 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
1077 /* already assembled, skip */
1080 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
1083 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1089 /* Maybe we can auto-assemble something.
1090 * Repeatedly call Assemble in auto-assmble mode
1095 ident
.autof
= autof
;
1097 mddev_dev_t devlist
= conf_get_devs();
1100 rv2
= Assemble(ss
, NULL
, -1,
1103 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1109 /* found something so even though assembly failed we
1110 * want to avoid auto-updates
1112 auto_update_home
= 0;
1114 /* Incase there are stacked devices, we need to go around again */
1116 if (cnt
== 0 && auto_update_home
&& homehost
) {
1117 /* Nothing found, maybe we need to bootstrap homehost info */
1121 rv2
= Assemble(ss
, NULL
, -1,
1124 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1130 /* Incase there are stacked devices, we need to go around again */
1133 if (cnt
== 0 && rv
== 0) {
1134 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1138 } else if (cnt
== 0 && rv
== 0) {
1139 fprintf(stderr
, Name
": No arrays found in config file\n");
1145 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1146 if (write_behind
&& !bitmap_file
) {
1147 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1151 if (raiddisks
== 0) {
1152 fprintf(stderr
, Name
": no raid-devices specified.\n");
1158 if (strcmp(bitmap_file
, "internal")==0) {
1159 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1164 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
,
1165 raiddisks
, devlist
->next
, assume_clean
,
1166 bitmap_file
, bitmap_chunk
, write_behind
, delay
, verbose
-quiet
);
1169 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1170 if (write_behind
&& !bitmap_file
) {
1171 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1175 if (raiddisks
== 0) {
1176 fprintf(stderr
, Name
": no raid-devices specified.\n");
1181 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1182 raiddisks
, sparedisks
, ident
.name
, homehost
,
1183 ident
.uuid_set
? ident
.uuid
: NULL
,
1184 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1185 bitmap_file
, bitmap_chunk
, write_behind
, delay
);
1188 if (devmode
== 'E') {
1189 if (devlist
== NULL
&& !scan
) {
1190 fprintf(stderr
, Name
": No devices to examine\n");
1193 if (devlist
== NULL
)
1194 devlist
= conf_get_devs();
1195 if (devlist
== NULL
) {
1196 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1199 if (brief
&& verbose
)
1201 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1203 SparcAdjust
, ss
, homehost
);
1205 if (devlist
== NULL
) {
1206 if (devmode
=='D' && scan
) {
1207 /* apply --detail to all devices in /proc/mdstat */
1208 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1209 struct mdstat_ent
*e
;
1210 for (e
=ms
; e
; e
=e
->next
) {
1211 char *name
= get_md_name(e
->devnum
);
1214 fprintf(stderr
, Name
": cannot find device file for %s\n",
1218 rv
|= Detail(name
, verbose
>1?0:verbose
+1,
1219 export
, test
, homehost
);
1222 } else if (devmode
== 'S' && scan
) {
1223 /* apply --stop to all devices in /proc/mdstat */
1224 /* Due to possible stacking of devices, repeat until
1225 * nothing more can be stopped
1227 int progress
=1, err
;
1230 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1231 struct mdstat_ent
*e
;
1233 if (!progress
) last
= 1;
1234 progress
= 0; err
= 0;
1235 for (e
=ms
; e
; e
=e
->next
) {
1236 char *name
= get_md_name(e
->devnum
);
1239 fprintf(stderr
, Name
": cannot find device file for %s\n",
1243 mdfd
= open_mddev(name
, 1);
1245 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1254 } while (!last
&& err
);
1257 fprintf(stderr
, Name
": No devices given.\n");
1261 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1262 switch(dv
->disposition
) {
1264 rv
|= Detail(dv
->devname
,
1266 export
, test
, homehost
);
1268 case 'K': /* Zero superblock */
1269 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1272 rv
|= Query(dv
->devname
); continue;
1274 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1276 rv
|= Wait(dv
->devname
); continue;
1278 rv
|= WaitClean(dv
->devname
, verbose
-quiet
); continue;
1280 mdfd
= open_mddev(dv
->devname
, 1);
1282 switch(dv
->disposition
) {
1284 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1286 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1288 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1290 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1299 if (!devlist
&& !scan
) {
1300 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1304 if (pidfile
&& !daemonise
) {
1305 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1309 rv
= Monitor(devlist
, mailaddr
, program
,
1310 delay
?delay
:60, daemonise
, scan
, oneshot
,
1311 dosyslog
, test
, pidfile
);
1315 if (devs_found
> 1) {
1318 if (size
>= 0 || raiddisks
) {
1319 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1323 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1324 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1328 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1329 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1332 } else if (layout
!= UnSet
)
1333 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1334 else if (size
>= 0 || raiddisks
)
1335 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1336 size
, level
, layout
, chunk
, raiddisks
);
1337 else if (bitmap_file
) {
1338 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1339 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1340 bitmap_chunk
, delay
, write_behind
, force
);
1342 fprintf(stderr
, Name
": no changes to --grow\n");
1350 fprintf(stderr
, Name
1351 ": --incremental --scan meaningless without --run.\n");
1354 rv
= IncrementalScan(verbose
);
1357 if (!rebuild_map
&& !scan
) {
1358 fprintf(stderr
, Name
1359 ": --incremental requires a device.\n");
1364 if (devlist
->next
) {
1365 fprintf(stderr
, Name
1366 ": --incremental can only handle one device.\n");
1370 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1371 ss
, homehost
, autof
);