2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 Neil Brown <neilb@suse.de>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Email: <neilb@suse.de>
24 * Additions for bitmap and write-behind RAID options, Copyright (C) 2003-2004,
25 * Paul Clements, SteelEye Technology, Inc.
33 int main(int argc
, char *argv
[])
44 long long array_size
= -1;
47 char *layout_str
= NULL
;
49 int max_disks
= MD_SB_DISKS
; /* just a default */
51 struct mddev_ident ident
;
52 char *configfile
= NULL
;
61 char *bitmap_file
= NULL
;
62 char *backup_file
= NULL
;
63 int bitmap_chunk
= UnSet
;
65 struct mddev_dev
*devlist
= NULL
;
66 struct mddev_dev
**devlistend
= & devlist
;
76 char *symlinks
= NULL
;
77 /* autof indicates whether and how to create device node.
78 * bottom 3 bits are style. Rest (when shifted) are number of parts
80 * 1 - don't create (no)
81 * 2 - if is_standard, then create (yes)
82 * 3 - create as 'md' - reject is_standard mdp (md)
83 * 4 - create as 'mdp' - reject is_standard md (mdp)
84 * 5 - default to md if not is_standard (md in config file)
85 * 6 - default to mdp if not is_standard (part, or mdp in config file)
89 char *homehost
= NULL
;
90 char sys_hostname
[256];
91 int require_homehost
= 1;
92 char *mailaddr
= NULL
;
99 int spare_sharing
= 1;
100 struct supertype
*ss
= NULL
;
103 char *shortopt
= short_options
;
106 int auto_update_home
= 0;
107 char *subarray
= NULL
;
108 char *remove_path
= NULL
;
115 srandom(time(0) ^ getpid());
119 ident
.raid_disks
= UnSet
;
120 ident
.super_minor
= UnSet
;
122 ident
.spare_group
= NULL
;
125 ident
.bitmap_fd
= -1;
126 ident
.bitmap_file
= NULL
;
128 ident
.container
= NULL
;
131 while ((option_index
= -1) ,
132 (opt
=getopt_long(argc
, argv
,
133 shortopt
, long_options
,
134 &option_index
)) != -1) {
136 /* firstly, some mode-independent options */
139 if (option_index
> 0 &&
140 strcmp(long_options
[option_index
].name
, "help-options")==0)
147 fputs(Version
, stderr
);
157 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
158 mode
== INCREMENTAL
|| mode
== MANAGE
)
159 break; /* b means bitmap */
167 if (strcasecmp(optarg
, "<ignore>") == 0)
168 require_homehost
= 0;
175 fputs(Usage
, stderr
);
178 /* second, figure out the mode.
179 * Some options force the mode. Others
180 * set the mode if it isn't already
184 case '@': /* just incase they say --manage */
186 shortopt
= short_bitmap_options
;
191 case ReAdd
: /* re-add */
194 shortopt
= short_bitmap_options
;
198 case 'A': newmode
= ASSEMBLE
; shortopt
= short_bitmap_auto_options
; break;
199 case 'B': newmode
= BUILD
; shortopt
= short_bitmap_auto_options
; break;
200 case 'C': newmode
= CREATE
; shortopt
= short_bitmap_auto_options
; break;
201 case 'F': newmode
= MONITOR
;break;
202 case 'G': newmode
= GROW
;
203 shortopt
= short_bitmap_options
;
205 case 'I': newmode
= INCREMENTAL
;
206 shortopt
= short_bitmap_auto_options
; break;
208 newmode
= AUTODETECT
; break;
214 case 'Q': newmode
= MISC
; break;
224 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
226 fprintf(stderr
, Name
": subarray can only be specified once\n");
231 case 'K': if (!mode
) newmode
= MISC
; break;
232 case NoSharing
: newmode
= MONITOR
; break;
234 if (mode
&& newmode
== mode
) {
235 /* everybody happy ! */
236 } else if (mode
&& newmode
!= mode
) {
238 fprintf(stderr
, Name
": ");
239 if (option_index
>= 0)
240 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
242 fprintf(stderr
, "-%c", opt
);
243 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
244 map_num(modes
, newmode
),
245 map_num(modes
, mode
));
247 } else if (!mode
&& newmode
) {
250 /* special case of -c --help */
252 ( strncmp(optarg
, "--h", 3)==0 ||
253 strncmp(optarg
, "-h", 2)==0)) {
254 fputs(Help_config
, stdout
);
258 /* If first option is a device, don't force the mode yet */
260 if (devs_found
== 0) {
261 dv
= malloc(sizeof(*dv
));
263 fprintf(stderr
, Name
": malloc failed\n");
266 dv
->devname
= optarg
;
267 dv
->disposition
= devmode
;
268 dv
->writemostly
= writemostly
;
273 devlistend
= &dv
->next
;
278 /* No mode yet, and this is the second device ... */
279 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
280 " (%s) is listed\n", optarg
);
283 if (option_index
>= 0)
284 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
286 fprintf(stderr
, Name
": -%c", opt
);
287 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
291 /* if we just set the mode, then done */
305 /* an undecorated option - must be a device name.
307 if (devs_found
> 0 && mode
== '@' && !devmode
) {
308 fprintf(stderr
, Name
": Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
311 if (devs_found
> 0 && mode
== 'G' && !devmode
) {
312 fprintf(stderr
, Name
": Must give one of -a for devices do add: %s\n", optarg
);
315 dv
= malloc(sizeof(*dv
));
317 fprintf(stderr
, Name
": malloc failed\n");
320 dv
->devname
= optarg
;
321 dv
->disposition
= devmode
;
322 dv
->writemostly
= writemostly
;
327 devlistend
= &dv
->next
;
333 /* We've got a mode, and opt is now something else which
334 * could depend on the mode */
335 #define O(a,b) ((a<<8)|b)
336 switch (O(mode
,opt
)) {
339 case O(BUILD
,'c'): /* chunk or rounding */
341 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
342 "Second value is %s.\n", optarg
);
345 chunk
= strtol(optarg
, &c
, 10);
346 if (!optarg
[0] || *c
|| chunk
<4 || ((chunk
-1)&chunk
)) {
347 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
354 case O(ASSEMBLE
,AutoHomeHost
):
355 auto_update_home
= 1;
358 case O(INCREMENTAL
, 'e'):
360 case O(ASSEMBLE
,'e'):
361 case O(MISC
,'e'): /* set metadata (superblock) information */
363 fprintf(stderr
, Name
": metadata information already given\n");
366 for(i
=0; !ss
&& superlist
[i
]; i
++)
367 ss
= superlist
[i
]->match_metadata_desc(optarg
);
370 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
373 max_disks
= ss
->max_devs
;
379 /* set write-mostly for following devices */
384 /* clear write-mostly for following devices */
391 case O(BUILD
,'z'): /* size */
393 fprintf(stderr
, Name
": size may only be specified once. "
394 "Second value is %s.\n", optarg
);
397 if (strcmp(optarg
, "max")==0)
400 size
= parse_size(optarg
);
402 fprintf(stderr
, Name
": invalid size: %s\n",
406 /* convert sectors to K */
411 case O(GROW
,'Z'): /* array size */
412 if (array_size
>= 0) {
413 fprintf(stderr
, Name
": array-size may only be specified once. "
414 "Second value is %s.\n", optarg
);
417 if (strcmp(optarg
, "max") == 0)
420 array_size
= parse_size(optarg
);
421 if (array_size
<= 0) {
422 fprintf(stderr
, Name
": invalid array size: %s\n",
431 case O(BUILD
,'l'): /* set raid level*/
432 if (level
!= UnSet
) {
433 fprintf(stderr
, Name
": raid level may only be set once. "
434 "Second value is %s.\n", optarg
);
437 level
= map_name(pers
, optarg
);
438 if (level
== UnSet
) {
439 fprintf(stderr
, Name
": invalid raid level: %s\n",
443 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
444 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
447 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
451 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
452 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
459 case O(GROW
, 'p'): /* new layout */
461 fprintf(stderr
,Name
": layout may only be sent once. "
462 "Second value was %s\n", optarg
);
466 /* 'Grow' will parse the value */
469 case O(CREATE
,'p'): /* raid5 layout */
470 case O(BUILD
,'p'): /* faulty layout */
471 if (layout
!= UnSet
) {
472 fprintf(stderr
,Name
": layout may only be sent once. "
473 "Second value was %s\n", optarg
);
478 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
479 map_num(pers
, level
));
482 fprintf(stderr
, Name
": raid level must be given before layout.\n");
486 layout
= map_name(r5layout
, optarg
);
488 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
494 layout
= map_name(r6layout
, optarg
);
496 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
503 layout
= parse_layout_10(optarg
);
505 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
513 layout
= parse_layout_faulty(optarg
);
515 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
523 case O(CREATE
,AssumeClean
):
524 case O(BUILD
,AssumeClean
): /* assume clean */
530 case O(BUILD
,'n'): /* number of raid disks */
532 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
536 raiddisks
= strtol(optarg
, &c
, 10);
537 if (!optarg
[0] || *c
|| raiddisks
<=0) {
538 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
542 ident
.raid_disks
= raiddisks
;
545 case O(CREATE
,'x'): /* number of spare (eXtra) discs */
547 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
551 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
552 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
556 sparedisks
= strtol(optarg
, &c
, 10);
557 if (!optarg
[0] || *c
|| sparedisks
< 0) {
558 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
566 case O(INCREMENTAL
,'a'):
567 case O(ASSEMBLE
,'a'): /* auto-creation of device node */
568 autof
= parse_auto(optarg
, "--auto flag", 0);
571 case O(CREATE
,Symlinks
):
572 case O(BUILD
,Symlinks
):
573 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
577 case O(BUILD
,'f'): /* force honouring '-n 1' */
578 case O(GROW
,'f'): /* ditto */
579 case O(CREATE
,'f'): /* force honouring of device list */
580 case O(ASSEMBLE
,'f'): /* force assembly */
581 case O(MISC
,'f'): /* force zero */
585 /* now for the Assemble options */
586 case O(CREATE
,'u'): /* uuid of array */
587 case O(ASSEMBLE
,'u'): /* uuid of array */
588 if (ident
.uuid_set
) {
589 fprintf(stderr
, Name
": uuid cannot be set twice. "
590 "Second value %s.\n", optarg
);
593 if (parse_uuid(optarg
, ident
.uuid
))
596 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
602 case O(ASSEMBLE
,'N'):
605 fprintf(stderr
, Name
": name cannot be set twice. "
606 "Second value %s.\n", optarg
);
609 if (mode
== MISC
&& !subarray
) {
610 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
613 if (strlen(optarg
) > 32) {
614 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
618 strcpy(ident
.name
, optarg
);
621 case O(ASSEMBLE
,'m'): /* super-minor for array */
622 if (ident
.super_minor
!= UnSet
) {
623 fprintf(stderr
, Name
": super-minor cannot be set twice. "
624 "Second value: %s.\n", optarg
);
627 if (strcmp(optarg
, "dev")==0)
628 ident
.super_minor
= -2;
630 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
631 if (!optarg
[0] || *cp
) {
632 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
638 case O(ASSEMBLE
,'U'): /* update the superblock */
641 fprintf(stderr
, Name
": Can only update one aspect of superblock, both %s and %s given.\n",
645 if (mode
== MISC
&& !subarray
) {
646 fprintf(stderr
, Name
": Only subarrays can be updated in misc mode\n");
650 if (strcmp(update
, "sparc2.2")==0)
652 if (strcmp(update
, "super-minor") == 0)
654 if (strcmp(update
, "summaries")==0)
656 if (strcmp(update
, "resync")==0)
658 if (strcmp(update
, "uuid")==0)
660 if (strcmp(update
, "name")==0)
662 if (strcmp(update
, "homehost")==0)
664 if (strcmp(update
, "devicesize")==0)
666 if (strcmp(update
, "byteorder")==0) {
668 fprintf(stderr
, Name
": must not set metadata type with --update=byteorder.\n");
671 for(i
=0; !ss
&& superlist
[i
]; i
++)
672 ss
= superlist
[i
]->match_metadata_desc("0.swap");
674 fprintf(stderr
, Name
": INTERNAL ERROR cannot find 0.swap\n");
680 if (strcmp(update
,"?") == 0 ||
681 strcmp(update
, "help") == 0) {
683 fprintf(outf
, Name
": ");
687 Name
": '--update=%s' is invalid. ",
690 fprintf(outf
, "Valid --update options are:\n"
691 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
692 " 'summaries', 'homehost', 'byteorder', 'devicesize'.\n");
693 exit(outf
== stdout
? 0 : 2);
695 case O(INCREMENTAL
,NoDegraded
):
696 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
697 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
698 runstop
= -1; /* --stop isn't allowed for --assemble,
699 * so we overload slightly */
702 case O(ASSEMBLE
,'c'): /* config file */
703 case O(INCREMENTAL
, 'c'):
707 fprintf(stderr
, Name
": configfile cannot be set twice. "
708 "Second value is %s.\n", optarg
);
712 set_conffile(configfile
);
713 /* FIXME possibly check that config file exists. Even parse it */
715 case O(ASSEMBLE
,'s'): /* scan */
718 case O(INCREMENTAL
,'s'):
722 case O(MONITOR
,'m'): /* mail address */
724 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
730 case O(MONITOR
,'p'): /* alert program */
732 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
738 case O(MONITOR
,'r'): /* rebuild increments */
739 increments
= atoi(optarg
);
740 if (increments
>99 || increments
<1) {
741 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
746 case O(MONITOR
,'d'): /* delay in seconds */
748 case O(BUILD
,'d'): /* delay for bitmap updates */
751 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
754 delay
= strtol(optarg
, &c
, 10);
755 if (!optarg
[0] || *c
|| delay
<1) {
756 fprintf(stderr
, Name
": invalid delay: %s\n",
762 case O(MONITOR
,'f'): /* daemonise */
765 case O(MONITOR
,'i'): /* pid */
767 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
772 case O(MONITOR
,'1'): /* oneshot */
775 case O(MONITOR
,'t'): /* test */
778 case O(MONITOR
,'y'): /* log messages to syslog */
779 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
782 case O(MONITOR
, NoSharing
):
785 /* now the general management options. Some are applicable
786 * to other modes. None have arguments.
789 case O(MANAGE
,'a'): /* add a drive */
793 case O(MANAGE
,ReAdd
):
797 case O(MANAGE
,'r'): /* remove a drive */
800 case O(MANAGE
,'f'): /* set faulty */
801 case O(INCREMENTAL
,'f'): /* r for incremental is taken, use f
802 * even though we will both fail and
803 * remove the device */
806 case O(INCREMENTAL
,'R'):
808 case O(ASSEMBLE
,'R'):
810 case O(CREATE
,'R'): /* Run the array */
812 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
819 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
838 case O(MISC
, Waitclean
):
839 case O(MISC
, DetailPlatform
):
840 case O(MISC
, KillSubarray
):
841 case O(MISC
, UpdateSubarray
):
842 if (devmode
&& devmode
!= opt
&&
843 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
844 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
845 if (devmode
== 'E') {
846 if (option_index
>= 0)
847 fprintf(stderr
, "--%s\n",
848 long_options
[option_index
].name
);
850 fprintf(stderr
, "-%c\n", opt
);
851 } else if (isalpha(devmode
))
852 fprintf(stderr
, "-%c\n", devmode
);
854 fprintf(stderr
, "previous option\n");
863 case O(MISC
, Sparc22
):
864 if (devmode
!= 'E') {
865 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
871 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
873 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
876 if (strcmp(optarg
, "internal")==0) {
877 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
880 bitmap_fd
= open(optarg
, O_RDWR
);
881 if (!*optarg
|| bitmap_fd
< 0) {
882 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
885 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
888 case O(ASSEMBLE
, BackupFile
):
889 case O(GROW
, BackupFile
):
890 /* Specify a file into which grow might place a backup,
891 * or from which assemble might recover a backup
894 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
897 backup_file
= optarg
;
901 case O(CREATE
,'b'): /* here we create the bitmap */
902 if (strcmp(optarg
, "none") == 0) {
903 fprintf(stderr
, Name
": '--bitmap none' only"
904 " support for --grow\n");
909 if (strcmp(optarg
, "internal")== 0 ||
910 strcmp(optarg
, "none")== 0 ||
911 strchr(optarg
, '/') != NULL
) {
912 bitmap_file
= optarg
;
916 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
917 " not '%s'\n", optarg
);
920 case O(GROW
,BitmapChunk
):
921 case O(BUILD
,BitmapChunk
):
922 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
923 bitmap_chunk
= strtol(optarg
, &c
, 10);
924 if (!optarg
[0] || *c
|| bitmap_chunk
< 0 ||
925 bitmap_chunk
& (bitmap_chunk
- 1)) {
926 fprintf(stderr
, Name
": invalid bitmap chunksize: %s\n",
930 /* convert K to B, chunk of 0K means 512B */
931 bitmap_chunk
= bitmap_chunk
? bitmap_chunk
* 1024 : 512;
934 case O(GROW
, WriteBehind
):
935 case O(BUILD
, WriteBehind
):
936 case O(CREATE
, WriteBehind
): /* write-behind mode */
937 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
939 write_behind
= strtol(optarg
, &c
, 10);
940 if (write_behind
< 0 || *c
||
941 write_behind
> 16383) {
942 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
948 case O(INCREMENTAL
, 'r'):
951 case O(INCREMENTAL
, IncrementalPath
):
952 remove_path
= optarg
;
955 /* We have now processed all the valid options. Anything else is
958 if (option_index
> 0)
959 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
960 long_options
[option_index
].name
,
961 map_num(modes
, mode
));
963 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
964 opt
, map_num(modes
, mode
));
970 char *help_text
= Help
;
972 help_text
= OptionHelp
;
975 case ASSEMBLE
: help_text
= Help_assemble
; break;
976 case BUILD
: help_text
= Help_build
; break;
977 case CREATE
: help_text
= Help_create
; break;
978 case MANAGE
: help_text
= Help_manage
; break;
979 case MISC
: help_text
= Help_misc
; break;
980 case MONITOR
: help_text
= Help_monitor
; break;
981 case GROW
: help_text
= Help_grow
; break;
982 case INCREMENTAL
:help_text
= Help_incr
; break;
984 fputs(help_text
,stdout
);
988 if (!mode
&& devs_found
) {
991 if (devlist
->disposition
== 0)
992 devlist
->disposition
= devmode
;
995 fputs(Usage
, stderr
);
1000 struct createinfo
*ci
= conf_get_create_info();
1002 if (strcasecmp(symlinks
, "yes") == 0)
1004 else if (strcasecmp(symlinks
, "no") == 0)
1007 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1011 /* Ok, got the option parsing out of the way
1012 * hopefully it's mostly right but there might be some stuff
1015 * That is mosty checked in the per-mode stuff but...
1017 * For @,B,C and A without -s, the first device listed must be an md device
1018 * we check that here and open it.
1021 if (mode
==MANAGE
|| mode
== BUILD
|| mode
== CREATE
|| mode
== GROW
||
1022 (mode
== ASSEMBLE
&& ! scan
)) {
1023 if (devs_found
< 1) {
1024 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1027 if ((int)ident
.super_minor
== -2 && autof
) {
1028 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1031 if (mode
== MANAGE
|| mode
== GROW
) {
1032 mdfd
= open_mddev(devlist
->devname
, 1);
1036 /* non-existent device is OK */
1037 mdfd
= open_mddev(devlist
->devname
, 0);
1039 fprintf(stderr
, Name
": device %s exists but is not an "
1040 "md array.\n", devlist
->devname
);
1043 if ((int)ident
.super_minor
== -2) {
1046 fprintf(stderr
, Name
": --super-minor=dev given, and "
1047 "listed device %s doesn't exist.\n",
1052 ident
.super_minor
= minor(stb
.st_rdev
);
1054 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1055 /* We don't really want this open yet, we just might
1056 * have wanted to check some things
1064 if (raiddisks
> max_disks
) {
1065 fprintf(stderr
, Name
": invalid number of raid devices: %d\n",
1069 if (raiddisks
== 1 && !force
&& level
!= -5) {
1070 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1071 " a mistake. If you really mean it you will need to specify --force before\n"
1072 " setting the number of drives.\n");
1077 if ( sparedisks
> max_disks
- raiddisks
) {
1078 fprintf(stderr
, Name
": invalid number of spare-devices: %d\n",
1084 if (homehost
== NULL
)
1085 homehost
= conf_get_homehost(&require_homehost
);
1086 if (homehost
== NULL
|| strcmp(homehost
, "<system>")==0) {
1087 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1088 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1089 homehost
= sys_hostname
;
1093 if ((mode
!= MISC
|| devmode
!= 'E') &&
1095 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1099 ident
.autof
= autof
;
1104 /* readonly, add/remove, readwrite, runstop */
1106 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1107 if (!rv
&& devs_found
>1)
1108 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1109 devlist
->next
, verbose
-quiet
, test
);
1110 if (!rv
&& readonly
< 0)
1111 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1113 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1116 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1117 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1118 /* Only a device has been given, so get details from config file */
1119 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1120 if (array_ident
== NULL
) {
1121 fprintf(stderr
, Name
": %s not identified in config file.\n",
1127 if (array_ident
->autof
== 0)
1128 array_ident
->autof
= autof
;
1129 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1131 readonly
, runstop
, update
,
1132 homehost
, require_homehost
,
1133 verbose
-quiet
, force
);
1136 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1137 devlist
->next
, backup_file
,
1138 readonly
, runstop
, update
,
1139 homehost
, require_homehost
,
1140 verbose
-quiet
, force
);
1141 else if (devs_found
>0) {
1142 if (update
&& devs_found
> 1) {
1143 fprintf(stderr
, Name
": can only update a single array at a time\n");
1146 if (backup_file
&& devs_found
> 1) {
1147 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1150 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1151 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1152 if (array_ident
== NULL
) {
1153 fprintf(stderr
, Name
": %s not identified in config file.\n",
1158 if (array_ident
->autof
== 0)
1159 array_ident
->autof
= autof
;
1160 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1162 readonly
, runstop
, update
,
1163 homehost
, require_homehost
,
1164 verbose
-quiet
, force
);
1167 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1168 struct mddev_dev
*devlist
= conf_get_devs();
1170 int failures
, successes
;
1171 if (devlist
== NULL
) {
1172 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1176 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1180 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1183 for (a
= array_list
; a
; a
= a
->next
) {
1192 for (a
= array_list
; a
; a
= a
->next
) {
1197 strcasecmp(a
->devname
, "<ignore>") == 0)
1200 r
= Assemble(ss
, a
->devname
,
1203 readonly
, runstop
, NULL
,
1204 homehost
, require_homehost
,
1205 verbose
-quiet
, force
);
1214 } while (failures
&& successes
);
1215 if (homehost
&& cnt
== 0) {
1216 /* Maybe we can auto-assemble something.
1217 * Repeatedly call Assemble in auto-assemble mode
1222 ident
.autof
= autof
;
1224 struct mddev_dev
*devlist
= conf_get_devs();
1227 rv2
= Assemble(ss
, NULL
,
1230 readonly
, runstop
, NULL
,
1231 homehost
, require_homehost
,
1232 verbose
-quiet
, force
);
1238 /* found something so even though assembly failed we
1239 * want to avoid auto-updates
1241 auto_update_home
= 0;
1243 /* Incase there are stacked devices, we need to go around again */
1246 if (cnt
== 0 && auto_update_home
&& homehost
) {
1247 /* Nothing found, maybe we need to bootstrap homehost info */
1251 rv2
= Assemble(ss
, NULL
,
1254 readonly
, runstop
, "homehost",
1255 homehost
, require_homehost
,
1256 verbose
-quiet
, force
);
1262 /* Incase there are stacked devices, we need to go around again */
1266 if (cnt
== 0 && rv
== 0) {
1267 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1271 } else if (cnt
== 0 && rv
== 0) {
1272 fprintf(stderr
, Name
": No arrays found in config file\n");
1278 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1279 if (write_behind
&& !bitmap_file
) {
1280 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1284 if (raiddisks
== 0) {
1285 fprintf(stderr
, Name
": no raid-devices specified.\n");
1291 if (strcmp(bitmap_file
, "internal")==0) {
1292 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1297 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1298 raiddisks
, devlist
->next
, assume_clean
,
1299 bitmap_file
, bitmap_chunk
, write_behind
,
1300 delay
, verbose
-quiet
, autof
, size
);
1303 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1304 if (write_behind
&& !bitmap_file
) {
1305 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1309 if (raiddisks
== 0) {
1310 fprintf(stderr
, Name
": no raid-devices specified.\n");
1315 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1316 raiddisks
, sparedisks
, ident
.name
, homehost
,
1317 ident
.uuid_set
? ident
.uuid
: NULL
,
1318 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1319 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1322 if (devmode
== 'E') {
1323 if (devlist
== NULL
&& !scan
) {
1324 fprintf(stderr
, Name
": No devices to examine\n");
1327 if (devlist
== NULL
)
1328 devlist
= conf_get_devs();
1329 if (devlist
== NULL
) {
1330 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1333 if (brief
&& verbose
)
1335 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1337 SparcAdjust
, ss
, homehost
);
1338 } else if (devmode
== DetailPlatform
) {
1339 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1341 if (devlist
== NULL
) {
1342 if ((devmode
=='D' || devmode
== Waitclean
) && scan
) {
1343 /* apply --detail or --wait-clean to
1344 * all devices in /proc/mdstat
1346 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1347 struct mdstat_ent
*e
;
1348 struct map_ent
*map
= NULL
;
1350 int v
= verbose
>1?0:verbose
+1;
1352 for (members
= 0; members
<= 1; members
++) {
1353 for (e
=ms
; e
; e
=e
->next
) {
1356 int member
= e
->metadata_version
&&
1357 strncmp(e
->metadata_version
,
1358 "external:/", 10) == 0;
1359 if (members
!= member
)
1361 me
= map_by_devnum(&map
, e
->devnum
);
1363 && strcmp(me
->path
, "/unknown") != 0)
1366 name
= get_md_name(e
->devnum
);
1369 fprintf(stderr
, Name
": cannot find device file for %s\n",
1374 rv
|= Detail(name
, v
,
1378 rv
|= WaitClean(name
, -1, v
);
1383 } else if (devmode
== 'S' && scan
) {
1384 /* apply --stop to all devices in /proc/mdstat */
1385 /* Due to possible stacking of devices, repeat until
1386 * nothing more can be stopped
1388 int progress
=1, err
;
1391 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1392 struct mdstat_ent
*e
;
1394 if (!progress
) last
= 1;
1395 progress
= 0; err
= 0;
1396 for (e
=ms
; e
; e
=e
->next
) {
1397 char *name
= get_md_name(e
->devnum
);
1400 fprintf(stderr
, Name
": cannot find device file for %s\n",
1404 mdfd
= open_mddev(name
, 1);
1406 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1416 } while (!last
&& err
);
1419 fprintf(stderr
, Name
": No devices given.\n");
1423 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1424 switch(dv
->disposition
) {
1426 rv
|= Detail(dv
->devname
,
1428 export
, test
, homehost
);
1430 case 'K': /* Zero superblock */
1432 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1436 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1443 rv
|= Query(dv
->devname
); continue;
1445 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1447 rv
|= Wait(dv
->devname
); continue;
1449 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1451 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1453 case UpdateSubarray
:
1454 if (update
== NULL
) {
1456 Name
": -U/--update must be specified with --update-subarray\n");
1460 rv
|= Update_subarray(dv
->devname
, subarray
, update
, &ident
, quiet
);
1463 mdfd
= open_mddev(dv
->devname
, 1);
1465 switch(dv
->disposition
) {
1467 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1469 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1471 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1473 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1482 if (!devlist
&& !scan
) {
1483 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1487 if (pidfile
&& !daemonise
) {
1488 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1493 if (get_linux_version() > 2006016)
1494 /* mdstat responds to poll */
1499 rv
= Monitor(devlist
, mailaddr
, program
,
1500 delay
?delay
:60, daemonise
, scan
, oneshot
,
1501 dosyslog
, test
, pidfile
, increments
, spare_sharing
);
1505 if (array_size
>= 0) {
1506 /* alway impose array size first, independent of
1508 * Do not allow level or raid_disks changes at the
1509 * same time as that can be irreversibly destructive.
1513 if (raiddisks
|| level
!= UnSet
) {
1514 fprintf(stderr
, Name
": cannot change array size in same operation "
1515 "as changing raiddisks or level.\n"
1516 " Change size first, then check that data is still intact.\n");
1520 sysfs_init(&sra
, mdfd
, 0);
1521 if (array_size
== 0)
1522 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1524 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1527 fprintf(stderr
, Name
": --array-size setting"
1528 " is too large.\n");
1530 fprintf(stderr
, Name
": current kernel does"
1531 " not support setting --array-size\n");
1536 if (devs_found
> 1) {
1539 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1540 fprintf(stderr
, Name
": --add cannot be used with other geometry changes in --grow mode\n");
1544 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1545 rv
= Grow_Add_device(devlist
->devname
, mdfd
, dv
->devname
);
1549 } else if (bitmap_file
) {
1550 if (size
>= 0 || raiddisks
|| chunk
|| layout_str
!= NULL
) {
1551 fprintf(stderr
, Name
": --bitmap changes cannot be used with other geometry changes in --grow mode\n");
1556 delay
= DEFAULT_BITMAP_DELAY
;
1557 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1558 bitmap_chunk
, delay
, write_behind
, force
);
1559 } else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1560 || chunk
!= 0 || level
!= UnSet
) {
1561 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1562 size
, level
, layout_str
, chunk
, raiddisks
);
1563 } else if (array_size
< 0)
1564 fprintf(stderr
, Name
": no changes to --grow\n");
1572 fprintf(stderr
, Name
1573 ": --incremental --scan meaningless without --run.\n");
1576 if (devmode
== 'f') {
1577 fprintf(stderr
, Name
1578 ": --incremental --scan --fail not supported.\n");
1581 rv
= IncrementalScan(verbose
);
1584 if (!rebuild_map
&& !scan
) {
1585 fprintf(stderr
, Name
1586 ": --incremental requires a device.\n");
1591 if (devlist
->next
) {
1592 fprintf(stderr
, Name
1593 ": --incremental can only handle one device.\n");
1598 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1601 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1602 runstop
, ss
, homehost
,
1603 require_homehost
, autof
);