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
;
78 char *symlinks
= NULL
;
79 /* autof indicates whether and how to create device node.
80 * bottom 3 bits are style. Rest (when shifted) are number of parts
82 * 1 - don't create (no)
83 * 2 - if is_standard, then create (yes)
84 * 3 - create as 'md' - reject is_standard mdp (md)
85 * 4 - create as 'mdp' - reject is_standard md (mdp)
86 * 5 - default to md if not is_standard (md in config file)
87 * 6 - default to mdp if not is_standard (part, or mdp in config file)
91 char *homehost
= NULL
;
92 char sys_hostname
[256];
93 char *mailaddr
= NULL
;
99 struct supertype
*ss
= NULL
;
102 char *shortopt
= short_options
;
105 int auto_update_home
= 0;
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
;
127 while ((option_index
= -1) ,
128 (opt
=getopt_long(argc
, argv
,
129 shortopt
, long_options
,
130 &option_index
)) != -1) {
132 /* firstly, some mode-independant options */
135 if (option_index
> 0 &&
136 strcmp(long_options
[option_index
].name
, "help-options")==0)
143 fputs(Version
, stderr
);
153 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
154 break; /* b means bitmap */
157 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
168 fputs(Usage
, stderr
);
171 /* second, figure out the mode.
172 * Some options force the mode. Others
173 * set the mode if it isn't already
177 case '@': /* just incase they say --manage */
179 shortopt
= short_bitmap_auto_options
;
184 case ReAdd
: /* re-add */
187 shortopt
= short_bitmap_auto_options
;
191 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
192 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
193 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
194 case 'F': newmode
= MONITOR
;break;
195 case 'G': newmode
= GROW
; shortopt
= short_bitmap_auto_options
; break;
196 case 'I': newmode
= INCREMENTAL
; break;
202 case 'Q': newmode
= MISC
; break;
208 case 'K': if (!mode
) newmode
= MISC
; break;
210 if (mode
&& newmode
== mode
) {
211 /* everybody happy ! */
212 } else if (mode
&& newmode
!= mode
) {
214 fprintf(stderr
, Name
": ");
215 if (option_index
>= 0)
216 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
218 fprintf(stderr
, "-%c", opt
);
219 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
220 map_num(modes
, newmode
),
221 map_num(modes
, mode
));
223 } else if (!mode
&& newmode
) {
226 /* special case of -c --help */
228 ( strncmp(optarg
, "--h", 3)==0 ||
229 strncmp(optarg
, "-h", 2)==0)) {
230 fputs(Help_config
, stdout
);
234 /* If first option is a device, don't force the mode yet */
236 if (devs_found
== 0) {
237 dv
= malloc(sizeof(*dv
));
239 fprintf(stderr
, Name
": malloc failed\n");
242 dv
->devname
= optarg
;
243 dv
->disposition
= devmode
;
244 dv
->writemostly
= writemostly
;
249 devlistend
= &dv
->next
;
254 /* No mode yet, and this is the second device ... */
255 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
258 if (option_index
>= 0)
259 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
261 fprintf(stderr
, Name
": -%c", opt
);
262 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
266 /* if we just set the mode, then done */
279 /* an undecorated option - must be a device name.
281 if (devs_found
> 0 && mode
== '@' && !devmode
) {
282 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
285 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
286 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
289 dv
= malloc(sizeof(*dv
));
291 fprintf(stderr
, Name
": malloc failed\n");
294 dv
->devname
= optarg
;
295 dv
->disposition
= devmode
;
296 dv
->writemostly
= writemostly
;
300 devlistend
= &dv
->next
;
306 /* We've got a mode, and opt is now something else which
307 * could depend on the mode */
308 #define O(a,b) ((a<<8)|b)
309 switch (O(mode
,opt
)) {
311 case O(BUILD
,'c'): /* chunk or rounding */
313 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
314 "Second value is %s.\n", optarg
);
317 chunk
= strtol(optarg
, &c
, 10);
318 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
319 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
325 case O(ASSEMBLE
,AutoHomeHost
):
326 auto_update_home
= 1;
328 case O(INCREMENTAL
, 'e'):
330 case O(ASSEMBLE
,'e'):
331 case O(MISC
,'e'): /* set metadata (superblock) information */
333 fprintf(stderr
, Name
": metadata information already given\n");
336 for(i
=0; !ss
&& superlist
[i
]; i
++)
337 ss
= superlist
[i
]->match_metadata_desc(optarg
);
340 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
343 max_disks
= ss
->max_devs
;
349 /* set write-mostly for following devices */
354 case O(CREATE
,'z'): /* size */
356 fprintf(stderr
, Name
": size may only be specified once. "
357 "Second value is %s.\n", optarg
);
360 if (strcmp(optarg
, "max")==0)
363 size
= strtoll(optarg
, &c
, 10);
364 if (!optarg
[0] || *c
|| size
< 4) {
365 fprintf(stderr
, Name
": invalid size: %s\n",
372 case O(GROW
,'l'): /* hack - needed to understand layout */
374 case O(BUILD
,'l'): /* set raid level*/
375 if (level
!= UnSet
) {
376 fprintf(stderr
, Name
": raid level may only be set once. "
377 "Second value is %s.\n", optarg
);
380 level
= map_name(pers
, optarg
);
381 if (level
== UnSet
) {
382 fprintf(stderr
, Name
": invalid raid level: %s\n",
386 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
387 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
391 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
392 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
399 case O(CREATE
,'p'): /* raid5 layout */
400 case O(BUILD
,'p'): /* faulty layout */
401 case O(GROW
, 'p'): /* faulty reconfig */
402 if (layout
!= UnSet
) {
403 fprintf(stderr
,Name
": layout may only be sent once. "
404 "Second value was %s\n", optarg
);
409 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
410 map_num(pers
, level
));
413 fprintf(stderr
, Name
": raid level must be given before layout.\n");
418 layout
= map_name(r5layout
, optarg
);
420 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
427 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
428 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
429 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
432 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
435 if (optarg
[0] == 'n')
436 layout
= 256 + copies
;
437 else if (optarg
[0] == 'o')
438 layout
= 0x10000 + (copies
<<8) + 1;
440 layout
= 1 + (copies
<<8);
447 int ln
= strcspn(optarg
, "0123456789");
448 char *m
= strdup(optarg
);
451 mode
= map_name(faultylayout
, m
);
453 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
457 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
462 case O(CREATE
,AssumeClean
):
463 case O(BUILD
,AssumeClean
): /* assume clean */
469 case O(BUILD
,'n'): /* number of raid disks */
471 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
475 raiddisks
= strtol(optarg
, &c
, 10);
476 if (!optarg
[0] || *c
|| raiddisks
<=0) {
477 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
481 ident
.raid_disks
= raiddisks
;
484 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
486 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
490 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
491 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
495 sparedisks
= strtol(optarg
, &c
, 10);
496 if (!optarg
[0] || *c
|| sparedisks
< 0) {
497 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
505 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
506 autof
= parse_auto(optarg
, "--auto flag", 0);
509 case O(CREATE
,Symlinks
):
510 case O(BUILD
,Symlinks
):
511 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
515 case O(BUILD
,'f'): /* force honouring '-n 1' */
516 case O(GROW
,'f'): /* ditto */
517 case O(CREATE
,'f'): /* force honouring of device list */
518 case O(ASSEMBLE
,'f'): /* force assembly */
519 case O(MISC
,'f'): /* force zero */
523 /* now for the Assemble options */
524 case O(CREATE
,'u'): /* uuid of array */
525 case O(ASSEMBLE
,'u'): /* uuid of array */
526 if (ident
.uuid_set
) {
527 fprintf(stderr
, Name
": uuid cannot be set twice. "
528 "Second value %s.\n", optarg
);
531 if (parse_uuid(optarg
, ident
.uuid
))
534 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
540 case O(ASSEMBLE
,'N'):
542 fprintf(stderr
, Name
": name cannot be set twice. "
543 "Second value %s.\n", optarg
);
546 if (strlen(optarg
) > 32) {
547 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
551 strcpy(ident
.name
, optarg
);
554 case O(ASSEMBLE
,'m'): /* super-minor for array */
555 if (ident
.super_minor
!= UnSet
) {
556 fprintf(stderr
, Name
": super-minor cannot be set twice. "
557 "Second value: %s.\n", optarg
);
560 if (strcmp(optarg
, "dev")==0)
561 ident
.super_minor
= -2;
563 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
564 if (!optarg
[0] || *cp
) {
565 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
571 case O(ASSEMBLE
,'U'): /* update the superblock */
573 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
578 if (strcmp(update
, "sparc2.2")==0)
580 if (strcmp(update
, "super-minor") == 0)
582 if (strcmp(update
, "summaries")==0)
584 if (strcmp(update
, "resync")==0)
586 if (strcmp(update
, "uuid")==0)
588 if (strcmp(update
, "name")==0)
590 if (strcmp(update
, "homehost")==0)
592 if (strcmp(update
, "devicesize")==0)
594 if (strcmp(update
, "byteorder")==0) {
596 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
599 for(i
=0; !ss
&& superlist
[i
]; i
++)
600 ss
= superlist
[i
]->match_metadata_desc("0.swap");
602 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
608 if (strcmp(update
,"?") == 0 ||
609 strcmp(update
, "help") == 0) {
611 fprintf(outf
, Name
": ");
615 Name
": '--update=%s' is invalid. ",
618 fprintf(outf
, "Valid --update options are:\n"
619 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
620 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
621 exit(outf
== stdout
? 0 : 2);
623 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
624 runstop
= -1; /* --stop isn't allowed for --assemble,
625 * so we overload slightly */
628 case O(ASSEMBLE
,'c'): /* config file */
632 fprintf(stderr
, Name
": configfile cannot be set twice. "
633 "Second value is %s.\n", optarg
);
637 set_conffile(configfile
);
638 /* FIXME possibly check that config file exists. Even parse it */
640 case O(ASSEMBLE
,'s'): /* scan */
643 case O(INCREMENTAL
,'s'):
647 case O(MONITOR
,'m'): /* mail address */
649 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
655 case O(MONITOR
,'p'): /* alert program */
657 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
663 case O(MONITOR
,'d'): /* delay in seconds */
665 case O(BUILD
,'d'): /* delay for bitmap updates */
668 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
671 delay
= strtol(optarg
, &c
, 10);
672 if (!optarg
[0] || *c
|| delay
<1) {
673 fprintf(stderr
, Name
": invalid delay: %s\n",
679 case O(MONITOR
,'f'): /* daemonise */
682 case O(MONITOR
,'i'): /* pid */
684 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
689 case O(MONITOR
,'1'): /* oneshot */
692 case O(MONITOR
,'t'): /* test */
695 case O(MONITOR
,'y'): /* log messages to syslog */
696 openlog("mdadm", 0, SYSLOG_FACILITY
);
700 /* now the general management options. Some are applicable
701 * to other modes. None have arguments.
704 case O(MANAGE
,'a'): /* add a drive */
708 case O(MANAGE
,ReAdd
):
712 case O(MANAGE
,'r'): /* remove a drive */
715 case O(MANAGE
,'f'): /* set faulty */
718 case O(INCREMENTAL
,'R'):
720 case O(ASSEMBLE
,'R'):
722 case O(CREATE
,'R'): /* Run the array */
724 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
731 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
739 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
746 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
762 if (devmode
&& devmode
!= opt
&&
763 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
764 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
765 devmode
=='E'?opt
:devmode
);
774 case O(MISC
, Sparc22
):
775 if (devmode
!= 'E') {
776 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
782 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
784 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
787 if (strcmp(optarg
, "internal")==0) {
788 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
791 bitmap_fd
= open(optarg
, O_RDWR
);
792 if (!*optarg
|| bitmap_fd
< 0) {
793 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
796 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
799 case O(ASSEMBLE
, BackupFile
):
800 case O(GROW
, BackupFile
):
801 /* Specify a file into which grow might place a backup,
802 * or from which assemble might recover a backup
805 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
808 backup_file
= optarg
;
813 case O(CREATE
,'b'): /* here we create the bitmap */
814 if (strcmp(optarg
, "internal")== 0 ||
815 strcmp(optarg
, "none")== 0 ||
816 strchr(optarg
, '/') != NULL
) {
817 bitmap_file
= optarg
;
821 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
824 case O(GROW
,BitmapChunk
):
825 case O(BUILD
,BitmapChunk
):
826 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
827 bitmap_chunk
= strtol(optarg
, &c
, 10);
828 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
829 bitmap_chunk
& (bitmap_chunk
- 1)) {
830 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
834 /* convert K to B, chunk of 0K means 512B */
835 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
838 case O(BUILD
, WriteBehind
):
839 case O(CREATE
, WriteBehind
): /* write-behind mode */
840 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
842 write_behind
= strtol(optarg
, &c
, 10);
843 if (write_behind
< 0 || *c
||
844 write_behind
> 16383) {
845 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
851 case O(INCREMENTAL
, 'r'):
855 /* We have now processed all the valid options. Anything else is
858 if (option_index
> 0)
859 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
860 long_options
[option_index
].name
,
861 map_num(modes
, mode
));
863 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
864 opt
, map_num(modes
, mode
));
870 char *help_text
= Help
;
872 help_text
= OptionHelp
;
875 case ASSEMBLE
: help_text
= Help_assemble
; break;
876 case BUILD
: help_text
= Help_build
; break;
877 case CREATE
: help_text
= Help_create
; break;
878 case MANAGE
: help_text
= Help_manage
; break;
879 case MISC
: help_text
= Help_misc
; break;
880 case MONITOR
: help_text
= Help_monitor
; break;
881 case GROW
: help_text
= Help_grow
; break;
882 case INCREMENTAL
:help_text
= Help_incr
; break;
884 fputs(help_text
,stdout
);
888 if (!mode
&& devs_found
) {
891 if (devlist
->disposition
== 0)
892 devlist
->disposition
= devmode
;
895 fputs(Usage
, stderr
);
900 struct createinfo
*ci
= conf_get_create_info();
902 if (strcasecmp(symlinks
, "yes") == 0)
904 else if (strcasecmp(symlinks
, "no") == 0)
907 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
911 /* Ok, got the option parsing out of the way
912 * hopefully it's mostly right but there might be some stuff
915 * That is mosty checked in the per-mode stuff but...
917 * For @,B,C and A without -s, the first device listed must be an md device
918 * we check that here and open it.
921 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
922 (mode
== ASSEMBLE
&& ! scan
)) {
923 if (devs_found
< 1) {
924 fprintf(stderr
, Name
": an md device must be given in this mode\n");
927 if ((int)ident
.super_minor
== -2 && autof
) {
928 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
931 if (mode
== MANAGE
|| mode
== GROW
)
932 autof
=1; /* Don't create */
933 mdfd
= open_mddev(devlist
->devname
, autof
);
936 if ((int)ident
.super_minor
== -2) {
939 ident
.super_minor
= minor(stb
.st_rdev
);
944 if (raiddisks
> max_disks
) {
945 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
949 if (raiddisks
== 1 && !force
&& level
!= -5) {
950 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
951 " a mistake. If you really mean it you will need to specify --force before\n"
952 " setting the number of drives.\n");
957 if ( sparedisks
> max_disks
- raiddisks
) {
958 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
964 if (homehost
== NULL
)
965 homehost
= conf_get_homehost();
966 if (homehost
&& strcmp(homehost
, "<system>")==0) {
967 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
968 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
969 homehost
= sys_hostname
;
976 /* readonly, add/remove, readwrite, runstop */
978 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
979 if (!rv
&& devs_found
>1)
980 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
981 devlist
->next
, verbose
-quiet
);
982 if (!rv
&& readonly
< 0)
983 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
985 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
988 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
989 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
990 /* Only a device has been given, so get details from config file */
991 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
992 if (array_ident
== NULL
) {
993 fprintf(stderr
, Name
": %s not identified in config file.\n",
997 mdfd
= open_mddev(devlist
->devname
,
998 array_ident
->autof
? array_ident
->autof
: autof
);
1002 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
,
1004 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1009 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
,
1010 devlist
->next
, backup_file
,
1011 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1012 else if (devs_found
>0) {
1013 if (update
&& devs_found
> 1) {
1014 fprintf(stderr
, Name
": can only update a single array at a time\n");
1017 if (backup_file
&& devs_found
> 1) {
1018 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1021 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1022 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1023 if (array_ident
== NULL
) {
1024 fprintf(stderr
, Name
": %s not identified in config file.\n",
1029 mdfd
= open_mddev(dv
->devname
,
1030 array_ident
->autof
?array_ident
->autof
: autof
);
1035 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
,
1037 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1041 mddev_ident_t array_list
= conf_get_ident(NULL
);
1042 mddev_dev_t devlist
= conf_get_devs();
1044 if (devlist
== NULL
) {
1045 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1049 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1053 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1056 for (; array_list
; array_list
= array_list
->next
) {
1057 mdu_array_info_t array
;
1058 mdfd
= open_mddev(array_list
->devname
,
1059 array_list
->autof
? array_list
->autof
: autof
);
1064 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
1065 /* already assembled, skip */
1068 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
1071 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1077 /* Maybe we can auto-assemble something.
1078 * Repeatedly call Assemble in auto-assmble mode
1083 ident
.autof
= autof
;
1087 rv2
= Assemble(ss
, NULL
, -1,
1090 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1096 /* found something so even though assembly failed we
1097 * want to avoid auto-updates
1099 auto_update_home
= 0;
1101 /* Incase there are stacked devices, we need to go around again */
1103 if (cnt
== 0 && auto_update_home
&& homehost
) {
1104 /* Nothing found, maybe we need to bootstrap homehost info */
1108 rv2
= Assemble(ss
, NULL
, -1,
1111 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1117 /* Incase there are stacked devices, we need to go around again */
1120 if (cnt
== 0 && rv
== 0) {
1121 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1124 } else if (cnt
== 0 && rv
== 0) {
1125 fprintf(stderr
, Name
": No arrays found in config file\n");
1131 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1132 if (write_behind
&& !bitmap_file
) {
1133 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1139 if (strcmp(bitmap_file
, "internal")==0) {
1140 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1145 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
,
1146 raiddisks
, devlist
->next
, assume_clean
,
1147 bitmap_file
, bitmap_chunk
, write_behind
, delay
, verbose
-quiet
);
1150 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1151 if (write_behind
&& !bitmap_file
) {
1152 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1157 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1158 raiddisks
, sparedisks
, ident
.name
, homehost
,
1159 ident
.uuid_set
? ident
.uuid
: NULL
,
1160 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1161 bitmap_file
, bitmap_chunk
, write_behind
, delay
);
1164 if (devmode
== 'E') {
1165 if (devlist
== NULL
&& !scan
) {
1166 fprintf(stderr
, Name
": No devices to examine\n");
1169 if (devlist
== NULL
)
1170 devlist
= conf_get_devs();
1171 if (devlist
== NULL
) {
1172 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1175 if (brief
&& verbose
)
1177 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
, scan
, SparcAdjust
, ss
, homehost
);
1179 if (devlist
== NULL
) {
1180 if (devmode
=='D' && scan
) {
1181 /* apply --detail to all devices in /proc/mdstat */
1182 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1183 struct mdstat_ent
*e
;
1184 for (e
=ms
; e
; e
=e
->next
) {
1185 char *name
= get_md_name(e
->devnum
);
1188 fprintf(stderr
, Name
": cannot find device file for %s\n",
1192 rv
|= Detail(name
, verbose
>1?0:verbose
+1, test
, homehost
);
1195 } else if (devmode
== 'S' && scan
) {
1196 /* apply --stop to all devices in /proc/mdstat */
1197 /* Due to possible stacking of devices, repeat until
1198 * nothing more can be stopped
1200 int progress
=1, err
;
1203 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1204 struct mdstat_ent
*e
;
1206 if (!progress
) last
= 1;
1207 progress
= 0; err
= 0;
1208 for (e
=ms
; e
; e
=e
->next
) {
1209 char *name
= get_md_name(e
->devnum
);
1212 fprintf(stderr
, Name
": cannot find device file for %s\n",
1216 mdfd
= open_mddev(name
, 1);
1218 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1227 } while (!last
&& err
);
1230 fprintf(stderr
, Name
": No devices given.\n");
1234 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1235 switch(dv
->disposition
) {
1237 rv
|= Detail(dv
->devname
, brief
?1+verbose
:0, test
, homehost
); continue;
1238 case 'K': /* Zero superblock */
1239 rv
|= Kill(dv
->devname
, force
, quiet
); continue;
1241 rv
|= Query(dv
->devname
); continue;
1243 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1245 rv
|= Wait(dv
->devname
); continue;
1247 mdfd
= open_mddev(dv
->devname
, 1);
1249 switch(dv
->disposition
) {
1251 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1253 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1255 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1257 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1266 if (!devlist
&& !scan
) {
1267 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1271 if (pidfile
&& !daemonise
) {
1272 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1276 rv
= Monitor(devlist
, mailaddr
, program
,
1277 delay
?delay
:60, daemonise
, scan
, oneshot
,
1278 dosyslog
, test
, pidfile
);
1282 if (devs_found
> 1) {
1285 if (size
>= 0 || raiddisks
) {
1286 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1290 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1291 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1295 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1296 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1299 } else if (layout
!= UnSet
)
1300 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1301 else if (size
>= 0 || raiddisks
)
1302 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1303 size
, level
, layout
, chunk
, raiddisks
);
1304 else if (bitmap_file
) {
1305 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1306 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1307 bitmap_chunk
, delay
, write_behind
, force
);
1309 fprintf(stderr
, Name
": no changes to --grow\n");
1317 fprintf(stderr
, Name
1318 ": --incremental --scan meaningless without --run.\n");
1321 rv
= IncrementalScan(verbose
);
1324 if (!rebuild_map
&& !scan
) {
1325 fprintf(stderr
, Name
1326 ": --incremental requires a device.\n");
1331 if (devlist
->next
) {
1332 fprintf(stderr
, Name
1333 ": --incremental can only handle one device.\n");
1337 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1338 ss
, homehost
, autof
);