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 */
202 shortopt
= short_bitmap_options
;
206 case 'A': newmode
= ASSEMBLE
;
207 shortopt
= short_bitmap_auto_options
;
209 case 'B': newmode
= BUILD
;
210 shortopt
= short_bitmap_auto_options
;
212 case 'C': newmode
= CREATE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'F': newmode
= MONITOR
;
217 case 'G': newmode
= GROW
;
218 shortopt
= short_bitmap_options
;
220 case 'I': newmode
= INCREMENTAL
;
221 shortopt
= short_bitmap_auto_options
;
224 newmode
= AUTODETECT
;
259 if (mode
&& newmode
== mode
) {
260 /* everybody happy ! */
261 } else if (mode
&& newmode
!= mode
) {
264 if (option_index
>= 0)
265 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
267 fprintf(stderr
, "-%c", opt
);
268 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
269 map_num(modes
, newmode
),
270 map_num(modes
, mode
));
272 } else if (!mode
&& newmode
) {
274 if (mode
== MISC
&& devs_found
) {
275 pr_err("No action given for %s in --misc mode\n",
277 cont_err("Action options must come before device names\n");
281 /* special case of -c --help */
282 if ((opt
== 'c' || opt
== ConfigFile
) &&
283 ( strncmp(optarg
, "--h", 3)==0 ||
284 strncmp(optarg
, "-h", 2)==0)) {
285 fputs(Help_config
, stdout
);
289 /* If first option is a device, don't force the mode yet */
291 if (devs_found
== 0) {
292 dv
= xmalloc(sizeof(*dv
));
293 dv
->devname
= optarg
;
294 dv
->disposition
= devmode
;
295 dv
->writemostly
= writemostly
;
299 devlistend
= &dv
->next
;
304 /* No mode yet, and this is the second device ... */
305 pr_err("An option must be given to set the mode before a second device\n"
306 " (%s) is listed\n", optarg
);
309 if (option_index
>= 0)
310 pr_err("--%s", long_options
[option_index
].name
);
313 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
317 /* if we just set the mode, then done */
331 /* an undecorated option - must be a device name.
334 if (devs_found
> 0 && devmode
== DetailPlatform
) {
335 pr_err("controller may only be specified once. %s ignored\n",
340 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
341 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
344 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
345 pr_err("Must give -a/--add for devices to add: %s\n", optarg
);
348 dv
= xmalloc(sizeof(*dv
));
349 dv
->devname
= optarg
;
350 dv
->disposition
= devmode
;
351 dv
->writemostly
= writemostly
;
355 devlistend
= &dv
->next
;
361 /* We've got a mode, and opt is now something else which
362 * could depend on the mode */
363 #define O(a,b) ((a<<16)|b)
364 switch (O(mode
,opt
)) {
366 case O(GROW
,ChunkSize
):
368 case O(CREATE
,ChunkSize
):
369 case O(BUILD
,'c'): /* chunk or rounding */
370 case O(BUILD
,ChunkSize
): /* chunk or rounding */
372 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg
);
375 s
.chunk
= parse_size(optarg
);
376 if (s
.chunk
== INVALID_SECTORS
||
377 s
.chunk
< 8 || (s
.chunk
&1)) {
378 pr_err("invalid chunk/rounding value: %s\n",
382 /* Convert sectors to K */
386 case O(INCREMENTAL
, 'e'):
388 case O(ASSEMBLE
,'e'):
389 case O(MISC
,'e'): /* set metadata (superblock) information */
391 pr_err("metadata information already given\n");
394 for(i
=0; !ss
&& superlist
[i
]; i
++)
395 ss
= superlist
[i
]->match_metadata_desc(optarg
);
398 pr_err("unrecognised metadata identifier: %s\n", optarg
);
404 case O(MANAGE
,WriteMostly
):
406 case O(BUILD
,WriteMostly
):
408 case O(CREATE
,WriteMostly
):
409 /* set write-mostly for following devices */
414 /* clear write-mostly for following devices */
420 case O(BUILD
,'z'): /* size */
422 pr_err("size may only be specified once. Second value is %s.\n", optarg
);
425 if (strcmp(optarg
, "max")==0)
428 s
.size
= parse_size(optarg
);
429 if (s
.size
== INVALID_SECTORS
||
431 pr_err("invalid size: %s\n",
435 /* convert sectors to K */
440 case O(GROW
,'Z'): /* array size */
441 if (array_size
> 0) {
442 pr_err("array-size may only be specified once. Second value is %s.\n", optarg
);
445 if (strcmp(optarg
, "max") == 0)
446 array_size
= MAX_SIZE
;
448 array_size
= parse_size(optarg
);
449 if (array_size
== 0 ||
450 array_size
== INVALID_SECTORS
) {
451 pr_err("invalid array size: %s\n",
458 case O(CREATE
,DataOffset
):
459 case O(GROW
,DataOffset
):
460 if (data_offset
!= INVALID_SECTORS
) {
461 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg
);
464 if (mode
== CREATE
&&
465 strcmp(optarg
, "variable") == 0)
466 data_offset
= VARIABLE_OFFSET
;
468 data_offset
= parse_size(optarg
);
469 if (data_offset
== INVALID_SECTORS
) {
470 pr_err("invalid data-offset: %s\n",
478 case O(BUILD
,'l'): /* set raid level*/
479 if (s
.level
!= UnSet
) {
480 pr_err("raid level may only be set once. Second value is %s.\n", optarg
);
483 s
.level
= map_name(pers
, optarg
);
484 if (s
.level
== UnSet
) {
485 pr_err("invalid raid level: %s\n",
489 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
490 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
493 pr_err("Raid level %s not permitted with --build.\n",
497 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
498 pr_err("raid level %s is incompatible with spare-devices setting.\n",
502 ident
.level
= s
.level
;
505 case O(GROW
, 'p'): /* new layout */
506 case O(GROW
, Layout
):
508 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
511 s
.layout_str
= optarg
;
512 /* 'Grow' will parse the value */
515 case O(CREATE
,'p'): /* raid5 layout */
516 case O(CREATE
,Layout
):
517 case O(BUILD
,'p'): /* faulty layout */
518 case O(BUILD
,Layout
):
519 if (s
.layout
!= UnSet
) {
520 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
525 pr_err("layout not meaningful for %s arrays.\n",
526 map_num(pers
, s
.level
));
529 pr_err("raid level must be given before layout.\n");
533 s
.layout
= map_name(r5layout
, optarg
);
534 if (s
.layout
==UnSet
) {
535 pr_err("layout %s not understood for raid5.\n",
541 s
.layout
= map_name(r6layout
, optarg
);
542 if (s
.layout
==UnSet
) {
543 pr_err("layout %s not understood for raid6.\n",
550 s
.layout
= parse_layout_10(optarg
);
552 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
560 s
.layout
= parse_layout_faulty(optarg
);
561 if (s
.layout
== -1) {
562 pr_err("layout %s not understood for faulty.\n",
570 case O(CREATE
,AssumeClean
):
571 case O(BUILD
,AssumeClean
): /* assume clean */
572 case O(GROW
,AssumeClean
):
578 case O(BUILD
,'n'): /* number of raid disks */
580 pr_err("raid-devices set twice: %d and %s\n",
581 s
.raiddisks
, optarg
);
584 s
.raiddisks
= parse_num(optarg
);
585 if (s
.raiddisks
<= 0) {
586 pr_err("invalid number of raid devices: %s\n",
590 ident
.raid_disks
= s
.raiddisks
;
592 case O(CREATE
, Nodes
):
593 c
.nodes
= parse_num(optarg
);
595 pr_err("invalid number for the number of cluster nodes: %s\n",
600 case O(CREATE
, ClusterName
):
601 case O(ASSEMBLE
, ClusterName
):
602 c
.homecluster
= optarg
;
603 if (strlen(c
.homecluster
) > 64) {
604 pr_err("Cluster name too big.\n");
608 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
610 pr_err("spare-devices set twice: %d and %s\n",
611 s
.sparedisks
, optarg
);
614 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
615 pr_err("spare-devices setting is incompatible with raid level %d\n",
619 s
.sparedisks
= parse_num(optarg
);
620 if (s
.sparedisks
< 0) {
621 pr_err("invalid number of spare-devices: %s\n",
631 case O(INCREMENTAL
,'a'):
632 case O(INCREMENTAL
,Auto
):
633 case O(ASSEMBLE
,'a'):
634 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
635 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
638 case O(CREATE
,Symlinks
):
639 case O(BUILD
,Symlinks
):
640 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
644 case O(BUILD
,'f'): /* force honouring '-n 1' */
645 case O(BUILD
,Force
): /* force honouring '-n 1' */
646 case O(GROW
,'f'): /* ditto */
647 case O(GROW
,Force
): /* ditto */
648 case O(CREATE
,'f'): /* force honouring of device list */
649 case O(CREATE
,Force
): /* force honouring of device list */
650 case O(ASSEMBLE
,'f'): /* force assembly */
651 case O(ASSEMBLE
,Force
): /* force assembly */
652 case O(MISC
,'f'): /* force zero */
653 case O(MISC
,Force
): /* force zero */
654 case O(MANAGE
,Force
): /* add device which is too large */
657 /* now for the Assemble options */
658 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
660 case O(INCREMENTAL
, FreezeReshape
):
661 c
.freeze_reshape
= 1;
663 case O(CREATE
,'u'): /* uuid of array */
664 case O(ASSEMBLE
,'u'): /* uuid of array */
665 if (ident
.uuid_set
) {
666 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
669 if (parse_uuid(optarg
, ident
.uuid
))
672 pr_err("Bad uuid: %s\n", optarg
);
678 case O(ASSEMBLE
,'N'):
681 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
684 if (mode
== MISC
&& !c
.subarray
) {
685 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
688 if (strlen(optarg
) > 32) {
689 pr_err("name '%s' is too long, 32 chars max.\n",
693 strcpy(ident
.name
, optarg
);
696 case O(ASSEMBLE
,'m'): /* super-minor for array */
697 case O(ASSEMBLE
,SuperMinor
):
698 if (ident
.super_minor
!= UnSet
) {
699 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
702 if (strcmp(optarg
, "dev")==0)
703 ident
.super_minor
= -2;
705 ident
.super_minor
= parse_num(optarg
);
706 if (ident
.super_minor
< 0) {
707 pr_err("Bad super-minor number: %s.\n", optarg
);
713 case O(ASSEMBLE
,'o'):
719 case O(ASSEMBLE
,'U'): /* update the superblock */
722 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
726 if (mode
== MISC
&& !c
.subarray
) {
727 pr_err("Only subarrays can be updated in misc mode\n");
731 if (strcmp(c
.update
, "sparc2.2")==0)
733 if (strcmp(c
.update
, "super-minor") == 0)
735 if (strcmp(c
.update
, "summaries")==0)
737 if (strcmp(c
.update
, "resync")==0)
739 if (strcmp(c
.update
, "uuid")==0)
741 if (strcmp(c
.update
, "name")==0)
743 if (strcmp(c
.update
, "homehost")==0)
745 if (strcmp(c
.update
, "home-cluster")==0)
747 if (strcmp(c
.update
, "devicesize")==0)
749 if (strcmp(c
.update
, "no-bitmap")==0)
751 if (strcmp(c
.update
, "bbl") == 0)
753 if (strcmp(c
.update
, "no-bbl") == 0)
755 if (strcmp(c
.update
, "metadata") == 0)
757 if (strcmp(c
.update
, "revert-reshape") == 0)
759 if (strcmp(c
.update
, "byteorder")==0) {
761 pr_err("must not set metadata type with --update=byteorder.\n");
764 for(i
=0; !ss
&& superlist
[i
]; i
++)
765 ss
= superlist
[i
]->match_metadata_desc(
768 pr_err("INTERNAL ERROR cannot find 0.swap\n");
774 if (strcmp(c
.update
,"?") == 0 ||
775 strcmp(c
.update
, "help") == 0) {
777 fprintf(outf
, "%s: ", Name
);
781 "%s: '--update=%s' is invalid. ",
784 fprintf(outf
, "Valid --update options are:\n"
785 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
786 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
787 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
790 exit(outf
== stdout
? 0 : 2);
793 /* update=devicesize is allowed with --re-add */
794 if (devmode
!= 'A') {
795 pr_err("--update in Manage mode only allowed with --re-add.\n");
799 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
804 if (strcmp(c
.update
, "devicesize") != 0 &&
805 strcmp(c
.update
, "bbl") != 0 &&
806 strcmp(c
.update
, "no-bbl") != 0) {
807 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
812 case O(INCREMENTAL
,NoDegraded
):
813 pr_err("--no-degraded is deprecated in Incremental mode\n");
814 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
815 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
816 * so we overload slightly */
819 case O(ASSEMBLE
,'c'):
820 case O(ASSEMBLE
,ConfigFile
):
821 case O(INCREMENTAL
, 'c'):
822 case O(INCREMENTAL
, ConfigFile
):
824 case O(MISC
, ConfigFile
):
826 case O(MONITOR
,ConfigFile
):
827 case O(CREATE
,ConfigFile
):
829 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
833 set_conffile(configfile
);
834 /* FIXME possibly check that config file exists. Even parse it */
836 case O(ASSEMBLE
,'s'): /* scan */
839 case O(INCREMENTAL
,'s'):
843 case O(MONITOR
,'m'): /* mail address */
844 case O(MONITOR
,EMail
):
846 pr_err("only specify one mailaddress. %s ignored.\n",
852 case O(MONITOR
,'p'): /* alert program */
853 case O(MONITOR
,ProgramOpt
): /* alert program */
855 pr_err("only specify one alter program. %s ignored.\n",
861 case O(MONITOR
,'r'): /* rebuild increments */
862 case O(MONITOR
,Increment
):
863 increments
= atoi(optarg
);
864 if (increments
> 99 || increments
< 1) {
865 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
870 case O(MONITOR
,'d'): /* delay in seconds */
872 case O(BUILD
,'d'): /* delay for bitmap updates */
875 pr_err("only specify delay once. %s ignored.\n",
878 c
.delay
= parse_num(optarg
);
880 pr_err("invalid delay: %s\n",
886 case O(MONITOR
,'f'): /* daemonise */
887 case O(MONITOR
,Fork
):
890 case O(MONITOR
,'i'): /* pid */
892 pr_err("only specify one pid file. %s ignored.\n",
897 case O(MONITOR
,'1'): /* oneshot */
901 case O(MONITOR
,'t'): /* test */
904 case O(MONITOR
,'y'): /* log messages to syslog */
905 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
908 case O(MONITOR
, NoSharing
):
912 /* now the general management options. Some are applicable
913 * to other modes. None have arguments.
918 case O(MANAGE
,Add
): /* add a drive */
921 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
924 case O(MANAGE
,ReAdd
):
927 case O(MANAGE
,'r'): /* remove a drive */
928 case O(MANAGE
,Remove
):
931 case O(MANAGE
,'f'): /* set faulty */
933 case O(INCREMENTAL
,'f'):
934 case O(INCREMENTAL
,Remove
):
935 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
936 * even though we will both fail and
937 * remove the device */
940 case O(MANAGE
, ClusterConfirm
):
943 case O(MANAGE
,Replace
):
944 /* Mark these devices for replacement */
948 /* These are the replacements to use */
949 if (devmode
!= 'R') {
950 pr_err("--with must follow --replace\n");
955 case O(INCREMENTAL
,'R'):
957 case O(ASSEMBLE
,'R'):
959 case O(CREATE
,'R'): /* Run the array */
961 pr_err("Cannot both Stop and Run an array\n");
968 pr_err("Cannot both Run and Stop an array\n");
980 case O(MISC
,KillOpt
):
984 case O(MISC
, ExamineBB
):
988 case O(MISC
, WaitOpt
):
989 case O(MISC
, Waitclean
):
990 case O(MISC
, DetailPlatform
):
991 case O(MISC
, KillSubarray
):
992 case O(MISC
, UpdateSubarray
):
994 case O(MISC
, Restore
):
995 case O(MISC
,Action
):
996 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
998 pr_err("subarray can only be specified once\n");
1001 c
.subarray
= optarg
;
1003 if (opt
== Action
) {
1005 pr_err("Only one --action can be specified\n");
1008 if (strcmp(optarg
, "idle") == 0 ||
1009 strcmp(optarg
, "frozen") == 0 ||
1010 strcmp(optarg
, "check") == 0 ||
1011 strcmp(optarg
, "repair") == 0)
1014 pr_err("action must be one of idle, frozen, check, repair\n");
1018 if (devmode
&& devmode
!= opt
&&
1019 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1020 pr_err("--examine/-E cannot be given with ");
1021 if (devmode
== 'E') {
1022 if (option_index
>= 0)
1023 fprintf(stderr
, "--%s\n",
1024 long_options
[option_index
].name
);
1026 fprintf(stderr
, "-%c\n", opt
);
1027 } else if (isalpha(devmode
))
1028 fprintf(stderr
, "-%c\n", devmode
);
1030 fprintf(stderr
, "previous option\n");
1034 if (opt
== Dump
|| opt
== Restore
) {
1035 if (dump_directory
!= NULL
) {
1036 pr_err("dump/restore directory specified twice: %s and %s\n",
1037 dump_directory
, optarg
);
1040 dump_directory
= optarg
;
1043 case O(MISC
, UdevRules
):
1044 if (devmode
&& devmode
!= opt
) {
1045 pr_err("--udev-rules must be the only option.\n");
1048 pr_err("only specify one udev rule filename. %s ignored.\n",
1051 udev_filename
= optarg
;
1059 case O(MISC
, Sparc22
):
1060 if (devmode
!= 'E') {
1061 pr_err("--sparc2.2 only allowed with --examine\n");
1067 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1068 case O(ASSEMBLE
,Bitmap
):
1070 pr_err("bitmap file needed with -b in --assemble mode\n");
1073 if (strcmp(optarg
, "internal")==0) {
1074 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1077 bitmap_fd
= open(optarg
, O_RDWR
);
1078 if (!*optarg
|| bitmap_fd
< 0) {
1079 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1082 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1085 case O(ASSEMBLE
, BackupFile
):
1086 case O(GROW
, BackupFile
):
1087 /* Specify a file into which grow might place a backup,
1088 * or from which assemble might recover a backup
1090 if (c
.backup_file
) {
1091 pr_err("backup file already specified, rejecting %s\n", optarg
);
1094 c
.backup_file
= optarg
;
1097 case O(GROW
, Continue
):
1098 /* Continue interrupted grow
1102 case O(ASSEMBLE
, InvalidBackup
):
1103 /* Acknowledge that the backupfile is invalid, but ask
1104 * to continue anyway
1106 c
.invalid_backup
= 1;
1110 case O(BUILD
,Bitmap
):
1112 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1114 case O(GROW
,Bitmap
):
1115 if (strcmp(optarg
, "internal")== 0 ||
1116 strcmp(optarg
, "none")== 0 ||
1117 strchr(optarg
, '/') != NULL
) {
1118 s
.bitmap_file
= optarg
;
1121 if (strcmp(optarg
, "clustered")== 0) {
1122 s
.bitmap_file
= optarg
;
1123 /* Set the default number of cluster nodes
1124 * to 4 if not already set by user
1131 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1132 " not '%s'\n", optarg
);
1135 case O(GROW
,BitmapChunk
):
1136 case O(BUILD
,BitmapChunk
):
1137 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1138 s
.bitmap_chunk
= parse_size(optarg
);
1139 if (s
.bitmap_chunk
== 0 ||
1140 s
.bitmap_chunk
== INVALID_SECTORS
||
1141 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1142 pr_err("invalid bitmap chunksize: %s\n",
1146 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1149 case O(GROW
, WriteBehind
):
1150 case O(BUILD
, WriteBehind
):
1151 case O(CREATE
, WriteBehind
): /* write-behind mode */
1152 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1154 s
.write_behind
= parse_num(optarg
);
1155 if (s
.write_behind
< 0 ||
1156 s
.write_behind
> 16383) {
1157 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1163 case O(INCREMENTAL
, 'r'):
1164 case O(INCREMENTAL
, RebuildMapOpt
):
1167 case O(INCREMENTAL
, IncrementalPath
):
1168 remove_path
= optarg
;
1171 /* We have now processed all the valid options. Anything else is
1174 if (option_index
> 0)
1175 pr_err(":option --%s not valid in %s mode\n",
1176 long_options
[option_index
].name
,
1177 map_num(modes
, mode
));
1179 pr_err("option -%c not valid in %s mode\n",
1180 opt
, map_num(modes
, mode
));
1187 if (print_help
== 2)
1188 help_text
= OptionHelp
;
1190 help_text
= mode_help
[mode
];
1191 if (help_text
== NULL
)
1193 fputs(help_text
,stdout
);
1197 if (!mode
&& devs_found
) {
1200 if (devlist
->disposition
== 0)
1201 devlist
->disposition
= devmode
;
1204 fputs(Usage
, stderr
);
1209 struct createinfo
*ci
= conf_get_create_info();
1211 if (strcasecmp(symlinks
, "yes") == 0)
1213 else if (strcasecmp(symlinks
, "no") == 0)
1216 pr_err("option --symlinks must be 'no' or 'yes'\n");
1220 /* Ok, got the option parsing out of the way
1221 * hopefully it's mostly right but there might be some stuff
1224 * That is mosty checked in the per-mode stuff but...
1226 * For @,B,C and A without -s, the first device listed must be
1227 * an md device. We check that here and open it.
1230 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1232 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1233 if (devs_found
< 1) {
1234 pr_err("an md device must be given in this mode\n");
1237 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1238 pr_err("--super-minor=dev is incompatible with --auto\n");
1241 if (mode
== MANAGE
|| mode
== GROW
) {
1242 mdfd
= open_mddev(devlist
->devname
, 1);
1246 /* non-existent device is OK */
1247 mdfd
= open_mddev(devlist
->devname
, 0);
1249 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1252 if ((int)ident
.super_minor
== -2) {
1255 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1260 ident
.super_minor
= minor(stb
.st_rdev
);
1262 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1263 /* We don't really want this open yet, we just might
1264 * have wanted to check some things
1272 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1273 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1274 " a mistake. If you really mean it you will need to specify --force before\n"
1275 " setting the number of drives.\n");
1280 if (c
.homehost
== NULL
)
1281 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1282 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1283 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1284 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1285 c
.homehost
= sys_hostname
;
1288 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1290 c
.require_homehost
= 0;
1293 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1294 c
.homecluster
= conf_get_homecluster();
1295 if (c
.homecluster
== NULL
)
1296 rv
= get_cluster_name(&c
.homecluster
);
1298 pr_err("The md can't get cluster name\n");
1303 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1304 pr_err("--backup-file and --data-offset are incompatible\n");
1308 if ((mode
== MISC
&& devmode
== 'E')
1309 || (mode
== MONITOR
&& spare_sharing
== 0))
1310 /* Anyone may try this */;
1311 else if (geteuid() != 0) {
1312 pr_err("must be super-user to perform this action\n");
1316 ident
.autof
= c
.autof
;
1318 if (c
.scan
&& c
.verbose
< 2)
1319 /* --scan implied --brief unless -vv */
1325 /* readonly, add/remove, readwrite, runstop */
1327 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1328 if (!rv
&& devs_found
>1)
1329 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1330 devlist
->next
, c
.verbose
, c
.test
,
1332 if (!rv
&& c
.readonly
< 0)
1333 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1334 if (!rv
&& c
.runstop
> 0)
1335 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1336 if (!rv
&& c
.runstop
< 0)
1337 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1340 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1341 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1342 /* Only a device has been given, so get details from config file */
1343 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1344 if (array_ident
== NULL
) {
1345 pr_err("%s not identified in config file.\n",
1351 if (array_ident
->autof
== 0)
1352 array_ident
->autof
= c
.autof
;
1353 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1357 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1359 else if (devs_found
> 0) {
1360 if (c
.update
&& devs_found
> 1) {
1361 pr_err("can only update a single array at a time\n");
1364 if (c
.backup_file
&& devs_found
> 1) {
1365 pr_err("can only assemble a single array when providing a backup file.\n");
1368 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1369 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1370 if (array_ident
== NULL
) {
1371 pr_err("%s not identified in config file.\n",
1376 if (array_ident
->autof
== 0)
1377 array_ident
->autof
= c
.autof
;
1378 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1383 pr_err("--update not meaningful with a --scan assembly.\n");
1386 if (c
.backup_file
) {
1387 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1390 rv
= scan_assemble(ss
, &c
, &ident
);
1396 c
.delay
= DEFAULT_BITMAP_DELAY
;
1397 if (s
.write_behind
&& !s
.bitmap_file
) {
1398 pr_err("write-behind mode requires a bitmap.\n");
1402 if (s
.raiddisks
== 0) {
1403 pr_err("no raid-devices specified.\n");
1408 if (s
.bitmap_file
) {
1409 if (strcmp(s
.bitmap_file
, "internal")==0) {
1410 pr_err("'internal' bitmaps not supported with --build\n");
1415 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1419 c
.delay
= DEFAULT_BITMAP_DELAY
;
1422 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1423 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1429 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1435 if (s
.write_behind
&& !s
.bitmap_file
) {
1436 pr_err("write-behind mode requires a bitmap.\n");
1440 if (s
.raiddisks
== 0) {
1441 pr_err("no raid-devices specified.\n");
1446 rv
= Create(ss
, devlist
->devname
,
1447 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1448 devs_found
-1, devlist
->next
,
1449 &s
, &c
, data_offset
);
1452 if (devmode
== 'E') {
1453 if (devlist
== NULL
&& !c
.scan
) {
1454 pr_err("No devices to examine\n");
1457 if (devlist
== NULL
)
1458 devlist
= conf_get_devs();
1459 if (devlist
== NULL
) {
1460 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1463 rv
= Examine(devlist
, &c
, ss
);
1464 } else if (devmode
== DetailPlatform
) {
1465 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1466 c
.verbose
, c
.export
,
1467 devlist
? devlist
->devname
: NULL
);
1468 } else if (devlist
== NULL
) {
1469 if (devmode
== 'S' && c
.scan
)
1470 rv
= stop_scan(c
.verbose
);
1471 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1472 rv
= misc_scan(devmode
, &c
);
1473 else if (devmode
== UdevRules
)
1474 rv
= Write_rules(udev_filename
);
1476 pr_err("No devices given.\n");
1480 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1483 if (!devlist
&& !c
.scan
) {
1484 pr_err("Cannot monitor: need --scan or at least one device\n");
1488 if (pidfile
&& !daemonise
) {
1489 pr_err("Cannot write a pid file when not in daemon mode\n");
1494 if (get_linux_version() > 2006016)
1495 /* mdstat responds to poll */
1502 rv
= Monitor(devlist
, mailaddr
, program
,
1503 &c
, daemonise
, oneshot
,
1504 dosyslog
, pidfile
, increments
,
1509 if (array_size
> 0) {
1510 /* alway impose array size first, independent of
1512 * Do not allow level or raid_disks changes at the
1513 * same time as that can be irreversibly destructive.
1517 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1518 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1519 " Change size first, then check that data is still intact.\n");
1523 sysfs_init(&sra
, mdfd
, NULL
);
1524 if (array_size
== MAX_SIZE
)
1525 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1527 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1530 pr_err("--array-size setting is too large.\n");
1532 pr_err("current kernel does not support setting --array-size\n");
1537 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1539 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1540 pr_err("--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
,
1550 } else if (s
.bitmap_file
) {
1551 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1552 s
.layout_str
!= NULL
|| devs_found
> 1) {
1553 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1558 c
.delay
= DEFAULT_BITMAP_DELAY
;
1559 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1560 } else if (grow_continue
)
1561 rv
= Grow_continue_command(devlist
->devname
,
1562 mdfd
, c
.backup_file
,
1564 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1565 || s
.chunk
!= 0 || s
.level
!= UnSet
1566 || data_offset
!= INVALID_SECTORS
) {
1567 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1569 data_offset
, &c
, &s
);
1570 } else if (array_size
== 0)
1571 pr_err("no changes to --grow\n");
1580 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1583 if (c
.runstop
<= 0) {
1584 pr_err("--incremental --scan meaningless without --run.\n");
1587 if (devmode
== 'f') {
1588 pr_err("--incremental --scan --fail not supported.\n");
1591 rv
= IncrementalScan(&c
, NULL
);
1594 if (!rebuild_map
&& !c
.scan
) {
1595 pr_err("--incremental requires a device.\n");
1600 if (devmode
== 'f') {
1601 if (devlist
->next
) {
1602 pr_err("'--incremental --fail' can only handle one device.\n");
1606 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1609 rv
= Incremental(devlist
, &c
, ss
);
1618 static int scan_assemble(struct supertype
*ss
,
1620 struct mddev_ident
*ident
)
1622 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1623 struct mddev_dev
*devlist
= conf_get_devs();
1624 struct map_ent
*map
= NULL
;
1627 int failures
, successes
;
1629 if (conf_verify_devnames(array_list
)) {
1630 pr_err("Duplicate MD device names in conf file were found.\n");
1633 if (devlist
== NULL
) {
1634 pr_err("No devices listed in conf file were found.\n");
1637 for (a
= array_list
; a
; a
= a
->next
) {
1640 a
->autof
= c
->autof
;
1643 pr_err("failed to get exclusive lock on mapfile\n");
1648 for (a
= array_list
; a
; a
= a
->next
) {
1653 strcasecmp(a
->devname
, "<ignore>") == 0)
1656 r
= Assemble(ss
, a
->devname
,
1666 } while (failures
&& successes
);
1667 if (c
->homehost
&& cnt
== 0) {
1668 /* Maybe we can auto-assemble something.
1669 * Repeatedly call Assemble in auto-assemble mode
1674 ident
->autof
= c
->autof
;
1676 struct mddev_dev
*devlist
= conf_get_devs();
1679 rv2
= Assemble(ss
, NULL
,
1687 /* Incase there are stacked devices, we need to go around again */
1689 if (cnt
== 0 && rv
== 0) {
1690 pr_err("No arrays found in config file or automatically\n");
1694 } else if (cnt
== 0 && rv
== 0) {
1695 pr_err("No arrays found in config file\n");
1702 static int misc_scan(char devmode
, struct context
*c
)
1704 /* apply --detail or --wait-clean to
1705 * all devices in /proc/mdstat
1707 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1708 struct mdstat_ent
*e
;
1709 struct map_ent
*map
= NULL
;
1713 for (members
= 0; members
<= 1; members
++) {
1714 for (e
=ms
; e
; e
=e
->next
) {
1718 int member
= e
->metadata_version
&&
1719 strncmp(e
->metadata_version
,
1720 "external:/", 10) == 0;
1721 if (members
!= member
)
1723 me
= map_by_devnm(&map
, e
->devnm
);
1725 && strcmp(me
->path
, "/unknown") != 0)
1728 stat(name
, &stb
) != 0)
1729 name
= get_md_name(e
->devnm
);
1732 pr_err("cannot find device file for %s\n",
1737 rv
|= Detail(name
, c
);
1739 rv
|= WaitClean(name
, -1, c
->verbose
);
1747 static int stop_scan(int verbose
)
1749 /* apply --stop to all devices in /proc/mdstat */
1750 /* Due to possible stacking of devices, repeat until
1751 * nothing more can be stopped
1753 int progress
=1, err
;
1757 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1758 struct mdstat_ent
*e
;
1760 if (!progress
) last
= 1;
1761 progress
= 0; err
= 0;
1762 for (e
=ms
; e
; e
=e
->next
) {
1763 char *name
= get_md_name(e
->devnm
);
1767 pr_err("cannot find device file for %s\n",
1771 mdfd
= open_mddev(name
, 1);
1773 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1783 } while (!last
&& err
);
1789 static int misc_list(struct mddev_dev
*devlist
,
1790 struct mddev_ident
*ident
,
1791 char *dump_directory
,
1792 struct supertype
*ss
, struct context
*c
)
1794 struct mddev_dev
*dv
;
1797 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1800 switch(dv
->disposition
) {
1802 rv
|= Detail(dv
->devname
, c
);
1804 case KillOpt
: /* Zero superblock */
1806 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1810 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1817 rv
|= Query(dv
->devname
); continue;
1819 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1821 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1824 rv
|= Wait(dv
->devname
); continue;
1826 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1828 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1830 case UpdateSubarray
:
1831 if (c
->update
== NULL
) {
1832 pr_err("-U/--update must be specified with --update-subarray\n");
1836 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1837 c
->update
, ident
, c
->verbose
);
1840 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1843 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1844 (dv
== devlist
&& dv
->next
== NULL
));
1847 rv
|= SetAction(dv
->devname
, c
->action
);
1850 if (dv
->devname
[0] == '/')
1851 mdfd
= open_mddev(dv
->devname
, 1);
1853 mdfd
= open_dev(dv
->devname
);
1855 pr_err("Cannot open %s\n", dv
->devname
);
1858 switch(dv
->disposition
) {
1861 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1863 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1865 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1867 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1876 int SetAction(char *dev
, char *action
)
1878 int fd
= open(dev
, O_RDONLY
);
1881 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1884 sysfs_init(&mdi
, fd
, NULL
);
1886 if (!mdi
.sys_name
[0]) {
1887 pr_err("%s is no an md array\n", dev
);
1891 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1892 pr_err("Count not set action for %s to %s: %s\n",
1893 dev
, action
, strerror(errno
));