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(int autof
, struct supertype
*ss
,
34 int readonly
, int runstop
,
35 struct mddev_ident
*ident
,
36 char *homehost
, int require_homehost
,
37 int verbose
, int force
,
39 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
40 char *homehost
, char *prefer
);
41 static int stop_scan(int quiet
);
42 static int misc_list(struct mddev_dev
*devlist
,
43 int brief
, int verbose
, int export
, int test
,
44 char *homehost
, char *prefer
, char *subarray
,
45 char *update
, struct mddev_ident
*ident
,
46 struct supertype
*ss
, int force
, int quiet
);
49 int main(int argc
, char *argv
[])
60 long long array_size
= -1;
63 char *layout_str
= NULL
;
66 struct mddev_ident ident
;
67 char *configfile
= NULL
;
76 char *bitmap_file
= NULL
;
77 char *backup_file
= NULL
;
78 int invalid_backup
= 0;
79 int bitmap_chunk
= UnSet
;
81 struct mddev_dev
*devlist
= NULL
;
82 struct mddev_dev
**devlistend
= & devlist
;
93 char *symlinks
= NULL
;
94 int grow_continue
= 0;
95 /* autof indicates whether and how to create device node.
96 * bottom 3 bits are style. Rest (when shifted) are number of parts
98 * 1 - don't create (no)
99 * 2 - if is_standard, then create (yes)
100 * 3 - create as 'md' - reject is_standard mdp (md)
101 * 4 - create as 'mdp' - reject is_standard md (mdp)
102 * 5 - default to md if not is_standard (md in config file)
103 * 6 - default to mdp if not is_standard (part, or mdp in config file)
107 char *homehost
= NULL
;
108 char sys_hostname
[256];
109 int require_homehost
= 1;
110 char *mailaddr
= NULL
;
111 char *program
= NULL
;
115 char *pidfile
= NULL
;
117 int spare_sharing
= 1;
118 struct supertype
*ss
= NULL
;
121 char *shortopt
= short_options
;
124 char *subarray
= NULL
;
125 char *remove_path
= NULL
;
126 char *udev_filename
= NULL
;
133 int freeze_reshape
= 0;
135 srandom(time(0) ^ getpid());
139 ident
.raid_disks
= UnSet
;
140 ident
.super_minor
= UnSet
;
142 ident
.spare_group
= NULL
;
145 ident
.bitmap_fd
= -1;
146 ident
.bitmap_file
= NULL
;
148 ident
.container
= NULL
;
151 while ((option_index
= -1) ,
152 (opt
=getopt_long(argc
, argv
,
153 shortopt
, long_options
,
154 &option_index
)) != -1) {
156 /* firstly, some mode-independent options */
166 fputs(Version
, stderr
);
176 if (mode
== ASSEMBLE
|| mode
== BUILD
|| mode
== CREATE
177 || mode
== GROW
|| mode
== INCREMENTAL
179 break; /* b means bitmap */
188 if (strcasecmp(optarg
, "<ignore>") == 0)
189 require_homehost
= 0;
195 * --offroot sets first char of argv[0] to @. This is used
196 * by systemd to signal that the task was launched from
197 * initrd/initramfs and should be preserved during shutdown
207 if (asprintf(&prefer
, "/%s/", optarg
) <= 0)
213 fputs(Usage
, stderr
);
216 /* second, figure out the mode.
217 * Some options force the mode. Others
218 * set the mode if it isn't already
224 shortopt
= short_bitmap_options
;
232 case ReAdd
: /* re-add */
235 shortopt
= short_bitmap_options
;
239 case 'A': newmode
= ASSEMBLE
;
240 shortopt
= short_bitmap_auto_options
;
242 case 'B': newmode
= BUILD
;
243 shortopt
= short_bitmap_auto_options
;
245 case 'C': newmode
= CREATE
;
246 shortopt
= short_bitmap_auto_options
;
248 case 'F': newmode
= MONITOR
;
250 case 'G': newmode
= GROW
;
251 shortopt
= short_bitmap_options
;
253 case 'I': newmode
= INCREMENTAL
;
254 shortopt
= short_bitmap_auto_options
;
257 newmode
= AUTODETECT
;
288 if (mode
&& newmode
== mode
) {
289 /* everybody happy ! */
290 } else if (mode
&& newmode
!= mode
) {
292 fprintf(stderr
, Name
": ");
293 if (option_index
>= 0)
294 fprintf(stderr
, "--%s", long_options
[option_index
].name
);
296 fprintf(stderr
, "-%c", opt
);
297 fprintf(stderr
, " would set mdadm mode to \"%s\", but it is already set to \"%s\".\n",
298 map_num(modes
, newmode
),
299 map_num(modes
, mode
));
301 } else if (!mode
&& newmode
) {
303 if (mode
== MISC
&& devs_found
) {
304 fprintf(stderr
, Name
": No action given for %s in --misc mode\n",
306 fprintf(stderr
," Action options must come before device names\n");
310 /* special case of -c --help */
311 if ((opt
== 'c' || opt
== ConfigFile
) &&
312 ( strncmp(optarg
, "--h", 3)==0 ||
313 strncmp(optarg
, "-h", 2)==0)) {
314 fputs(Help_config
, stdout
);
318 /* If first option is a device, don't force the mode yet */
320 if (devs_found
== 0) {
321 dv
= malloc(sizeof(*dv
));
323 fprintf(stderr
, Name
": malloc failed\n");
326 dv
->devname
= optarg
;
327 dv
->disposition
= devmode
;
328 dv
->writemostly
= writemostly
;
333 devlistend
= &dv
->next
;
338 /* No mode yet, and this is the second device ... */
339 fprintf(stderr
, Name
": An option must be given to set the mode before a second device\n"
340 " (%s) is listed\n", optarg
);
343 if (option_index
>= 0)
344 fprintf(stderr
, Name
": --%s", long_options
[option_index
].name
);
346 fprintf(stderr
, Name
": -%c", opt
);
347 fprintf(stderr
, " does not set the mode, and so cannot be the first option.\n");
351 /* if we just set the mode, then done */
365 /* an undecorated option - must be a device name.
367 if (devs_found
> 0 && mode
== MANAGE
&& !devmode
) {
368 fprintf(stderr
, Name
": Must give one of -a/-r/-f"
369 " for subsequent devices at %s\n", optarg
);
372 if (devs_found
> 0 && mode
== GROW
&& !devmode
) {
373 fprintf(stderr
, Name
": Must give -a/--add for"
374 " devices to add: %s\n", optarg
);
377 dv
= malloc(sizeof(*dv
));
379 fprintf(stderr
, Name
": malloc failed\n");
382 dv
->devname
= optarg
;
383 dv
->disposition
= devmode
;
384 dv
->writemostly
= writemostly
;
389 devlistend
= &dv
->next
;
395 /* We've got a mode, and opt is now something else which
396 * could depend on the mode */
397 #define O(a,b) ((a<<16)|b)
398 switch (O(mode
,opt
)) {
400 case O(GROW
,ChunkSize
):
402 case O(CREATE
,ChunkSize
):
403 case O(BUILD
,'c'): /* chunk or rounding */
404 case O(BUILD
,ChunkSize
): /* chunk or rounding */
406 fprintf(stderr
, Name
": chunk/rounding may only be specified once. "
407 "Second value is %s.\n", optarg
);
410 chunk
= parse_size(optarg
);
411 if (chunk
< 8 || (chunk
&1)) {
412 fprintf(stderr
, Name
": invalid chunk/rounding value: %s\n",
416 /* Convert sectors to K */
420 case O(INCREMENTAL
, 'e'):
422 case O(ASSEMBLE
,'e'):
423 case O(MISC
,'e'): /* set metadata (superblock) information */
425 fprintf(stderr
, Name
": metadata information already given\n");
428 for(i
=0; !ss
&& superlist
[i
]; i
++)
429 ss
= superlist
[i
]->match_metadata_desc(optarg
);
432 fprintf(stderr
, Name
": unrecognised metadata identifier: %s\n", optarg
);
438 case O(MANAGE
,WriteMostly
):
440 case O(BUILD
,WriteMostly
):
442 case O(CREATE
,WriteMostly
):
443 /* set write-mostly for following devices */
448 /* clear write-mostly for following devices */
455 case O(BUILD
,'z'): /* size */
457 fprintf(stderr
, Name
": size may only be specified once. "
458 "Second value is %s.\n", optarg
);
461 if (strcmp(optarg
, "max")==0)
464 size
= parse_size(optarg
);
466 fprintf(stderr
, Name
": invalid size: %s\n",
470 /* convert sectors to K */
475 case O(GROW
,'Z'): /* array size */
476 if (array_size
>= 0) {
477 fprintf(stderr
, Name
": array-size may only be specified once. "
478 "Second value is %s.\n", optarg
);
481 if (strcmp(optarg
, "max") == 0)
484 array_size
= parse_size(optarg
);
485 if (array_size
<= 0) {
486 fprintf(stderr
, Name
": invalid array size: %s\n",
495 case O(BUILD
,'l'): /* set raid level*/
496 if (level
!= UnSet
) {
497 fprintf(stderr
, Name
": raid level may only be set once. "
498 "Second value is %s.\n", optarg
);
501 level
= map_name(pers
, optarg
);
502 if (level
== UnSet
) {
503 fprintf(stderr
, Name
": invalid raid level: %s\n",
507 if (level
!= 0 && level
!= LEVEL_LINEAR
&& level
!= 1 &&
508 level
!= LEVEL_MULTIPATH
&& level
!= LEVEL_FAULTY
&&
511 fprintf(stderr
, Name
": Raid level %s not permitted with --build.\n",
515 if (sparedisks
> 0 && level
< 1 && level
>= -1) {
516 fprintf(stderr
, Name
": raid level %s is incompatible with spare-devices setting.\n",
523 case O(GROW
, 'p'): /* new layout */
524 case O(GROW
, Layout
):
526 fprintf(stderr
,Name
": layout may only be sent once. "
527 "Second value was %s\n", optarg
);
531 /* 'Grow' will parse the value */
534 case O(CREATE
,'p'): /* raid5 layout */
535 case O(CREATE
,Layout
):
536 case O(BUILD
,'p'): /* faulty layout */
537 case O(BUILD
,Layout
):
538 if (layout
!= UnSet
) {
539 fprintf(stderr
,Name
": layout may only be sent once. "
540 "Second value was %s\n", optarg
);
545 fprintf(stderr
, Name
": layout not meaningful for %s arrays.\n",
546 map_num(pers
, level
));
549 fprintf(stderr
, Name
": raid level must be given before layout.\n");
553 layout
= map_name(r5layout
, optarg
);
555 fprintf(stderr
, Name
": layout %s not understood for raid5.\n",
561 layout
= map_name(r6layout
, optarg
);
563 fprintf(stderr
, Name
": layout %s not understood for raid6.\n",
570 layout
= parse_layout_10(optarg
);
572 fprintf(stderr
, Name
": layout for raid10 must be 'nNN', 'oNN' or 'fNN' where NN is a number, not %s\n", optarg
);
580 layout
= parse_layout_faulty(optarg
);
582 fprintf(stderr
, Name
": layout %s not understood for faulty.\n",
590 case O(CREATE
,AssumeClean
):
591 case O(BUILD
,AssumeClean
): /* assume clean */
592 case O(GROW
,AssumeClean
):
598 case O(BUILD
,'n'): /* number of raid disks */
600 fprintf(stderr
, Name
": raid-devices set twice: %d and %s\n",
604 raiddisks
= strtol(optarg
, &c
, 10);
605 if (!optarg
[0] || *c
|| raiddisks
<=0) {
606 fprintf(stderr
, Name
": invalid number of raid devices: %s\n",
610 ident
.raid_disks
= raiddisks
;
613 case O(CREATE
,'x'): /* number of spare (eXtra) disks */
615 fprintf(stderr
,Name
": spare-devices set twice: %d and %s\n",
619 if (level
!= UnSet
&& level
<= 0 && level
>= -1) {
620 fprintf(stderr
, Name
": spare-devices setting is incompatible with raid level %d\n",
624 sparedisks
= strtol(optarg
, &c
, 10);
625 if (!optarg
[0] || *c
|| sparedisks
< 0) {
626 fprintf(stderr
, Name
": invalid number of spare-devices: %s\n",
636 case O(INCREMENTAL
,'a'):
637 case O(INCREMENTAL
,Auto
):
638 case O(ASSEMBLE
,'a'):
639 case O(ASSEMBLE
,Auto
): /* auto-creation of device node */
640 autof
= parse_auto(optarg
, "--auto flag", 0);
643 case O(CREATE
,Symlinks
):
644 case O(BUILD
,Symlinks
):
645 case O(ASSEMBLE
,Symlinks
): /* auto creation of symlinks in /dev to /dev/md */
649 case O(BUILD
,'f'): /* force honouring '-n 1' */
650 case O(BUILD
,Force
): /* force honouring '-n 1' */
651 case O(GROW
,'f'): /* ditto */
652 case O(GROW
,Force
): /* ditto */
653 case O(CREATE
,'f'): /* force honouring of device list */
654 case O(CREATE
,Force
): /* force honouring of device list */
655 case O(ASSEMBLE
,'f'): /* force assembly */
656 case O(ASSEMBLE
,Force
): /* force assembly */
657 case O(MISC
,'f'): /* force zero */
658 case O(MISC
,Force
): /* force zero */
659 case O(MANAGE
,Force
): /* add device which is too large */
662 /* now for the Assemble options */
663 case O(ASSEMBLE
, FreezeReshape
): /* Freeze reshape during
665 case O(INCREMENTAL
, FreezeReshape
):
668 case O(CREATE
,'u'): /* uuid of array */
669 case O(ASSEMBLE
,'u'): /* uuid of array */
670 if (ident
.uuid_set
) {
671 fprintf(stderr
, Name
": uuid cannot be set twice. "
672 "Second value %s.\n", optarg
);
675 if (parse_uuid(optarg
, ident
.uuid
))
678 fprintf(stderr
,Name
": Bad uuid: %s\n", optarg
);
684 case O(ASSEMBLE
,'N'):
687 fprintf(stderr
, Name
": name cannot be set twice. "
688 "Second value %s.\n", optarg
);
691 if (mode
== MISC
&& !subarray
) {
692 fprintf(stderr
, Name
": -N/--name only valid with --update-subarray in misc mode\n");
695 if (strlen(optarg
) > 32) {
696 fprintf(stderr
, Name
": name '%s' is too long, 32 chars max.\n",
700 strcpy(ident
.name
, optarg
);
703 case O(ASSEMBLE
,'m'): /* super-minor for array */
704 case O(ASSEMBLE
,SuperMinor
):
705 if (ident
.super_minor
!= UnSet
) {
706 fprintf(stderr
, Name
": super-minor cannot be set twice. "
707 "Second value: %s.\n", optarg
);
710 if (strcmp(optarg
, "dev")==0)
711 ident
.super_minor
= -2;
713 ident
.super_minor
= strtoul(optarg
, &cp
, 10);
714 if (!optarg
[0] || *cp
) {
715 fprintf(stderr
, Name
": Bad super-minor number: %s.\n", optarg
);
721 case O(ASSEMBLE
,'U'): /* update the superblock */
724 fprintf(stderr
, Name
": Can only update one aspect"
725 " of superblock, both %s and %s given.\n",
729 if (mode
== MISC
&& !subarray
) {
730 fprintf(stderr
, Name
": Only subarrays can be"
731 " updated in misc mode\n");
735 if (strcmp(update
, "sparc2.2")==0)
737 if (strcmp(update
, "super-minor") == 0)
739 if (strcmp(update
, "summaries")==0)
741 if (strcmp(update
, "resync")==0)
743 if (strcmp(update
, "uuid")==0)
745 if (strcmp(update
, "name")==0)
747 if (strcmp(update
, "homehost")==0)
749 if (strcmp(update
, "devicesize")==0)
751 if (strcmp(update
, "no-bitmap")==0)
753 if (strcmp(update
, "byteorder")==0) {
756 Name
": must not set metadata"
757 " type with --update=byteorder.\n");
760 for(i
=0; !ss
&& superlist
[i
]; i
++)
761 ss
= superlist
[i
]->match_metadata_desc(
764 fprintf(stderr
, Name
": INTERNAL ERROR"
765 " cannot find 0.swap\n");
771 if (strcmp(update
,"?") == 0 ||
772 strcmp(update
, "help") == 0) {
774 fprintf(outf
, Name
": ");
778 Name
": '--update=%s' is invalid. ",
781 fprintf(outf
, "Valid --update options are:\n"
782 " 'sparc2.2', 'super-minor', 'uuid', 'name', 'resync',\n"
783 " 'summaries', 'homehost', 'byteorder', 'devicesize',\n"
785 exit(outf
== stdout
? 0 : 2);
788 /* update=devicesize is allowed with --re-add */
789 if (devmode
!= 'a' || re_add
!= 1) {
790 fprintf(stderr
, Name
"--update in Manage mode only"
791 " allowed with --re-add.\n");
795 fprintf(stderr
, Name
": Can only update one aspect"
796 " of superblock, both %s and %s given.\n",
801 if (strcmp(update
, "devicesize") != 0) {
802 fprintf(stderr
, Name
": only 'devicesize' can be"
803 " updated with --re-add\n");
808 case O(INCREMENTAL
,NoDegraded
):
809 fprintf(stderr
, Name
": --no-degraded is deprecated in Incremental mode\n");
810 case O(ASSEMBLE
,NoDegraded
): /* --no-degraded */
811 runstop
= -1; /* --stop isn't allowed for --assemble,
812 * so we overload slightly */
815 case O(ASSEMBLE
,'c'):
816 case O(ASSEMBLE
,ConfigFile
):
817 case O(INCREMENTAL
, 'c'):
818 case O(INCREMENTAL
, ConfigFile
):
820 case O(MISC
, ConfigFile
):
822 case O(MONITOR
,ConfigFile
):
824 fprintf(stderr
, Name
": configfile cannot be set twice. "
825 "Second value is %s.\n", optarg
);
829 set_conffile(configfile
);
830 /* FIXME possibly check that config file exists. Even parse it */
832 case O(ASSEMBLE
,'s'): /* scan */
835 case O(INCREMENTAL
,'s'):
839 case O(MONITOR
,'m'): /* mail address */
840 case O(MONITOR
,EMail
):
842 fprintf(stderr
, Name
": only specify one mailaddress. %s ignored.\n",
848 case O(MONITOR
,'p'): /* alert program */
849 case O(MONITOR
,ProgramOpt
): /* alert program */
851 fprintf(stderr
, Name
": only specify one alter program. %s ignored.\n",
857 case O(MONITOR
,'r'): /* rebuild increments */
858 case O(MONITOR
,Increment
):
859 increments
= atoi(optarg
);
860 if (increments
> 99 || increments
< 1) {
861 fprintf(stderr
, Name
": please specify positive integer between 1 and 99 as rebuild increments.\n");
866 case O(MONITOR
,'d'): /* delay in seconds */
868 case O(BUILD
,'d'): /* delay for bitmap updates */
871 fprintf(stderr
, Name
": only specify delay once. %s ignored.\n",
874 delay
= strtol(optarg
, &c
, 10);
875 if (!optarg
[0] || *c
|| delay
<1) {
876 fprintf(stderr
, Name
": invalid delay: %s\n",
882 case O(MONITOR
,'f'): /* daemonise */
883 case O(MONITOR
,Fork
):
886 case O(MONITOR
,'i'): /* pid */
888 fprintf(stderr
, Name
": only specify one pid file. %s ignored.\n",
893 case O(MONITOR
,'1'): /* oneshot */
897 case O(MONITOR
,'t'): /* test */
900 case O(MONITOR
,'y'): /* log messages to syslog */
901 openlog("mdadm", LOG_PID
, SYSLOG_FACILITY
);
904 case O(MONITOR
, NoSharing
):
908 /* now the general management options. Some are applicable
909 * to other modes. None have arguments.
914 case O(MANAGE
,Add
): /* add a drive */
918 case O(MANAGE
,ReAdd
):
922 case O(MANAGE
,'r'): /* remove a drive */
923 case O(MANAGE
,Remove
):
926 case O(MANAGE
,'f'): /* set faulty */
928 case O(INCREMENTAL
,'f'):
929 case O(INCREMENTAL
,Remove
):
930 case O(INCREMENTAL
,Fail
): /* r for incremental is taken, use f
931 * even though we will both fail and
932 * remove the device */
935 case O(INCREMENTAL
,'R'):
937 case O(ASSEMBLE
,'R'):
939 case O(CREATE
,'R'): /* Run the array */
941 fprintf(stderr
, Name
": Cannot both Stop and Run an array\n");
948 fprintf(stderr
, Name
": Cannot both Run and Stop an array\n");
960 case O(MISC
,KillOpt
):
967 case O(MISC
, WaitOpt
):
968 case O(MISC
, Waitclean
):
969 case O(MISC
, DetailPlatform
):
970 case O(MISC
, KillSubarray
):
971 case O(MISC
, UpdateSubarray
):
972 if (opt
== KillSubarray
|| opt
== UpdateSubarray
) {
974 fprintf(stderr
, Name
": subarray can only"
975 " be specified once\n");
980 if (devmode
&& devmode
!= opt
&&
981 (devmode
== 'E' || (opt
== 'E' && devmode
!= 'Q'))) {
982 fprintf(stderr
, Name
": --examine/-E cannot be given with ");
983 if (devmode
== 'E') {
984 if (option_index
>= 0)
985 fprintf(stderr
, "--%s\n",
986 long_options
[option_index
].name
);
988 fprintf(stderr
, "-%c\n", opt
);
989 } else if (isalpha(devmode
))
990 fprintf(stderr
, "-%c\n", devmode
);
992 fprintf(stderr
, "previous option\n");
997 case O(MISC
, UdevRules
):
998 if (devmode
&& devmode
!= opt
) {
999 fprintf(stderr
, Name
": --udev-rules must"
1000 " be the only option.\n");
1003 fprintf(stderr
, Name
": only specify one udev "
1004 "rule filename. %s ignored.\n",
1007 udev_filename
= optarg
;
1015 case O(MISC
, Sparc22
):
1016 if (devmode
!= 'E') {
1017 fprintf(stderr
, Name
": --sparc2.2 only allowed with --examine\n");
1023 case O(ASSEMBLE
,'b'): /* here we simply set the bitmap file */
1024 case O(ASSEMBLE
,Bitmap
):
1026 fprintf(stderr
, Name
": bitmap file needed with -b in --assemble mode\n");
1029 if (strcmp(optarg
, "internal")==0) {
1030 fprintf(stderr
, Name
": there is no need to specify --bitmap when assembling arrays with internal bitmaps\n");
1033 bitmap_fd
= open(optarg
, O_RDWR
);
1034 if (!*optarg
|| bitmap_fd
< 0) {
1035 fprintf(stderr
, Name
": cannot open bitmap file %s: %s\n", optarg
, strerror(errno
));
1038 ident
.bitmap_fd
= bitmap_fd
; /* for Assemble */
1041 case O(ASSEMBLE
, BackupFile
):
1042 case O(GROW
, BackupFile
):
1043 /* Specify a file into which grow might place a backup,
1044 * or from which assemble might recover a backup
1047 fprintf(stderr
, Name
": backup file already specified, rejecting %s\n", optarg
);
1050 backup_file
= optarg
;
1053 case O(GROW
, Continue
):
1054 /* Continue interrupted grow
1058 case O(ASSEMBLE
, InvalidBackup
):
1059 /* Acknowledge that the backupfile is invalid, but ask
1060 * to continue anyway
1066 case O(BUILD
,Bitmap
):
1068 case O(CREATE
,Bitmap
): /* here we create the bitmap */
1069 if (strcmp(optarg
, "none") == 0) {
1070 fprintf(stderr
, Name
": '--bitmap none' only"
1071 " supported for --grow\n");
1076 case O(GROW
,Bitmap
):
1077 if (strcmp(optarg
, "internal")== 0 ||
1078 strcmp(optarg
, "none")== 0 ||
1079 strchr(optarg
, '/') != NULL
) {
1080 bitmap_file
= optarg
;
1084 fprintf(stderr
, Name
": bitmap file must contain a '/', or be 'internal', or 'none'\n"
1085 " not '%s'\n", optarg
);
1088 case O(GROW
,BitmapChunk
):
1089 case O(BUILD
,BitmapChunk
):
1090 case O(CREATE
,BitmapChunk
): /* bitmap chunksize */
1091 bitmap_chunk
= parse_size(optarg
);
1092 if (bitmap_chunk
<= 0 ||
1093 bitmap_chunk
& (bitmap_chunk
- 1)) {
1095 Name
": invalid bitmap chunksize: %s\n",
1099 bitmap_chunk
= bitmap_chunk
* 512;
1102 case O(GROW
, WriteBehind
):
1103 case O(BUILD
, WriteBehind
):
1104 case O(CREATE
, WriteBehind
): /* write-behind mode */
1105 write_behind
= DEFAULT_MAX_WRITE_BEHIND
;
1107 write_behind
= strtol(optarg
, &c
, 10);
1108 if (write_behind
< 0 || *c
||
1109 write_behind
> 16383) {
1110 fprintf(stderr
, Name
": Invalid value for maximum outstanding write-behind writes: %s.\n\tMust be between 0 and 16383.\n", optarg
);
1116 case O(INCREMENTAL
, 'r'):
1117 case O(INCREMENTAL
, RebuildMapOpt
):
1120 case O(INCREMENTAL
, IncrementalPath
):
1121 remove_path
= optarg
;
1124 /* We have now processed all the valid options. Anything else is
1127 if (option_index
> 0)
1128 fprintf(stderr
, Name
":option --%s not valid in %s mode\n",
1129 long_options
[option_index
].name
,
1130 map_num(modes
, mode
));
1132 fprintf(stderr
, Name
": option -%c not valid in %s mode\n",
1133 opt
, map_num(modes
, mode
));
1140 if (print_help
== 2)
1141 help_text
= OptionHelp
;
1143 help_text
= mode_help
[mode
];
1144 if (help_text
== NULL
)
1146 fputs(help_text
,stdout
);
1150 if (!mode
&& devs_found
) {
1153 if (devlist
->disposition
== 0)
1154 devlist
->disposition
= devmode
;
1157 fputs(Usage
, stderr
);
1162 struct createinfo
*ci
= conf_get_create_info();
1164 if (strcasecmp(symlinks
, "yes") == 0)
1166 else if (strcasecmp(symlinks
, "no") == 0)
1169 fprintf(stderr
, Name
": option --symlinks must be 'no' or 'yes'\n");
1173 /* Ok, got the option parsing out of the way
1174 * hopefully it's mostly right but there might be some stuff
1177 * That is mosty checked in the per-mode stuff but...
1179 * For @,B,C and A without -s, the first device listed must be
1180 * an md device. We check that here and open it.
1183 if (mode
== MANAGE
|| mode
== BUILD
|| mode
== CREATE
1185 || (mode
== ASSEMBLE
&& ! scan
)) {
1186 if (devs_found
< 1) {
1187 fprintf(stderr
, Name
": an md device must be given in this mode\n");
1190 if ((int)ident
.super_minor
== -2 && autof
) {
1191 fprintf(stderr
, Name
": --super-minor=dev is incompatible with --auto\n");
1194 if (mode
== MANAGE
|| mode
== GROW
) {
1195 mdfd
= open_mddev(devlist
->devname
, 1);
1199 /* non-existent device is OK */
1200 mdfd
= open_mddev(devlist
->devname
, 0);
1202 fprintf(stderr
, Name
": device %s exists but is not an "
1203 "md array.\n", devlist
->devname
);
1206 if ((int)ident
.super_minor
== -2) {
1209 fprintf(stderr
, Name
": --super-minor=dev given, and "
1210 "listed device %s doesn't exist.\n",
1215 ident
.super_minor
= minor(stb
.st_rdev
);
1217 if (mdfd
>= 0 && mode
!= MANAGE
&& mode
!= GROW
) {
1218 /* We don't really want this open yet, we just might
1219 * have wanted to check some things
1227 if (raiddisks
== 1 && !force
&& level
!= LEVEL_FAULTY
) {
1228 fprintf(stderr
, Name
": '1' is an unusual number of drives for an array, so it is probably\n"
1229 " a mistake. If you really mean it you will need to specify --force before\n"
1230 " setting the number of drives.\n");
1235 if (homehost
== NULL
)
1236 homehost
= conf_get_homehost(&require_homehost
);
1237 if (homehost
== NULL
|| strcasecmp(homehost
, "<system>")==0) {
1238 if (gethostname(sys_hostname
, sizeof(sys_hostname
)) == 0) {
1239 sys_hostname
[sizeof(sys_hostname
)-1] = 0;
1240 homehost
= sys_hostname
;
1243 if (homehost
&& (!homehost
[0] || strcasecmp(homehost
, "<none>") == 0)) {
1245 require_homehost
= 0;
1248 if ((mode
== MISC
&& devmode
== 'E')
1249 || (mode
== MONITOR
&& spare_sharing
== 0))
1250 /* Anyone may try this */;
1251 else if (geteuid() != 0) {
1252 fprintf(stderr
, Name
": must be super-user to perform this action\n");
1256 ident
.autof
= autof
;
1261 /* readonly, add/remove, readwrite, runstop */
1263 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1264 if (!rv
&& devs_found
>1)
1265 rv
= Manage_subdevs(devlist
->devname
, mdfd
,
1266 devlist
->next
, verbose
-quiet
, test
,
1268 if (!rv
&& readonly
< 0)
1269 rv
= Manage_ro(devlist
->devname
, mdfd
, readonly
);
1271 rv
= Manage_runstop(devlist
->devname
, mdfd
, runstop
, quiet
);
1274 if (devs_found
== 1 && ident
.uuid_set
== 0 &&
1275 ident
.super_minor
== UnSet
&& ident
.name
[0] == 0 && !scan
) {
1276 /* Only a device has been given, so get details from config file */
1277 struct mddev_ident
*array_ident
= conf_get_ident(devlist
->devname
);
1278 if (array_ident
== NULL
) {
1279 fprintf(stderr
, Name
": %s not identified in config file.\n",
1285 if (array_ident
->autof
== 0)
1286 array_ident
->autof
= autof
;
1287 rv
|= Assemble(ss
, devlist
->devname
, array_ident
,
1288 NULL
, backup_file
, invalid_backup
,
1289 readonly
, runstop
, update
,
1290 homehost
, require_homehost
,
1291 verbose
-quiet
, force
,
1295 rv
= Assemble(ss
, devlist
->devname
, &ident
,
1296 devlist
->next
, backup_file
, invalid_backup
,
1297 readonly
, runstop
, update
,
1298 homehost
, require_homehost
,
1299 verbose
-quiet
, force
,
1301 else if (devs_found
> 0) {
1302 if (update
&& devs_found
> 1) {
1303 fprintf(stderr
, Name
": can only update a single array at a time\n");
1306 if (backup_file
&& devs_found
> 1) {
1307 fprintf(stderr
, Name
": can only assemble a single array when providing a backup file.\n");
1310 for (dv
= devlist
; dv
; dv
=dv
->next
) {
1311 struct mddev_ident
*array_ident
= conf_get_ident(dv
->devname
);
1312 if (array_ident
== NULL
) {
1313 fprintf(stderr
, Name
": %s not identified in config file.\n",
1318 if (array_ident
->autof
== 0)
1319 array_ident
->autof
= autof
;
1320 rv
|= Assemble(ss
, dv
->devname
, array_ident
,
1321 NULL
, backup_file
, invalid_backup
,
1322 readonly
, runstop
, update
,
1323 homehost
, require_homehost
,
1324 verbose
-quiet
, force
,
1329 fprintf(stderr
, Name
": --update not meaningful with a --scan assembly.\n");
1333 fprintf(stderr
, Name
": --backup_file not meaningful with a --scan assembly.\n");
1336 rv
= scan_assemble(autof
, ss
, readonly
, runstop
,
1340 force
, freeze_reshape
);
1345 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1346 if (write_behind
&& !bitmap_file
) {
1347 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1351 if (raiddisks
== 0) {
1352 fprintf(stderr
, Name
": no raid-devices specified.\n");
1358 if (strcmp(bitmap_file
, "internal")==0) {
1359 fprintf(stderr
, Name
": 'internal' bitmaps not supported with --build\n");
1364 rv
= Build(devlist
->devname
, chunk
, level
, layout
,
1365 raiddisks
, devlist
->next
, assume_clean
,
1366 bitmap_file
, bitmap_chunk
, write_behind
,
1367 delay
, verbose
-quiet
, autof
, size
);
1370 if (delay
== 0) delay
= DEFAULT_BITMAP_DELAY
;
1371 if (write_behind
&& !bitmap_file
) {
1372 fprintf(stderr
, Name
": write-behind mode requires a bitmap.\n");
1376 if (raiddisks
== 0) {
1377 fprintf(stderr
, Name
": no raid-devices specified.\n");
1382 rv
= Create(ss
, devlist
->devname
, chunk
, level
, layout
, size
<0 ? 0 : size
,
1383 raiddisks
, sparedisks
, ident
.name
, homehost
,
1384 ident
.uuid_set
? ident
.uuid
: NULL
,
1385 devs_found
-1, devlist
->next
, runstop
, verbose
-quiet
, force
, assume_clean
,
1386 bitmap_file
, bitmap_chunk
, write_behind
, delay
, autof
);
1389 if (devmode
== 'E') {
1390 if (devlist
== NULL
&& !scan
) {
1391 fprintf(stderr
, Name
": No devices to examine\n");
1394 if (devlist
== NULL
)
1395 devlist
= conf_get_devs();
1396 if (devlist
== NULL
) {
1397 fprintf(stderr
, Name
": No devices listed in %s\n", configfile
?configfile
:DefaultConfFile
);
1400 if (brief
&& verbose
)
1402 rv
= Examine(devlist
, scan
?(verbose
>1?0:verbose
+1):brief
,
1404 SparcAdjust
, ss
, homehost
);
1405 } else if (devmode
== DetailPlatform
) {
1406 rv
= Detail_Platform(ss
? ss
->ss
: NULL
, ss
? scan
: 1, verbose
);
1407 } else if (devlist
== NULL
) {
1408 if (devmode
== 'S' && scan
)
1409 rv
= stop_scan(quiet
);
1410 else if ((devmode
== 'D' || devmode
== Waitclean
) && scan
)
1411 rv
= misc_scan(devmode
, verbose
, export
,
1412 test
, homehost
, prefer
);
1413 else if (devmode
== UdevRules
)
1414 rv
= Write_rules(udev_filename
);
1416 fprintf(stderr
, Name
": No devices given.\n");
1420 rv
= misc_list(devlist
, brief
, verbose
, export
, test
,
1421 homehost
, prefer
, subarray
, update
,
1426 if (!devlist
&& !scan
) {
1427 fprintf(stderr
, Name
": Cannot monitor: need --scan or at least one device\n");
1431 if (pidfile
&& !daemonise
) {
1432 fprintf(stderr
, Name
": Cannot write a pid file when not in daemon mode\n");
1437 if (get_linux_version() > 2006016)
1438 /* mdstat responds to poll */
1443 rv
= Monitor(devlist
, mailaddr
, program
,
1444 delay
?delay
:60, daemonise
, scan
, oneshot
,
1445 dosyslog
, test
, pidfile
, increments
,
1446 spare_sharing
, prefer
);
1450 if (array_size
>= 0) {
1451 /* alway impose array size first, independent of
1453 * Do not allow level or raid_disks changes at the
1454 * same time as that can be irreversibly destructive.
1458 if (raiddisks
|| level
!= UnSet
) {
1459 fprintf(stderr
, Name
": cannot change array size in same operation "
1460 "as changing raiddisks or level.\n"
1461 " Change size first, then check that data is still intact.\n");
1465 sysfs_init(&sra
, mdfd
, 0);
1466 if (array_size
== 0)
1467 err
= sysfs_set_str(&sra
, NULL
, "array_size", "default");
1469 err
= sysfs_set_num(&sra
, NULL
, "array_size", array_size
/ 2);
1472 fprintf(stderr
, Name
": --array-size setting"
1473 " is too large.\n");
1475 fprintf(stderr
, Name
": current kernel does"
1476 " not support setting --array-size\n");
1481 if (devs_found
> 1 && raiddisks
== 0) {
1483 if (size
>= 0 || chunk
|| layout_str
!= NULL
|| bitmap_file
) {
1484 fprintf(stderr
, Name
": --add cannot be used with "
1485 "other geometry changes in --grow mode\n");
1489 for (dv
=devlist
->next
; dv
; dv
=dv
->next
) {
1490 rv
= Grow_Add_device(devlist
->devname
, mdfd
,
1495 } else if (bitmap_file
) {
1496 if (size
>= 0 || raiddisks
|| chunk
||
1497 layout_str
!= NULL
|| devs_found
> 1) {
1498 fprintf(stderr
, Name
": --bitmap changes cannot be "
1499 "used with other geometry changes "
1500 "in --grow mode\n");
1505 delay
= DEFAULT_BITMAP_DELAY
;
1506 rv
= Grow_addbitmap(devlist
->devname
, mdfd
, bitmap_file
,
1507 bitmap_chunk
, delay
, write_behind
, force
);
1508 } else if (grow_continue
)
1509 rv
= Grow_continue_command(devlist
->devname
,
1512 else if (size
>= 0 || raiddisks
!= 0 || layout_str
!= NULL
1513 || chunk
!= 0 || level
!= UnSet
) {
1514 rv
= Grow_reshape(devlist
->devname
, mdfd
, quiet
, backup_file
,
1515 size
, level
, layout_str
, chunk
, raiddisks
,
1517 assume_clean
, force
);
1518 } else if (array_size
< 0)
1519 fprintf(stderr
, Name
": no changes to --grow\n");
1527 fprintf(stderr
, Name
1528 ": --incremental --scan meaningless without --run.\n");
1531 if (devmode
== 'f') {
1532 fprintf(stderr
, Name
1533 ": --incremental --scan --fail not supported.\n");
1536 rv
= IncrementalScan(verbose
);
1539 if (!rebuild_map
&& !scan
) {
1540 fprintf(stderr
, Name
1541 ": --incremental requires a device.\n");
1546 if (devlist
->next
) {
1547 fprintf(stderr
, Name
1548 ": --incremental can only handle one device.\n");
1553 rv
= IncrementalRemove(devlist
->devname
, remove_path
,
1556 rv
= Incremental(devlist
->devname
, verbose
-quiet
,
1557 runstop
, ss
, homehost
,
1558 require_homehost
, autof
,
1568 static int scan_assemble(int autof
, struct supertype
*ss
,
1569 int readonly
, int runstop
,
1570 struct mddev_ident
*ident
,
1571 char *homehost
, int require_homehost
,
1572 int verbose
, int force
,
1575 struct mddev_ident
*a
, *array_list
= conf_get_ident(NULL
);
1576 struct mddev_dev
*devlist
= conf_get_devs();
1577 struct map_ent
*map
= NULL
;
1580 int failures
, successes
;
1582 if (conf_verify_devnames(array_list
)) {
1583 fprintf(stderr
, Name
1584 ": Duplicate MD device names in "
1585 "conf file were found.\n");
1588 if (devlist
== NULL
) {
1589 fprintf(stderr
, Name
": No devices listed in conf file were found.\n");
1592 for (a
= array_list
; a
; a
= a
->next
) {
1598 fprintf(stderr
, Name
" %s: failed to get "
1599 "exclusive lock on mapfile\n",
1605 for (a
= array_list
; a
; a
= a
->next
) {
1610 strcasecmp(a
->devname
, "<ignore>") == 0)
1613 r
= Assemble(ss
, a
->devname
,
1616 readonly
, runstop
, NULL
,
1617 homehost
, require_homehost
,
1628 } while (failures
&& successes
);
1629 if (homehost
&& cnt
== 0) {
1630 /* Maybe we can auto-assemble something.
1631 * Repeatedly call Assemble in auto-assemble mode
1636 ident
->autof
= autof
;
1638 struct mddev_dev
*devlist
= conf_get_devs();
1641 rv2
= Assemble(ss
, NULL
,
1656 /* Incase there are stacked devices, we need to go around again */
1658 if (cnt
== 0 && rv
== 0) {
1659 fprintf(stderr
, Name
": No arrays found in config file or automatically\n");
1663 } else if (cnt
== 0 && rv
== 0) {
1664 fprintf(stderr
, Name
": No arrays found in config file\n");
1671 static int misc_scan(char devmode
, int verbose
, int export
, int test
,
1672 char *homehost
, char *prefer
)
1674 /* apply --detail or --wait-clean to
1675 * all devices in /proc/mdstat
1677 struct mdstat_ent
*ms
= mdstat_read(0, 1);
1678 struct mdstat_ent
*e
;
1679 struct map_ent
*map
= NULL
;
1681 int v
= verbose
>1?0:verbose
+1;
1684 for (members
= 0; members
<= 1; members
++) {
1685 for (e
=ms
; e
; e
=e
->next
) {
1688 int member
= e
->metadata_version
&&
1689 strncmp(e
->metadata_version
,
1690 "external:/", 10) == 0;
1691 if (members
!= member
)
1693 me
= map_by_devnum(&map
, e
->devnum
);
1695 && strcmp(me
->path
, "/unknown") != 0)
1698 name
= get_md_name(e
->devnum
);
1701 fprintf(stderr
, Name
": cannot find device file for %s\n",
1706 rv
|= Detail(name
, v
,
1710 rv
|= WaitClean(name
, -1, v
);
1718 static int stop_scan(int quiet
)
1720 /* apply --stop to all devices in /proc/mdstat */
1721 /* Due to possible stacking of devices, repeat until
1722 * nothing more can be stopped
1724 int progress
=1, err
;
1728 struct mdstat_ent
*ms
= mdstat_read(0, 0);
1729 struct mdstat_ent
*e
;
1731 if (!progress
) last
= 1;
1732 progress
= 0; err
= 0;
1733 for (e
=ms
; e
; e
=e
->next
) {
1734 char *name
= get_md_name(e
->devnum
);
1738 fprintf(stderr
, Name
": cannot find device file for %s\n",
1742 mdfd
= open_mddev(name
, 1);
1744 if (Manage_runstop(name
, mdfd
, -1, quiet
?1:last
?0:-1))
1754 } while (!last
&& err
);
1760 static int misc_list(struct mddev_dev
*devlist
,
1761 int brief
, int verbose
, int export
, int test
,
1762 char *homehost
, char *prefer
, char *subarray
,
1763 char *update
, struct mddev_ident
*ident
,
1764 struct supertype
*ss
, int force
, int quiet
)
1766 struct mddev_dev
*dv
;
1769 for (dv
=devlist
; dv
; dv
=dv
->next
) {
1772 switch(dv
->disposition
) {
1774 rv
|= Detail(dv
->devname
,
1776 export
, test
, homehost
, prefer
);
1778 case KillOpt
: /* Zero superblock */
1780 rv
|= Kill(dv
->devname
, ss
, force
, quiet
,0);
1784 rv
|= Kill(dv
->devname
, NULL
, force
, q
, 0);
1791 rv
|= Query(dv
->devname
); continue;
1793 rv
|= ExamineBitmap(dv
->devname
, brief
, ss
); continue;
1796 rv
|= Wait(dv
->devname
); continue;
1798 rv
|= WaitClean(dv
->devname
, -1, verbose
-quiet
); continue;
1800 rv
|= Kill_subarray(dv
->devname
, subarray
, quiet
);
1802 case UpdateSubarray
:
1803 if (update
== NULL
) {
1805 Name
": -U/--update must be specified with --update-subarray\n");
1809 rv
|= Update_subarray(dv
->devname
, subarray
,
1810 update
, ident
, quiet
);
1813 mdfd
= open_mddev(dv
->devname
, 1);
1815 switch(dv
->disposition
) {
1817 rv
|= Manage_runstop(dv
->devname
, mdfd
, 1, quiet
); break;
1819 rv
|= Manage_runstop(dv
->devname
, mdfd
, -1, quiet
); break;
1821 rv
|= Manage_ro(dv
->devname
, mdfd
, 1); break;
1823 rv
|= Manage_ro(dv
->devname
, mdfd
, -1); break;