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
[])
49 long long array_size
= -1;
52 char *layout_str
= NULL
;
54 int max_disks
= MD_SB_DISKS
; /* just a default */
56 struct mddev_ident_s ident
;
57 char *configfile
= NULL
;
66 char *bitmap_file
= NULL
;
67 char *backup_file
= NULL
;
68 int bitmap_chunk
= UnSet
;
70 mddev_dev_t devlist
= NULL
;
71 mddev_dev_t
*devlistend
= & devlist
;
81 char *symlinks
= NULL
;
82 /* autof indicates whether and how to create device node.
83 * bottom 3 bits are style. Rest (when shifted) are number of parts
85 * 1 - don't create (no)
86 * 2 - if is_standard, then create (yes)
87 * 3 - create as 'md' - reject is_standard mdp (md)
88 * 4 - create as 'mdp' - reject is_standard md (mdp)
89 * 5 - default to md if not is_standard (md in config file)
90 * 6 - default to mdp if not is_standard (part, or mdp in config file)
94 char *homehost
= NULL
;
95 char sys_hostname
[256];
96 int require_homehost
= 1;
97 char *mailaddr
= NULL
;
101 char *pidfile
= NULL
;
103 struct supertype
*ss
= NULL
;
106 char *shortopt
= short_options
;
109 int auto_update_home
= 0;
116 srandom(time(0) ^ getpid());
120 ident
.raid_disks
= UnSet
;
121 ident
.super_minor
= UnSet
;
123 ident
.spare_group
= NULL
;
126 ident
.bitmap_fd
= -1;
127 ident
.bitmap_file
= NULL
;
129 ident
.container
= NULL
;
132 while ((option_index
= -1) ,
133 (opt
=getopt_long(argc
, argv
,
134 shortopt
, long_options
,
135 &option_index
)) != -1) {
137 /* firstly, some mode-independent options */
140 if (option_index
> 0 &&
141 strcmp(long_options
[option_index
].name
, "help-options")==0)
148 fputs(Version
, stderr
);
158 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
)
159 break; /* b means bitmap */
162 fprintf(stderr
, Name
": -b cannot have any extra immediately after it, sorry.\n");
171 if (strcasecmp(optarg
, "<ignore>") == 0)
172 require_homehost
= 0;
179 fputs(Usage
, stderr
);
182 /* second, figure out the mode.
183 * Some options force the mode. Others
184 * set the mode if it isn't already
188 case '@': /* just incase they say --manage */
190 shortopt
= short_bitmap_options
;
195 case ReAdd
: /* re-add */
198 shortopt
= short_bitmap_options
;
202 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
203 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
204 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
205 case 'F': newmode
= MONITOR
;break;
206 case 'G': newmode
= GROW
;
207 shortopt
= short_bitmap_options
;
209 case 'I': newmode
= INCREMENTAL
;
210 shortopt
= short_bitmap_auto_options
; break;
212 newmode
= AUTODETECT
; break;
218 case 'Q': newmode
= MISC
; break;
226 case 'K': if (!mode
) newmode
= MISC
; break;
228 if (mode
&& newmode
== mode
) {
229 /* everybody happy ! */
230 } else if (mode
&& newmode
!= mode
) {
232 fprintf(stderr
, Name
": ");
233 if (option_index
>= 0)
234 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
236 fprintf(stderr
, "-%c", opt
);
237 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
238 map_num(modes
, newmode
),
239 map_num(modes
, mode
));
241 } else if (!mode
&& newmode
) {
244 /* special case of -c --help */
246 ( strncmp(optarg
, "--h", 3)==0 ||
247 strncmp(optarg
, "-h", 2)==0)) {
248 fputs(Help_config
, stdout
);
252 /* If first option is a device, don't force the mode yet */
254 if (devs_found
== 0) {
255 dv
= malloc(sizeof(*dv
));
257 fprintf(stderr
, Name
": malloc failed\n");
260 dv
->devname
= optarg
;
261 dv
->disposition
= devmode
;
262 dv
->writemostly
= writemostly
;
268 devlistend
= &dv
->next
;
273 /* No mode yet, and this is the second device ... */
274 fprintf(stderr
, Name
": An option must be given to set the mode before a second device is listed\n");
277 if (option_index
>= 0)
278 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
280 fprintf(stderr
, Name
": -%c", opt
);
281 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
285 /* if we just set the mode, then done */
299 /* an undecorated option - must be a device name.
301 if (devs_found
> 0 && mode
== '@' && !devmode
) {
302 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
305 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
306 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
309 dv
= malloc(sizeof(*dv
));
311 fprintf(stderr
, Name
": malloc failed\n");
314 dv
->devname
= optarg
;
315 dv
->disposition
= devmode
;
316 dv
->writemostly
= writemostly
;
322 devlistend
= &dv
->next
;
328 /* We've got a mode, and opt is now something else which
329 * could depend on the mode */
330 #define O(a,b) ((a<<8)|b)
331 switch (O(mode
,opt
)) {
334 case O(BUILD
,'c'): /* chunk or rounding */
336 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
337 "Second value is %s.\n", optarg
);
340 chunk
= strtol(optarg
, &c
, 10);
341 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
342 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
348 case O(ASSEMBLE
,AutoHomeHost
):
349 auto_update_home
= 1;
351 case O(INCREMENTAL
, 'e'):
353 case O(ASSEMBLE
,'e'):
354 case O(MISC
,'e'): /* set metadata (superblock) information */
356 fprintf(stderr
, Name
": metadata information already given\n");
359 for(i
=0; !ss
&& superlist
[i
]; i
++)
360 ss
= superlist
[i
]->match_metadata_desc(optarg
);
363 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
366 max_disks
= ss
->max_devs
;
372 /* set write-mostly for following devices */
377 /* clear write-mostly for following devices */
384 case O(BUILD
,'z'): /* size */
386 fprintf(stderr
, Name
": size may only be specified once. "
387 "Second value is %s.\n", optarg
);
390 if (strcmp(optarg
, "max")==0)
393 size
= parse_size(optarg
);
395 fprintf(stderr
, Name
": invalid size: %s\n",
399 /* convert sectors to K */
404 case O(GROW
,'Z'): /* array size */
405 if (array_size
>= 0) {
406 fprintf(stderr
, Name
": array-size may only be specified once. "
407 "Second value is %s.\n", optarg
);
410 if (strcmp(optarg
, "max") == 0)
413 array_size
= parse_size(optarg
);
414 if (array_size
<= 0) {
415 fprintf(stderr
, Name
": invalid array size: %s\n",
424 case O(BUILD
,'l'): /* set raid level*/
425 if (level
!= UnSet
) {
426 fprintf(stderr
, Name
": raid level may only be set once. "
427 "Second value is %s.\n", optarg
);
430 level
= map_name(pers
, optarg
);
431 if (level
== UnSet
) {
432 fprintf(stderr
, Name
": invalid raid level: %s\n",
436 if (level
!= 0 && level
!= -1 && level
!= 1 && level
!= -4 && level
!= -5 && mode
== BUILD
) {
437 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
441 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
442 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
449 case O(GROW
, 'p'): /* new layout */
451 fprintf(stderr
,Name
": layout may only be sent once. "
452 "Second value was %s\n", optarg
);
456 /* 'Grow' will parse the value */
459 case O(CREATE
,'p'): /* raid5 layout */
460 case O(BUILD
,'p'): /* faulty layout */
461 if (layout
!= UnSet
) {
462 fprintf(stderr
,Name
": layout may only be sent once. "
463 "Second value was %s\n", optarg
);
468 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
469 map_num(pers
, level
));
472 fprintf(stderr
, Name
": raid level must be given before layout.\n");
476 layout
= map_name(r5layout
, optarg
);
478 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
484 layout
= map_name(r6layout
, optarg
);
486 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
493 layout
= parse_layout_10(optarg
);
495 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
503 layout
= parse_layout_faulty(optarg
);
505 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
513 case O(CREATE
,AssumeClean
):
514 case O(BUILD
,AssumeClean
): /* assume clean */
520 case O(BUILD
,'n'): /* number of raid disks */
522 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
526 raiddisks
= strtol(optarg
, &c
, 10);
527 if (!optarg
[0] || *c
|| raiddisks
<=0) {
528 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
532 ident
.raid_disks
= raiddisks
;
535 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
537 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
541 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
542 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
546 sparedisks
= strtol(optarg
, &c
, 10);
547 if (!optarg
[0] || *c
|| sparedisks
< 0) {
548 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
556 case O(INCREMENTAL
,'a'):
557 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
558 autof
= parse_auto(optarg
, "--auto flag", 0);
561 case O(CREATE
,Symlinks
):
562 case O(BUILD
,Symlinks
):
563 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
567 case O(BUILD
,'f'): /* force honouring '-n 1' */
568 case O(GROW
,'f'): /* ditto */
569 case O(CREATE
,'f'): /* force honouring of device list */
570 case O(ASSEMBLE
,'f'): /* force assembly */
571 case O(MISC
,'f'): /* force zero */
575 /* now for the Assemble options */
576 case O(CREATE
,'u'): /* uuid of array */
577 case O(ASSEMBLE
,'u'): /* uuid of array */
578 if (ident
.uuid_set
) {
579 fprintf(stderr
, Name
": uuid cannot be set twice. "
580 "Second value %s.\n", optarg
);
583 if (parse_uuid(optarg
, ident
.uuid
))
586 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
592 case O(ASSEMBLE
,'N'):
594 fprintf(stderr
, Name
": name cannot be set twice. "
595 "Second value %s.\n", optarg
);
598 if (strlen(optarg
) > 32) {
599 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
603 strcpy(ident
.name
, optarg
);
606 case O(ASSEMBLE
,'m'): /* super-minor for array */
607 if (ident
.super_minor
!= UnSet
) {
608 fprintf(stderr
, Name
": super-minor cannot be set twice. "
609 "Second value: %s.\n", optarg
);
612 if (strcmp(optarg
, "dev")==0)
613 ident
.super_minor
= -2;
615 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
616 if (!optarg
[0] || *cp
) {
617 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
623 case O(ASSEMBLE
,'U'): /* update the superblock */
625 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
630 if (strcmp(update
, "sparc2.2")==0)
632 if (strcmp(update
, "super-minor") == 0)
634 if (strcmp(update
, "summaries")==0)
636 if (strcmp(update
, "resync")==0)
638 if (strcmp(update
, "uuid")==0)
640 if (strcmp(update
, "name")==0)
642 if (strcmp(update
, "homehost")==0)
644 if (strcmp(update
, "devicesize")==0)
646 if (strcmp(update
, "byteorder")==0) {
648 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
651 for(i
=0; !ss
&& superlist
[i
]; i
++)
652 ss
= superlist
[i
]->match_metadata_desc("0.swap");
654 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
660 if (strcmp(update
,"?") == 0 ||
661 strcmp(update
, "help") == 0) {
663 fprintf(outf
, Name
": ");
667 Name
": '--update=%s' is invalid. ",
670 fprintf(outf
, "Valid --update options are:\n"
671 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
672 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
673 exit(outf
== stdout
? 0 : 2);
675 case O(INCREMENTAL
,NoDegraded
):
676 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
677 runstop
= -1; /* --stop isn't allowed for --assemble,
678 * so we overload slightly */
681 case O(ASSEMBLE
,'c'): /* config file */
682 case O(INCREMENTAL
, 'c'):
686 fprintf(stderr
, Name
": configfile cannot be set twice. "
687 "Second value is %s.\n", optarg
);
691 set_conffile(configfile
);
692 /* FIXME possibly check that config file exists. Even parse it */
694 case O(ASSEMBLE
,'s'): /* scan */
697 case O(INCREMENTAL
,'s'):
701 case O(MONITOR
,'m'): /* mail address */
703 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
709 case O(MONITOR
,'p'): /* alert program */
711 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
717 case O(MONITOR
,'d'): /* delay in seconds */
719 case O(BUILD
,'d'): /* delay for bitmap updates */
722 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
725 delay
= strtol(optarg
, &c
, 10);
726 if (!optarg
[0] || *c
|| delay
<1) {
727 fprintf(stderr
, Name
": invalid delay: %s\n",
733 case O(MONITOR
,'f'): /* daemonise */
736 case O(MONITOR
,'i'): /* pid */
738 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
743 case O(MONITOR
,'1'): /* oneshot */
746 case O(MONITOR
,'t'): /* test */
749 case O(MONITOR
,'y'): /* log messages to syslog */
750 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
754 /* now the general management options. Some are applicable
755 * to other modes. None have arguments.
758 case O(MANAGE
,'a'): /* add a drive */
762 case O(MANAGE
,ReAdd
):
766 case O(MANAGE
,'r'): /* remove a drive */
769 case O(MANAGE
,'f'): /* set faulty */
772 case O(INCREMENTAL
,'R'):
774 case O(ASSEMBLE
,'R'):
776 case O(CREATE
,'R'): /* Run the array */
778 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
785 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
801 case O(MISC
, Waitclean
):
802 case O(MISC
, DetailPlatform
):
803 if (devmode
&& devmode
!= opt
&&
804 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
805 fprintf(stderr
, Name
": --examine/-E cannot be given with -%c\n",
806 devmode
=='E'?opt
:devmode
);
815 case O(MISC
, Sparc22
):
816 if (devmode
!= 'E') {
817 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
823 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
825 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
828 if (strcmp(optarg
, "internal")==0) {
829 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
832 bitmap_fd
= open(optarg
, O_RDWR
);
833 if (!*optarg
|| bitmap_fd
< 0) {
834 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
837 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
840 case O(ASSEMBLE
, BackupFile
):
841 case O(GROW
, BackupFile
):
842 /* Specify a file into which grow might place a backup,
843 * or from which assemble might recover a backup
846 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
849 backup_file
= optarg
;
853 case O(CREATE
,'b'): /* here we create the bitmap */
854 if (strcmp(optarg
, "none") == 0) {
855 fprintf(stderr
, Name
": '--bitmap none' only"
856 " support for --grow\n");
861 if (strcmp(optarg
, "internal")== 0 ||
862 strcmp(optarg
, "none")== 0 ||
863 strchr(optarg
, '/') != NULL
) {
864 bitmap_file
= optarg
;
868 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n");
871 case O(GROW
,BitmapChunk
):
872 case O(BUILD
,BitmapChunk
):
873 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
874 bitmap_chunk
= strtol(optarg
, &c
, 10);
875 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
876 bitmap_chunk
& (bitmap_chunk
- 1)) {
877 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
881 /* convert K to B, chunk of 0K means 512B */
882 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
885 case O(GROW
, WriteBehind
):
886 case O(BUILD
, WriteBehind
):
887 case O(CREATE
, WriteBehind
): /* write-behind mode */
888 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
890 write_behind
= strtol(optarg
, &c
, 10);
891 if (write_behind
< 0 || *c
||
892 write_behind
> 16383) {
893 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
899 case O(INCREMENTAL
, 'r'):
903 /* We have now processed all the valid options. Anything else is
906 if (option_index
> 0)
907 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
908 long_options
[option_index
].name
,
909 map_num(modes
, mode
));
911 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
912 opt
, map_num(modes
, mode
));
918 char *help_text
= Help
;
920 help_text
= OptionHelp
;
923 case ASSEMBLE
: help_text
= Help_assemble
; break;
924 case BUILD
: help_text
= Help_build
; break;
925 case CREATE
: help_text
= Help_create
; break;
926 case MANAGE
: help_text
= Help_manage
; break;
927 case MISC
: help_text
= Help_misc
; break;
928 case MONITOR
: help_text
= Help_monitor
; break;
929 case GROW
: help_text
= Help_grow
; break;
930 case INCREMENTAL
:help_text
= Help_incr
; break;
932 fputs(help_text
,stdout
);
936 if (!mode
&& devs_found
) {
939 if (devlist
->disposition
== 0)
940 devlist
->disposition
= devmode
;
943 fputs(Usage
, stderr
);
948 struct createinfo
*ci
= conf_get_create_info();
950 if (strcasecmp(symlinks
, "yes") == 0)
952 else if (strcasecmp(symlinks
, "no") == 0)
955 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
959 /* Ok, got the option parsing out of the way
960 * hopefully it's mostly right but there might be some stuff
963 * That is mosty checked in the per-mode stuff but...
965 * For @,B,C and A without -s, the first device listed must be an md device
966 * we check that here and open it.
969 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
970 (mode
== ASSEMBLE
&& ! scan
)) {
971 if (devs_found
< 1) {
972 fprintf(stderr
, Name
": an md device must be given in this mode\n");
975 if ((int)ident
.super_minor
== -2 && autof
) {
976 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
979 if (mode
== MANAGE
|| mode
== GROW
) {
980 mdfd
= open_mddev(devlist
->devname
, 1);
984 /* non-existent device is OK */
985 mdfd
= open_mddev(devlist
->devname
, 0);
987 fprintf(stderr
, Name
": device %s exists but is not an "
988 "md array.\n", devlist
->devname
);
991 if ((int)ident
.super_minor
== -2) {
994 fprintf(stderr
, Name
": --super-minor=dev given, and "
995 "listed device %s doesn't exist.\n",
1000 ident
.super_minor
= minor(stb
.st_rdev
);
1002 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1003 /* We don't really want this open yet, we just might
1004 * have wanted to check some things
1012 if (raiddisks
> max_disks
) {
1013 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1017 if (raiddisks
== 1 && !force
&& level
!= -5) {
1018 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1019 " a mistake. If you really mean it you will need to specify --force before\n"
1020 " setting the number of drives.\n");
1025 if ( sparedisks
> max_disks
- raiddisks
) {
1026 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1032 if (homehost
== NULL
)
1033 homehost
= conf_get_homehost(&require_homehost
);
1034 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1035 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1036 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1037 homehost
= sys_hostname
;
1041 ident
.autof
= autof
;
1046 /* readonly, add/remove, readwrite, runstop */
1048 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1049 if (!rv
&& devs_found
>1)
1050 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1051 devlist
->next
, verbose
-quiet
);
1052 if (!rv
&& readonly
< 0)
1053 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1055 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1058 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1059 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1060 /* Only a device has been given, so get details from config file */
1061 mddev_ident_t array_ident
= conf_get_ident(devlist
->devname
);
1062 if (array_ident
== NULL
) {
1063 fprintf(stderr
, Name
": %s not identified in config file.\n",
1069 if (array_ident
->autof
== 0)
1070 array_ident
->autof
= autof
;
1071 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1073 readonly
, runstop
, update
,
1074 homehost
, require_homehost
,
1075 verbose
-quiet
, force
);
1078 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1079 devlist
->next
, backup_file
,
1080 readonly
, runstop
, update
,
1081 homehost
, require_homehost
,
1082 verbose
-quiet
, force
);
1083 else if (devs_found
>0) {
1084 if (update
&& devs_found
> 1) {
1085 fprintf(stderr
, Name
": can only update a single array at a time\n");
1088 if (backup_file
&& devs_found
> 1) {
1089 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1092 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1093 mddev_ident_t array_ident
= conf_get_ident(dv
->devname
);
1094 if (array_ident
== NULL
) {
1095 fprintf(stderr
, Name
": %s not identified in config file.\n",
1100 if (array_ident
->autof
== 0)
1101 array_ident
->autof
= autof
;
1102 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1104 readonly
, runstop
, update
,
1105 homehost
, require_homehost
,
1106 verbose
-quiet
, force
);
1109 mddev_ident_t array_list
= conf_get_ident(NULL
);
1110 mddev_dev_t devlist
= conf_get_devs();
1112 if (devlist
== NULL
) {
1113 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1117 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1121 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1124 for (; array_list
; array_list
= array_list
->next
) {
1125 if (array_list
->devname
&&
1126 strcasecmp(array_list
->devname
, "<ignore>") == 0)
1128 if (array_list
->autof
== 0)
1129 array_list
->autof
= autof
;
1131 rv
|= Assemble(ss
, array_list
->devname
,
1134 readonly
, runstop
, NULL
,
1135 homehost
, require_homehost
,
1136 verbose
-quiet
, force
);
1139 if (homehost
&& cnt
== 0) {
1140 /* Maybe we can auto-assemble something.
1141 * Repeatedly call Assemble in auto-assemble mode
1146 ident
.autof
= autof
;
1148 mddev_dev_t devlist
= conf_get_devs();
1151 rv2
= Assemble(ss
, NULL
,
1154 readonly
, runstop
, NULL
,
1155 homehost
, require_homehost
,
1156 verbose
-quiet
, force
);
1162 /* found something so even though assembly failed we
1163 * want to avoid auto-updates
1165 auto_update_home
= 0;
1167 /* Incase there are stacked devices, we need to go around again */
1169 if (cnt
== 0 && auto_update_home
&& homehost
) {
1170 /* Nothing found, maybe we need to bootstrap homehost info */
1174 rv2
= Assemble(ss
, NULL
,
1177 readonly
, runstop
, "homehost",
1178 homehost
, require_homehost
,
1179 verbose
-quiet
, force
);
1185 /* Incase there are stacked devices, we need to go around again */
1188 if (cnt
== 0 && rv
== 0) {
1189 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1193 } else if (cnt
== 0 && rv
== 0) {
1194 fprintf(stderr
, Name
": No arrays found in config file\n");
1200 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1201 if (write_behind
&& !bitmap_file
) {
1202 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1206 if (raiddisks
== 0) {
1207 fprintf(stderr
, Name
": no raid-devices specified.\n");
1213 if (strcmp(bitmap_file
, "internal")==0) {
1214 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1219 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1220 raiddisks
, devlist
->next
, assume_clean
,
1221 bitmap_file
, bitmap_chunk
, write_behind
,
1222 delay
, verbose
-quiet
, autof
, size
);
1225 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1226 if (write_behind
&& !bitmap_file
) {
1227 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1231 if (raiddisks
== 0) {
1232 fprintf(stderr
, Name
": no raid-devices specified.\n");
1237 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1238 raiddisks
, sparedisks
, ident
.name
, homehost
,
1239 ident
.uuid_set
? ident
.uuid
: NULL
,
1240 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1241 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1244 if (devmode
== 'E') {
1245 if (devlist
== NULL
&& !scan
) {
1246 fprintf(stderr
, Name
": No devices to examine\n");
1249 if (devlist
== NULL
)
1250 devlist
= conf_get_devs();
1251 if (devlist
== NULL
) {
1252 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1255 if (brief
&& verbose
)
1257 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1259 SparcAdjust
, ss
, homehost
);
1260 } else if (devmode
== DetailPlatform
) {
1261 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1263 if (devlist
== NULL
) {
1264 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1265 /* apply --detail or --wait-clean to
1266 * all devices in /proc/mdstat
1268 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1269 struct mdstat_ent
*e
;
1270 struct map_ent
*map
= NULL
;
1271 int v
= verbose
>1?0:verbose
+1;
1273 for (e
=ms
; e
; e
=e
->next
) {
1276 me
= map_by_devnum(&map
, e
->devnum
);
1278 && strcmp(me
->path
, "/unknown") != 0)
1281 name
= get_md_name(e
->devnum
);
1284 fprintf(stderr
, Name
": cannot find device file for %s\n",
1289 rv
|= Detail(name
, v
,
1293 rv
|= WaitClean(name
, v
);
1297 } else if (devmode
== 'S' && scan
) {
1298 /* apply --stop to all devices in /proc/mdstat */
1299 /* Due to possible stacking of devices, repeat until
1300 * nothing more can be stopped
1302 int progress
=1, err
;
1305 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1306 struct mdstat_ent
*e
;
1308 if (!progress
) last
= 1;
1309 progress
= 0; err
= 0;
1310 for (e
=ms
; e
; e
=e
->next
) {
1311 char *name
= get_md_name(e
->devnum
);
1314 fprintf(stderr
, Name
": cannot find device file for %s\n",
1318 mdfd
= open_mddev(name
, 1);
1320 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1330 } while (!last
&& err
);
1333 fprintf(stderr
, Name
": No devices given.\n");
1337 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1338 switch(dv
->disposition
) {
1340 rv
|= Detail(dv
->devname
,
1342 export
, test
, homehost
);
1344 case 'K': /* Zero superblock */
1345 rv
|= Kill(dv
->devname
, force
, quiet
,0);
1348 rv
|= Query(dv
->devname
); continue;
1350 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1352 rv
|= Wait(dv
->devname
); continue;
1354 rv
|= WaitClean(dv
->devname
, verbose
-quiet
); continue;
1356 mdfd
= open_mddev(dv
->devname
, 1);
1358 switch(dv
->disposition
) {
1360 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1362 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1364 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1366 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1375 if (!devlist
&& !scan
) {
1376 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1380 if (pidfile
&& !daemonise
) {
1381 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1385 rv
= Monitor(devlist
, mailaddr
, program
,
1386 delay
?delay
:60, daemonise
, scan
, oneshot
,
1387 dosyslog
, test
, pidfile
);
1391 if (array_size
>= 0) {
1392 /* alway impose array size first, independent of
1397 sysfs_init(&sra
, mdfd
, 0);
1398 if (array_size
== 0)
1399 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1401 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1404 fprintf(stderr
, Name
": --array-size setting"
1405 " is too large.\n");
1407 fprintf(stderr
, Name
": current kernel does"
1408 " not support setting --array-size\n");
1413 if (devs_found
> 1) {
1416 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1417 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1421 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1422 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1426 } else if (bitmap_file
) {
1427 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1428 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1433 delay
= DEFAULT_BITMAP_DELAY
;
1434 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1435 bitmap_chunk
, delay
, write_behind
, force
);
1436 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1437 || chunk
!= 0 || level
!= UnSet
) {
1438 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1439 size
, level
, layout_str
, chunk
, raiddisks
);
1440 } else if (array_size
< 0)
1441 fprintf(stderr
, Name
": no changes to --grow\n");
1449 fprintf(stderr
, Name
1450 ": --incremental --scan meaningless without --run.\n");
1453 rv
= IncrementalScan(verbose
);
1456 if (!rebuild_map
&& !scan
) {
1457 fprintf(stderr
, Name
1458 ": --incremental requires a device.\n");
1463 if (devlist
->next
) {
1464 fprintf(stderr
, Name
1465 ": --incremental can only handle one device.\n");
1469 rv
= Incremental(devlist
->devname
, verbose
-quiet
, runstop
,
1470 ss
, homehost
, require_homehost
, autof
);