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
;
591 case O(CREATE
, Nodes
):
592 c
.nodes
= parse_num(optarg
);
594 pr_err("invalid number for the number of cluster nodes: %s\n",
599 case O(CREATE
, ClusterName
):
600 c
.homecluster
= optarg
;
601 if (strlen(c
.homecluster
) > 64) {
602 pr_err("Cluster name too big.\n");
606 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
608 pr_err("spare-devices set twice: %d and %s\n",
609 s
.sparedisks
, optarg
);
612 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
613 pr_err("spare-devices setting is incompatible with raid level %d\n",
617 s
.sparedisks
= parse_num(optarg
);
618 if (s
.sparedisks
< 0) {
619 pr_err("invalid number of spare-devices: %s\n",
629 case O(INCREMENTAL
,'a'):
630 case O(INCREMENTAL
,Auto
):
631 case O(ASSEMBLE
,'a'):
632 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
633 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
636 case O(CREATE
,Symlinks
):
637 case O(BUILD
,Symlinks
):
638 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
642 case O(BUILD
,'f'): /* force honouring '-n 1' */
643 case O(BUILD
,Force
): /* force honouring '-n 1' */
644 case O(GROW
,'f'): /* ditto */
645 case O(GROW
,Force
): /* ditto */
646 case O(CREATE
,'f'): /* force honouring of device list */
647 case O(CREATE
,Force
): /* force honouring of device list */
648 case O(ASSEMBLE
,'f'): /* force assembly */
649 case O(ASSEMBLE
,Force
): /* force assembly */
650 case O(MISC
,'f'): /* force zero */
651 case O(MISC
,Force
): /* force zero */
652 case O(MANAGE
,Force
): /* add device which is too large */
655 /* now for the Assemble options */
656 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
658 case O(INCREMENTAL
, FreezeReshape
):
659 c
.freeze_reshape
= 1;
661 case O(CREATE
,'u'): /* uuid of array */
662 case O(ASSEMBLE
,'u'): /* uuid of array */
663 if (ident
.uuid_set
) {
664 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
667 if (parse_uuid(optarg
, ident
.uuid
))
670 pr_err("Bad uuid: %s\n", optarg
);
676 case O(ASSEMBLE
,'N'):
679 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
682 if (mode
== MISC
&& !c
.subarray
) {
683 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
686 if (strlen(optarg
) > 32) {
687 pr_err("name '%s' is too long, 32 chars max.\n",
691 strcpy(ident
.name
, optarg
);
694 case O(ASSEMBLE
,'m'): /* super-minor for array */
695 case O(ASSEMBLE
,SuperMinor
):
696 if (ident
.super_minor
!= UnSet
) {
697 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
700 if (strcmp(optarg
, "dev")==0)
701 ident
.super_minor
= -2;
703 ident
.super_minor
= parse_num(optarg
);
704 if (ident
.super_minor
< 0) {
705 pr_err("Bad super-minor number: %s.\n", optarg
);
711 case O(ASSEMBLE
,'o'):
717 case O(ASSEMBLE
,'U'): /* update the superblock */
720 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
724 if (mode
== MISC
&& !c
.subarray
) {
725 pr_err("Only subarrays can be updated in misc mode\n");
729 if (strcmp(c
.update
, "sparc2.2")==0)
731 if (strcmp(c
.update
, "super-minor") == 0)
733 if (strcmp(c
.update
, "summaries")==0)
735 if (strcmp(c
.update
, "resync")==0)
737 if (strcmp(c
.update
, "uuid")==0)
739 if (strcmp(c
.update
, "name")==0)
741 if (strcmp(c
.update
, "homehost")==0)
743 if (strcmp(c
.update
, "devicesize")==0)
745 if (strcmp(c
.update
, "no-bitmap")==0)
747 if (strcmp(c
.update
, "bbl") == 0)
749 if (strcmp(c
.update
, "no-bbl") == 0)
751 if (strcmp(c
.update
, "metadata") == 0)
753 if (strcmp(c
.update
, "revert-reshape") == 0)
755 if (strcmp(c
.update
, "byteorder")==0) {
757 pr_err("must not set metadata type with --update=byteorder.\n");
760 for(i
=0; !ss
&& superlist
[i
]; i
++)
761 ss
= superlist
[i
]->match_metadata_desc(
764 pr_err("INTERNAL ERROR cannot find 0.swap\n");
770 if (strcmp(c
.update
,"?") == 0 ||
771 strcmp(c
.update
, "help") == 0) {
773 fprintf(outf
, "%s: ", Name
);
777 "%s: '--update=%s' is invalid. ",
780 fprintf(outf
, "Valid --update options are:\n"
781 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
782 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
783 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
786 exit(outf
== stdout
? 0 : 2);
789 /* update=devicesize is allowed with --re-add */
790 if (devmode
!= 'A') {
791 pr_err("--update in Manage mode only allowed with --re-add.\n");
795 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
800 if (strcmp(c
.update
, "devicesize") != 0 &&
801 strcmp(c
.update
, "bbl") != 0 &&
802 strcmp(c
.update
, "no-bbl") != 0) {
803 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
808 case O(INCREMENTAL
,NoDegraded
):
809 pr_err("--no-degraded is deprecated in Incremental mode\n");
810 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
811 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
812 * so we overload slightly */
815 case O(ASSEMBLE
,'c'):
816 case O(ASSEMBLE
,ConfigFile
):
817 case O(INCREMENTAL
, 'c'):
818 case O(INCREMENTAL
, ConfigFile
):
820 case O(MISC
, ConfigFile
):
822 case O(MONITOR
,ConfigFile
):
823 case O(CREATE
,ConfigFile
):
825 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
829 set_conffile(configfile
);
830 /* FIXME possibly check that config file exists. Even parse it */
832 case O(ASSEMBLE
,'s'): /* scan */
835 case O(INCREMENTAL
,'s'):
839 case O(MONITOR
,'m'): /* mail address */
840 case O(MONITOR
,EMail
):
842 pr_err("only specify one mailaddress. %s ignored.\n",
848 case O(MONITOR
,'p'): /* alert program */
849 case O(MONITOR
,ProgramOpt
): /* alert program */
851 pr_err("only specify one alter program. %s ignored.\n",
857 case O(MONITOR
,'r'): /* rebuild increments */
858 case O(MONITOR
,Increment
):
859 increments
= atoi(optarg
);
860 if (increments
> 99 || increments
< 1) {
861 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
866 case O(MONITOR
,'d'): /* delay in seconds */
868 case O(BUILD
,'d'): /* delay for bitmap updates */
871 pr_err("only specify delay once. %s ignored.\n",
874 c
.delay
= parse_num(optarg
);
876 pr_err("invalid delay: %s\n",
882 case O(MONITOR
,'f'): /* daemonise */
883 case O(MONITOR
,Fork
):
886 case O(MONITOR
,'i'): /* pid */
888 pr_err("only specify one pid file. %s ignored.\n",
893 case O(MONITOR
,'1'): /* oneshot */
897 case O(MONITOR
,'t'): /* test */
900 case O(MONITOR
,'y'): /* log messages to syslog */
901 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
904 case O(MONITOR
, NoSharing
):
908 /* now the general management options. Some are applicable
909 * to other modes. None have arguments.
914 case O(MANAGE
,Add
): /* add a drive */
917 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
920 case O(MANAGE
,ReAdd
):
923 case O(MANAGE
,'r'): /* remove a drive */
924 case O(MANAGE
,Remove
):
927 case O(MANAGE
,'f'): /* set faulty */
929 case O(INCREMENTAL
,'f'):
930 case O(INCREMENTAL
,Remove
):
931 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
932 * even though we will both fail and
933 * remove the device */
936 case O(MANAGE
,Replace
):
937 /* Mark these devices for replacement */
941 /* These are the replacements to use */
942 if (devmode
!= 'R') {
943 pr_err("--with must follow --replace\n");
948 case O(INCREMENTAL
,'R'):
950 case O(ASSEMBLE
,'R'):
952 case O(CREATE
,'R'): /* Run the array */
954 pr_err("Cannot both Stop and Run an array\n");
961 pr_err("Cannot both Run and Stop an array\n");
973 case O(MISC
,KillOpt
):
977 case O(MISC
, ExamineBB
):
981 case O(MISC
, WaitOpt
):
982 case O(MISC
, Waitclean
):
983 case O(MISC
, DetailPlatform
):
984 case O(MISC
, KillSubarray
):
985 case O(MISC
, UpdateSubarray
):
987 case O(MISC
, Restore
):
988 case O(MISC
,Action
):
989 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
991 pr_err("subarray can only be specified once\n");
998 pr_err("Only one --action can be specified\n");
1001 if (strcmp(optarg
, "idle") == 0 ||
1002 strcmp(optarg
, "frozen") == 0 ||
1003 strcmp(optarg
, "check") == 0 ||
1004 strcmp(optarg
, "repair") == 0)
1007 pr_err("action must be one of idle, frozen, check, repair\n");
1011 if (devmode
&& devmode
!= opt
&&
1012 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1013 pr_err("--examine/-E cannot be given with ");
1014 if (devmode
== 'E') {
1015 if (option_index
>= 0)
1016 fprintf(stderr
, "--%s\n",
1017 long_options
[option_index
].name
);
1019 fprintf(stderr
, "-%c\n", opt
);
1020 } else if (isalpha(devmode
))
1021 fprintf(stderr
, "-%c\n", devmode
);
1023 fprintf(stderr
, "previous option\n");
1027 if (opt
== Dump
|| opt
== Restore
) {
1028 if (dump_directory
!= NULL
) {
1029 pr_err("dump/restore directory specified twice: %s and %s\n",
1030 dump_directory
, optarg
);
1033 dump_directory
= optarg
;
1036 case O(MISC
, UdevRules
):
1037 if (devmode
&& devmode
!= opt
) {
1038 pr_err("--udev-rules must be the only option.\n");
1041 pr_err("only specify one udev rule filename. %s ignored.\n",
1044 udev_filename
= optarg
;
1052 case O(MISC
, Sparc22
):
1053 if (devmode
!= 'E') {
1054 pr_err("--sparc2.2 only allowed with --examine\n");
1060 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1061 case O(ASSEMBLE
,Bitmap
):
1063 pr_err("bitmap file needed with -b in --assemble mode\n");
1066 if (strcmp(optarg
, "internal")==0) {
1067 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1070 bitmap_fd
= open(optarg
, O_RDWR
);
1071 if (!*optarg
|| bitmap_fd
< 0) {
1072 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1075 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1078 case O(ASSEMBLE
, BackupFile
):
1079 case O(GROW
, BackupFile
):
1080 /* Specify a file into which grow might place a backup,
1081 * or from which assemble might recover a backup
1083 if (c
.backup_file
) {
1084 pr_err("backup file already specified, rejecting %s\n", optarg
);
1087 c
.backup_file
= optarg
;
1090 case O(GROW
, Continue
):
1091 /* Continue interrupted grow
1095 case O(ASSEMBLE
, InvalidBackup
):
1096 /* Acknowledge that the backupfile is invalid, but ask
1097 * to continue anyway
1099 c
.invalid_backup
= 1;
1103 case O(BUILD
,Bitmap
):
1105 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1107 case O(GROW
,Bitmap
):
1108 if (strcmp(optarg
, "internal")== 0 ||
1109 strcmp(optarg
, "none")== 0 ||
1110 strchr(optarg
, '/') != NULL
) {
1111 s
.bitmap_file
= optarg
;
1114 if (strcmp(optarg
, "clustered")== 0) {
1115 s
.bitmap_file
= optarg
;
1116 /* Set the default number of cluster nodes
1117 * to 4 if not already set by user
1124 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1125 " not '%s'\n", optarg
);
1128 case O(GROW
,BitmapChunk
):
1129 case O(BUILD
,BitmapChunk
):
1130 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1131 s
.bitmap_chunk
= parse_size(optarg
);
1132 if (s
.bitmap_chunk
== 0 ||
1133 s
.bitmap_chunk
== INVALID_SECTORS
||
1134 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1135 pr_err("invalid bitmap chunksize: %s\n",
1139 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1142 case O(GROW
, WriteBehind
):
1143 case O(BUILD
, WriteBehind
):
1144 case O(CREATE
, WriteBehind
): /* write-behind mode */
1145 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1147 s
.write_behind
= parse_num(optarg
);
1148 if (s
.write_behind
< 0 ||
1149 s
.write_behind
> 16383) {
1150 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1156 case O(INCREMENTAL
, 'r'):
1157 case O(INCREMENTAL
, RebuildMapOpt
):
1160 case O(INCREMENTAL
, IncrementalPath
):
1161 remove_path
= optarg
;
1164 /* We have now processed all the valid options. Anything else is
1167 if (option_index
> 0)
1168 pr_err(":option --%s not valid in %s mode\n",
1169 long_options
[option_index
].name
,
1170 map_num(modes
, mode
));
1172 pr_err("option -%c not valid in %s mode\n",
1173 opt
, map_num(modes
, mode
));
1180 if (print_help
== 2)
1181 help_text
= OptionHelp
;
1183 help_text
= mode_help
[mode
];
1184 if (help_text
== NULL
)
1186 fputs(help_text
,stdout
);
1190 if (!mode
&& devs_found
) {
1193 if (devlist
->disposition
== 0)
1194 devlist
->disposition
= devmode
;
1197 fputs(Usage
, stderr
);
1202 struct createinfo
*ci
= conf_get_create_info();
1204 if (strcasecmp(symlinks
, "yes") == 0)
1206 else if (strcasecmp(symlinks
, "no") == 0)
1209 pr_err("option --symlinks must be 'no' or 'yes'\n");
1213 /* Ok, got the option parsing out of the way
1214 * hopefully it's mostly right but there might be some stuff
1217 * That is mosty checked in the per-mode stuff but...
1219 * For @,B,C and A without -s, the first device listed must be
1220 * an md device. We check that here and open it.
1223 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1225 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1226 if (devs_found
< 1) {
1227 pr_err("an md device must be given in this mode\n");
1230 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1231 pr_err("--super-minor=dev is incompatible with --auto\n");
1234 if (mode
== MANAGE
|| mode
== GROW
) {
1235 mdfd
= open_mddev(devlist
->devname
, 1);
1239 /* non-existent device is OK */
1240 mdfd
= open_mddev(devlist
->devname
, 0);
1242 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1245 if ((int)ident
.super_minor
== -2) {
1248 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1253 ident
.super_minor
= minor(stb
.st_rdev
);
1255 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1256 /* We don't really want this open yet, we just might
1257 * have wanted to check some things
1265 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1266 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1267 " a mistake. If you really mean it you will need to specify --force before\n"
1268 " setting the number of drives.\n");
1273 if (c
.homehost
== NULL
)
1274 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1275 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1276 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1277 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1278 c
.homehost
= sys_hostname
;
1281 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1283 c
.require_homehost
= 0;
1286 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1287 c
.homecluster
= conf_get_homecluster();
1288 if (c
.homecluster
== NULL
)
1289 rv
= get_cluster_name(&c
.homecluster
);
1291 pr_err("The md can't get cluster name\n");
1296 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1297 pr_err("--backup-file and --data-offset are incompatible\n");
1301 if ((mode
== MISC
&& devmode
== 'E')
1302 || (mode
== MONITOR
&& spare_sharing
== 0))
1303 /* Anyone may try this */;
1304 else if (geteuid() != 0) {
1305 pr_err("must be super-user to perform this action\n");
1309 ident
.autof
= c
.autof
;
1311 if (c
.scan
&& c
.verbose
< 2)
1312 /* --scan implied --brief unless -vv */
1318 /* readonly, add/remove, readwrite, runstop */
1320 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1321 if (!rv
&& devs_found
>1)
1322 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1323 devlist
->next
, c
.verbose
, c
.test
,
1325 if (!rv
&& c
.readonly
< 0)
1326 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1327 if (!rv
&& c
.runstop
> 0)
1328 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1329 if (!rv
&& c
.runstop
< 0)
1330 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1333 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1334 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1335 /* Only a device has been given, so get details from config file */
1336 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1337 if (array_ident
== NULL
) {
1338 pr_err("%s not identified in config file.\n",
1344 if (array_ident
->autof
== 0)
1345 array_ident
->autof
= c
.autof
;
1346 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1350 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1352 else if (devs_found
> 0) {
1353 if (c
.update
&& devs_found
> 1) {
1354 pr_err("can only update a single array at a time\n");
1357 if (c
.backup_file
&& devs_found
> 1) {
1358 pr_err("can only assemble a single array when providing a backup file.\n");
1361 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1362 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1363 if (array_ident
== NULL
) {
1364 pr_err("%s not identified in config file.\n",
1369 if (array_ident
->autof
== 0)
1370 array_ident
->autof
= c
.autof
;
1371 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1376 pr_err("--update not meaningful with a --scan assembly.\n");
1379 if (c
.backup_file
) {
1380 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1383 rv
= scan_assemble(ss
, &c
, &ident
);
1389 c
.delay
= DEFAULT_BITMAP_DELAY
;
1390 if (s
.write_behind
&& !s
.bitmap_file
) {
1391 pr_err("write-behind mode requires a bitmap.\n");
1395 if (s
.raiddisks
== 0) {
1396 pr_err("no raid-devices specified.\n");
1401 if (s
.bitmap_file
) {
1402 if (strcmp(s
.bitmap_file
, "internal")==0) {
1403 pr_err("'internal' bitmaps not supported with --build\n");
1408 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1412 c
.delay
= DEFAULT_BITMAP_DELAY
;
1415 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1416 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1422 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1428 if (s
.write_behind
&& !s
.bitmap_file
) {
1429 pr_err("write-behind mode requires a bitmap.\n");
1433 if (s
.raiddisks
== 0) {
1434 pr_err("no raid-devices specified.\n");
1439 rv
= Create(ss
, devlist
->devname
,
1440 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1441 devs_found
-1, devlist
->next
,
1442 &s
, &c
, data_offset
);
1445 if (devmode
== 'E') {
1446 if (devlist
== NULL
&& !c
.scan
) {
1447 pr_err("No devices to examine\n");
1450 if (devlist
== NULL
)
1451 devlist
= conf_get_devs();
1452 if (devlist
== NULL
) {
1453 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1456 rv
= Examine(devlist
, &c
, ss
);
1457 } else if (devmode
== DetailPlatform
) {
1458 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1459 c
.verbose
, c
.export
,
1460 devlist
? devlist
->devname
: NULL
);
1461 } else if (devlist
== NULL
) {
1462 if (devmode
== 'S' && c
.scan
)
1463 rv
= stop_scan(c
.verbose
);
1464 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1465 rv
= misc_scan(devmode
, &c
);
1466 else if (devmode
== UdevRules
)
1467 rv
= Write_rules(udev_filename
);
1469 pr_err("No devices given.\n");
1473 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1476 if (!devlist
&& !c
.scan
) {
1477 pr_err("Cannot monitor: need --scan or at least one device\n");
1481 if (pidfile
&& !daemonise
) {
1482 pr_err("Cannot write a pid file when not in daemon mode\n");
1487 if (get_linux_version() > 2006016)
1488 /* mdstat responds to poll */
1495 rv
= Monitor(devlist
, mailaddr
, program
,
1496 &c
, daemonise
, oneshot
,
1497 dosyslog
, pidfile
, increments
,
1502 if (array_size
> 0) {
1503 /* alway impose array size first, independent of
1505 * Do not allow level or raid_disks changes at the
1506 * same time as that can be irreversibly destructive.
1510 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1511 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1512 " Change size first, then check that data is still intact.\n");
1516 sysfs_init(&sra
, mdfd
, NULL
);
1517 if (array_size
== MAX_SIZE
)
1518 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1520 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1523 pr_err("--array-size setting is too large.\n");
1525 pr_err("current kernel does not support setting --array-size\n");
1530 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1532 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1533 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1537 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1538 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1543 } else if (s
.bitmap_file
) {
1544 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1545 s
.layout_str
!= NULL
|| devs_found
> 1) {
1546 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1551 c
.delay
= DEFAULT_BITMAP_DELAY
;
1552 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1553 } else if (grow_continue
)
1554 rv
= Grow_continue_command(devlist
->devname
,
1555 mdfd
, c
.backup_file
,
1557 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1558 || s
.chunk
!= 0 || s
.level
!= UnSet
1559 || data_offset
!= INVALID_SECTORS
) {
1560 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1562 data_offset
, &c
, &s
);
1563 } else if (array_size
== 0)
1564 pr_err("no changes to --grow\n");
1573 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1576 if (c
.runstop
<= 0) {
1577 pr_err("--incremental --scan meaningless without --run.\n");
1580 if (devmode
== 'f') {
1581 pr_err("--incremental --scan --fail not supported.\n");
1584 rv
= IncrementalScan(&c
, NULL
);
1587 if (!rebuild_map
&& !c
.scan
) {
1588 pr_err("--incremental requires a device.\n");
1593 if (devmode
== 'f') {
1594 if (devlist
->next
) {
1595 pr_err("'--incremental --fail' can only handle one device.\n");
1599 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1602 rv
= Incremental(devlist
, &c
, ss
);
1611 static int scan_assemble(struct supertype
*ss
,
1613 struct mddev_ident
*ident
)
1615 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1616 struct mddev_dev
*devlist
= conf_get_devs();
1617 struct map_ent
*map
= NULL
;
1620 int failures
, successes
;
1622 if (conf_verify_devnames(array_list
)) {
1623 pr_err("Duplicate MD device names in conf file were found.\n");
1626 if (devlist
== NULL
) {
1627 pr_err("No devices listed in conf file were found.\n");
1630 for (a
= array_list
; a
; a
= a
->next
) {
1633 a
->autof
= c
->autof
;
1636 pr_err("failed to get exclusive lock on mapfile\n");
1641 for (a
= array_list
; a
; a
= a
->next
) {
1646 strcasecmp(a
->devname
, "<ignore>") == 0)
1649 r
= Assemble(ss
, a
->devname
,
1659 } while (failures
&& successes
);
1660 if (c
->homehost
&& cnt
== 0) {
1661 /* Maybe we can auto-assemble something.
1662 * Repeatedly call Assemble in auto-assemble mode
1667 ident
->autof
= c
->autof
;
1669 struct mddev_dev
*devlist
= conf_get_devs();
1672 rv2
= Assemble(ss
, NULL
,
1680 /* Incase there are stacked devices, we need to go around again */
1682 if (cnt
== 0 && rv
== 0) {
1683 pr_err("No arrays found in config file or automatically\n");
1687 } else if (cnt
== 0 && rv
== 0) {
1688 pr_err("No arrays found in config file\n");
1695 static int misc_scan(char devmode
, struct context
*c
)
1697 /* apply --detail or --wait-clean to
1698 * all devices in /proc/mdstat
1700 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1701 struct mdstat_ent
*e
;
1702 struct map_ent
*map
= NULL
;
1706 for (members
= 0; members
<= 1; members
++) {
1707 for (e
=ms
; e
; e
=e
->next
) {
1711 int member
= e
->metadata_version
&&
1712 strncmp(e
->metadata_version
,
1713 "external:/", 10) == 0;
1714 if (members
!= member
)
1716 me
= map_by_devnm(&map
, e
->devnm
);
1718 && strcmp(me
->path
, "/unknown") != 0)
1721 stat(name
, &stb
) != 0)
1722 name
= get_md_name(e
->devnm
);
1725 pr_err("cannot find device file for %s\n",
1730 rv
|= Detail(name
, c
);
1732 rv
|= WaitClean(name
, -1, c
->verbose
);
1740 static int stop_scan(int verbose
)
1742 /* apply --stop to all devices in /proc/mdstat */
1743 /* Due to possible stacking of devices, repeat until
1744 * nothing more can be stopped
1746 int progress
=1, err
;
1750 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1751 struct mdstat_ent
*e
;
1753 if (!progress
) last
= 1;
1754 progress
= 0; err
= 0;
1755 for (e
=ms
; e
; e
=e
->next
) {
1756 char *name
= get_md_name(e
->devnm
);
1760 pr_err("cannot find device file for %s\n",
1764 mdfd
= open_mddev(name
, 1);
1766 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1776 } while (!last
&& err
);
1782 static int misc_list(struct mddev_dev
*devlist
,
1783 struct mddev_ident
*ident
,
1784 char *dump_directory
,
1785 struct supertype
*ss
, struct context
*c
)
1787 struct mddev_dev
*dv
;
1790 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1793 switch(dv
->disposition
) {
1795 rv
|= Detail(dv
->devname
, c
);
1797 case KillOpt
: /* Zero superblock */
1799 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1803 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1810 rv
|= Query(dv
->devname
); continue;
1812 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1814 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1817 rv
|= Wait(dv
->devname
); continue;
1819 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1821 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1823 case UpdateSubarray
:
1824 if (c
->update
== NULL
) {
1825 pr_err("-U/--update must be specified with --update-subarray\n");
1829 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1830 c
->update
, ident
, c
->verbose
);
1833 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1836 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1837 (dv
== devlist
&& dv
->next
== NULL
));
1840 rv
|= SetAction(dv
->devname
, c
->action
);
1843 if (dv
->devname
[0] == '/')
1844 mdfd
= open_mddev(dv
->devname
, 1);
1846 mdfd
= open_dev(dv
->devname
);
1848 pr_err("Cannot open %s\n", dv
->devname
);
1851 switch(dv
->disposition
) {
1854 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1856 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1858 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1860 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1869 int SetAction(char *dev
, char *action
)
1871 int fd
= open(dev
, O_RDONLY
);
1874 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1877 sysfs_init(&mdi
, fd
, NULL
);
1879 if (!mdi
.sys_name
[0]) {
1880 pr_err("%s is no an md array\n", dev
);
1884 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1885 pr_err("Count not set action for %s to %s: %s\n",
1886 dev
, action
, strerror(errno
));