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(ASSEMBLE
, Nodes
):
593 case O(CREATE
, Nodes
):
594 c
.nodes
= parse_num(optarg
);
596 pr_err("invalid number for the number of cluster nodes: %s\n",
601 case O(CREATE
, ClusterName
):
602 case O(ASSEMBLE
, ClusterName
):
603 c
.homecluster
= optarg
;
604 if (strlen(c
.homecluster
) > 64) {
605 pr_err("Cluster name too big.\n");
609 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
611 pr_err("spare-devices set twice: %d and %s\n",
612 s
.sparedisks
, optarg
);
615 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
616 pr_err("spare-devices setting is incompatible with raid level %d\n",
620 s
.sparedisks
= parse_num(optarg
);
621 if (s
.sparedisks
< 0) {
622 pr_err("invalid number of spare-devices: %s\n",
632 case O(INCREMENTAL
,'a'):
633 case O(INCREMENTAL
,Auto
):
634 case O(ASSEMBLE
,'a'):
635 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
636 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
639 case O(CREATE
,Symlinks
):
640 case O(BUILD
,Symlinks
):
641 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
645 case O(BUILD
,'f'): /* force honouring '-n 1' */
646 case O(BUILD
,Force
): /* force honouring '-n 1' */
647 case O(GROW
,'f'): /* ditto */
648 case O(GROW
,Force
): /* ditto */
649 case O(CREATE
,'f'): /* force honouring of device list */
650 case O(CREATE
,Force
): /* force honouring of device list */
651 case O(ASSEMBLE
,'f'): /* force assembly */
652 case O(ASSEMBLE
,Force
): /* force assembly */
653 case O(MISC
,'f'): /* force zero */
654 case O(MISC
,Force
): /* force zero */
655 case O(MANAGE
,Force
): /* add device which is too large */
658 /* now for the Assemble options */
659 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
661 case O(INCREMENTAL
, FreezeReshape
):
662 c
.freeze_reshape
= 1;
664 case O(CREATE
,'u'): /* uuid of array */
665 case O(ASSEMBLE
,'u'): /* uuid of array */
666 if (ident
.uuid_set
) {
667 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
670 if (parse_uuid(optarg
, ident
.uuid
))
673 pr_err("Bad uuid: %s\n", optarg
);
679 case O(ASSEMBLE
,'N'):
682 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
685 if (mode
== MISC
&& !c
.subarray
) {
686 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
689 if (strlen(optarg
) > 32) {
690 pr_err("name '%s' is too long, 32 chars max.\n",
694 strcpy(ident
.name
, optarg
);
697 case O(ASSEMBLE
,'m'): /* super-minor for array */
698 case O(ASSEMBLE
,SuperMinor
):
699 if (ident
.super_minor
!= UnSet
) {
700 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
703 if (strcmp(optarg
, "dev")==0)
704 ident
.super_minor
= -2;
706 ident
.super_minor
= parse_num(optarg
);
707 if (ident
.super_minor
< 0) {
708 pr_err("Bad super-minor number: %s.\n", optarg
);
714 case O(ASSEMBLE
,'o'):
720 case O(ASSEMBLE
,'U'): /* update the superblock */
723 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
727 if (mode
== MISC
&& !c
.subarray
) {
728 pr_err("Only subarrays can be updated in misc mode\n");
732 if (strcmp(c
.update
, "sparc2.2")==0)
734 if (strcmp(c
.update
, "super-minor") == 0)
736 if (strcmp(c
.update
, "summaries")==0)
738 if (strcmp(c
.update
, "resync")==0)
740 if (strcmp(c
.update
, "uuid")==0)
742 if (strcmp(c
.update
, "name")==0)
744 if (strcmp(c
.update
, "homehost")==0)
746 if (strcmp(c
.update
, "home-cluster")==0)
748 if (strcmp(c
.update
, "nodes")==0)
750 if (strcmp(c
.update
, "devicesize")==0)
752 if (strcmp(c
.update
, "no-bitmap")==0)
754 if (strcmp(c
.update
, "bbl") == 0)
756 if (strcmp(c
.update
, "no-bbl") == 0)
758 if (strcmp(c
.update
, "metadata") == 0)
760 if (strcmp(c
.update
, "revert-reshape") == 0)
762 if (strcmp(c
.update
, "byteorder")==0) {
764 pr_err("must not set metadata type with --update=byteorder.\n");
767 for(i
=0; !ss
&& superlist
[i
]; i
++)
768 ss
= superlist
[i
]->match_metadata_desc(
771 pr_err("INTERNAL ERROR cannot find 0.swap\n");
777 if (strcmp(c
.update
,"?") == 0 ||
778 strcmp(c
.update
, "help") == 0) {
780 fprintf(outf
, "%s: ", Name
);
784 "%s: '--update=%s' is invalid. ",
787 fprintf(outf
, "Valid --update options are:\n"
788 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
789 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
790 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
793 exit(outf
== stdout
? 0 : 2);
796 /* update=devicesize is allowed with --re-add */
797 if (devmode
!= 'A') {
798 pr_err("--update in Manage mode only allowed with --re-add.\n");
802 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
807 if (strcmp(c
.update
, "devicesize") != 0 &&
808 strcmp(c
.update
, "bbl") != 0 &&
809 strcmp(c
.update
, "no-bbl") != 0) {
810 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
815 case O(INCREMENTAL
,NoDegraded
):
816 pr_err("--no-degraded is deprecated in Incremental mode\n");
817 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
818 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
819 * so we overload slightly */
822 case O(ASSEMBLE
,'c'):
823 case O(ASSEMBLE
,ConfigFile
):
824 case O(INCREMENTAL
, 'c'):
825 case O(INCREMENTAL
, ConfigFile
):
827 case O(MISC
, ConfigFile
):
829 case O(MONITOR
,ConfigFile
):
830 case O(CREATE
,ConfigFile
):
832 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
836 set_conffile(configfile
);
837 /* FIXME possibly check that config file exists. Even parse it */
839 case O(ASSEMBLE
,'s'): /* scan */
842 case O(INCREMENTAL
,'s'):
846 case O(MONITOR
,'m'): /* mail address */
847 case O(MONITOR
,EMail
):
849 pr_err("only specify one mailaddress. %s ignored.\n",
855 case O(MONITOR
,'p'): /* alert program */
856 case O(MONITOR
,ProgramOpt
): /* alert program */
858 pr_err("only specify one alter program. %s ignored.\n",
864 case O(MONITOR
,'r'): /* rebuild increments */
865 case O(MONITOR
,Increment
):
866 increments
= atoi(optarg
);
867 if (increments
> 99 || increments
< 1) {
868 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
873 case O(MONITOR
,'d'): /* delay in seconds */
875 case O(BUILD
,'d'): /* delay for bitmap updates */
878 pr_err("only specify delay once. %s ignored.\n",
881 c
.delay
= parse_num(optarg
);
883 pr_err("invalid delay: %s\n",
889 case O(MONITOR
,'f'): /* daemonise */
890 case O(MONITOR
,Fork
):
893 case O(MONITOR
,'i'): /* pid */
895 pr_err("only specify one pid file. %s ignored.\n",
900 case O(MONITOR
,'1'): /* oneshot */
904 case O(MONITOR
,'t'): /* test */
907 case O(MONITOR
,'y'): /* log messages to syslog */
908 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
911 case O(MONITOR
, NoSharing
):
915 /* now the general management options. Some are applicable
916 * to other modes. None have arguments.
921 case O(MANAGE
,Add
): /* add a drive */
924 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
927 case O(MANAGE
,ReAdd
):
930 case O(MANAGE
,'r'): /* remove a drive */
931 case O(MANAGE
,Remove
):
934 case O(MANAGE
,'f'): /* set faulty */
936 case O(INCREMENTAL
,'f'):
937 case O(INCREMENTAL
,Remove
):
938 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
939 * even though we will both fail and
940 * remove the device */
943 case O(MANAGE
, ClusterConfirm
):
946 case O(MANAGE
,Replace
):
947 /* Mark these devices for replacement */
951 /* These are the replacements to use */
952 if (devmode
!= 'R') {
953 pr_err("--with must follow --replace\n");
958 case O(INCREMENTAL
,'R'):
960 case O(ASSEMBLE
,'R'):
962 case O(CREATE
,'R'): /* Run the array */
964 pr_err("Cannot both Stop and Run an array\n");
971 pr_err("Cannot both Run and Stop an array\n");
983 case O(MISC
,KillOpt
):
987 case O(MISC
, ExamineBB
):
991 case O(MISC
, WaitOpt
):
992 case O(MISC
, Waitclean
):
993 case O(MISC
, DetailPlatform
):
994 case O(MISC
, KillSubarray
):
995 case O(MISC
, UpdateSubarray
):
997 case O(MISC
, Restore
):
998 case O(MISC
,Action
):
999 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
1001 pr_err("subarray can only be specified once\n");
1004 c
.subarray
= optarg
;
1006 if (opt
== Action
) {
1008 pr_err("Only one --action can be specified\n");
1011 if (strcmp(optarg
, "idle") == 0 ||
1012 strcmp(optarg
, "frozen") == 0 ||
1013 strcmp(optarg
, "check") == 0 ||
1014 strcmp(optarg
, "repair") == 0)
1017 pr_err("action must be one of idle, frozen, check, repair\n");
1021 if (devmode
&& devmode
!= opt
&&
1022 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1023 pr_err("--examine/-E cannot be given with ");
1024 if (devmode
== 'E') {
1025 if (option_index
>= 0)
1026 fprintf(stderr
, "--%s\n",
1027 long_options
[option_index
].name
);
1029 fprintf(stderr
, "-%c\n", opt
);
1030 } else if (isalpha(devmode
))
1031 fprintf(stderr
, "-%c\n", devmode
);
1033 fprintf(stderr
, "previous option\n");
1037 if (opt
== Dump
|| opt
== Restore
) {
1038 if (dump_directory
!= NULL
) {
1039 pr_err("dump/restore directory specified twice: %s and %s\n",
1040 dump_directory
, optarg
);
1043 dump_directory
= optarg
;
1046 case O(MISC
, UdevRules
):
1047 if (devmode
&& devmode
!= opt
) {
1048 pr_err("--udev-rules must be the only option.\n");
1051 pr_err("only specify one udev rule filename. %s ignored.\n",
1054 udev_filename
= optarg
;
1062 case O(MISC
, Sparc22
):
1063 if (devmode
!= 'E') {
1064 pr_err("--sparc2.2 only allowed with --examine\n");
1070 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1071 case O(ASSEMBLE
,Bitmap
):
1073 pr_err("bitmap file needed with -b in --assemble mode\n");
1076 if (strcmp(optarg
, "internal")==0) {
1077 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1080 bitmap_fd
= open(optarg
, O_RDWR
);
1081 if (!*optarg
|| bitmap_fd
< 0) {
1082 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1085 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1088 case O(ASSEMBLE
, BackupFile
):
1089 case O(GROW
, BackupFile
):
1090 /* Specify a file into which grow might place a backup,
1091 * or from which assemble might recover a backup
1093 if (c
.backup_file
) {
1094 pr_err("backup file already specified, rejecting %s\n", optarg
);
1097 c
.backup_file
= optarg
;
1100 case O(GROW
, Continue
):
1101 /* Continue interrupted grow
1105 case O(ASSEMBLE
, InvalidBackup
):
1106 /* Acknowledge that the backupfile is invalid, but ask
1107 * to continue anyway
1109 c
.invalid_backup
= 1;
1113 case O(BUILD
,Bitmap
):
1115 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1117 case O(GROW
,Bitmap
):
1118 if (strcmp(optarg
, "internal")== 0 ||
1119 strcmp(optarg
, "none")== 0 ||
1120 strchr(optarg
, '/') != NULL
) {
1121 s
.bitmap_file
= optarg
;
1124 if (strcmp(optarg
, "clustered")== 0) {
1125 s
.bitmap_file
= optarg
;
1126 /* Set the default number of cluster nodes
1127 * to 4 if not already set by user
1134 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1135 " not '%s'\n", optarg
);
1138 case O(GROW
,BitmapChunk
):
1139 case O(BUILD
,BitmapChunk
):
1140 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1141 s
.bitmap_chunk
= parse_size(optarg
);
1142 if (s
.bitmap_chunk
== 0 ||
1143 s
.bitmap_chunk
== INVALID_SECTORS
||
1144 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1145 pr_err("invalid bitmap chunksize: %s\n",
1149 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1152 case O(GROW
, WriteBehind
):
1153 case O(BUILD
, WriteBehind
):
1154 case O(CREATE
, WriteBehind
): /* write-behind mode */
1155 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1157 s
.write_behind
= parse_num(optarg
);
1158 if (s
.write_behind
< 0 ||
1159 s
.write_behind
> 16383) {
1160 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1166 case O(INCREMENTAL
, 'r'):
1167 case O(INCREMENTAL
, RebuildMapOpt
):
1170 case O(INCREMENTAL
, IncrementalPath
):
1171 remove_path
= optarg
;
1174 /* We have now processed all the valid options. Anything else is
1177 if (option_index
> 0)
1178 pr_err(":option --%s not valid in %s mode\n",
1179 long_options
[option_index
].name
,
1180 map_num(modes
, mode
));
1182 pr_err("option -%c not valid in %s mode\n",
1183 opt
, map_num(modes
, mode
));
1190 if (print_help
== 2)
1191 help_text
= OptionHelp
;
1193 help_text
= mode_help
[mode
];
1194 if (help_text
== NULL
)
1196 fputs(help_text
,stdout
);
1200 if (!mode
&& devs_found
) {
1203 if (devlist
->disposition
== 0)
1204 devlist
->disposition
= devmode
;
1207 fputs(Usage
, stderr
);
1212 struct createinfo
*ci
= conf_get_create_info();
1214 if (strcasecmp(symlinks
, "yes") == 0)
1216 else if (strcasecmp(symlinks
, "no") == 0)
1219 pr_err("option --symlinks must be 'no' or 'yes'\n");
1223 /* Ok, got the option parsing out of the way
1224 * hopefully it's mostly right but there might be some stuff
1227 * That is mosty checked in the per-mode stuff but...
1229 * For @,B,C and A without -s, the first device listed must be
1230 * an md device. We check that here and open it.
1233 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1235 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1236 if (devs_found
< 1) {
1237 pr_err("an md device must be given in this mode\n");
1240 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1241 pr_err("--super-minor=dev is incompatible with --auto\n");
1244 if (mode
== MANAGE
|| mode
== GROW
) {
1245 mdfd
= open_mddev(devlist
->devname
, 1);
1249 /* non-existent device is OK */
1250 mdfd
= open_mddev(devlist
->devname
, 0);
1252 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1255 if ((int)ident
.super_minor
== -2) {
1258 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1263 ident
.super_minor
= minor(stb
.st_rdev
);
1265 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1266 /* We don't really want this open yet, we just might
1267 * have wanted to check some things
1275 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1276 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1277 " a mistake. If you really mean it you will need to specify --force before\n"
1278 " setting the number of drives.\n");
1283 if (c
.homehost
== NULL
&& c
.require_homehost
)
1284 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1285 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1286 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1287 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1288 c
.homehost
= sys_hostname
;
1291 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1293 c
.require_homehost
= 0;
1296 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1297 c
.homecluster
= conf_get_homecluster();
1298 if (c
.homecluster
== NULL
)
1299 rv
= get_cluster_name(&c
.homecluster
);
1301 pr_err("The md can't get cluster name\n");
1306 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1307 pr_err("--backup-file and --data-offset are incompatible\n");
1311 if ((mode
== MISC
&& devmode
== 'E')
1312 || (mode
== MONITOR
&& spare_sharing
== 0))
1313 /* Anyone may try this */;
1314 else if (geteuid() != 0) {
1315 pr_err("must be super-user to perform this action\n");
1319 ident
.autof
= c
.autof
;
1321 if (c
.scan
&& c
.verbose
< 2)
1322 /* --scan implied --brief unless -vv */
1328 /* readonly, add/remove, readwrite, runstop */
1330 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1331 if (!rv
&& devs_found
>1)
1332 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1333 devlist
->next
, c
.verbose
, c
.test
,
1335 if (!rv
&& c
.readonly
< 0)
1336 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1337 if (!rv
&& c
.runstop
> 0)
1338 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1339 if (!rv
&& c
.runstop
< 0)
1340 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1343 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1344 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1345 /* Only a device has been given, so get details from config file */
1346 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1347 if (array_ident
== NULL
) {
1348 pr_err("%s not identified in config file.\n",
1354 if (array_ident
->autof
== 0)
1355 array_ident
->autof
= c
.autof
;
1356 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1360 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1362 else if (devs_found
> 0) {
1363 if (c
.update
&& devs_found
> 1) {
1364 pr_err("can only update a single array at a time\n");
1367 if (c
.backup_file
&& devs_found
> 1) {
1368 pr_err("can only assemble a single array when providing a backup file.\n");
1371 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1372 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1373 if (array_ident
== NULL
) {
1374 pr_err("%s not identified in config file.\n",
1379 if (array_ident
->autof
== 0)
1380 array_ident
->autof
= c
.autof
;
1381 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1386 pr_err("--update not meaningful with a --scan assembly.\n");
1389 if (c
.backup_file
) {
1390 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1393 rv
= scan_assemble(ss
, &c
, &ident
);
1399 c
.delay
= DEFAULT_BITMAP_DELAY
;
1400 if (s
.write_behind
&& !s
.bitmap_file
) {
1401 pr_err("write-behind mode requires a bitmap.\n");
1405 if (s
.raiddisks
== 0) {
1406 pr_err("no raid-devices specified.\n");
1411 if (s
.bitmap_file
) {
1412 if (strcmp(s
.bitmap_file
, "internal")==0) {
1413 pr_err("'internal' bitmaps not supported with --build\n");
1418 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1422 c
.delay
= DEFAULT_BITMAP_DELAY
;
1425 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1426 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1432 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1438 if (s
.write_behind
&& !s
.bitmap_file
) {
1439 pr_err("write-behind mode requires a bitmap.\n");
1443 if (s
.raiddisks
== 0) {
1444 pr_err("no raid-devices specified.\n");
1449 rv
= Create(ss
, devlist
->devname
,
1450 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1451 devs_found
-1, devlist
->next
,
1452 &s
, &c
, data_offset
);
1455 if (devmode
== 'E') {
1456 if (devlist
== NULL
&& !c
.scan
) {
1457 pr_err("No devices to examine\n");
1460 if (devlist
== NULL
)
1461 devlist
= conf_get_devs();
1462 if (devlist
== NULL
) {
1463 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1466 rv
= Examine(devlist
, &c
, ss
);
1467 } else if (devmode
== DetailPlatform
) {
1468 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1469 c
.verbose
, c
.export
,
1470 devlist
? devlist
->devname
: NULL
);
1471 } else if (devlist
== NULL
) {
1472 if (devmode
== 'S' && c
.scan
)
1473 rv
= stop_scan(c
.verbose
);
1474 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1475 rv
= misc_scan(devmode
, &c
);
1476 else if (devmode
== UdevRules
)
1477 rv
= Write_rules(udev_filename
);
1479 pr_err("No devices given.\n");
1483 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1486 if (!devlist
&& !c
.scan
) {
1487 pr_err("Cannot monitor: need --scan or at least one device\n");
1491 if (pidfile
&& !daemonise
) {
1492 pr_err("Cannot write a pid file when not in daemon mode\n");
1497 if (get_linux_version() > 2006016)
1498 /* mdstat responds to poll */
1505 rv
= Monitor(devlist
, mailaddr
, program
,
1506 &c
, daemonise
, oneshot
,
1507 dosyslog
, pidfile
, increments
,
1512 if (array_size
> 0) {
1513 /* alway impose array size first, independent of
1515 * Do not allow level or raid_disks changes at the
1516 * same time as that can be irreversibly destructive.
1520 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1521 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1522 " Change size first, then check that data is still intact.\n");
1526 sysfs_init(&sra
, mdfd
, NULL
);
1527 if (array_size
== MAX_SIZE
)
1528 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1530 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1533 pr_err("--array-size setting is too large.\n");
1535 pr_err("current kernel does not support setting --array-size\n");
1540 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1542 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1543 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1547 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1548 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1553 } else if (s
.bitmap_file
) {
1554 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1555 s
.layout_str
!= NULL
|| devs_found
> 1) {
1556 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1561 c
.delay
= DEFAULT_BITMAP_DELAY
;
1562 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1563 } else if (grow_continue
)
1564 rv
= Grow_continue_command(devlist
->devname
,
1565 mdfd
, c
.backup_file
,
1567 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1568 || s
.chunk
!= 0 || s
.level
!= UnSet
1569 || data_offset
!= INVALID_SECTORS
) {
1570 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1572 data_offset
, &c
, &s
);
1573 } else if (array_size
== 0)
1574 pr_err("no changes to --grow\n");
1583 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1586 if (c
.runstop
<= 0) {
1587 pr_err("--incremental --scan meaningless without --run.\n");
1590 if (devmode
== 'f') {
1591 pr_err("--incremental --scan --fail not supported.\n");
1594 rv
= IncrementalScan(&c
, NULL
);
1597 if (!rebuild_map
&& !c
.scan
) {
1598 pr_err("--incremental requires a device.\n");
1603 if (devmode
== 'f') {
1604 if (devlist
->next
) {
1605 pr_err("'--incremental --fail' can only handle one device.\n");
1609 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1612 rv
= Incremental(devlist
, &c
, ss
);
1621 static int scan_assemble(struct supertype
*ss
,
1623 struct mddev_ident
*ident
)
1625 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1626 struct mddev_dev
*devlist
= conf_get_devs();
1627 struct map_ent
*map
= NULL
;
1630 int failures
, successes
;
1632 if (conf_verify_devnames(array_list
)) {
1633 pr_err("Duplicate MD device names in conf file were found.\n");
1636 if (devlist
== NULL
) {
1637 pr_err("No devices listed in conf file were found.\n");
1640 for (a
= array_list
; a
; a
= a
->next
) {
1643 a
->autof
= c
->autof
;
1646 pr_err("failed to get exclusive lock on mapfile\n");
1651 for (a
= array_list
; a
; a
= a
->next
) {
1656 strcasecmp(a
->devname
, "<ignore>") == 0)
1659 r
= Assemble(ss
, a
->devname
,
1669 } while (failures
&& successes
);
1670 if (c
->homehost
&& cnt
== 0) {
1671 /* Maybe we can auto-assemble something.
1672 * Repeatedly call Assemble in auto-assemble mode
1677 ident
->autof
= c
->autof
;
1679 struct mddev_dev
*devlist
= conf_get_devs();
1682 rv2
= Assemble(ss
, NULL
,
1690 /* Incase there are stacked devices, we need to go around again */
1692 if (cnt
== 0 && rv
== 0) {
1693 pr_err("No arrays found in config file or automatically\n");
1697 } else if (cnt
== 0 && rv
== 0) {
1698 pr_err("No arrays found in config file\n");
1705 static int misc_scan(char devmode
, struct context
*c
)
1707 /* apply --detail or --wait-clean to
1708 * all devices in /proc/mdstat
1710 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1711 struct mdstat_ent
*e
;
1712 struct map_ent
*map
= NULL
;
1716 for (members
= 0; members
<= 1; members
++) {
1717 for (e
=ms
; e
; e
=e
->next
) {
1721 int member
= e
->metadata_version
&&
1722 strncmp(e
->metadata_version
,
1723 "external:/", 10) == 0;
1724 if (members
!= member
)
1726 me
= map_by_devnm(&map
, e
->devnm
);
1728 && strcmp(me
->path
, "/unknown") != 0)
1731 stat(name
, &stb
) != 0)
1732 name
= get_md_name(e
->devnm
);
1735 pr_err("cannot find device file for %s\n",
1740 rv
|= Detail(name
, c
);
1742 rv
|= WaitClean(name
, -1, c
->verbose
);
1750 static int stop_scan(int verbose
)
1752 /* apply --stop to all devices in /proc/mdstat */
1753 /* Due to possible stacking of devices, repeat until
1754 * nothing more can be stopped
1756 int progress
=1, err
;
1760 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1761 struct mdstat_ent
*e
;
1763 if (!progress
) last
= 1;
1764 progress
= 0; err
= 0;
1765 for (e
=ms
; e
; e
=e
->next
) {
1766 char *name
= get_md_name(e
->devnm
);
1770 pr_err("cannot find device file for %s\n",
1774 mdfd
= open_mddev(name
, 1);
1776 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1786 } while (!last
&& err
);
1792 static int misc_list(struct mddev_dev
*devlist
,
1793 struct mddev_ident
*ident
,
1794 char *dump_directory
,
1795 struct supertype
*ss
, struct context
*c
)
1797 struct mddev_dev
*dv
;
1800 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1803 switch(dv
->disposition
) {
1805 rv
|= Detail(dv
->devname
, c
);
1807 case KillOpt
: /* Zero superblock */
1809 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1813 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1820 rv
|= Query(dv
->devname
); continue;
1822 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1824 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1827 rv
|= Wait(dv
->devname
); continue;
1829 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1831 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1833 case UpdateSubarray
:
1834 if (c
->update
== NULL
) {
1835 pr_err("-U/--update must be specified with --update-subarray\n");
1839 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1840 c
->update
, ident
, c
->verbose
);
1843 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1846 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1847 (dv
== devlist
&& dv
->next
== NULL
));
1850 rv
|= SetAction(dv
->devname
, c
->action
);
1853 if (dv
->devname
[0] == '/')
1854 mdfd
= open_mddev(dv
->devname
, 1);
1856 mdfd
= open_dev(dv
->devname
);
1858 pr_err("Cannot open %s\n", dv
->devname
);
1861 switch(dv
->disposition
) {
1864 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1866 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1868 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1870 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1879 int SetAction(char *dev
, char *action
)
1881 int fd
= open(dev
, O_RDONLY
);
1884 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1887 sysfs_init(&mdi
, fd
, NULL
);
1889 if (!mdi
.sys_name
[0]) {
1890 pr_err("%s is no an md array\n", dev
);
1894 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1895 pr_err("Count not set action for %s to %s: %s\n",
1896 dev
, action
, strerror(errno
));