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_auto_options
;
188 case ReAdd
: /* re-add */
191 shortopt
= short_bitmap_auto_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
; shortopt
= short_bitmap_auto_options
; break;
200 case 'I': newmode
= INCREMENTAL
; break;
202 newmode
= AUTODETECT
; break;
208 case 'Q': newmode
= MISC
; break;
214 case 'K': if (!mode
) newmode
= MISC
; break;
216 if (mode
&& newmode
== mode
) {
217 /* everybody happy ! */
218 } else if (mode
&& newmode
!= mode
) {
220 fprintf(stderr
, Name
": ");
221 if (option_index
>= 0)
222 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
224 fprintf(stderr
, "-%c", opt
);
225 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
226 map_num(modes
, newmode
),
227 map_num(modes
, mode
));
229 } else if (!mode
&& newmode
) {
232 /* special case of -c --help */
234 ( strncmp(optarg
, "--h", 3)==0 ||
235 strncmp(optarg
, "-h", 2)==0)) {
236 fputs(Help_config
, stdout
);
240 /* If first option is a device, don't force the mode yet */
242 if (devs_found
== 0) {
243 dv
= malloc(sizeof(*dv
));
245 fprintf(stderr
, Name
": malloc failed\n");
248 dv
->devname
= optarg
;
249 dv
->disposition
= devmode
;
250 dv
->writemostly
= writemostly
;
255 devlistend
= &dv
->next
;
260 /* No mode yet, and this is the second device ... */
261 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
264 if (option_index
>= 0)
265 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
267 fprintf(stderr
, Name
": -%c", opt
);
268 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
272 /* if we just set the mode, then done */
286 /* an undecorated option - must be a device name.
288 if (devs_found
> 0 && mode
== '@' && !devmode
) {
289 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
292 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
293 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
296 dv
= malloc(sizeof(*dv
));
298 fprintf(stderr
, Name
": malloc failed\n");
301 dv
->devname
= optarg
;
302 dv
->disposition
= devmode
;
303 dv
->writemostly
= writemostly
;
307 devlistend
= &dv
->next
;
313 /* We've got a mode, and opt is now something else which
314 * could depend on the mode */
315 #define O(a,b) ((a<<8)|b)
316 switch (O(mode
,opt
)) {
318 case O(BUILD
,'c'): /* chunk or rounding */
320 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
321 "Second value is %s.\n", optarg
);
324 chunk
= strtol(optarg
, &c
, 10);
325 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
326 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
332 case O(ASSEMBLE
,AutoHomeHost
):
333 auto_update_home
= 1;
335 case O(INCREMENTAL
, 'e'):
337 case O(ASSEMBLE
,'e'):
338 case O(MISC
,'e'): /* set metadata (superblock) information */
340 fprintf(stderr
, Name
": metadata information already given\n");
343 for(i
=0; !ss
&& superlist
[i
]; i
++)
344 ss
= superlist
[i
]->match_metadata_desc(optarg
);
347 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
350 max_disks
= ss
->max_devs
;
356 /* set write-mostly for following devices */
361 case O(CREATE
,'z'): /* size */
363 fprintf(stderr
, Name
": size may only be specified once. "
364 "Second value is %s.\n", optarg
);
367 if (strcmp(optarg
, "max")==0)
370 size
= strtoll(optarg
, &c
, 10);
371 if (!optarg
[0] || *c
|| size
< 4) {
372 fprintf(stderr
, Name
": invalid size: %s\n",
379 case O(GROW
,'l'): /* hack - needed to understand layout */
381 case O(BUILD
,'l'): /* set raid level*/
382 if (level
!= UnSet
) {
383 fprintf(stderr
, Name
": raid level may only be set once. "
384 "Second value is %s.\n", optarg
);
387 level
= map_name(pers
, optarg
);
388 if (level
== UnSet
) {
389 fprintf(stderr
, Name
": invalid raid level: %s\n",
393 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
394 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
398 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
399 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
406 case O(CREATE
,'p'): /* raid5 layout */
407 case O(BUILD
,'p'): /* faulty layout */
408 case O(GROW
, 'p'): /* faulty reconfig */
409 if (layout
!= UnSet
) {
410 fprintf(stderr
,Name
": layout may only be sent once. "
411 "Second value was %s\n", optarg
);
416 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
417 map_num(pers
, level
));
420 fprintf(stderr
, Name
": raid level must be given before layout.\n");
425 layout
= map_name(r5layout
, optarg
);
427 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
434 /* 'f', 'o' or 'n' followed by a number <= raid_disks */
435 if ((optarg
[0] != 'n' && optarg
[0] != 'f' && optarg
[0] != 'o') ||
436 (copies
= strtoul(optarg
+1, &cp
, 10)) < 1 ||
439 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
442 if (optarg
[0] == 'n')
443 layout
= 256 + copies
;
444 else if (optarg
[0] == 'o')
445 layout
= 0x10000 + (copies
<<8) + 1;
447 layout
= 1 + (copies
<<8);
454 int ln
= strcspn(optarg
, "0123456789");
455 char *m
= strdup(optarg
);
458 mode
= map_name(faultylayout
, m
);
460 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
464 layout
= mode
| (atoi(optarg
+ln
)<< ModeShift
);
469 case O(CREATE
,AssumeClean
):
470 case O(BUILD
,AssumeClean
): /* assume clean */
476 case O(BUILD
,'n'): /* number of raid disks */
478 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
482 raiddisks
= strtol(optarg
, &c
, 10);
483 if (!optarg
[0] || *c
|| raiddisks
<=0) {
484 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
488 ident
.raid_disks
= raiddisks
;
491 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
493 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
497 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
498 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
502 sparedisks
= strtol(optarg
, &c
, 10);
503 if (!optarg
[0] || *c
|| sparedisks
< 0) {
504 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
512 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
513 autof
= parse_auto(optarg
, "--auto flag", 0);
516 case O(CREATE
,Symlinks
):
517 case O(BUILD
,Symlinks
):
518 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
522 case O(BUILD
,'f'): /* force honouring '-n 1' */
523 case O(GROW
,'f'): /* ditto */
524 case O(CREATE
,'f'): /* force honouring of device list */
525 case O(ASSEMBLE
,'f'): /* force assembly */
526 case O(MISC
,'f'): /* force zero */
530 /* now for the Assemble options */
531 case O(CREATE
,'u'): /* uuid of array */
532 case O(ASSEMBLE
,'u'): /* uuid of array */
533 if (ident
.uuid_set
) {
534 fprintf(stderr
, Name
": uuid cannot be set twice. "
535 "Second value %s.\n", optarg
);
538 if (parse_uuid(optarg
, ident
.uuid
))
541 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
547 case O(ASSEMBLE
,'N'):
549 fprintf(stderr
, Name
": name cannot be set twice. "
550 "Second value %s.\n", optarg
);
553 if (strlen(optarg
) > 32) {
554 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
558 strcpy(ident
.name
, optarg
);
561 case O(ASSEMBLE
,'m'): /* super-minor for array */
562 if (ident
.super_minor
!= UnSet
) {
563 fprintf(stderr
, Name
": super-minor cannot be set twice. "
564 "Second value: %s.\n", optarg
);
567 if (strcmp(optarg
, "dev")==0)
568 ident
.super_minor
= -2;
570 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
571 if (!optarg
[0] || *cp
) {
572 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
578 case O(ASSEMBLE
,'U'): /* update the superblock */
580 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
585 if (strcmp(update
, "sparc2.2")==0)
587 if (strcmp(update
, "super-minor") == 0)
589 if (strcmp(update
, "summaries")==0)
591 if (strcmp(update
, "resync")==0)
593 if (strcmp(update
, "uuid")==0)
595 if (strcmp(update
, "name")==0)
597 if (strcmp(update
, "homehost")==0)
599 if (strcmp(update
, "devicesize")==0)
601 if (strcmp(update
, "byteorder")==0) {
603 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
606 for(i
=0; !ss
&& superlist
[i
]; i
++)
607 ss
= superlist
[i
]->match_metadata_desc("0.swap");
609 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
615 if (strcmp(update
,"?") == 0 ||
616 strcmp(update
, "help") == 0) {
618 fprintf(outf
, Name
": ");
622 Name
": '--update=%s' is invalid. ",
625 fprintf(outf
, "Valid --update options are:\n"
626 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
627 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
628 exit(outf
== stdout
? 0 : 2);
630 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
631 runstop
= -1; /* --stop isn't allowed for --assemble,
632 * so we overload slightly */
635 case O(ASSEMBLE
,'c'): /* config file */
639 fprintf(stderr
, Name
": configfile cannot be set twice. "
640 "Second value is %s.\n", optarg
);
644 set_conffile(configfile
);
645 /* FIXME possibly check that config file exists. Even parse it */
647 case O(ASSEMBLE
,'s'): /* scan */
650 case O(INCREMENTAL
,'s'):
654 case O(MONITOR
,'m'): /* mail address */
656 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
662 case O(MONITOR
,'p'): /* alert program */
664 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
670 case O(MONITOR
,'d'): /* delay in seconds */
672 case O(BUILD
,'d'): /* delay for bitmap updates */
675 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
678 delay
= strtol(optarg
, &c
, 10);
679 if (!optarg
[0] || *c
|| delay
<1) {
680 fprintf(stderr
, Name
": invalid delay: %s\n",
686 case O(MONITOR
,'f'): /* daemonise */
689 case O(MONITOR
,'i'): /* pid */
691 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
696 case O(MONITOR
,'1'): /* oneshot */
699 case O(MONITOR
,'t'): /* test */
702 case O(MONITOR
,'y'): /* log messages to syslog */
703 openlog("mdadm", 0, SYSLOG_FACILITY
);
707 /* now the general management options. Some are applicable
708 * to other modes. None have arguments.
711 case O(MANAGE
,'a'): /* add a drive */
715 case O(MANAGE
,ReAdd
):
719 case O(MANAGE
,'r'): /* remove a drive */
722 case O(MANAGE
,'f'): /* set faulty */
725 case O(INCREMENTAL
,'R'):
727 case O(ASSEMBLE
,'R'):
729 case O(CREATE
,'R'): /* Run the array */
731 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
738 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
746 fprintf(stderr
, Name
": Cannot have both readonly and readwrite\n");
753 fprintf(stderr
, Name
": Cannot have both readwrite and readonly.\n");
769 if (devmode
&& devmode
!= opt
&&
770 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
771 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
772 devmode
=='E'?opt
:devmode
);
781 case O(MISC
, Sparc22
):
782 if (devmode
!= 'E') {
783 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
789 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
791 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
794 if (strcmp(optarg
, "internal")==0) {
795 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
798 bitmap_fd
= open(optarg
, O_RDWR
);
799 if (!*optarg
|| bitmap_fd
< 0) {
800 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
803 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
806 case O(ASSEMBLE
, BackupFile
):
807 case O(GROW
, BackupFile
):
808 /* Specify a file into which grow might place a backup,
809 * or from which assemble might recover a backup
812 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
815 backup_file
= optarg
;
820 case O(CREATE
,'b'): /* here we create the bitmap */
821 if (strcmp(optarg
, "internal")== 0 ||
822 strcmp(optarg
, "none")== 0 ||
823 strchr(optarg
, '/') != NULL
) {
824 bitmap_file
= optarg
;
828 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
831 case O(GROW
,BitmapChunk
):
832 case O(BUILD
,BitmapChunk
):
833 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
834 bitmap_chunk
= strtol(optarg
, &c
, 10);
835 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
836 bitmap_chunk
& (bitmap_chunk
- 1)) {
837 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
841 /* convert K to B, chunk of 0K means 512B */
842 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
845 case O(BUILD
, WriteBehind
):
846 case O(CREATE
, WriteBehind
): /* write-behind mode */
847 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
849 write_behind
= strtol(optarg
, &c
, 10);
850 if (write_behind
< 0 || *c
||
851 write_behind
> 16383) {
852 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
858 case O(INCREMENTAL
, 'r'):
862 /* We have now processed all the valid options. Anything else is
865 if (option_index
> 0)
866 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
867 long_options
[option_index
].name
,
868 map_num(modes
, mode
));
870 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
871 opt
, map_num(modes
, mode
));
877 char *help_text
= Help
;
879 help_text
= OptionHelp
;
882 case ASSEMBLE
: help_text
= Help_assemble
; break;
883 case BUILD
: help_text
= Help_build
; break;
884 case CREATE
: help_text
= Help_create
; break;
885 case MANAGE
: help_text
= Help_manage
; break;
886 case MISC
: help_text
= Help_misc
; break;
887 case MONITOR
: help_text
= Help_monitor
; break;
888 case GROW
: help_text
= Help_grow
; break;
889 case INCREMENTAL
:help_text
= Help_incr
; break;
891 fputs(help_text
,stdout
);
895 if (!mode
&& devs_found
) {
898 if (devlist
->disposition
== 0)
899 devlist
->disposition
= devmode
;
902 fputs(Usage
, stderr
);
907 struct createinfo
*ci
= conf_get_create_info();
909 if (strcasecmp(symlinks
, "yes") == 0)
911 else if (strcasecmp(symlinks
, "no") == 0)
914 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
918 /* Ok, got the option parsing out of the way
919 * hopefully it's mostly right but there might be some stuff
922 * That is mosty checked in the per-mode stuff but...
924 * For @,B,C and A without -s, the first device listed must be an md device
925 * we check that here and open it.
928 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
929 (mode
== ASSEMBLE
&& ! scan
)) {
930 if (devs_found
< 1) {
931 fprintf(stderr
, Name
": an md device must be given in this mode\n");
934 if ((int)ident
.super_minor
== -2 && autof
) {
935 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
938 if (mode
== MANAGE
|| mode
== GROW
)
939 autof
=1; /* Don't create */
940 mdfd
= open_mddev(devlist
->devname
, autof
);
943 if ((int)ident
.super_minor
== -2) {
946 ident
.super_minor
= minor(stb
.st_rdev
);
951 if (raiddisks
> max_disks
) {
952 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
956 if (raiddisks
== 1 && !force
&& level
!= -5) {
957 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
958 " a mistake. If you really mean it you will need to specify --force before\n"
959 " setting the number of drives.\n");
964 if ( sparedisks
> max_disks
- raiddisks
) {
965 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
971 if (homehost
== NULL
)
972 homehost
= conf_get_homehost();
973 if (homehost
&& strcmp(homehost
, "<system>")==0) {
974 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
975 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
976 homehost
= sys_hostname
;
983 /* readonly, add/remove, readwrite, runstop */
985 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
986 if (!rv
&& devs_found
>1)
987 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
988 devlist
->next
, verbose
-quiet
);
989 if (!rv
&& readonly
< 0)
990 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
992 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
995 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
996 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
997 /* Only a device has been given, so get details from config file */
998 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
999 if (array_ident
== NULL
) {
1000 fprintf(stderr
, Name
": %s not identified in config file.\n",
1004 mdfd
= open_mddev(devlist
->devname
,
1005 array_ident
->autof
? array_ident
->autof
: autof
);
1009 rv
|= Assemble(ss
, devlist
->devname
, mdfd
, array_ident
,
1011 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1016 rv
= Assemble(ss
, devlist
->devname
, mdfd
, &ident
,
1017 devlist
->next
, backup_file
,
1018 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1019 else if (devs_found
>0) {
1020 if (update
&& devs_found
> 1) {
1021 fprintf(stderr
, Name
": can only update a single array at a time\n");
1024 if (backup_file
&& devs_found
> 1) {
1025 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1028 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1029 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1030 if (array_ident
== NULL
) {
1031 fprintf(stderr
, Name
": %s not identified in config file.\n",
1036 mdfd
= open_mddev(dv
->devname
,
1037 array_ident
->autof
?array_ident
->autof
: autof
);
1042 rv
|= Assemble(ss
, dv
->devname
, mdfd
, array_ident
,
1044 readonly
, runstop
, update
, homehost
, verbose
-quiet
, force
);
1048 mddev_ident_t array_list
= conf_get_ident(NULL
);
1049 mddev_dev_t devlist
= conf_get_devs();
1051 if (devlist
== NULL
) {
1052 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1056 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1060 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1063 for (; array_list
; array_list
= array_list
->next
) {
1064 mdu_array_info_t array
;
1065 mdfd
= open_mddev(array_list
->devname
,
1066 array_list
->autof
? array_list
->autof
: autof
);
1071 if (ioctl(mdfd
, GET_ARRAY_INFO
, &array
)>=0)
1072 /* already assembled, skip */
1075 rv
|= Assemble(ss
, array_list
->devname
, mdfd
,
1078 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1084 /* Maybe we can auto-assemble something.
1085 * Repeatedly call Assemble in auto-assmble mode
1090 ident
.autof
= autof
;
1094 rv2
= Assemble(ss
, NULL
, -1,
1097 readonly
, runstop
, NULL
, homehost
, verbose
-quiet
, force
);
1103 /* found something so even though assembly failed we
1104 * want to avoid auto-updates
1106 auto_update_home
= 0;
1108 /* Incase there are stacked devices, we need to go around again */
1110 if (cnt
== 0 && auto_update_home
&& homehost
) {
1111 /* Nothing found, maybe we need to bootstrap homehost info */
1115 rv2
= Assemble(ss
, NULL
, -1,
1118 readonly
, runstop
, "homehost", homehost
, verbose
-quiet
, force
);
1124 /* Incase there are stacked devices, we need to go around again */
1127 if (cnt
== 0 && rv
== 0) {
1128 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1131 } else if (cnt
== 0 && rv
== 0) {
1132 fprintf(stderr
, Name
": No arrays found in config file\n");
1138 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1139 if (write_behind
&& !bitmap_file
) {
1140 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1146 if (strcmp(bitmap_file
, "internal")==0) {
1147 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1152 rv
= Build(devlist
->devname
, mdfd
, chunk
, level
, layout
,
1153 raiddisks
, devlist
->next
, assume_clean
,
1154 bitmap_file
, bitmap_chunk
, write_behind
, delay
, verbose
-quiet
);
1157 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1158 if (write_behind
&& !bitmap_file
) {
1159 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1164 rv
= Create(ss
, devlist
->devname
, mdfd
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1165 raiddisks
, sparedisks
, ident
.name
, homehost
,
1166 ident
.uuid_set
? ident
.uuid
: NULL
,
1167 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1168 bitmap_file
, bitmap_chunk
, write_behind
, delay
);
1171 if (devmode
== 'E') {
1172 if (devlist
== NULL
&& !scan
) {
1173 fprintf(stderr
, Name
": No devices to examine\n");
1176 if (devlist
== NULL
)
1177 devlist
= conf_get_devs();
1178 if (devlist
== NULL
) {
1179 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1182 if (brief
&& verbose
)
1184 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
, scan
, SparcAdjust
, ss
, homehost
);
1186 if (devlist
== NULL
) {
1187 if (devmode
=='D' && scan
) {
1188 /* apply --detail to all devices in /proc/mdstat */
1189 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1190 struct mdstat_ent
*e
;
1191 for (e
=ms
; e
; e
=e
->next
) {
1192 char *name
= get_md_name(e
->devnum
);
1195 fprintf(stderr
, Name
": cannot find device file for %s\n",
1199 rv
|= Detail(name
, verbose
>1?0:verbose
+1,
1200 export
, test
, homehost
);
1203 } else if (devmode
== 'S' && scan
) {
1204 /* apply --stop to all devices in /proc/mdstat */
1205 /* Due to possible stacking of devices, repeat until
1206 * nothing more can be stopped
1208 int progress
=1, err
;
1211 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1212 struct mdstat_ent
*e
;
1214 if (!progress
) last
= 1;
1215 progress
= 0; err
= 0;
1216 for (e
=ms
; e
; e
=e
->next
) {
1217 char *name
= get_md_name(e
->devnum
);
1220 fprintf(stderr
, Name
": cannot find device file for %s\n",
1224 mdfd
= open_mddev(name
, 1);
1226 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1235 } while (!last
&& err
);
1238 fprintf(stderr
, Name
": No devices given.\n");
1242 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1243 switch(dv
->disposition
) {
1245 rv
|= Detail(dv
->devname
,
1247 export
, test
, homehost
);
1249 case 'K': /* Zero superblock */
1250 rv
|= Kill(dv
->devname
, force
, quiet
); continue;
1252 rv
|= Query(dv
->devname
); continue;
1254 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1256 rv
|= Wait(dv
->devname
); continue;
1258 mdfd
= open_mddev(dv
->devname
, 1);
1260 switch(dv
->disposition
) {
1262 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1264 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1266 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1268 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1277 if (!devlist
&& !scan
) {
1278 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1282 if (pidfile
&& !daemonise
) {
1283 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1287 rv
= Monitor(devlist
, mailaddr
, program
,
1288 delay
?delay
:60, daemonise
, scan
, oneshot
,
1289 dosyslog
, test
, pidfile
);
1293 if (devs_found
> 1) {
1296 if (size
>= 0 || raiddisks
) {
1297 fprintf(stderr
, Name
": --size, --raiddisks, and --add are exclusing in --grow mode\n");
1301 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1302 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1306 } else if ((size
>= 0) + (raiddisks
!= 0) + (layout
!= UnSet
) + (bitmap_file
!= NULL
)> 1) {
1307 fprintf(stderr
, Name
": can change at most one of size, raiddisks, bitmap, and layout\n");
1310 } else if (layout
!= UnSet
)
1311 rv
= Manage_reconfig(devlist
->devname
, mdfd
, layout
);
1312 else if (size
>= 0 || raiddisks
)
1313 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1314 size
, level
, layout
, chunk
, raiddisks
);
1315 else if (bitmap_file
) {
1316 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1317 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1318 bitmap_chunk
, delay
, write_behind
, force
);
1320 fprintf(stderr
, Name
": no changes to --grow\n");
1328 fprintf(stderr
, Name
1329 ": --incremental --scan meaningless without --run.\n");
1332 rv
= IncrementalScan(verbose
);
1335 if (!rebuild_map
&& !scan
) {
1336 fprintf(stderr
, Name
1337 ": --incremental requires a device.\n");
1342 if (devlist
->next
) {
1343 fprintf(stderr
, Name
1344 ": --incremental can only handle one device.\n");
1348 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1349 ss
, homehost
, autof
);