2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2012 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.
33 static int scan_assemble(struct supertype
*ss
,
35 struct mddev_ident
*ident
);
36 static int misc_scan(char devmode
, struct context
*c
);
37 static int stop_scan(int verbose
);
38 static int misc_list(struct mddev_dev
*devlist
,
39 struct mddev_ident
*ident
,
41 struct supertype
*ss
, struct context
*c
);
44 int main(int argc
, char *argv
[])
52 unsigned long long array_size
= 0;
53 unsigned long long data_offset
= INVALID_SECTORS
;
54 struct mddev_ident ident
;
55 char *configfile
= NULL
;
58 struct mddev_dev
*devlist
= NULL
;
59 struct mddev_dev
**devlistend
= & devlist
;
62 char *symlinks
= NULL
;
63 int grow_continue
= 0;
64 /* autof indicates whether and how to create device node.
65 * bottom 3 bits are style. Rest (when shifted) are number of parts
67 * 1 - don't create (no)
68 * 2 - if is_standard, then create (yes)
69 * 3 - create as 'md' - reject is_standard mdp (md)
70 * 4 - create as 'mdp' - reject is_standard md (mdp)
71 * 5 - default to md if not is_standard (md in config file)
72 * 6 - default to mdp if not is_standard (part, or mdp in config file)
75 .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
;
122 * set first char of argv[0] to @. This is used by
123 * systemd to signal that the task was launched from
124 * initrd/initramfs and should be preserved during shutdown
128 while ((option_index
= -1) ,
129 (opt
=getopt_long(argc
, argv
,
130 shortopt
, long_options
,
131 &option_index
)) != -1) {
133 /* firstly, some mode-independent options */
143 fputs(Version
, stderr
);
146 case 'v': c
.verbose
++;
149 case 'q': c
.verbose
--;
153 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
154 || mode
== GROW
|| mode
== INCREMENTAL
156 break; /* b means bitmap */
161 case 'Y': c
.export
++;
165 if (strcasecmp(optarg
, "<ignore>") == 0)
166 c
.require_homehost
= 0;
172 /* Silently ignore old option */
178 if (asprintf(&c
.prefer
, "/%s/", optarg
) <= 0)
184 fputs(Usage
, stderr
);
187 /* second, figure out the mode.
188 * Some options force the mode. Others
189 * set the mode if it isn't already
195 shortopt
= short_bitmap_options
;
205 case ReAdd
: /* re-add */
208 shortopt
= short_bitmap_options
;
212 case 'A': newmode
= ASSEMBLE
;
213 shortopt
= short_bitmap_auto_options
;
215 case 'B': newmode
= BUILD
;
216 shortopt
= short_bitmap_auto_options
;
218 case 'C': newmode
= CREATE
;
219 shortopt
= short_bitmap_auto_options
;
221 case 'F': newmode
= MONITOR
;
223 case 'G': newmode
= GROW
;
224 shortopt
= short_bitmap_options
;
226 case 'I': newmode
= INCREMENTAL
;
227 shortopt
= short_bitmap_auto_options
;
230 newmode
= AUTODETECT
;
264 if (mode
&& newmode
== mode
) {
265 /* everybody happy ! */
266 } else if (mode
&& newmode
!= mode
) {
269 if (option_index
>= 0)
270 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
272 fprintf(stderr
, "-%c", opt
);
273 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
274 map_num(modes
, newmode
),
275 map_num(modes
, mode
));
277 } else if (!mode
&& newmode
) {
279 if (mode
== MISC
&& devs_found
) {
280 pr_err("No action given for %s in --misc mode\n",
282 cont_err("Action options must come before device names\n");
286 /* special case of -c --help */
287 if ((opt
== 'c' || opt
== ConfigFile
) &&
288 ( strncmp(optarg
, "--h", 3)==0 ||
289 strncmp(optarg
, "-h", 2)==0)) {
290 fputs(Help_config
, stdout
);
294 /* If first option is a device, don't force the mode yet */
296 if (devs_found
== 0) {
297 dv
= xmalloc(sizeof(*dv
));
298 dv
->devname
= optarg
;
299 dv
->disposition
= devmode
;
300 dv
->writemostly
= writemostly
;
304 devlistend
= &dv
->next
;
309 /* No mode yet, and this is the second device ... */
310 pr_err("An option must be given to set the mode before a second device\n"
311 " (%s) is listed\n", optarg
);
314 if (option_index
>= 0)
315 pr_err("--%s", long_options
[option_index
].name
);
318 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
322 /* if we just set the mode, then done */
336 /* an undecorated option - must be a device name.
339 if (devs_found
> 0 && devmode
== DetailPlatform
) {
340 pr_err("controller may only be specified once. %s ignored\n",
345 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
346 pr_err("Must give one of -a/-r/-f"
347 " for subsequent devices at %s\n", optarg
);
350 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
351 pr_err("Must give -a/--add for"
352 " devices to add: %s\n", optarg
);
355 dv
= xmalloc(sizeof(*dv
));
356 dv
->devname
= optarg
;
357 dv
->disposition
= devmode
;
358 dv
->writemostly
= writemostly
;
362 devlistend
= &dv
->next
;
368 /* We've got a mode, and opt is now something else which
369 * could depend on the mode */
370 #define O(a,b) ((a<<16)|b)
371 switch (O(mode
,opt
)) {
373 case O(GROW
,ChunkSize
):
375 case O(CREATE
,ChunkSize
):
376 case O(BUILD
,'c'): /* chunk or rounding */
377 case O(BUILD
,ChunkSize
): /* chunk or rounding */
379 pr_err("chunk/rounding may only be specified once. "
380 "Second value is %s.\n", optarg
);
383 s
.chunk
= parse_size(optarg
);
384 if (s
.chunk
== INVALID_SECTORS
||
385 s
.chunk
< 8 || (s
.chunk
&1)) {
386 pr_err("invalid chunk/rounding value: %s\n",
390 /* Convert sectors to K */
394 case O(INCREMENTAL
, 'e'):
396 case O(ASSEMBLE
,'e'):
397 case O(MISC
,'e'): /* set metadata (superblock) information */
399 pr_err("metadata information already given\n");
402 for(i
=0; !ss
&& superlist
[i
]; i
++)
403 ss
= superlist
[i
]->match_metadata_desc(optarg
);
406 pr_err("unrecognised metadata identifier: %s\n", optarg
);
412 case O(MANAGE
,WriteMostly
):
414 case O(BUILD
,WriteMostly
):
416 case O(CREATE
,WriteMostly
):
417 /* set write-mostly for following devices */
422 /* clear write-mostly for following devices */
429 case O(BUILD
,'z'): /* size */
431 pr_err("size may only be specified once. "
432 "Second value is %s.\n", optarg
);
435 if (strcmp(optarg
, "max")==0)
438 s
.size
= parse_size(optarg
);
439 if (s
.size
== INVALID_SECTORS
||
441 pr_err("invalid size: %s\n",
445 /* convert sectors to K */
450 case O(GROW
,'Z'): /* array size */
451 if (array_size
> 0) {
452 pr_err("array-size may only be specified once. "
453 "Second value is %s.\n", optarg
);
456 if (strcmp(optarg
, "max") == 0)
457 array_size
= MAX_SIZE
;
459 array_size
= parse_size(optarg
);
460 if (array_size
== 0 ||
461 array_size
== INVALID_SECTORS
) {
462 pr_err("invalid array size: %s\n",
469 case O(CREATE
,DataOffset
):
470 case O(GROW
,DataOffset
):
471 if (data_offset
!= INVALID_SECTORS
) {
472 pr_err("data-offset may only be specified one. "
473 "Second value is %s.\n", optarg
);
476 if (mode
== CREATE
&&
477 strcmp(optarg
, "variable") == 0)
478 data_offset
= VARIABLE_OFFSET
;
480 data_offset
= parse_size(optarg
);
481 if (data_offset
== INVALID_SECTORS
) {
482 pr_err("invalid data-offset: %s\n",
490 case O(BUILD
,'l'): /* set raid level*/
491 if (s
.level
!= UnSet
) {
492 pr_err("raid level may only be set once. "
493 "Second value is %s.\n", optarg
);
496 s
.level
= map_name(pers
, optarg
);
497 if (s
.level
== UnSet
) {
498 pr_err("invalid raid level: %s\n",
502 if (s
.level
!= 0 && s
.level
!= LEVEL_LINEAR
&& s
.level
!= 1 &&
503 s
.level
!= LEVEL_MULTIPATH
&& s
.level
!= LEVEL_FAULTY
&&
506 pr_err("Raid level %s not permitted with --build.\n",
510 if (s
.sparedisks
> 0 && s
.level
< 1 && s
.level
>= -1) {
511 pr_err("raid level %s is incompatible with spare-devices setting.\n",
515 ident
.level
= s
.level
;
518 case O(GROW
, 'p'): /* new layout */
519 case O(GROW
, Layout
):
521 pr_err("layout may only be sent once. "
522 "Second value was %s\n", optarg
);
525 s
.layout_str
= optarg
;
526 /* 'Grow' will parse the value */
529 case O(CREATE
,'p'): /* raid5 layout */
530 case O(CREATE
,Layout
):
531 case O(BUILD
,'p'): /* faulty layout */
532 case O(BUILD
,Layout
):
533 if (s
.layout
!= UnSet
) {
534 pr_err("layout may only be sent once. "
535 "Second value was %s\n", optarg
);
540 pr_err("layout not meaningful for %s arrays.\n",
541 map_num(pers
, s
.level
));
544 pr_err("raid level must be given before layout.\n");
548 s
.layout
= map_name(r5layout
, optarg
);
549 if (s
.layout
==UnSet
) {
550 pr_err("layout %s not understood for raid5.\n",
556 s
.layout
= map_name(r6layout
, optarg
);
557 if (s
.layout
==UnSet
) {
558 pr_err("layout %s not understood for raid6.\n",
565 s
.layout
= parse_layout_10(optarg
);
567 pr_err("layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
575 s
.layout
= parse_layout_faulty(optarg
);
576 if (s
.layout
== -1) {
577 pr_err("layout %s not understood for faulty.\n",
585 case O(CREATE
,AssumeClean
):
586 case O(BUILD
,AssumeClean
): /* assume clean */
587 case O(GROW
,AssumeClean
):
593 case O(BUILD
,'n'): /* number of raid disks */
595 pr_err("raid-devices set twice: %d and %s\n",
596 s
.raiddisks
, optarg
);
599 s
.raiddisks
= parse_num(optarg
);
600 if (s
.raiddisks
<= 0) {
601 pr_err("invalid number of raid devices: %s\n",
605 ident
.raid_disks
= s
.raiddisks
;
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. "
667 "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. "
683 "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. "
702 "Second value: %s.\n", optarg
);
705 if (strcmp(optarg
, "dev")==0)
706 ident
.super_minor
= -2;
708 ident
.super_minor
= parse_num(optarg
);
709 if (ident
.super_minor
< 0) {
710 pr_err("Bad super-minor number: %s.\n", optarg
);
716 case O(ASSEMBLE
,'o'):
722 case O(ASSEMBLE
,'U'): /* update the superblock */
725 pr_err("Can only update one aspect"
726 " of superblock, both %s and %s given.\n",
730 if (mode
== MISC
&& !c
.subarray
) {
731 pr_err("Only subarrays can be"
732 " updated in misc mode\n");
736 if (strcmp(c
.update
, "sparc2.2")==0)
738 if (strcmp(c
.update
, "super-minor") == 0)
740 if (strcmp(c
.update
, "summaries")==0)
742 if (strcmp(c
.update
, "resync")==0)
744 if (strcmp(c
.update
, "uuid")==0)
746 if (strcmp(c
.update
, "name")==0)
748 if (strcmp(c
.update
, "homehost")==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
, "byteorder")==0) {
760 pr_err("must not set metadata"
761 " 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"
769 " cannot find 0.swap\n");
775 if (strcmp(c
.update
,"?") == 0 ||
776 strcmp(c
.update
, "help") == 0) {
778 fprintf(outf
, Name
": ");
782 Name
": '--update=%s' is invalid. ",
785 fprintf(outf
, "Valid --update options are:\n"
786 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
787 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
789 exit(outf
== stdout
? 0 : 2);
792 /* update=devicesize is allowed with --re-add */
793 if (devmode
!= 'A') {
794 pr_err("--update in Manage mode only"
795 " allowed with --re-add.\n");
799 pr_err("Can only update one aspect"
800 " of superblock, both %s and %s given.\n",
805 if (strcmp(c
.update
, "devicesize") != 0 &&
806 strcmp(c
.update
, "bbl") != 0 &&
807 strcmp(c
.update
, "no-bbl") != 0) {
808 pr_err("only 'devicesize', 'bbl' and 'no-bbl' can be"
809 " updated with --re-add\n");
814 case O(INCREMENTAL
,NoDegraded
):
815 pr_err("--no-degraded is deprecated in Incremental mode\n");
816 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
817 c
.runstop
= -1; /* --stop isn't allowed for --assemble,
818 * so we overload slightly */
821 case O(ASSEMBLE
,'c'):
822 case O(ASSEMBLE
,ConfigFile
):
823 case O(INCREMENTAL
, 'c'):
824 case O(INCREMENTAL
, ConfigFile
):
826 case O(MISC
, ConfigFile
):
828 case O(MONITOR
,ConfigFile
):
830 pr_err("configfile cannot be set twice. "
831 "Second value is %s.\n", optarg
);
835 set_conffile(configfile
);
836 /* FIXME possibly check that config file exists. Even parse it */
838 case O(ASSEMBLE
,'s'): /* scan */
841 case O(INCREMENTAL
,'s'):
845 case O(MONITOR
,'m'): /* mail address */
846 case O(MONITOR
,EMail
):
848 pr_err("only specify one mailaddress. %s ignored.\n",
854 case O(MONITOR
,'p'): /* alert program */
855 case O(MONITOR
,ProgramOpt
): /* alert program */
857 pr_err("only specify one alter program. %s ignored.\n",
863 case O(MONITOR
,'r'): /* rebuild increments */
864 case O(MONITOR
,Increment
):
865 increments
= atoi(optarg
);
866 if (increments
> 99 || increments
< 1) {
867 pr_err("please specify positive integer between 1 and 99 as rebuild increments.\n");
872 case O(MONITOR
,'d'): /* delay in seconds */
874 case O(BUILD
,'d'): /* delay for bitmap updates */
877 pr_err("only specify delay once. %s ignored.\n",
880 c
.delay
= parse_num(optarg
);
882 pr_err("invalid delay: %s\n",
888 case O(MONITOR
,'f'): /* daemonise */
889 case O(MONITOR
,Fork
):
892 case O(MONITOR
,'i'): /* pid */
894 pr_err("only specify one pid file. %s ignored.\n",
899 case O(MONITOR
,'1'): /* oneshot */
903 case O(MONITOR
,'t'): /* test */
906 case O(MONITOR
,'y'): /* log messages to syslog */
907 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
910 case O(MONITOR
, NoSharing
):
914 /* now the general management options. Some are applicable
915 * to other modes. None have arguments.
920 case O(MANAGE
,Add
): /* add a drive */
923 case O(MANAGE
,ReAdd
):
926 case O(MANAGE
,'r'): /* remove a drive */
927 case O(MANAGE
,Remove
):
930 case O(MANAGE
,'f'): /* set faulty */
932 case O(INCREMENTAL
,'f'):
933 case O(INCREMENTAL
,Remove
):
934 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
935 * even though we will both fail and
936 * remove the device */
939 case O(MANAGE
,Replace
):
940 /* Mark these devices for replacement */
944 /* These are the replacements to use */
945 if (devmode
!= 'R') {
946 pr_err("--with must follow --replace\n");
951 case O(INCREMENTAL
,'R'):
953 case O(ASSEMBLE
,'R'):
955 case O(CREATE
,'R'): /* Run the array */
957 pr_err("Cannot both Stop and Run an array\n");
964 pr_err("Cannot both Run and Stop an array\n");
976 case O(MISC
,KillOpt
):
980 case O(MISC
, ExamineBB
):
984 case O(MISC
, WaitOpt
):
985 case O(MISC
, Waitclean
):
986 case O(MISC
, DetailPlatform
):
987 case O(MISC
, KillSubarray
):
988 case O(MISC
, UpdateSubarray
):
990 case O(MISC
, Restore
):
991 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
993 pr_err("subarray can only"
994 " be specified once\n");
999 if (devmode
&& devmode
!= opt
&&
1000 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
1001 pr_err("--examine/-E cannot be given with ");
1002 if (devmode
== 'E') {
1003 if (option_index
>= 0)
1004 fprintf(stderr
, "--%s\n",
1005 long_options
[option_index
].name
);
1007 fprintf(stderr
, "-%c\n", opt
);
1008 } else if (isalpha(devmode
))
1009 fprintf(stderr
, "-%c\n", devmode
);
1011 fprintf(stderr
, "previous option\n");
1015 if (opt
== Dump
|| opt
== Restore
) {
1016 if (dump_directory
!= NULL
) {
1017 pr_err("dump/restore directory specified twice: %s and %s\n",
1018 dump_directory
, optarg
);
1021 dump_directory
= optarg
;
1024 case O(MISC
, UdevRules
):
1025 if (devmode
&& devmode
!= opt
) {
1026 pr_err("--udev-rules must"
1027 " be the only option.\n");
1030 pr_err("only specify one udev "
1031 "rule filename. %s ignored.\n",
1034 udev_filename
= optarg
;
1042 case O(MISC
, Sparc22
):
1043 if (devmode
!= 'E') {
1044 pr_err("--sparc2.2 only allowed with --examine\n");
1050 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1051 case O(ASSEMBLE
,Bitmap
):
1053 pr_err("bitmap file needed with -b in --assemble mode\n");
1056 if (strcmp(optarg
, "internal")==0) {
1057 pr_err("there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1060 bitmap_fd
= open(optarg
, O_RDWR
);
1061 if (!*optarg
|| bitmap_fd
< 0) {
1062 pr_err("cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1065 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1068 case O(ASSEMBLE
, BackupFile
):
1069 case O(GROW
, BackupFile
):
1070 /* Specify a file into which grow might place a backup,
1071 * or from which assemble might recover a backup
1073 if (c
.backup_file
) {
1074 pr_err("backup file already specified, rejecting %s\n", optarg
);
1077 c
.backup_file
= optarg
;
1080 case O(GROW
, Continue
):
1081 /* Continue interrupted grow
1085 case O(ASSEMBLE
, InvalidBackup
):
1086 /* Acknowledge that the backupfile is invalid, but ask
1087 * to continue anyway
1089 c
.invalid_backup
= 1;
1093 case O(BUILD
,Bitmap
):
1095 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1097 case O(GROW
,Bitmap
):
1098 if (strcmp(optarg
, "internal")== 0 ||
1099 strcmp(optarg
, "none")== 0 ||
1100 strchr(optarg
, '/') != NULL
) {
1101 s
.bitmap_file
= optarg
;
1105 pr_err("bitmap file must contain a '/', or be 'internal', or 'none'\n"
1106 " not '%s'\n", optarg
);
1109 case O(GROW
,BitmapChunk
):
1110 case O(BUILD
,BitmapChunk
):
1111 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1112 s
.bitmap_chunk
= parse_size(optarg
);
1113 if (s
.bitmap_chunk
== 0 ||
1114 s
.bitmap_chunk
== INVALID_SECTORS
||
1115 s
.bitmap_chunk
& (s
.bitmap_chunk
- 1)) {
1116 pr_err("invalid bitmap chunksize: %s\n",
1120 s
.bitmap_chunk
= s
.bitmap_chunk
* 512;
1123 case O(GROW
, WriteBehind
):
1124 case O(BUILD
, WriteBehind
):
1125 case O(CREATE
, WriteBehind
): /* write-behind mode */
1126 s
.write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1128 s
.write_behind
= parse_num(optarg
);
1129 if (s
.write_behind
< 0 ||
1130 s
.write_behind
> 16383) {
1131 pr_err("Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1137 case O(INCREMENTAL
, 'r'):
1138 case O(INCREMENTAL
, RebuildMapOpt
):
1141 case O(INCREMENTAL
, IncrementalPath
):
1142 remove_path
= optarg
;
1145 /* We have now processed all the valid options. Anything else is
1148 if (option_index
> 0)
1149 pr_err(":option --%s not valid in %s mode\n",
1150 long_options
[option_index
].name
,
1151 map_num(modes
, mode
));
1153 pr_err("option -%c not valid in %s mode\n",
1154 opt
, map_num(modes
, mode
));
1161 if (print_help
== 2)
1162 help_text
= OptionHelp
;
1164 help_text
= mode_help
[mode
];
1165 if (help_text
== NULL
)
1167 fputs(help_text
,stdout
);
1171 if (!mode
&& devs_found
) {
1174 if (devlist
->disposition
== 0)
1175 devlist
->disposition
= devmode
;
1178 fputs(Usage
, stderr
);
1183 struct createinfo
*ci
= conf_get_create_info();
1185 if (strcasecmp(symlinks
, "yes") == 0)
1187 else if (strcasecmp(symlinks
, "no") == 0)
1190 pr_err("option --symlinks must be 'no' or 'yes'\n");
1194 /* Ok, got the option parsing out of the way
1195 * hopefully it's mostly right but there might be some stuff
1198 * That is mosty checked in the per-mode stuff but...
1200 * For @,B,C and A without -s, the first device listed must be
1201 * an md device. We check that here and open it.
1204 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1206 || (mode
== ASSEMBLE
&& ! c
.scan
)) {
1207 if (devs_found
< 1) {
1208 pr_err("an md device must be given in this mode\n");
1211 if ((int)ident
.super_minor
== -2 && c
.autof
) {
1212 pr_err("--super-minor=dev is incompatible with --auto\n");
1215 if (mode
== MANAGE
|| mode
== GROW
) {
1216 mdfd
= open_mddev(devlist
->devname
, 1);
1220 /* non-existent device is OK */
1221 mdfd
= open_mddev(devlist
->devname
, 0);
1223 pr_err("device %s exists but is not an "
1224 "md array.\n", devlist
->devname
);
1227 if ((int)ident
.super_minor
== -2) {
1230 pr_err("--super-minor=dev given, and "
1231 "listed device %s doesn't exist.\n",
1236 ident
.super_minor
= minor(stb
.st_rdev
);
1238 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1239 /* We don't really want this open yet, we just might
1240 * have wanted to check some things
1248 if (s
.raiddisks
== 1 && !c
.force
&& s
.level
!= LEVEL_FAULTY
) {
1249 pr_err("'1' is an unusual number of drives for an array, so it is probably\n"
1250 " a mistake. If you really mean it you will need to specify --force before\n"
1251 " setting the number of drives.\n");
1256 if (c
.homehost
== NULL
)
1257 c
.homehost
= conf_get_homehost(&c
.require_homehost
);
1258 if (c
.homehost
== NULL
|| strcasecmp(c
.homehost
, "<system>")==0) {
1259 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1260 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1261 c
.homehost
= sys_hostname
;
1264 if (c
.homehost
&& (!c
.homehost
[0] || strcasecmp(c
.homehost
, "<none>") == 0)) {
1266 c
.require_homehost
= 0;
1269 if ((mode
== MISC
&& devmode
== 'E')
1270 || (mode
== MONITOR
&& spare_sharing
== 0))
1271 /* Anyone may try this */;
1272 else if (geteuid() != 0) {
1273 pr_err("must be super-user to perform this action\n");
1277 ident
.autof
= c
.autof
;
1279 if (c
.scan
&& c
.verbose
< 2)
1280 /* --scan implied --brief unless -vv */
1286 /* readonly, add/remove, readwrite, runstop */
1288 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1289 if (!rv
&& devs_found
>1)
1290 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1291 devlist
->next
, c
.verbose
, c
.test
,
1293 if (!rv
&& c
.readonly
< 0)
1294 rv
= Manage_ro(devlist
->devname
, mdfd
, c
.readonly
);
1295 if (!rv
&& c
.runstop
)
1296 rv
= Manage_runstop(devlist
->devname
, mdfd
, c
.runstop
, c
.verbose
, 0);
1299 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1300 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !c
.scan
) {
1301 /* Only a device has been given, so get details from config file */
1302 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1303 if (array_ident
== NULL
) {
1304 pr_err("%s not identified in config file.\n",
1310 if (array_ident
->autof
== 0)
1311 array_ident
->autof
= c
.autof
;
1312 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1316 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1318 else if (devs_found
> 0) {
1319 if (c
.update
&& devs_found
> 1) {
1320 pr_err("can only update a single array at a time\n");
1323 if (c
.backup_file
&& devs_found
> 1) {
1324 pr_err("can only assemble a single array when providing a backup file.\n");
1327 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1328 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1329 if (array_ident
== NULL
) {
1330 pr_err("%s not identified in config file.\n",
1335 if (array_ident
->autof
== 0)
1336 array_ident
->autof
= c
.autof
;
1337 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1342 pr_err("--update not meaningful with a --scan assembly.\n");
1345 if (c
.backup_file
) {
1346 pr_err("--backup_file not meaningful with a --scan assembly.\n");
1349 rv
= scan_assemble(ss
, &c
, &ident
);
1355 c
.delay
= DEFAULT_BITMAP_DELAY
;
1356 if (s
.write_behind
&& !s
.bitmap_file
) {
1357 pr_err("write-behind mode requires a bitmap.\n");
1361 if (s
.raiddisks
== 0) {
1362 pr_err("no raid-devices specified.\n");
1367 if (s
.bitmap_file
) {
1368 if (strcmp(s
.bitmap_file
, "internal")==0) {
1369 pr_err("'internal' bitmaps not supported with --build\n");
1374 rv
= Build(devlist
->devname
, devlist
->next
, &s
, &c
);
1378 c
.delay
= DEFAULT_BITMAP_DELAY
;
1379 if (s
.write_behind
&& !s
.bitmap_file
) {
1380 pr_err("write-behind mode requires a bitmap.\n");
1384 if (s
.raiddisks
== 0) {
1385 pr_err("no raid-devices specified.\n");
1390 rv
= Create(ss
, devlist
->devname
,
1391 ident
.name
, ident
.uuid_set
? ident
.uuid
: NULL
,
1392 devs_found
-1, devlist
->next
,
1393 &s
, &c
, data_offset
);
1396 if (devmode
== 'E') {
1397 if (devlist
== NULL
&& !c
.scan
) {
1398 pr_err("No devices to examine\n");
1401 if (devlist
== NULL
)
1402 devlist
= conf_get_devs();
1403 if (devlist
== NULL
) {
1404 pr_err("No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1407 rv
= Examine(devlist
, &c
, ss
);
1408 } else if (devmode
== DetailPlatform
) {
1409 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? c
.scan
: 1,
1410 c
.verbose
, c
.export
,
1411 devlist
? devlist
->devname
: NULL
);
1412 } else if (devlist
== NULL
) {
1413 if (devmode
== 'S' && c
.scan
)
1414 rv
= stop_scan(c
.verbose
);
1415 else if ((devmode
== 'D' || devmode
== Waitclean
) && c
.scan
)
1416 rv
= misc_scan(devmode
, &c
);
1417 else if (devmode
== UdevRules
)
1418 rv
= Write_rules(udev_filename
);
1420 pr_err("No devices given.\n");
1424 rv
= misc_list(devlist
, &ident
, dump_directory
, ss
, &c
);
1427 if (!devlist
&& !c
.scan
) {
1428 pr_err("Cannot monitor: need --scan or at least one device\n");
1432 if (pidfile
&& !daemonise
) {
1433 pr_err("Cannot write a pid file when not in daemon mode\n");
1438 if (get_linux_version() > 2006016)
1439 /* mdstat responds to poll */
1446 rv
= Monitor(devlist
, mailaddr
, program
,
1447 &c
, daemonise
, oneshot
,
1448 dosyslog
, pidfile
, increments
,
1453 if (array_size
> 0) {
1454 /* alway impose array size first, independent of
1456 * Do not allow level or raid_disks changes at the
1457 * same time as that can be irreversibly destructive.
1461 if (s
.raiddisks
|| s
.level
!= UnSet
) {
1462 pr_err("cannot change array size in same operation "
1463 "as changing raiddisks or level.\n"
1464 " Change size first, then check that data is still intact.\n");
1468 sysfs_init(&sra
, mdfd
, NULL
);
1469 if (array_size
== MAX_SIZE
)
1470 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1472 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1475 pr_err("--array-size setting"
1476 " is too large.\n");
1478 pr_err("current kernel does"
1479 " not support setting --array-size\n");
1484 if (devs_found
> 1 && s
.raiddisks
== 0) {
1486 if (s
.size
> 0 || s
.chunk
|| s
.layout_str
!= NULL
|| s
.bitmap_file
) {
1487 pr_err("--add cannot be used with "
1488 "other geometry changes in --grow mode\n");
1492 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1493 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1498 } else if (s
.bitmap_file
) {
1499 if (s
.size
> 0 || s
.raiddisks
|| s
.chunk
||
1500 s
.layout_str
!= NULL
|| devs_found
> 1) {
1501 pr_err("--bitmap changes cannot be "
1502 "used with other geometry changes "
1503 "in --grow mode\n");
1508 c
.delay
= DEFAULT_BITMAP_DELAY
;
1509 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, &c
, &s
);
1510 } else if (grow_continue
)
1511 rv
= Grow_continue_command(devlist
->devname
,
1512 mdfd
, c
.backup_file
,
1514 else if (s
.size
> 0 || s
.raiddisks
|| s
.layout_str
!= NULL
1515 || s
.chunk
!= 0 || s
.level
!= UnSet
) {
1516 rv
= Grow_reshape(devlist
->devname
, mdfd
,
1518 data_offset
, &c
, &s
);
1519 } else if (array_size
== 0)
1520 pr_err("no changes to --grow\n");
1527 if (c
.runstop
<= 0) {
1528 pr_err("--incremental --scan meaningless without --run.\n");
1531 if (devmode
== 'f') {
1532 pr_err("--incremental --scan --fail not supported.\n");
1535 rv
= IncrementalScan(c
.verbose
);
1538 if (!rebuild_map
&& !c
.scan
) {
1539 pr_err("--incremental requires a device.\n");
1544 if (devlist
->next
) {
1545 pr_err("--incremental can only handle one device.\n");
1550 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1553 rv
= Incremental(devlist
->devname
, &c
, ss
);
1562 static int scan_assemble(struct supertype
*ss
,
1564 struct mddev_ident
*ident
)
1566 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1567 struct mddev_dev
*devlist
= conf_get_devs();
1568 struct map_ent
*map
= NULL
;
1571 int failures
, successes
;
1573 if (conf_verify_devnames(array_list
)) {
1574 pr_err("Duplicate MD device names in "
1575 "conf file were found.\n");
1578 if (devlist
== NULL
) {
1579 pr_err("No devices listed in conf file were found.\n");
1582 for (a
= array_list
; a
; a
= a
->next
) {
1585 a
->autof
= c
->autof
;
1588 pr_err("%s: failed to get "
1589 "exclusive lock on mapfile\n",
1595 for (a
= array_list
; a
; a
= a
->next
) {
1600 strcasecmp(a
->devname
, "<ignore>") == 0)
1603 r
= Assemble(ss
, a
->devname
,
1613 } while (failures
&& successes
);
1614 if (c
->homehost
&& cnt
== 0) {
1615 /* Maybe we can auto-assemble something.
1616 * Repeatedly call Assemble in auto-assemble mode
1621 ident
->autof
= c
->autof
;
1623 struct mddev_dev
*devlist
= conf_get_devs();
1626 rv2
= Assemble(ss
, NULL
,
1634 /* Incase there are stacked devices, we need to go around again */
1636 if (cnt
== 0 && rv
== 0) {
1637 pr_err("No arrays found in config file or automatically\n");
1641 } else if (cnt
== 0 && rv
== 0) {
1642 pr_err("No arrays found in config file\n");
1649 static int misc_scan(char devmode
, struct context
*c
)
1651 /* apply --detail or --wait-clean to
1652 * all devices in /proc/mdstat
1654 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1655 struct mdstat_ent
*e
;
1656 struct map_ent
*map
= NULL
;
1660 for (members
= 0; members
<= 1; members
++) {
1661 for (e
=ms
; e
; e
=e
->next
) {
1665 int member
= e
->metadata_version
&&
1666 strncmp(e
->metadata_version
,
1667 "external:/", 10) == 0;
1668 if (members
!= member
)
1670 me
= map_by_devnm(&map
, e
->devnm
);
1672 && strcmp(me
->path
, "/unknown") != 0)
1675 stat(name
, &stb
) != 0)
1676 name
= get_md_name(e
->devnm
);
1679 pr_err("cannot find device file for %s\n",
1684 rv
|= Detail(name
, c
);
1686 rv
|= WaitClean(name
, -1, c
->verbose
);
1694 static int stop_scan(int verbose
)
1696 /* apply --stop to all devices in /proc/mdstat */
1697 /* Due to possible stacking of devices, repeat until
1698 * nothing more can be stopped
1700 int progress
=1, err
;
1704 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1705 struct mdstat_ent
*e
;
1707 if (!progress
) last
= 1;
1708 progress
= 0; err
= 0;
1709 for (e
=ms
; e
; e
=e
->next
) {
1710 char *name
= get_md_name(e
->devnm
);
1714 pr_err("cannot find device file for %s\n",
1718 mdfd
= open_mddev(name
, 1);
1720 if (Manage_runstop(name
, mdfd
, -1, verbose
, !last
))
1730 } while (!last
&& err
);
1736 static int misc_list(struct mddev_dev
*devlist
,
1737 struct mddev_ident
*ident
,
1738 char *dump_directory
,
1739 struct supertype
*ss
, struct context
*c
)
1741 struct mddev_dev
*dv
;
1744 for (dv
=devlist
; dv
; dv
=(rv
& 16) ? NULL
: dv
->next
) {
1747 switch(dv
->disposition
) {
1749 rv
|= Detail(dv
->devname
, c
);
1751 case KillOpt
: /* Zero superblock */
1753 rv
|= Kill(dv
->devname
, ss
, c
->force
, c
->verbose
,0);
1757 rv
|= Kill(dv
->devname
, NULL
, c
->force
, v
, 0);
1764 rv
|= Query(dv
->devname
); continue;
1766 rv
|= ExamineBitmap(dv
->devname
, c
->brief
, ss
); continue;
1768 rv
|= ExamineBadblocks(dv
->devname
, c
->brief
, ss
); continue;
1771 rv
|= Wait(dv
->devname
); continue;
1773 rv
|= WaitClean(dv
->devname
, -1, c
->verbose
); continue;
1775 rv
|= Kill_subarray(dv
->devname
, c
->subarray
, c
->verbose
);
1777 case UpdateSubarray
:
1778 if (c
->update
== NULL
) {
1779 pr_err("-U/--update must be specified with --update-subarray\n");
1783 rv
|= Update_subarray(dv
->devname
, c
->subarray
,
1784 c
->update
, ident
, c
->verbose
);
1787 rv
|= Dump_metadata(dv
->devname
, dump_directory
, c
, ss
);
1790 rv
|= Restore_metadata(dv
->devname
, dump_directory
, c
, ss
,
1791 (dv
== devlist
&& dv
->next
== NULL
));
1794 mdfd
= open_mddev(dv
->devname
, 1);
1796 switch(dv
->disposition
) {
1798 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, c
->verbose
, 0); break;
1800 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, c
->verbose
, 0); break;
1802 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1804 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;