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,
80 .bitmap_chunk
= UnSet
,
83 char sys_hostname
[256];
84 char *mailaddr
= NULL
;
90 int spare_sharing
= 1;
91 struct supertype
*ss
= NULL
;
93 char *shortopt
= short_options
;
96 char *remove_path
= NULL
;
97 char *udev_filename
= NULL
;
98 char *dump_directory
= NULL
;
105 srandom(time(0) ^ getpid());
109 ident
.raid_disks
= UnSet
;
110 ident
.super_minor
= UnSet
;
112 ident
.spare_group
= NULL
;
115 ident
.bitmap_fd
= -1;
116 ident
.bitmap_file
= NULL
;
118 ident
.container
= NULL
;
121 while ((option_index
= -1) ,
122 (opt
=getopt_long(argc
, argv
,
123 shortopt
, long_options
,
124 &option_index
)) != -1) {
126 /* firstly, some mode-independent options */
136 fputs(Version
, stderr
);
139 case 'v': c
.verbose
++;
142 case 'q': c
.verbose
--;
146 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
147 || mode
== GROW
|| mode
== INCREMENTAL
149 break; /* b means bitmap */
154 case 'Y': c
.export
++;
158 if (strcasecmp(optarg
, "<ignore>") == 0)
159 c
.require_homehost
= 0;
165 /* Silently ignore old option */
171 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
177 fputs(Usage
, stderr
);
180 /* second, figure out the mode.
181 * Some options force the mode. Others
182 * set the mode if it isn't already
188 shortopt
= short_bitmap_options
;
199 case ReAdd
: /* re-add */
203 shortopt
= short_bitmap_options
;
207 case 'A': newmode
= ASSEMBLE
;
208 shortopt
= short_bitmap_auto_options
;
210 case 'B': newmode
= BUILD
;
211 shortopt
= short_bitmap_auto_options
;
213 case 'C': newmode
= CREATE
;
214 shortopt
= short_bitmap_auto_options
;
216 case 'F': newmode
= MONITOR
;
218 case 'G': newmode
= GROW
;
219 shortopt
= short_bitmap_options
;
221 case 'I': newmode
= INCREMENTAL
;
222 shortopt
= short_bitmap_auto_options
;
225 newmode
= AUTODETECT
;
260 if (mode
&& newmode
== mode
) {
261 /* everybody happy ! */
262 } else if (mode
&& newmode
!= mode
) {
265 if (option_index
>= 0)
266 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
268 fprintf(stderr
, "-%c", opt
);
269 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
270 map_num(modes
, newmode
),
271 map_num(modes
, mode
));
273 } else if (!mode
&& newmode
) {
275 if (mode
== MISC
&& devs_found
) {
276 pr_err("No action given for %s in --misc mode\n",
278 cont_err("Action options must come before device names\n");
282 /* special case of -c --help */
283 if ((opt
== 'c' || opt
== ConfigFile
) &&
284 ( strncmp(optarg
, "--h", 3)==0 ||
285 strncmp(optarg
, "-h", 2)==0)) {
286 fputs(Help_config
, stdout
);
290 /* If first option is a device, don't force the mode yet */
292 if (devs_found
== 0) {
293 dv
= xmalloc(sizeof(*dv
));
294 dv
->devname
= optarg
;
295 dv
->disposition
= devmode
;
296 dv
->writemostly
= writemostly
;
300 devlistend
= &dv
->next
;
305 /* No mode yet, and this is the second device ... */
306 pr_err("An option must be given to set the mode before a second device\n"
307 " (%s) is listed\n", optarg
);
310 if (option_index
>= 0)
311 pr_err("--%s", long_options
[option_index
].name
);
314 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
318 /* if we just set the mode, then done */
332 /* an undecorated option - must be a device name.
335 if (devs_found
> 0 && devmode
== DetailPlatform
) {
336 pr_err("controller may only be specified once. %s ignored\n",
341 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
342 pr_err("Must give one of -a/-r/-f for subsequent devices at %s\n", optarg
);
345 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
346 pr_err("Must give -a/--add for devices to add: %s\n", optarg
);
349 dv
= xmalloc(sizeof(*dv
));
350 dv
->devname
= optarg
;
351 dv
->disposition
= devmode
;
352 dv
->writemostly
= writemostly
;
356 devlistend
= &dv
->next
;
362 /* We've got a mode, and opt is now something else which
363 * could depend on the mode */
364 #define O(a,b) ((a<<16)|b)
365 switch (O(mode
,opt
)) {
367 case O(GROW
,ChunkSize
):
369 case O(CREATE
,ChunkSize
):
370 case O(BUILD
,'c'): /* chunk or rounding */
371 case O(BUILD
,ChunkSize
): /* chunk or rounding */
373 pr_err("chunk/rounding may only be specified once. Second value is %s.\n", optarg
);
376 s
.chunk
= parse_size(optarg
);
377 if (s
.chunk
== INVALID_SECTORS
||
378 s
.chunk
< 8 || (s
.chunk
&1)) {
379 pr_err("invalid chunk/rounding value: %s\n",
383 /* Convert sectors to K */
387 case O(INCREMENTAL
, 'e'):
389 case O(ASSEMBLE
,'e'):
390 case O(MISC
,'e'): /* set metadata (superblock) information */
392 pr_err("metadata information already given\n");
395 for(i
=0; !ss
&& superlist
[i
]; i
++)
396 ss
= superlist
[i
]->match_metadata_desc(optarg
);
399 pr_err("unrecognised metadata identifier: %s\n", optarg
);
405 case O(MANAGE
,WriteMostly
):
407 case O(BUILD
,WriteMostly
):
409 case O(CREATE
,WriteMostly
):
410 /* set write-mostly for following devices */
415 /* clear write-mostly for following devices */
421 case O(BUILD
,'z'): /* size */
423 pr_err("size may only be specified once. Second value is %s.\n", optarg
);
426 if (strcmp(optarg
, "max")==0)
429 s
.size
= parse_size(optarg
);
430 if (s
.size
== INVALID_SECTORS
||
432 pr_err("invalid size: %s\n",
436 /* convert sectors to K */
441 case O(GROW
,'Z'): /* array size */
442 if (array_size
> 0) {
443 pr_err("array-size may only be specified once. Second value is %s.\n", optarg
);
446 if (strcmp(optarg
, "max") == 0)
447 array_size
= MAX_SIZE
;
449 array_size
= parse_size(optarg
);
450 if (array_size
== 0 ||
451 array_size
== INVALID_SECTORS
) {
452 pr_err("invalid array size: %s\n",
459 case O(CREATE
,DataOffset
):
460 case O(GROW
,DataOffset
):
461 if (data_offset
!= INVALID_SECTORS
) {
462 pr_err("data-offset may only be specified one. Second value is %s.\n", optarg
);
465 if (mode
== CREATE
&&
466 strcmp(optarg
, "variable") == 0)
467 data_offset
= VARIABLE_OFFSET
;
469 data_offset
= parse_size(optarg
);
470 if (data_offset
== INVALID_SECTORS
) {
471 pr_err("invalid data-offset: %s\n",
479 case O(BUILD
,'l'): /* set raid level*/
480 if (s
.level
!= UnSet
) {
481 pr_err("raid level may only be set once. Second value is %s.\n", optarg
);
484 s
.level
= map_name(pers
, optarg
);
485 if (s
.level
== UnSet
) {
486 pr_err("invalid raid level: %s\n",
490 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
491 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
494 pr_err("Raid level %s not permitted with --build.\n",
498 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
499 pr_err("raid level %s is incompatible with spare-devices setting.\n",
503 ident
.level
= s
.level
;
506 case O(GROW
, 'p'): /* new layout */
507 case O(GROW
, Layout
):
509 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
512 s
.layout_str
= optarg
;
513 /* 'Grow' will parse the value */
516 case O(CREATE
,'p'): /* raid5 layout */
517 case O(CREATE
,Layout
):
518 case O(BUILD
,'p'): /* faulty layout */
519 case O(BUILD
,Layout
):
520 if (s
.layout
!= UnSet
) {
521 pr_err("layout may only be sent once. Second value was %s\n", optarg
);
526 pr_err("layout not meaningful for %s arrays.\n",
527 map_num(pers
, s
.level
));
530 pr_err("raid level must be given before layout.\n");
534 s
.layout
= map_name(r5layout
, optarg
);
535 if (s
.layout
==UnSet
) {
536 pr_err("layout %s not understood for raid5.\n",
542 s
.layout
= map_name(r6layout
, optarg
);
543 if (s
.layout
==UnSet
) {
544 pr_err("layout %s not understood for raid6.\n",
551 s
.layout
= parse_layout_10(optarg
);
553 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
561 s
.layout
= parse_layout_faulty(optarg
);
562 if (s
.layout
== -1) {
563 pr_err("layout %s not understood for faulty.\n",
571 case O(CREATE
,AssumeClean
):
572 case O(BUILD
,AssumeClean
): /* assume clean */
573 case O(GROW
,AssumeClean
):
579 case O(BUILD
,'n'): /* number of raid disks */
581 pr_err("raid-devices set twice: %d and %s\n",
582 s
.raiddisks
, optarg
);
585 s
.raiddisks
= parse_num(optarg
);
586 if (s
.raiddisks
<= 0) {
587 pr_err("invalid number of raid devices: %s\n",
591 ident
.raid_disks
= s
.raiddisks
;
593 case O(ASSEMBLE
, Nodes
):
594 case O(CREATE
, Nodes
):
595 c
.nodes
= parse_num(optarg
);
597 pr_err("invalid number for the number of cluster nodes: %s\n",
602 case O(CREATE
, ClusterName
):
603 case O(ASSEMBLE
, ClusterName
):
604 c
.homecluster
= optarg
;
605 if (strlen(c
.homecluster
) > 64) {
606 pr_err("Cluster name too big.\n");
610 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
612 pr_err("spare-devices set twice: %d and %s\n",
613 s
.sparedisks
, optarg
);
616 if (s
.level
!= UnSet
&& s
.level
<= 0 && s
.level
>= -1) {
617 pr_err("spare-devices setting is incompatible with raid level %d\n",
621 s
.sparedisks
= parse_num(optarg
);
622 if (s
.sparedisks
< 0) {
623 pr_err("invalid number of spare-devices: %s\n",
633 case O(INCREMENTAL
,'a'):
634 case O(INCREMENTAL
,Auto
):
635 case O(ASSEMBLE
,'a'):
636 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
637 c
.autof
= parse_auto(optarg
, "--auto flag", 0);
640 case O(CREATE
,Symlinks
):
641 case O(BUILD
,Symlinks
):
642 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
646 case O(BUILD
,'f'): /* force honouring '-n 1' */
647 case O(BUILD
,Force
): /* force honouring '-n 1' */
648 case O(GROW
,'f'): /* ditto */
649 case O(GROW
,Force
): /* ditto */
650 case O(CREATE
,'f'): /* force honouring of device list */
651 case O(CREATE
,Force
): /* force honouring of device list */
652 case O(ASSEMBLE
,'f'): /* force assembly */
653 case O(ASSEMBLE
,Force
): /* force assembly */
654 case O(MISC
,'f'): /* force zero */
655 case O(MISC
,Force
): /* force zero */
656 case O(MANAGE
,Force
): /* add device which is too large */
659 /* now for the Assemble options */
660 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
662 case O(INCREMENTAL
, FreezeReshape
):
663 c
.freeze_reshape
= 1;
665 case O(CREATE
,'u'): /* uuid of array */
666 case O(ASSEMBLE
,'u'): /* uuid of array */
667 if (ident
.uuid_set
) {
668 pr_err("uuid cannot be set twice. Second value %s.\n", optarg
);
671 if (parse_uuid(optarg
, ident
.uuid
))
674 pr_err("Bad uuid: %s\n", optarg
);
680 case O(ASSEMBLE
,'N'):
683 pr_err("name cannot be set twice. Second value %s.\n", optarg
);
686 if (mode
== MISC
&& !c
.subarray
) {
687 pr_err("-N/--name only valid with --update-subarray in misc mode\n");
690 if (strlen(optarg
) > 32) {
691 pr_err("name '%s' is too long, 32 chars max.\n",
695 strcpy(ident
.name
, optarg
);
698 case O(ASSEMBLE
,'m'): /* super-minor for array */
699 case O(ASSEMBLE
,SuperMinor
):
700 if (ident
.super_minor
!= UnSet
) {
701 pr_err("super-minor cannot be set twice. Second value: %s.\n", optarg
);
704 if (strcmp(optarg
, "dev")==0)
705 ident
.super_minor
= -2;
707 ident
.super_minor
= parse_num(optarg
);
708 if (ident
.super_minor
< 0) {
709 pr_err("Bad super-minor number: %s.\n", optarg
);
715 case O(ASSEMBLE
,'o'):
721 case O(ASSEMBLE
,'U'): /* update the superblock */
724 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
728 if (mode
== MISC
&& !c
.subarray
) {
729 pr_err("Only subarrays can be updated in misc mode\n");
733 if (strcmp(c
.update
, "sparc2.2")==0)
735 if (strcmp(c
.update
, "super-minor") == 0)
737 if (strcmp(c
.update
, "summaries")==0)
739 if (strcmp(c
.update
, "resync")==0)
741 if (strcmp(c
.update
, "uuid")==0)
743 if (strcmp(c
.update
, "name")==0)
745 if (strcmp(c
.update
, "homehost")==0)
747 if (strcmp(c
.update
, "home-cluster")==0)
749 if (strcmp(c
.update
, "nodes")==0)
751 if (strcmp(c
.update
, "devicesize")==0)
753 if (strcmp(c
.update
, "no-bitmap")==0)
755 if (strcmp(c
.update
, "bbl") == 0)
757 if (strcmp(c
.update
, "no-bbl") == 0)
759 if (strcmp(c
.update
, "metadata") == 0)
761 if (strcmp(c
.update
, "revert-reshape") == 0)
763 if (strcmp(c
.update
, "byteorder")==0) {
765 pr_err("must not set metadata type with --update=byteorder.\n");
768 for(i
=0; !ss
&& superlist
[i
]; i
++)
769 ss
= superlist
[i
]->match_metadata_desc(
772 pr_err("INTERNAL ERROR cannot find 0.swap\n");
778 if (strcmp(c
.update
,"?") == 0 ||
779 strcmp(c
.update
, "help") == 0) {
781 fprintf(outf
, "%s: ", Name
);
785 "%s: '--update=%s' is invalid. ",
788 fprintf(outf
, "Valid --update options are:\n"
789 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'nodes', 'resync',\n"
790 " 'summaries', 'homehost', 'home-cluster', 'byteorder', 'devicesize',\n"
791 " 'no-bitmap', 'metadata', 'revert-reshape'\n"
794 exit(outf
== stdout
? 0 : 2);
797 /* update=devicesize is allowed with --re-add */
798 if (devmode
!= 'A') {
799 pr_err("--update in Manage mode only allowed with --re-add.\n");
803 pr_err("Can only update one aspect of superblock, both %s and %s given.\n",
808 if (strcmp(c
.update
, "devicesize") != 0 &&
809 strcmp(c
.update
, "bbl") != 0 &&
810 strcmp(c
.update
, "no-bbl") != 0) {
811 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be updated with --re-add\n");
816 case O(INCREMENTAL
,NoDegraded
):
817 pr_err("--no-degraded is deprecated in Incremental mode\n");
818 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
819 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
820 * so we overload slightly */
823 case O(ASSEMBLE
,'c'):
824 case O(ASSEMBLE
,ConfigFile
):
825 case O(INCREMENTAL
, 'c'):
826 case O(INCREMENTAL
, ConfigFile
):
828 case O(MISC
, ConfigFile
):
830 case O(MONITOR
,ConfigFile
):
831 case O(CREATE
,ConfigFile
):
833 pr_err("configfile cannot be set twice. Second value is %s.\n", optarg
);
837 set_conffile(configfile
);
838 /* FIXME possibly check that config file exists. Even parse it */
840 case O(ASSEMBLE
,'s'): /* scan */
843 case O(INCREMENTAL
,'s'):
847 case O(MONITOR
,'m'): /* mail address */
848 case O(MONITOR
,EMail
):
850 pr_err("only specify one mailaddress. %s ignored.\n",
856 case O(MONITOR
,'p'): /* alert program */
857 case O(MONITOR
,ProgramOpt
): /* alert program */
859 pr_err("only specify one alter program. %s ignored.\n",
865 case O(MONITOR
,'r'): /* rebuild increments */
866 case O(MONITOR
,Increment
):
867 increments
= atoi(optarg
);
868 if (increments
> 99 || increments
< 1) {
869 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
874 case O(MONITOR
,'d'): /* delay in seconds */
876 case O(BUILD
,'d'): /* delay for bitmap updates */
879 pr_err("only specify delay once. %s ignored.\n",
882 c
.delay
= parse_num(optarg
);
884 pr_err("invalid delay: %s\n",
890 case O(MONITOR
,'f'): /* daemonise */
891 case O(MONITOR
,Fork
):
894 case O(MONITOR
,'i'): /* pid */
896 pr_err("only specify one pid file. %s ignored.\n",
901 case O(MONITOR
,'1'): /* oneshot */
905 case O(MONITOR
,'t'): /* test */
908 case O(MONITOR
,'y'): /* log messages to syslog */
909 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
912 case O(MONITOR
, NoSharing
):
916 /* now the general management options. Some are applicable
917 * to other modes. None have arguments.
922 case O(MANAGE
,Add
): /* add a drive */
925 case O(MANAGE
,AddSpare
): /* add drive - never re-add */
928 case O(MANAGE
,ReAdd
):
931 case O(MANAGE
,'r'): /* remove a drive */
932 case O(MANAGE
,Remove
):
935 case O(MANAGE
,'f'): /* set faulty */
937 case O(INCREMENTAL
,'f'):
938 case O(INCREMENTAL
,Remove
):
939 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
940 * even though we will both fail and
941 * remove the device */
944 case O(MANAGE
, ClusterConfirm
):
947 case O(MANAGE
,Replace
):
948 /* Mark these devices for replacement */
952 /* These are the replacements to use */
953 if (devmode
!= 'R') {
954 pr_err("--with must follow --replace\n");
959 case O(INCREMENTAL
,'R'):
961 case O(ASSEMBLE
,'R'):
963 case O(CREATE
,'R'): /* Run the array */
965 pr_err("Cannot both Stop and Run an array\n");
972 pr_err("Cannot both Run and Stop an array\n");
984 case O(MISC
,KillOpt
):
988 case O(MISC
, ExamineBB
):
992 case O(MISC
, WaitOpt
):
993 case O(MISC
, Waitclean
):
994 case O(MISC
, DetailPlatform
):
995 case O(MISC
, KillSubarray
):
996 case O(MISC
, UpdateSubarray
):
998 case O(MISC
, Restore
):
999 case O(MISC
,Action
):
1000 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
1002 pr_err("subarray can only be specified once\n");
1005 c
.subarray
= optarg
;
1007 if (opt
== Action
) {
1009 pr_err("Only one --action can be specified\n");
1012 if (strcmp(optarg
, "idle") == 0 ||
1013 strcmp(optarg
, "frozen") == 0 ||
1014 strcmp(optarg
, "check") == 0 ||
1015 strcmp(optarg
, "repair") == 0)
1018 pr_err("action must be one of idle, frozen, check, repair\n");
1022 if (devmode
&& devmode
!= opt
&&
1023 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1024 pr_err("--examine/-E cannot be given with ");
1025 if (devmode
== 'E') {
1026 if (option_index
>= 0)
1027 fprintf(stderr
, "--%s\n",
1028 long_options
[option_index
].name
);
1030 fprintf(stderr
, "-%c\n", opt
);
1031 } else if (isalpha(devmode
))
1032 fprintf(stderr
, "-%c\n", devmode
);
1034 fprintf(stderr
, "previous option\n");
1038 if (opt
== Dump
|| opt
== Restore
) {
1039 if (dump_directory
!= NULL
) {
1040 pr_err("dump/restore directory specified twice: %s and %s\n",
1041 dump_directory
, optarg
);
1044 dump_directory
= optarg
;
1047 case O(MISC
, UdevRules
):
1048 if (devmode
&& devmode
!= opt
) {
1049 pr_err("--udev-rules must be the only option.\n");
1052 pr_err("only specify one udev rule filename. %s ignored.\n",
1055 udev_filename
= optarg
;
1063 case O(MISC
, Sparc22
):
1064 if (devmode
!= 'E') {
1065 pr_err("--sparc2.2 only allowed with --examine\n");
1071 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1072 case O(ASSEMBLE
,Bitmap
):
1074 pr_err("bitmap file needed with -b in --assemble mode\n");
1077 if (strcmp(optarg
, "internal")==0) {
1078 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1081 bitmap_fd
= open(optarg
, O_RDWR
);
1082 if (!*optarg
|| bitmap_fd
< 0) {
1083 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1086 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1089 case O(ASSEMBLE
, BackupFile
):
1090 case O(GROW
, BackupFile
):
1091 /* Specify a file into which grow might place a backup,
1092 * or from which assemble might recover a backup
1094 if (c
.backup_file
) {
1095 pr_err("backup file already specified, rejecting %s\n", optarg
);
1098 c
.backup_file
= optarg
;
1101 case O(GROW
, Continue
):
1102 /* Continue interrupted grow
1106 case O(ASSEMBLE
, InvalidBackup
):
1107 /* Acknowledge that the backupfile is invalid, but ask
1108 * to continue anyway
1110 c
.invalid_backup
= 1;
1114 case O(BUILD
,Bitmap
):
1116 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1118 case O(GROW
,Bitmap
):
1119 if (strcmp(optarg
, "internal")== 0 ||
1120 strcmp(optarg
, "none")== 0 ||
1121 strchr(optarg
, '/') != NULL
) {
1122 s
.bitmap_file
= optarg
;
1125 if (strcmp(optarg
, "clustered")== 0) {
1126 s
.bitmap_file
= optarg
;
1127 /* Set the default number of cluster nodes
1128 * to 4 if not already set by user
1135 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1136 " not '%s'\n", optarg
);
1139 case O(GROW
,BitmapChunk
):
1140 case O(BUILD
,BitmapChunk
):
1141 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1142 s
.bitmap_chunk
= parse_size(optarg
);
1143 if (s
.bitmap_chunk
== 0 ||
1144 s
.bitmap_chunk
== INVALID_SECTORS
||
1145 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1146 pr_err("invalid bitmap chunksize: %s\n",
1150 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1153 case O(GROW
, WriteBehind
):
1154 case O(BUILD
, WriteBehind
):
1155 case O(CREATE
, WriteBehind
): /* write-behind mode */
1156 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1158 s
.write_behind
= parse_num(optarg
);
1159 if (s
.write_behind
< 0 ||
1160 s
.write_behind
> 16383) {
1161 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1167 case O(INCREMENTAL
, 'r'):
1168 case O(INCREMENTAL
, RebuildMapOpt
):
1171 case O(INCREMENTAL
, IncrementalPath
):
1172 remove_path
= optarg
;
1174 case O(CREATE
, WriteJournal
):
1175 if (s
.journaldisks
) {
1176 pr_err("Please specify only one journal device for the array.\n");
1177 pr_err("Ignoring --write-journal %s...\n", optarg
);
1180 dv
= xmalloc(sizeof(*dv
));
1181 dv
->devname
= optarg
;
1182 dv
->disposition
= 'j'; /* WriteJournal */
1186 devlistend
= &dv
->next
;
1192 /* We have now processed all the valid options. Anything else is
1195 if (option_index
> 0)
1196 pr_err(":option --%s not valid in %s mode\n",
1197 long_options
[option_index
].name
,
1198 map_num(modes
, mode
));
1200 pr_err("option -%c not valid in %s mode\n",
1201 opt
, map_num(modes
, mode
));
1208 if (print_help
== 2)
1209 help_text
= OptionHelp
;
1211 help_text
= mode_help
[mode
];
1212 if (help_text
== NULL
)
1214 fputs(help_text
,stdout
);
1218 if (s
.journaldisks
&& (s
.level
< 4 || s
.level
> 6)) {
1219 pr_err("--write-journal is only supported for RAID level 4/5/6.\n");
1223 if (!mode
&& devs_found
) {
1226 if (devlist
->disposition
== 0)
1227 devlist
->disposition
= devmode
;
1230 fputs(Usage
, stderr
);
1235 struct createinfo
*ci
= conf_get_create_info();
1237 if (strcasecmp(symlinks
, "yes") == 0)
1239 else if (strcasecmp(symlinks
, "no") == 0)
1242 pr_err("option --symlinks must be 'no' or 'yes'\n");
1246 /* Ok, got the option parsing out of the way
1247 * hopefully it's mostly right but there might be some stuff
1250 * That is mosty checked in the per-mode stuff but...
1252 * For @,B,C and A without -s, the first device listed must be
1253 * an md device. We check that here and open it.
1256 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1258 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1259 if (devs_found
< 1) {
1260 pr_err("an md device must be given in this mode\n");
1263 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1264 pr_err("--super-minor=dev is incompatible with --auto\n");
1267 if (mode
== MANAGE
|| mode
== GROW
) {
1268 mdfd
= open_mddev(devlist
->devname
, 1);
1272 /* non-existent device is OK */
1273 mdfd
= open_mddev(devlist
->devname
, 0);
1275 pr_err("device %s exists but is not an md array.\n", devlist
->devname
);
1278 if ((int)ident
.super_minor
== -2) {
1281 pr_err("--super-minor=dev given, and listed device %s doesn't exist.\n",
1286 ident
.super_minor
= minor(stb
.st_rdev
);
1288 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1289 /* We don't really want this open yet, we just might
1290 * have wanted to check some things
1298 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1299 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1300 " a mistake. If you really mean it you will need to specify --force before\n"
1301 " setting the number of drives.\n");
1306 if (c
.homehost
== NULL
&& c
.require_homehost
)
1307 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1308 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1309 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1310 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1311 c
.homehost
= sys_hostname
;
1314 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1316 c
.require_homehost
= 0;
1320 if (c
.homecluster
== NULL
&& (c
.nodes
> 0)) {
1321 c
.homecluster
= conf_get_homecluster();
1322 if (c
.homecluster
== NULL
)
1323 rv
= get_cluster_name(&c
.homecluster
);
1325 pr_err("The md can't get cluster name\n");
1330 if (c
.backup_file
&& data_offset
!= INVALID_SECTORS
) {
1331 pr_err("--backup-file and --data-offset are incompatible\n");
1335 if ((mode
== MISC
&& devmode
== 'E')
1336 || (mode
== MONITOR
&& spare_sharing
== 0))
1337 /* Anyone may try this */;
1338 else if (geteuid() != 0) {
1339 pr_err("must be super-user to perform this action\n");
1343 ident
.autof
= c
.autof
;
1345 if (c
.scan
&& c
.verbose
< 2)
1346 /* --scan implied --brief unless -vv */
1351 /* readonly, add/remove, readwrite, runstop */
1353 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1354 if (!rv
&& devs_found
>1)
1355 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1356 devlist
->next
, c
.verbose
, c
.test
,
1358 if (!rv
&& c
.readonly
< 0)
1359 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1360 if (!rv
&& c
.runstop
> 0)
1361 rv
= Manage_run(devlist
->devname
, mdfd
, &c
);
1362 if (!rv
&& c
.runstop
< 0)
1363 rv
= Manage_stop(devlist
->devname
, mdfd
, c
.verbose
, 0);
1366 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1367 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1368 /* Only a device has been given, so get details from config file */
1369 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1370 if (array_ident
== NULL
) {
1371 pr_err("%s not identified in config file.\n",
1377 if (array_ident
->autof
== 0)
1378 array_ident
->autof
= c
.autof
;
1379 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1383 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1385 else if (devs_found
> 0) {
1386 if (c
.update
&& devs_found
> 1) {
1387 pr_err("can only update a single array at a time\n");
1390 if (c
.backup_file
&& devs_found
> 1) {
1391 pr_err("can only assemble a single array when providing a backup file.\n");
1394 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1395 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1396 if (array_ident
== NULL
) {
1397 pr_err("%s not identified in config file.\n",
1402 if (array_ident
->autof
== 0)
1403 array_ident
->autof
= c
.autof
;
1404 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1409 pr_err("--update not meaningful with a --scan assembly.\n");
1412 if (c
.backup_file
) {
1413 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1416 rv
= scan_assemble(ss
, &c
, &ident
);
1422 c
.delay
= DEFAULT_BITMAP_DELAY
;
1423 if (s
.write_behind
&& !s
.bitmap_file
) {
1424 pr_err("write-behind mode requires a bitmap.\n");
1428 if (s
.raiddisks
== 0) {
1429 pr_err("no raid-devices specified.\n");
1434 if (s
.bitmap_file
) {
1435 if (strcmp(s
.bitmap_file
, "internal")==0 ||
1436 strcmp(s
.bitmap_file
, "clustered") == 0) {
1437 pr_err("'internal' and 'clustered' bitmaps not supported with --build\n");
1442 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1446 c
.delay
= DEFAULT_BITMAP_DELAY
;
1449 if (!s
.bitmap_file
|| strcmp(s
.bitmap_file
, "clustered") != 0) {
1450 pr_err("--nodes argument only compatible with --bitmap=clustered\n");
1456 pr_err("--bitmap=clustered is currently supported with RAID mirror only\n");
1462 if (s
.write_behind
&& !s
.bitmap_file
) {
1463 pr_err("write-behind mode requires a bitmap.\n");
1467 if (s
.raiddisks
== 0) {
1468 pr_err("no raid-devices specified.\n");
1473 rv
= Create(ss
, devlist
->devname
,
1474 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1475 devs_found
-1, devlist
->next
,
1476 &s
, &c
, data_offset
);
1479 if (devmode
== 'E') {
1480 if (devlist
== NULL
&& !c
.scan
) {
1481 pr_err("No devices to examine\n");
1484 if (devlist
== NULL
)
1485 devlist
= conf_get_devs();
1486 if (devlist
== NULL
) {
1487 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1490 rv
= Examine(devlist
, &c
, ss
);
1491 } else if (devmode
== DetailPlatform
) {
1492 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1493 c
.verbose
, c
.export
,
1494 devlist
? devlist
->devname
: NULL
);
1495 } else if (devlist
== NULL
) {
1496 if (devmode
== 'S' && c
.scan
)
1497 rv
= stop_scan(c
.verbose
);
1498 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1499 rv
= misc_scan(devmode
, &c
);
1500 else if (devmode
== UdevRules
)
1501 rv
= Write_rules(udev_filename
);
1503 pr_err("No devices given.\n");
1507 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1510 if (!devlist
&& !c
.scan
) {
1511 pr_err("Cannot monitor: need --scan or at least one device\n");
1515 if (pidfile
&& !daemonise
) {
1516 pr_err("Cannot write a pid file when not in daemon mode\n");
1521 if (get_linux_version() > 2006016)
1522 /* mdstat responds to poll */
1527 rv
= Monitor(devlist
, mailaddr
, program
,
1528 &c
, daemonise
, oneshot
,
1529 dosyslog
, pidfile
, increments
,
1534 if (array_size
> 0) {
1535 /* alway impose array size first, independent of
1537 * Do not allow level or raid_disks changes at the
1538 * same time as that can be irreversibly destructive.
1542 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1543 pr_err("cannot change array size in same operation as changing raiddisks or level.\n"
1544 " Change size first, then check that data is still intact.\n");
1548 sysfs_init(&sra
, mdfd
, NULL
);
1549 if (array_size
== MAX_SIZE
)
1550 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1552 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1555 pr_err("--array-size setting is too large.\n");
1557 pr_err("current kernel does not support setting --array-size\n");
1562 if (devs_found
> 1 && s
.raiddisks
== 0 && s
.level
== UnSet
) {
1564 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1565 pr_err("--add cannot be used with other geometry changes in --grow mode\n");
1569 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1570 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1575 } else if (s
.bitmap_file
) {
1576 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1577 s
.layout_str
!= NULL
|| devs_found
> 1) {
1578 pr_err("--bitmap changes cannot be used with other geometry changes in --grow mode\n");
1583 c
.delay
= DEFAULT_BITMAP_DELAY
;
1584 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1585 } else if (grow_continue
)
1586 rv
= Grow_continue_command(devlist
->devname
,
1587 mdfd
, c
.backup_file
,
1589 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1590 || s
.chunk
!= 0 || s
.level
!= UnSet
1591 || data_offset
!= INVALID_SECTORS
) {
1592 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1594 data_offset
, &c
, &s
);
1595 } else if (array_size
== 0)
1596 pr_err("no changes to --grow\n");
1605 pr_err("In --incremental mode, a device cannot be given with --scan.\n");
1608 if (c
.runstop
<= 0) {
1609 pr_err("--incremental --scan meaningless without --run.\n");
1612 if (devmode
== 'f') {
1613 pr_err("--incremental --scan --fail not supported.\n");
1616 rv
= IncrementalScan(&c
, NULL
);
1619 if (!rebuild_map
&& !c
.scan
) {
1620 pr_err("--incremental requires a device.\n");
1625 if (devmode
== 'f') {
1626 if (devlist
->next
) {
1627 pr_err("'--incremental --fail' can only handle one device.\n");
1631 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1634 rv
= Incremental(devlist
, &c
, ss
);
1643 static int scan_assemble(struct supertype
*ss
,
1645 struct mddev_ident
*ident
)
1647 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1648 struct mddev_dev
*devlist
= conf_get_devs();
1649 struct map_ent
*map
= NULL
;
1652 int failures
, successes
;
1654 if (conf_verify_devnames(array_list
)) {
1655 pr_err("Duplicate MD device names in conf file were found.\n");
1658 if (devlist
== NULL
) {
1659 pr_err("No devices listed in conf file were found.\n");
1662 for (a
= array_list
; a
; a
= a
->next
) {
1665 a
->autof
= c
->autof
;
1668 pr_err("failed to get exclusive lock on mapfile\n");
1673 for (a
= array_list
; a
; a
= a
->next
) {
1678 strcasecmp(a
->devname
, "<ignore>") == 0)
1681 r
= Assemble(ss
, a
->devname
,
1691 } while (failures
&& successes
);
1692 if (c
->homehost
&& cnt
== 0) {
1693 /* Maybe we can auto-assemble something.
1694 * Repeatedly call Assemble in auto-assemble mode
1699 ident
->autof
= c
->autof
;
1701 struct mddev_dev
*devlist
= conf_get_devs();
1704 rv2
= Assemble(ss
, NULL
,
1712 /* Incase there are stacked devices, we need to go around again */
1714 if (cnt
== 0 && rv
== 0) {
1715 pr_err("No arrays found in config file or automatically\n");
1719 } else if (cnt
== 0 && rv
== 0) {
1720 pr_err("No arrays found in config file\n");
1727 static int misc_scan(char devmode
, struct context
*c
)
1729 /* apply --detail or --wait-clean to
1730 * all devices in /proc/mdstat
1732 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1733 struct mdstat_ent
*e
;
1734 struct map_ent
*map
= NULL
;
1738 for (members
= 0; members
<= 1; members
++) {
1739 for (e
=ms
; e
; e
=e
->next
) {
1743 int member
= e
->metadata_version
&&
1744 strncmp(e
->metadata_version
,
1745 "external:/", 10) == 0;
1746 if (members
!= member
)
1748 me
= map_by_devnm(&map
, e
->devnm
);
1750 && strcmp(me
->path
, "/unknown") != 0)
1753 stat(name
, &stb
) != 0)
1754 name
= get_md_name(e
->devnm
);
1757 pr_err("cannot find device file for %s\n",
1762 rv
|= Detail(name
, c
);
1764 rv
|= WaitClean(name
, -1, c
->verbose
);
1772 static int stop_scan(int verbose
)
1774 /* apply --stop to all devices in /proc/mdstat */
1775 /* Due to possible stacking of devices, repeat until
1776 * nothing more can be stopped
1778 int progress
=1, err
;
1782 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1783 struct mdstat_ent
*e
;
1785 if (!progress
) last
= 1;
1786 progress
= 0; err
= 0;
1787 for (e
=ms
; e
; e
=e
->next
) {
1788 char *name
= get_md_name(e
->devnm
);
1792 pr_err("cannot find device file for %s\n",
1796 mdfd
= open_mddev(name
, 1);
1798 if (Manage_stop(name
, mdfd
, verbose
, !last
))
1808 } while (!last
&& err
);
1814 static int misc_list(struct mddev_dev
*devlist
,
1815 struct mddev_ident
*ident
,
1816 char *dump_directory
,
1817 struct supertype
*ss
, struct context
*c
)
1819 struct mddev_dev
*dv
;
1822 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1825 switch(dv
->disposition
) {
1827 rv
|= Detail(dv
->devname
, c
);
1829 case KillOpt
: /* Zero superblock */
1831 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1835 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1842 rv
|= Query(dv
->devname
); continue;
1844 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1846 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1849 rv
|= Wait(dv
->devname
); continue;
1851 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1853 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1855 case UpdateSubarray
:
1856 if (c
->update
== NULL
) {
1857 pr_err("-U/--update must be specified with --update-subarray\n");
1861 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1862 c
->update
, ident
, c
->verbose
);
1865 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1868 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1869 (dv
== devlist
&& dv
->next
== NULL
));
1872 rv
|= SetAction(dv
->devname
, c
->action
);
1875 if (dv
->devname
[0] == '/')
1876 mdfd
= open_mddev(dv
->devname
, 1);
1878 mdfd
= open_dev(dv
->devname
);
1880 pr_err("Cannot open %s\n", dv
->devname
);
1883 switch(dv
->disposition
) {
1886 rv
|= Manage_run(dv
->devname
, mdfd
, c
); break;
1888 rv
|= Manage_stop(dv
->devname
, mdfd
, c
->verbose
, 0); break;
1890 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1892 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;
1901 int SetAction(char *dev
, char *action
)
1903 int fd
= open(dev
, O_RDONLY
);
1906 pr_err("Couldn't open %s: %s\n", dev
, strerror(errno
));
1909 sysfs_init(&mdi
, fd
, NULL
);
1911 if (!mdi
.sys_name
[0]) {
1912 pr_err("%s is no an md array\n", dev
);
1916 if (sysfs_set_str(&mdi
, NULL
, "sync_action", action
) < 0) {
1917 pr_err("Count not set action for %s to %s: %s\n",
1918 dev
, action
, strerror(errno
));