2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2013 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.
32 static int scan_assemble(struct supertype
*ss
,
34 struct mddev_ident
*ident
);
35 static int misc_scan(char devmode
, struct context
*c
);
36 static int stop_scan(int verbose
);
37 static int misc_list(struct mddev_dev
*devlist
,
38 struct mddev_ident
*ident
,
40 struct supertype
*ss
, struct context
*c
);
41 const char Name
[] = "mdadm";
43 int main(int argc
, char *argv
[])
51 unsigned long long array_size
= 0;
52 unsigned long long data_offset
= INVALID_SECTORS
;
53 struct mddev_ident ident
;
54 char *configfile
= NULL
;
57 struct mddev_dev
*devlist
= NULL
;
58 struct mddev_dev
**devlistend
= & devlist
;
61 char *symlinks
= NULL
;
62 int grow_continue
= 0;
63 /* autof indicates whether and how to create device node.
64 * bottom 3 bits are style. Rest (when shifted) are number of parts
66 * 1 - don't create (no)
67 * 2 - if is_standard, then create (yes)
68 * 3 - create as 'md' - reject is_standard mdp (md)
69 * 4 - create as 'mdp' - reject is_standard md (mdp)
70 * 5 - default to md if not is_standard (md in config file)
71 * 6 - default to mdp if not is_standard (part, or mdp in config file)
74 .require_homehost
= 1,
79 .bitmap_chunk
= UnSet
,
82 char sys_hostname
[256];
83 char *mailaddr
= NULL
;
89 int spare_sharing
= 1;
90 struct supertype
*ss
= NULL
;
92 char *shortopt
= short_options
;
95 char *remove_path
= NULL
;
96 char *udev_filename
= NULL
;
97 char *dump_directory
= NULL
;
104 srandom(time(0) ^ getpid());
108 ident
.raid_disks
= UnSet
;
109 ident
.super_minor
= UnSet
;
111 ident
.spare_group
= NULL
;
114 ident
.bitmap_fd
= -1;
115 ident
.bitmap_file
= NULL
;
117 ident
.container
= NULL
;
120 while ((option_index
= -1) ,
121 (opt
=getopt_long(argc
, argv
,
122 shortopt
, long_options
,
123 &option_index
)) != -1) {
125 /* firstly, some mode-independent options */
135 fputs(Version
, stderr
);
138 case 'v': c
.verbose
++;
141 case 'q': c
.verbose
--;
145 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
146 || mode
== GROW
|| mode
== INCREMENTAL
148 break; /* b means bitmap */
153 case 'Y': c
.export
++;
157 if (strcasecmp(optarg
, "<ignore>") == 0)
158 c
.require_homehost
= 0;
164 /* Silently ignore old option */
170 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
176 fputs(Usage
, stderr
);
179 /* second, figure out the mode.
180 * Some options force the mode. Others
181 * set the mode if it isn't already
187 shortopt
= short_bitmap_options
;
198 case ReAdd
: /* re-add */
201 shortopt
= short_bitmap_options
;
205 case 'A': newmode
= ASSEMBLE
;
206 shortopt
= short_bitmap_auto_options
;
208 case 'B': newmode
= BUILD
;
209 shortopt
= short_bitmap_auto_options
;
211 case 'C': newmode
= CREATE
;
212 shortopt
= short_bitmap_auto_options
;
214 case 'F': newmode
= MONITOR
;
216 case 'G': newmode
= GROW
;
217 shortopt
= short_bitmap_options
;
219 case 'I': newmode
= INCREMENTAL
;
220 shortopt
= short_bitmap_auto_options
;
223 newmode
= AUTODETECT
;
258 if (mode
&& newmode
== mode
) {
259 /* everybody happy ! */
260 } else if (mode
&& newmode
!= mode
) {
263 if (option_index
>= 0)
264 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
266 fprintf(stderr
, "-%c", opt
);
267 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
268 map_num(modes
, newmode
),
269 map_num(modes
, mode
));
271 } else if (!mode
&& newmode
) {
273 if (mode
== MISC
&& devs_found
) {
274 pr_err("No action given for %s in --misc mode\n",
276 cont_err("Action options must come before device names\n");
280 /* special case of -c --help */
281 if ((opt
== 'c' || opt
== ConfigFile
) &&
282 ( strncmp(optarg
, "--h", 3)==0 ||
283 strncmp(optarg
, "-h", 2)==0)) {
284 fputs(Help_config
, stdout
);
288 /* If first option is a device, don't force the mode yet */
290 if (devs_found
== 0) {
291 dv
= xmalloc(sizeof(*dv
));
292 dv
->devname
= optarg
;
293 dv
->disposition
= devmode
;
294 dv
->writemostly
= writemostly
;
298 devlistend
= &dv
->next
;
303 /* No mode yet, and this is the second device ... */
304 pr_err("An option must be given to set the mode before a second device\n"
305 " (%s) is listed\n", optarg
);
308 if (option_index
>= 0)
309 pr_err("--%s", long_options
[option_index
].name
);
312 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
316 /* if we just set the mode, then done */
330 /* an undecorated option - must be a device name.
333 if (devs_found
> 0 && devmode
== DetailPlatform
) {
334 pr_err("controller may only be specified once. %s ignored\n",
339 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
340 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
343 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
344 pr_err("Must give -a/--add for devices to add: %s\n", optarg
);
347 dv
= xmalloc(sizeof(*dv
));
348 dv
->devname
= optarg
;
349 dv
->disposition
= devmode
;
350 dv
->writemostly
= writemostly
;
354 devlistend
= &dv
->next
;
360 /* We've got a mode, and opt is now something else which
361 * could depend on the mode */
362 #define O(a,b) ((a<<16)|b)
363 switch (O(mode
,opt
)) {
365 case O(GROW
,ChunkSize
):
367 case O(CREATE
,ChunkSize
):
368 case O(BUILD
,'c'): /* chunk or rounding */
369 case O(BUILD
,ChunkSize
): /* chunk or rounding */
371 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg
);
374 s
.chunk
= parse_size(optarg
);
375 if (s
.chunk
== INVALID_SECTORS
||
376 s
.chunk
< 8 || (s
.chunk
&1)) {
377 pr_err("invalid chunk/rounding value: %s\n",
381 /* Convert sectors to K */
385 case O(INCREMENTAL
, 'e'):
387 case O(ASSEMBLE
,'e'):
388 case O(MISC
,'e'): /* set metadata (superblock) information */
390 pr_err("metadata information already given\n");
393 for(i
=0; !ss
&& superlist
[i
]; i
++)
394 ss
= superlist
[i
]->match_metadata_desc(optarg
);
397 pr_err("unrecognised metadata identifier: %s\n", optarg
);
403 case O(MANAGE
,WriteMostly
):
405 case O(BUILD
,WriteMostly
):
407 case O(CREATE
,WriteMostly
):
408 /* set write-mostly for following devices */
413 /* clear write-mostly for following devices */
419 case O(BUILD
,'z'): /* size */
421 pr_err("size may only be specified once. Second value is %s.\n", optarg
);
424 if (strcmp(optarg
, "max")==0)
427 s
.size
= parse_size(optarg
);
428 if (s
.size
== INVALID_SECTORS
||
430 pr_err("invalid size: %s\n",
434 /* convert sectors to K */
439 case O(GROW
,'Z'): /* array size */
440 if (array_size
> 0) {
441 pr_err("array-size may only be specified once. Second value is %s.\n", optarg
);
444 if (strcmp(optarg
, "max") == 0)
445 array_size
= MAX_SIZE
;
447 array_size
= parse_size(optarg
);
448 if (array_size
== 0 ||
449 array_size
== INVALID_SECTORS
) {
450 pr_err("invalid array size: %s\n",
457 case O(CREATE
,DataOffset
):
458 case O(GROW
,DataOffset
):
459 if (data_offset
!= INVALID_SECTORS
) {
460 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg
);
463 if (mode
== CREATE
&&
464 strcmp(optarg
, "variable") == 0)
465 data_offset
= VARIABLE_OFFSET
;
467 data_offset
= parse_size(optarg
);
468 if (data_offset
== INVALID_SECTORS
) {
469 pr_err("invalid data-offset: %s\n",
477 case O(BUILD
,'l'): /* set raid level*/
478 if (s
.level
!= UnSet
) {
479 pr_err("raid level may only be set once. Second value is %s.\n", optarg
);
482 s
.level
= map_name(pers
, optarg
);
483 if (s
.level
== UnSet
) {
484 pr_err("invalid raid level: %s\n",
488 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
489 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
492 pr_err("Raid level %s not permitted with --build.\n",
496 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
497 pr_err("raid level %s is incompatible with spare-devices setting.\n",
501 ident
.level
= s
.level
;
504 case O(GROW
, 'p'): /* new layout */
505 case O(GROW
, Layout
):
507 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
510 s
.layout_str
= optarg
;
511 /* 'Grow' will parse the value */
514 case O(CREATE
,'p'): /* raid5 layout */
515 case O(CREATE
,Layout
):
516 case O(BUILD
,'p'): /* faulty layout */
517 case O(BUILD
,Layout
):
518 if (s
.layout
!= UnSet
) {
519 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
524 pr_err("layout not meaningful for %s arrays.\n",
525 map_num(pers
, s
.level
));
528 pr_err("raid level must be given before layout.\n");
532 s
.layout
= map_name(r5layout
, optarg
);
533 if (s
.layout
==UnSet
) {
534 pr_err("layout %s not understood for raid5.\n",
540 s
.layout
= map_name(r6layout
, optarg
);
541 if (s
.layout
==UnSet
) {
542 pr_err("layout %s not understood for raid6.\n",
549 s
.layout
= parse_layout_10(optarg
);
551 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
559 s
.layout
= parse_layout_faulty(optarg
);
560 if (s
.layout
== -1) {
561 pr_err("layout %s not understood for faulty.\n",
569 case O(CREATE
,AssumeClean
):
570 case O(BUILD
,AssumeClean
): /* assume clean */
571 case O(GROW
,AssumeClean
):
577 case O(BUILD
,'n'): /* number of raid disks */
579 pr_err("raid-devices set twice: %d and %s\n",
580 s
.raiddisks
, optarg
);
583 s
.raiddisks
= parse_num(optarg
);
584 if (s
.raiddisks
<= 0) {
585 pr_err("invalid number of raid devices: %s\n",
589 ident
.raid_disks
= s
.raiddisks
;
592 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
594 pr_err("spare-devices set twice: %d and %s\n",
595 s
.sparedisks
, optarg
);
598 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
599 pr_err("spare-devices setting is incompatible with raid level %d\n",
603 s
.sparedisks
= parse_num(optarg
);
604 if (s
.sparedisks
< 0) {
605 pr_err("invalid number of spare-devices: %s\n",
615 case O(INCREMENTAL
,'a'):
616 case O(INCREMENTAL
,Auto
):
617 case O(ASSEMBLE
,'a'):
618 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
619 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
622 case O(CREATE
,Symlinks
):
623 case O(BUILD
,Symlinks
):
624 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
628 case O(BUILD
,'f'): /* force honouring '-n 1' */
629 case O(BUILD
,Force
): /* force honouring '-n 1' */
630 case O(GROW
,'f'): /* ditto */
631 case O(GROW
,Force
): /* ditto */
632 case O(CREATE
,'f'): /* force honouring of device list */
633 case O(CREATE
,Force
): /* force honouring of device list */
634 case O(ASSEMBLE
,'f'): /* force assembly */
635 case O(ASSEMBLE
,Force
): /* force assembly */
636 case O(MISC
,'f'): /* force zero */
637 case O(MISC
,Force
): /* force zero */
638 case O(MANAGE
,Force
): /* add device which is too large */
641 /* now for the Assemble options */
642 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
644 case O(INCREMENTAL
, FreezeReshape
):
645 c
.freeze_reshape
= 1;
647 case O(CREATE
,'u'): /* uuid of array */
648 case O(ASSEMBLE
,'u'): /* uuid of array */
649 if (ident
.uuid_set
) {
650 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
653 if (parse_uuid(optarg
, ident
.uuid
))
656 pr_err("Bad uuid: %s\n", optarg
);
662 case O(ASSEMBLE
,'N'):
665 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
668 if (mode
== MISC
&& !c
.subarray
) {
669 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
672 if (strlen(optarg
) > 32) {
673 pr_err("name '%s' is too long, 32 chars max.\n",
677 strcpy(ident
.name
, optarg
);
680 case O(ASSEMBLE
,'m'): /* super-minor for array */
681 case O(ASSEMBLE
,SuperMinor
):
682 if (ident
.super_minor
!= UnSet
) {
683 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
686 if (strcmp(optarg
, "dev")==0)
687 ident
.super_minor
= -2;
689 ident
.super_minor
= parse_num(optarg
);
690 if (ident
.super_minor
< 0) {
691 pr_err("Bad super-minor number: %s.\n", optarg
);
697 case O(ASSEMBLE
,'o'):
703 case O(ASSEMBLE
,'U'): /* update the superblock */
706 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
710 if (mode
== MISC
&& !c
.subarray
) {
711 pr_err("Only subarrays can be updated in misc mode\n");
715 if (strcmp(c
.update
, "sparc2.2")==0)
717 if (strcmp(c
.update
, "super-minor") == 0)
719 if (strcmp(c
.update
, "summaries")==0)
721 if (strcmp(c
.update
, "resync")==0)
723 if (strcmp(c
.update
, "uuid")==0)
725 if (strcmp(c
.update
, "name")==0)
727 if (strcmp(c
.update
, "homehost")==0)
729 if (strcmp(c
.update
, "devicesize")==0)
731 if (strcmp(c
.update
, "no-bitmap")==0)
733 if (strcmp(c
.update
, "bbl") == 0)
735 if (strcmp(c
.update
, "no-bbl") == 0)
737 if (strcmp(c
.update
, "metadata") == 0)
739 if (strcmp(c
.update
, "revert-reshape") == 0)
741 if (strcmp(c
.update
, "byteorder")==0) {
743 pr_err("must not set metadata type with --update=byteorder.\n");
746 for(i
=0; !ss
&& superlist
[i
]; i
++)
747 ss
= superlist
[i
]->match_metadata_desc(
750 pr_err("INTERNAL ERROR cannot find 0.swap\n");
756 if (strcmp(c
.update
,"?") == 0 ||
757 strcmp(c
.update
, "help") == 0) {
759 fprintf(outf
, "%s: ", Name
);
763 "%s: '--update=%s' is invalid. ",
766 fprintf(outf
, "Valid --update options are:\n"
767 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
768 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
769 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
772 exit(outf
== stdout
? 0 : 2);
775 /* update=devicesize is allowed with --re-add */
776 if (devmode
!= 'A') {
777 pr_err("--update in Manage mode only allowed with --re-add.\n");
781 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
786 if (strcmp(c
.update
, "devicesize") != 0 &&
787 strcmp(c
.update
, "bbl") != 0 &&
788 strcmp(c
.update
, "no-bbl") != 0) {
789 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
794 case O(INCREMENTAL
,NoDegraded
):
795 pr_err("--no-degraded is deprecated in Incremental mode\n");
796 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
797 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
798 * so we overload slightly */
801 case O(ASSEMBLE
,'c'):
802 case O(ASSEMBLE
,ConfigFile
):
803 case O(INCREMENTAL
, 'c'):
804 case O(INCREMENTAL
, ConfigFile
):
806 case O(MISC
, ConfigFile
):
808 case O(MONITOR
,ConfigFile
):
809 case O(CREATE
,ConfigFile
):
811 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
815 set_conffile(configfile
);
816 /* FIXME possibly check that config file exists. Even parse it */
818 case O(ASSEMBLE
,'s'): /* scan */
821 case O(INCREMENTAL
,'s'):
825 case O(MONITOR
,'m'): /* mail address */
826 case O(MONITOR
,EMail
):
828 pr_err("only specify one mailaddress. %s ignored.\n",
834 case O(MONITOR
,'p'): /* alert program */
835 case O(MONITOR
,ProgramOpt
): /* alert program */
837 pr_err("only specify one alter program. %s ignored.\n",
843 case O(MONITOR
,'r'): /* rebuild increments */
844 case O(MONITOR
,Increment
):
845 increments
= atoi(optarg
);
846 if (increments
> 99 || increments
< 1) {
847 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
852 case O(MONITOR
,'d'): /* delay in seconds */
854 case O(BUILD
,'d'): /* delay for bitmap updates */
857 pr_err("only specify delay once. %s ignored.\n",
860 c
.delay
= parse_num(optarg
);
862 pr_err("invalid delay: %s\n",
868 case O(MONITOR
,'f'): /* daemonise */
869 case O(MONITOR
,Fork
):
872 case O(MONITOR
,'i'): /* pid */
874 pr_err("only specify one pid file. %s ignored.\n",
879 case O(MONITOR
,'1'): /* oneshot */
883 case O(MONITOR
,'t'): /* test */
886 case O(MONITOR
,'y'): /* log messages to syslog */
887 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
890 case O(MONITOR
, NoSharing
):
894 /* now the general management options. Some are applicable
895 * to other modes. None have arguments.
900 case O(MANAGE
,Add
): /* add a drive */
903 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
906 case O(MANAGE
,ReAdd
):
909 case O(MANAGE
,'r'): /* remove a drive */
910 case O(MANAGE
,Remove
):
913 case O(MANAGE
,'f'): /* set faulty */
915 case O(INCREMENTAL
,'f'):
916 case O(INCREMENTAL
,Remove
):
917 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
918 * even though we will both fail and
919 * remove the device */
922 case O(MANAGE
,Replace
):
923 /* Mark these devices for replacement */
927 /* These are the replacements to use */
928 if (devmode
!= 'R') {
929 pr_err("--with must follow --replace\n");
934 case O(INCREMENTAL
,'R'):
936 case O(ASSEMBLE
,'R'):
938 case O(CREATE
,'R'): /* Run the array */
940 pr_err("Cannot both Stop and Run an array\n");
947 pr_err("Cannot both Run and Stop an array\n");
959 case O(MISC
,KillOpt
):
963 case O(MISC
, ExamineBB
):
967 case O(MISC
, WaitOpt
):
968 case O(MISC
, Waitclean
):
969 case O(MISC
, DetailPlatform
):
970 case O(MISC
, KillSubarray
):
971 case O(MISC
, UpdateSubarray
):
973 case O(MISC
, Restore
):
974 case O(MISC
,Action
):
975 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
977 pr_err("subarray can only be specified once\n");
984 pr_err("Only one --action can be specified\n");
987 if (strcmp(optarg
, "idle") == 0 ||
988 strcmp(optarg
, "frozen") == 0 ||
989 strcmp(optarg
, "check") == 0 ||
990 strcmp(optarg
, "repair") == 0)
993 pr_err("action must be one of idle, frozen, check, repair\n");
997 if (devmode
&& devmode
!= opt
&&
998 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
999 pr_err("--examine/-E cannot be given with ");
1000 if (devmode
== 'E') {
1001 if (option_index
>= 0)
1002 fprintf(stderr
, "--%s\n",
1003 long_options
[option_index
].name
);
1005 fprintf(stderr
, "-%c\n", opt
);
1006 } else if (isalpha(devmode
))
1007 fprintf(stderr
, "-%c\n", devmode
);
1009 fprintf(stderr
, "previous option\n");
1013 if (opt
== Dump
|| opt
== Restore
) {
1014 if (dump_directory
!= NULL
) {
1015 pr_err("dump/restore directory specified twice: %s and %s\n",
1016 dump_directory
, optarg
);
1019 dump_directory
= optarg
;
1022 case O(MISC
, UdevRules
):
1023 if (devmode
&& devmode
!= opt
) {
1024 pr_err("--udev-rules must be the only option.\n");
1027 pr_err("only specify one udev rule filename. %s ignored.\n",
1030 udev_filename
= optarg
;
1038 case O(MISC
, Sparc22
):
1039 if (devmode
!= 'E') {
1040 pr_err("--sparc2.2 only allowed with --examine\n");
1046 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1047 case O(ASSEMBLE
,Bitmap
):
1049 pr_err("bitmap file needed with -b in --assemble mode\n");
1052 if (strcmp(optarg
, "internal")==0) {
1053 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1056 bitmap_fd
= open(optarg
, O_RDWR
);
1057 if (!*optarg
|| bitmap_fd
< 0) {
1058 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1061 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1064 case O(ASSEMBLE
, BackupFile
):
1065 case O(GROW
, BackupFile
):
1066 /* Specify a file into which grow might place a backup,
1067 * or from which assemble might recover a backup
1069 if (c
.backup_file
) {
1070 pr_err("backup file already specified, rejecting %s\n", optarg
);
1073 c
.backup_file
= optarg
;
1076 case O(GROW
, Continue
):
1077 /* Continue interrupted grow
1081 case O(ASSEMBLE
, InvalidBackup
):
1082 /* Acknowledge that the backupfile is invalid, but ask
1083 * to continue anyway
1085 c
.invalid_backup
= 1;
1089 case O(BUILD
,Bitmap
):
1091 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1093 case O(GROW
,Bitmap
):
1094 if (strcmp(optarg
, "internal")== 0 ||
1095 strcmp(optarg
, "none")== 0 ||
1096 strchr(optarg
, '/') != NULL
) {
1097 s
.bitmap_file
= optarg
;
1101 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1102 " not '%s'\n", optarg
);
1105 case O(GROW
,BitmapChunk
):
1106 case O(BUILD
,BitmapChunk
):
1107 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1108 s
.bitmap_chunk
= parse_size(optarg
);
1109 if (s
.bitmap_chunk
== 0 ||
1110 s
.bitmap_chunk
== INVALID_SECTORS
||
1111 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1112 pr_err("invalid bitmap chunksize: %s\n",
1116 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1119 case O(GROW
, WriteBehind
):
1120 case O(BUILD
, WriteBehind
):
1121 case O(CREATE
, WriteBehind
): /* write-behind mode */
1122 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1124 s
.write_behind
= parse_num(optarg
);
1125 if (s
.write_behind
< 0 ||
1126 s
.write_behind
> 16383) {
1127 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1133 case O(INCREMENTAL
, 'r'):
1134 case O(INCREMENTAL
, RebuildMapOpt
):
1137 case O(INCREMENTAL
, IncrementalPath
):
1138 remove_path
= optarg
;
1141 /* We have now processed all the valid options. Anything else is
1144 if (option_index
> 0)
1145 pr_err(":option --%s not valid in %s mode\n",
1146 long_options
[option_index
].name
,
1147 map_num(modes
, mode
));
1149 pr_err("option -%c not valid in %s mode\n",
1150 opt
, map_num(modes
, mode
));
1157 if (print_help
== 2)
1158 help_text
= OptionHelp
;
1160 help_text
= mode_help
[mode
];
1161 if (help_text
== NULL
)
1163 fputs(help_text
,stdout
);
1167 if (!mode
&& devs_found
) {
1170 if (devlist
->disposition
== 0)
1171 devlist
->disposition
= devmode
;
1174 fputs(Usage
, stderr
);
1179 struct createinfo
*ci
= conf_get_create_info();
1181 if (strcasecmp(symlinks
, "yes") == 0)
1183 else if (strcasecmp(symlinks
, "no") == 0)
1186 pr_err("option --symlinks must be 'no' or 'yes'\n");
1190 /* Ok, got the option parsing out of the way
1191 * hopefully it's mostly right but there might be some stuff
1194 * That is mosty checked in the per-mode stuff but...
1196 * For @,B,C and A without -s, the first device listed must be
1197 * an md device. We check that here and open it.
1200 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1202 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1203 if (devs_found
< 1) {
1204 pr_err("an md device must be given in this mode\n");
1207 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1208 pr_err("--super-minor=dev is incompatible with --auto\n");
1211 if (mode
== MANAGE
|| mode
== GROW
) {
1212 mdfd
= open_mddev(devlist
->devname
, 1);
1216 /* non-existent device is OK */
1217 mdfd
= open_mddev(devlist
->devname
, 0);
1219 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1222 if ((int)ident
.super_minor
== -2) {
1225 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1230 ident
.super_minor
= minor(stb
.st_rdev
);
1232 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1233 /* We don't really want this open yet, we just might
1234 * have wanted to check some things
1242 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1243 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1244 " a mistake. If you really mean it you will need to specify --force before\n"
1245 " setting the number of drives.\n");
1250 if (c
.homehost
== NULL
&& c
.require_homehost
)
1251 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1252 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1253 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1254 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1255 c
.homehost
= sys_hostname
;
1258 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1260 c
.require_homehost
= 0;
1263 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1264 pr_err("--backup-file and --data-offset are incompatible\n");
1268 if ((mode
== MISC
&& devmode
== 'E')
1269 || (mode
== MONITOR
&& spare_sharing
== 0))
1270 /* Anyone may try this */;
1271 else if (geteuid() != 0) {
1272 pr_err("must be super-user to perform this action\n");
1276 ident
.autof
= c
.autof
;
1278 if (c
.scan
&& c
.verbose
< 2)
1279 /* --scan implied --brief unless -vv */
1285 /* readonly, add/remove, readwrite, runstop */
1287 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1288 if (!rv
&& devs_found
>1)
1289 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1290 devlist
->next
, c
.verbose
, c
.test
,
1292 if (!rv
&& c
.readonly
< 0)
1293 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1294 if (!rv
&& c
.runstop
> 0)
1295 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1296 if (!rv
&& c
.runstop
< 0)
1297 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1300 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1301 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1302 /* Only a device has been given, so get details from config file */
1303 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1304 if (array_ident
== NULL
) {
1305 pr_err("%s not identified in config file.\n",
1311 if (array_ident
->autof
== 0)
1312 array_ident
->autof
= c
.autof
;
1313 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1317 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1319 else if (devs_found
> 0) {
1320 if (c
.update
&& devs_found
> 1) {
1321 pr_err("can only update a single array at a time\n");
1324 if (c
.backup_file
&& devs_found
> 1) {
1325 pr_err("can only assemble a single array when providing a backup file.\n");
1328 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1329 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1330 if (array_ident
== NULL
) {
1331 pr_err("%s not identified in config file.\n",
1336 if (array_ident
->autof
== 0)
1337 array_ident
->autof
= c
.autof
;
1338 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1343 pr_err("--update not meaningful with a --scan assembly.\n");
1346 if (c
.backup_file
) {
1347 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1350 rv
= scan_assemble(ss
, &c
, &ident
);
1356 c
.delay
= DEFAULT_BITMAP_DELAY
;
1357 if (s
.write_behind
&& !s
.bitmap_file
) {
1358 pr_err("write-behind mode requires a bitmap.\n");
1362 if (s
.raiddisks
== 0) {
1363 pr_err("no raid-devices specified.\n");
1368 if (s
.bitmap_file
) {
1369 if (strcmp(s
.bitmap_file
, "internal")==0) {
1370 pr_err("'internal' bitmaps not supported with --build\n");
1375 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1379 c
.delay
= DEFAULT_BITMAP_DELAY
;
1380 if (s
.write_behind
&& !s
.bitmap_file
) {
1381 pr_err("write-behind mode requires a bitmap.\n");
1385 if (s
.raiddisks
== 0) {
1386 pr_err("no raid-devices specified.\n");
1391 rv
= Create(ss
, devlist
->devname
,
1392 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1393 devs_found
-1, devlist
->next
,
1394 &s
, &c
, data_offset
);
1397 if (devmode
== 'E') {
1398 if (devlist
== NULL
&& !c
.scan
) {
1399 pr_err("No devices to examine\n");
1402 if (devlist
== NULL
)
1403 devlist
= conf_get_devs();
1404 if (devlist
== NULL
) {
1405 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1408 rv
= Examine(devlist
, &c
, ss
);
1409 } else if (devmode
== DetailPlatform
) {
1410 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1411 c
.verbose
, c
.export
,
1412 devlist
? devlist
->devname
: NULL
);
1413 } else if (devlist
== NULL
) {
1414 if (devmode
== 'S' && c
.scan
)
1415 rv
= stop_scan(c
.verbose
);
1416 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1417 rv
= misc_scan(devmode
, &c
);
1418 else if (devmode
== UdevRules
)
1419 rv
= Write_rules(udev_filename
);
1421 pr_err("No devices given.\n");
1425 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1428 if (!devlist
&& !c
.scan
) {
1429 pr_err("Cannot monitor: need --scan or at least one device\n");
1433 if (pidfile
&& !daemonise
) {
1434 pr_err("Cannot write a pid file when not in daemon mode\n");
1439 if (get_linux_version() > 2006016)
1440 /* mdstat responds to poll */
1447 rv
= Monitor(devlist
, mailaddr
, program
,
1448 &c
, daemonise
, oneshot
,
1449 dosyslog
, pidfile
, increments
,
1454 if (array_size
> 0) {
1455 /* alway impose array size first, independent of
1457 * Do not allow level or raid_disks changes at the
1458 * same time as that can be irreversibly destructive.
1462 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1463 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1464 " Change size first, then check that data is still intact.\n");
1468 sysfs_init(&sra
, mdfd
, NULL
);
1469 if (array_size
== MAX_SIZE
)
1470 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1472 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1475 pr_err("--array-size setting is too large.\n");
1477 pr_err("current kernel does not support setting --array-size\n");
1482 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1484 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1485 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1489 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1490 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1495 } else if (s
.bitmap_file
) {
1496 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1497 s
.layout_str
!= NULL
|| devs_found
> 1) {
1498 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1503 c
.delay
= DEFAULT_BITMAP_DELAY
;
1504 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1505 } else if (grow_continue
)
1506 rv
= Grow_continue_command(devlist
->devname
,
1507 mdfd
, c
.backup_file
,
1509 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1510 || s
.chunk
!= 0 || s
.level
!= UnSet
1511 || data_offset
!= INVALID_SECTORS
) {
1512 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1514 data_offset
, &c
, &s
);
1515 } else if (array_size
== 0)
1516 pr_err("no changes to --grow\n");
1525 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1528 if (c
.runstop
<= 0) {
1529 pr_err("--incremental --scan meaningless without --run.\n");
1532 if (devmode
== 'f') {
1533 pr_err("--incremental --scan --fail not supported.\n");
1536 rv
= IncrementalScan(&c
, NULL
);
1539 if (!rebuild_map
&& !c
.scan
) {
1540 pr_err("--incremental requires a device.\n");
1545 if (devmode
== 'f') {
1546 if (devlist
->next
) {
1547 pr_err("'--incremental --fail' can only handle one device.\n");
1551 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1554 rv
= Incremental(devlist
, &c
, ss
);
1563 static int scan_assemble(struct supertype
*ss
,
1565 struct mddev_ident
*ident
)
1567 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1568 struct mddev_dev
*devlist
= conf_get_devs();
1569 struct map_ent
*map
= NULL
;
1572 int failures
, successes
;
1574 if (conf_verify_devnames(array_list
)) {
1575 pr_err("Duplicate MD device names in conf file were found.\n");
1578 if (devlist
== NULL
) {
1579 pr_err("No devices listed in conf file were found.\n");
1582 for (a
= array_list
; a
; a
= a
->next
) {
1585 a
->autof
= c
->autof
;
1588 pr_err("failed to get exclusive lock on mapfile\n");
1593 for (a
= array_list
; a
; a
= a
->next
) {
1598 strcasecmp(a
->devname
, "<ignore>") == 0)
1601 r
= Assemble(ss
, a
->devname
,
1611 } while (failures
&& successes
);
1612 if (c
->homehost
&& cnt
== 0) {
1613 /* Maybe we can auto-assemble something.
1614 * Repeatedly call Assemble in auto-assemble mode
1619 ident
->autof
= c
->autof
;
1621 struct mddev_dev
*devlist
= conf_get_devs();
1624 rv2
= Assemble(ss
, NULL
,
1632 /* Incase there are stacked devices, we need to go around again */
1634 if (cnt
== 0 && rv
== 0) {
1635 pr_err("No arrays found in config file or automatically\n");
1639 } else if (cnt
== 0 && rv
== 0) {
1640 pr_err("No arrays found in config file\n");
1647 static int misc_scan(char devmode
, struct context
*c
)
1649 /* apply --detail or --wait-clean to
1650 * all devices in /proc/mdstat
1652 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1653 struct mdstat_ent
*e
;
1654 struct map_ent
*map
= NULL
;
1658 for (members
= 0; members
<= 1; members
++) {
1659 for (e
=ms
; e
; e
=e
->next
) {
1663 int member
= e
->metadata_version
&&
1664 strncmp(e
->metadata_version
,
1665 "external:/", 10) == 0;
1666 if (members
!= member
)
1668 me
= map_by_devnm(&map
, e
->devnm
);
1670 && strcmp(me
->path
, "/unknown") != 0)
1673 stat(name
, &stb
) != 0)
1674 name
= get_md_name(e
->devnm
);
1677 pr_err("cannot find device file for %s\n",
1682 rv
|= Detail(name
, c
);
1684 rv
|= WaitClean(name
, -1, c
->verbose
);
1692 static int stop_scan(int verbose
)
1694 /* apply --stop to all devices in /proc/mdstat */
1695 /* Due to possible stacking of devices, repeat until
1696 * nothing more can be stopped
1698 int progress
=1, err
;
1702 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1703 struct mdstat_ent
*e
;
1705 if (!progress
) last
= 1;
1706 progress
= 0; err
= 0;
1707 for (e
=ms
; e
; e
=e
->next
) {
1708 char *name
= get_md_name(e
->devnm
);
1712 pr_err("cannot find device file for %s\n",
1716 mdfd
= open_mddev(name
, 1);
1718 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1728 } while (!last
&& err
);
1734 static int misc_list(struct mddev_dev
*devlist
,
1735 struct mddev_ident
*ident
,
1736 char *dump_directory
,
1737 struct supertype
*ss
, struct context
*c
)
1739 struct mddev_dev
*dv
;
1742 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1745 switch(dv
->disposition
) {
1747 rv
|= Detail(dv
->devname
, c
);
1749 case KillOpt
: /* Zero superblock */
1751 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1755 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1762 rv
|= Query(dv
->devname
); continue;
1764 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1766 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1769 rv
|= Wait(dv
->devname
); continue;
1771 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1773 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1775 case UpdateSubarray
:
1776 if (c
->update
== NULL
) {
1777 pr_err("-U/--update must be specified with --update-subarray\n");
1781 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1782 c
->update
, ident
, c
->verbose
);
1785 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1788 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1789 (dv
== devlist
&& dv
->next
== NULL
));
1792 rv
|= SetAction(dv
->devname
, c
->action
);
1795 if (dv
->devname
[0] == '/')
1796 mdfd
= open_mddev(dv
->devname
, 1);
1798 mdfd
= open_dev(dv
->devname
);
1800 pr_err("Cannot open %s\n", dv
->devname
);
1803 switch(dv
->disposition
) {
1806 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1808 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1810 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1812 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1821 int SetAction(char *dev
, char *action
)
1823 int fd
= open(dev
, O_RDONLY
);
1826 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1829 sysfs_init(&mdi
, fd
, NULL
);
1831 if (!mdi
.sys_name
[0]) {
1832 pr_err("%s is no an md array\n", dev
);
1836 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1837 pr_err("Count not set action for %s to %s: %s\n",
1838 dev
, action
, strerror(errno
));