2
* mdadm - manage Linux "md" devices aka RAID arrays.
4
* Copyright (C) 2001-2009 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>
35
* Read the config file
37
* conf_get_uuids gets a list of devicename+uuid pairs
38
* conf_get_devs gets device names after expanding wildcards
40
* Each keeps the returned list and frees it when asked to make
43
* The format of the config file needs to be fairly extensible.
44
* Now, arrays only have names and uuids and devices merely are.
45
* But later arrays might want names, and devices might want superblock
46
* versions, and who knows what else.
47
* I like free format, abhore backslash line continuation, adore
48
* indentation for structure and am ok about # comments.
50
* So, each line that isn't blank or a #comment must either start
51
* with a key word, and not be indented, or must start with a
52
* non-key-word and must be indented.
54
* Keywords are DEVICE and ARRAY ... and several others.
55
* DEV{ICE} introduces some devices that might contain raid components.
57
* DEV style=0 /dev/sda* /dev/hd*
58
* DEV style=1 /dev/sd[b-f]*
59
* ARR{AY} describes an array giving md device and attributes like uuid=whatever
61
* ARRAY /dev/md0 uuid=whatever name=something
62
* Spaces separate words on each line. Quoting, with "" or '' protects them,
63
* but may not wrap over lines
68
#define CONFFILE "/etc/mdadm.conf"
71
/* for Debian compatibility .... */
72
#define CONFFILE2 "/etc/mdadm/mdadm.conf"
74
char DefaultConfFile[] = CONFFILE;
75
char DefaultAltConfFile[] = CONFFILE2;
77
enum linetype { Devices, Array, Mailaddr, Mailfrom, Program, CreateDev,
78
Homehost, AutoMode, LTEnd };
80
[Devices] = "devices",
82
[Mailaddr] = "mailaddr",
83
[Mailfrom] = "mailfrom",
84
[Program] = "program",
85
[CreateDev]= "create",
86
[Homehost] = "homehost",
92
* match_keyword returns an index into the keywords array, or -1 for no match
93
* case is ignored, and at least three characters must be given
96
int match_keyword(char *word)
98
int len = strlen(word);
101
if (len < 3) return -1;
102
for (n=0; keywords[n]; n++) {
103
if (strncasecmp(word, keywords[n], len)==0)
109
/* conf_word gets one word from the conf file.
110
* if "allow_key", then accept words at the start of a line,
111
* otherwise stop when such a word is found.
112
* We assume that the file pointer is at the end of a word, so the
113
* next character is a space, or a newline. If not, it is the start of a line.
116
char *conf_word(FILE *file, int allow_key)
123
char *word = malloc(wsize);
127
while (wordfound==0) {
128
/* at the end of a word.. */
131
while (c != EOF && c != '\n')
134
if (c == '\n') continue;
136
if (c != ' ' && c != '\t' && ! allow_key) {
140
/* looks like it is safe to get a word here, if there is one */
142
/* first, skip any spaces */
143
while (c == ' ' || c == '\t')
145
if (c != EOF && c != '\n' && c != '#') {
146
/* we really have a character of a word, so start saving it */
147
while (c != EOF && c != '\n' && (quote || (c!=' ' && c != '\t'))) {
149
if (quote && c == quote) quote = 0;
150
else if (quote == 0 && (c == '\'' || c == '"'))
153
if (len == wsize-1) {
155
word = realloc(word, wsize);
161
/* Hack for broken kernels (2.6.14-.24) that put
162
* "active(auto-read-only)"
163
* in /proc/mdstat instead of
164
* "active (auto-read-only)"
166
if (c == '(' && len >= 6
167
&& strncmp(word+len-6, "active", 6) == 0)
171
if (c != EOF) ungetc(c, file);
175
/* Further HACK for broken kernels.. 2.6.14-2.6.24 */
176
if (strcmp(word, "auto-read-only)") == 0)
177
strcpy(word, "(auto-read-only)");
179
/* printf("word is <%s>\n", word); */
188
* conf_line reads one logical line from the conffile.
189
* It skips comments and continues until it finds a line that starts
190
* with a non blank/comment. This character is pushed back for the next call
191
* A doubly linked list of words is returned.
192
* the first word will be a keyword. Other words will have had quotes removed.
195
char *conf_line(FILE *file)
200
w = conf_word(file, 1);
201
if (w == NULL) return NULL;
207
while ((w = conf_word(file,0))){
208
char *w2 = dl_strdup(w);
212
/* printf("got a line\n");*/
216
void free_line(char *line)
219
for (w=dl_next(line); w != line; w=dl_next(line)) {
228
struct conf_dev *next;
232
mddev_dev_t load_partitions(void)
234
FILE *f = fopen("/proc/partitions", "r");
236
mddev_dev_t rv = NULL;
238
fprintf(stderr, Name ": cannot open /proc/partitions\n");
241
while (fgets(buf, 1024, f)) {
249
major = strtoul(buf, &mp, 10);
250
if (mp == buf || *mp != ' ')
252
minor = strtoul(mp, NULL, 10);
254
name = map_dev(major, minor, 1);
257
d = malloc(sizeof(*d));
258
d->devname = strdup(name);
268
mddev_dev_t load_containers(void)
270
struct mdstat_ent *mdstat = mdstat_read(1, 0);
271
struct mdstat_ent *ent;
273
mddev_dev_t rv = NULL;
278
for (ent = mdstat; ent; ent = ent->next)
279
if (ent->metadata_version &&
280
strncmp(ent->metadata_version, "external:", 9) == 0 &&
281
!is_subarray(&ent->metadata_version[9])) {
282
d = malloc(sizeof(*d));
285
if (asprintf(&d->devname, "/dev/%s", ent->dev) < 0) {
299
struct createinfo createinfo = {
300
.autof = 2, /* by default, create devices with standard names */
310
int parse_auto(char *str, char *msg, int config)
313
if (str == NULL || *str == 0)
315
else if (strcasecmp(str,"no")==0)
317
else if (strcasecmp(str,"yes")==0)
319
else if (strcasecmp(str,"md")==0)
322
/* There might be digits, and maybe a hypen, at the end */
323
char *e = str + strlen(str);
326
while (e > str && isdigit(e[-1]))
330
if (num <= 0) num = 1;
332
if (e > str && e[-1] == '-')
335
if ((len == 2 && strncasecmp(str,"md",2)==0)) {
336
autof = config ? 5 : 3;
337
} else if ((len == 3 && strncasecmp(str,"yes",3)==0)) {
339
} else if ((len == 3 && strncasecmp(str,"mdp",3)==0)) {
340
autof = config ? 6 : 4;
341
} else if ((len == 1 && strncasecmp(str,"p",1)==0) ||
342
(len >= 4 && strncasecmp(str,"part",4)==0)) {
345
fprintf(stderr, Name ": %s arg of \"%s\" unrecognised: use no,yes,md,mdp,part\n"
346
" optionally followed by a number.\n",
355
static void createline(char *line)
360
for (w=dl_next(line); w!=line; w=dl_next(w)) {
361
if (strncasecmp(w, "auto=", 5) == 0)
362
createinfo.autof = parse_auto(w+5, "auto=", 1);
363
else if (strncasecmp(w, "owner=", 6) == 0) {
365
fprintf(stderr, Name ": missing owner name\n");
368
createinfo.uid = strtoul(w+6, &ep, 10);
374
createinfo.uid = pw->pw_uid;
376
fprintf(stderr, Name ": CREATE user %s not found\n", w+6);
378
} else if (strncasecmp(w, "group=", 6) == 0) {
380
fprintf(stderr, Name ": missing group name\n");
383
createinfo.gid = strtoul(w+6, &ep, 10);
389
createinfo.gid = gr->gr_gid;
391
fprintf(stderr, Name ": CREATE group %s not found\n", w+6);
393
} else if (strncasecmp(w, "mode=", 5) == 0) {
395
fprintf(stderr, Name ": missing CREATE mode\n");
398
createinfo.mode = strtoul(w+5, &ep, 8);
400
createinfo.mode = 0600;
401
fprintf(stderr, Name ": unrecognised CREATE mode %s\n",
404
} else if (strncasecmp(w, "metadata=", 9) == 0) {
405
/* style of metadata to use by default */
407
for (i=0; superlist[i] && !createinfo.supertype; i++)
408
createinfo.supertype =
409
superlist[i]->match_metadata_desc(w+9);
410
if (!createinfo.supertype)
411
fprintf(stderr, Name ": metadata format %s unknown, ignoring\n",
413
} else if (strncasecmp(w, "symlinks=yes", 12) == 0)
414
createinfo.symlinks = 1;
415
else if (strncasecmp(w, "symlinks=no", 11) == 0)
416
createinfo.symlinks = 0;
418
fprintf(stderr, Name ": unrecognised word on CREATE line: %s\n",
424
void devline(char *line)
429
for (w=dl_next(line); w != line; w=dl_next(w)) {
430
if (w[0] == '/' || strcasecmp(w, "partitions") == 0 ||
431
strcasecmp(w, "containers") == 0) {
432
cd = malloc(sizeof(*cd));
433
cd->name = strdup(w);
437
fprintf(stderr, Name ": unreconised word on DEVICE line: %s\n",
443
mddev_ident_t mddevlist = NULL;
444
mddev_ident_t *mddevlp = &mddevlist;
446
static int is_number(char *w)
448
/* check if there are 1 or more digits and nothing else */
450
while (*w && isdigit(*w)) {
454
return (digits && ! *w);
457
void arrayline(char *line)
461
struct mddev_ident_s mis;
465
mis.super_minor = UnSet;
467
mis.raid_disks = UnSet;
471
mis.spare_group = NULL;
476
mis.bitmap_file = NULL;
478
mis.container = NULL;
481
for (w=dl_next(line); w!=line; w=dl_next(w)) {
482
if (w[0] == '/' || strchr(w, '=') == NULL) {
483
/* This names the device, or is '<ignore>'.
484
* The rules match those in create_mddev.
490
* or anything that doesn't start '/' or '<'
492
if (strcasecmp(w, "<ignore>") == 0 ||
493
strncmp(w, "/dev/md/", 8) == 0 ||
494
(w[0] != '/' && w[0] != '<') ||
495
(strncmp(w, "/dev/md", 7) == 0 &&
497
(strncmp(w, "/dev/md_d", 9) == 0 &&
500
/* This is acceptable */;
502
fprintf(stderr, Name ": only give one "
503
"device per ARRAY line: %s and %s\n",
508
fprintf(stderr, Name ": %s is an invalid name for "
509
"an md device - ignored.\n", w);
511
} else if (strncasecmp(w, "uuid=", 5)==0 ) {
513
fprintf(stderr, Name ": only specify uuid once, %s ignored.\n",
516
if (parse_uuid(w+5, mis.uuid))
519
fprintf(stderr, Name ": bad uuid: %s\n", w);
521
} else if (strncasecmp(w, "super-minor=", 12)==0 ) {
522
if (mis.super_minor != UnSet)
523
fprintf(stderr, Name ": only specify super-minor once, %s ignored.\n",
527
int minor = strtol(w+12, &endptr, 10);
529
if (w[12]==0 || endptr[0]!=0 || minor < 0)
530
fprintf(stderr, Name ": invalid super-minor number: %s\n",
533
mis.super_minor = minor;
535
} else if (strncasecmp(w, "name=", 5)==0) {
537
fprintf(stderr, Name ": only specify name once, %s ignored.\n",
539
else if (strlen(w+5) > 32)
540
fprintf(stderr, Name ": name too long, ignoring %s\n", w);
542
strcpy(mis.name, w+5);
544
} else if (strncasecmp(w, "bitmap=", 7) == 0) {
546
fprintf(stderr, Name ": only specify bitmap file once. %s ignored\n",
549
mis.bitmap_file = strdup(w+7);
551
} else if (strncasecmp(w, "devices=", 8 ) == 0 ) {
553
fprintf(stderr, Name ": only specify devices once (use a comma separated list). %s ignored\n",
556
mis.devices = strdup(w+8);
557
} else if (strncasecmp(w, "spare-group=", 12) == 0 ) {
559
fprintf(stderr, Name ": only specify one spare group per array. %s ignored.\n",
562
mis.spare_group = strdup(w+12);
563
} else if (strncasecmp(w, "level=", 6) == 0 ) {
564
/* this is mainly for compatability with --brief output */
565
mis.level = map_name(pers, w+6);
566
} else if (strncasecmp(w, "disks=", 6) == 0 ) {
567
/* again, for compat */
568
mis.raid_disks = atoi(w+6);
569
} else if (strncasecmp(w, "num-devices=", 12) == 0 ) {
570
/* again, for compat */
571
mis.raid_disks = atoi(w+12);
572
} else if (strncasecmp(w, "spares=", 7) == 0 ) {
573
/* for warning if not all spares present */
574
mis.spare_disks = atoi(w+7);
575
} else if (strncasecmp(w, "metadata=", 9) == 0) {
576
/* style of metadata on the devices. */
579
for(i=0; superlist[i] && !mis.st; i++)
580
mis.st = superlist[i]->match_metadata_desc(w+9);
583
fprintf(stderr, Name ": metadata format %s unknown, ignored.\n", w+9);
584
} else if (strncasecmp(w, "auto=", 5) == 0 ) {
585
/* whether to create device special files as needed */
586
mis.autof = parse_auto(w+5, "auto type", 0);
587
} else if (strncasecmp(w, "member=", 7) == 0) {
588
/* subarray within a container */
589
mis.member = strdup(w+7);
590
} else if (strncasecmp(w, "container=", 10) == 0) {
591
/* the container holding this subarray. Either a device name
593
mis.container = strdup(w+10);
595
fprintf(stderr, Name ": unrecognised word on ARRAY line: %s\n",
599
if (mis.uuid_set == 0 && mis.devices == NULL &&
600
mis.super_minor == UnSet && mis.name[0] == 0 &&
601
(mis.container == NULL || mis.member == NULL))
602
fprintf(stderr, Name ": ARRAY line %s has no identity information.\n", mis.devname);
604
mi = malloc(sizeof(*mi));
606
mi->devname = mis.devname ? strdup(mis.devname) : NULL;
613
static char *alert_email = NULL;
614
void mailline(char *line)
618
for (w=dl_next(line); w != line ; w=dl_next(w)) {
619
if (alert_email == NULL)
620
alert_email = strdup(w);
622
fprintf(stderr, Name ": excess address on MAIL line: %s - ignored\n",
627
static char *alert_mail_from = NULL;
628
void mailfromline(char *line)
632
for (w=dl_next(line); w != line ; w=dl_next(w)) {
633
if (alert_mail_from == NULL)
634
alert_mail_from = strdup(w);
638
if (xasprintf(&t, "%s %s", alert_mail_from, w) > 0) {
639
free(alert_mail_from);
647
static char *alert_program = NULL;
648
void programline(char *line)
652
for (w=dl_next(line); w != line ; w=dl_next(w)) {
653
if (alert_program == NULL)
654
alert_program = strdup(w);
656
fprintf(stderr, Name ": excess program on PROGRAM line: %s - ignored\n",
661
static char *home_host = NULL;
662
static int require_homehost = 1;
663
void homehostline(char *line)
667
for (w=dl_next(line); w != line ; w=dl_next(w)) {
668
if (strcasecmp(w, "<ignore>")==0)
669
require_homehost = 0;
670
else if (home_host == NULL)
671
home_host = strdup(w);
673
fprintf(stderr, Name ": excess host name on HOMEHOST line: %s - ignored\n",
678
static char *auto_options = NULL;
679
void autoline(char *line)
684
fprintf(stderr, Name ": AUTO line may only be give once."
685
" Subsequent lines ignored\n");
689
auto_options = dl_strdup(line);
690
dl_init(auto_options);
692
for (w=dl_next(line); w != line ; w=dl_next(w)) {
693
char *w2 = dl_strdup(w);
694
dl_add(auto_options, w2);
700
static char *conffile = NULL;
701
void set_conffile(char *file)
706
void load_conffile(void)
712
if (conffile == NULL)
713
conffile = DefaultConfFile;
715
if (strcmp(conffile, "none") == 0) {
719
if (strcmp(conffile, "partitions")==0) {
720
char *list = dl_strdup("DEV");
722
dl_add(list, dl_strdup("partitions"));
728
f = fopen(conffile, "r");
729
/* Debian chose to relocate mdadm.conf into /etc/mdadm/.
730
* To allow Debian users to compile from clean source and still
731
* have a working mdadm, we read /etc/mdadm/mdadm.conf
732
* if /etc/mdadm.conf doesn't exist
735
conffile == DefaultConfFile) {
736
f = fopen(DefaultAltConfFile, "r");
738
conffile = DefaultAltConfFile;
744
while ((line=conf_line(f))) {
745
switch(match_keyword(line)) {
771
fprintf(stderr, Name ": Unknown keyword %s\n", line);
778
/* printf("got file\n"); */
781
char *conf_get_mailaddr(void)
787
char *conf_get_mailfrom(void)
790
return alert_mail_from;
793
char *conf_get_program(void)
796
return alert_program;
799
char *conf_get_homehost(int *require_homehostp)
802
if (require_homehostp)
803
*require_homehostp = require_homehost;
807
struct createinfo *conf_get_create_info(void)
813
mddev_ident_t conf_get_ident(char *dev)
818
while (dev && rv && (rv->devname == NULL
819
|| !devname_matches(dev, rv->devname)))
824
static void append_dlist(mddev_dev_t *dlp, mddev_dev_t list)
831
mddev_dev_t conf_get_devs()
836
static mddev_dev_t dlist = NULL;
840
mddev_dev_t t = dlist;
848
if (cdevlist == NULL) {
849
/* default to 'partitions' and 'containers' */
850
dlist = load_partitions();
851
append_dlist(&dlist, load_containers());
854
for (cd=cdevlist; cd; cd=cd->next) {
855
if (strcasecmp(cd->name, "partitions")==0)
856
append_dlist(&dlist, load_partitions());
857
else if (strcasecmp(cd->name, "containers")==0)
858
append_dlist(&dlist, load_containers());
860
glob(cd->name, flags, NULL, &globbuf);
861
flags |= GLOB_APPEND;
864
if (flags & GLOB_APPEND) {
865
for (i=0; i<globbuf.gl_pathc; i++) {
866
mddev_dev_t t = malloc(sizeof(*t));
867
t->devname = strdup(globbuf.gl_pathv[i]);
872
/* printf("one dev is %s\n", t->devname);*/
880
int conf_test_dev(char *devname)
883
if (cdevlist == NULL)
884
/* allow anything by default */
886
for (cd = cdevlist ; cd ; cd = cd->next) {
887
if (strcasecmp(cd->name, "partitions") == 0)
889
if (fnmatch(cd->name, devname, FNM_PATHNAME) == 0)
895
int conf_test_metadata(const char *version, int is_homehost)
897
/* Check if the given metadata version is allowed
898
* to be auto-assembled.
899
* The default is 'yes' but the 'auto' line might over-ride that.
900
* Words in auto_options are processed in order with the first
903
* +version - that version can be assembled
904
* -version - that version cannot be auto-assembled
905
* yes or +all - any other version can be assembled
906
* no or -all - no other version can be assembled.
907
* homehost - any array associated by 'homehost' to this
908
* host can be assembled.
911
* +ddf -0.90 homehost -all
912
* will auto-assemble any ddf array, no 0.90 array, and
913
* any other array (imsm, 1.x) if and only if it is identified
914
* as belonging to this host.
920
for (w = dl_next(auto_options); w != auto_options; w = dl_next(w)) {
922
if (strcasecmp(w, "yes") == 0)
924
if (strcasecmp(w, "no") == 0)
926
if (strcasecmp(w, "homehost") == 0) {
934
else if (w[0] == '-')
938
if (strcasecmp(w+1, "all") == 0)
940
if (strcasecmp(w+1, version) == 0)
942
/* allow '0' to match version '0.90'
943
* and 1 or 1.whatever to match version '1.x'
945
if (version[1] == '.' &&
949
if (version[1] == '.' && version[2] == 'x' &&
950
strncmp(w+1, version, 2) == 0)
956
int match_oneof(char *devices, char *devname)
958
/* check if one of the comma separated patterns in devices
963
while (devices && *devices) {
966
devices = strchr(devices, ',');
968
devices = p + strlen(p);
969
if (devices-p < 1024) {
970
strncpy(patn, p, devices-p);
972
if (fnmatch(patn, devname, FNM_PATHNAME)==0)
981
int devname_matches(char *name, char *match)
983
/* See if the given array name matches the
984
* given match from config file.
986
* First strip and /dev/md/ or /dev/, then
987
* see if there might be a numeric match of
991
if (strncmp(name, "/dev/md/", 8) == 0)
993
else if (strncmp(name, "/dev/", 5) == 0)
996
if (strncmp(match, "/dev/md/", 8) == 0)
998
else if (strncmp(match, "/dev/", 5) == 0)
1002
if (strncmp(name, "md", 2) == 0 &&
1005
if (strncmp(match, "md", 2) == 0 &&
1009
return (strcmp(name, match) == 0);
1012
int conf_name_is_free(char *name)
1014
/* Check if this name is already take by an ARRAY entry in
1016
* It can be taken either by a match on devname, name, or
1022
for (dev = mddevlist; dev; dev = dev->next) {
1024
if (dev->devname && devname_matches(name, dev->devname))
1026
if (dev->name[0] && devname_matches(name, dev->name))
1028
sprintf(nbuf, "%d", dev->super_minor);
1029
if (dev->super_minor != UnSet &&
1030
devname_matches(name, nbuf))
1036
struct mddev_ident_s *conf_match(struct mdinfo *info, struct supertype *st)
1038
struct mddev_ident_s *array_list, *match;
1040
char *devname = NULL;
1041
array_list = conf_get_ident(NULL);
1043
for (; array_list; array_list = array_list->next) {
1044
if (array_list->uuid_set &&
1045
same_uuid(array_list->uuid, info->uuid, st->ss->swapuuid)
1047
if (verbose >= 2 && array_list->devname)
1048
fprintf(stderr, Name
1049
": UUID differs from %s.\n",
1050
array_list->devname);
1053
if (array_list->name[0] &&
1054
strcasecmp(array_list->name, info->name) != 0) {
1055
if (verbose >= 2 && array_list->devname)
1056
fprintf(stderr, Name
1057
": Name differs from %s.\n",
1058
array_list->devname);
1061
if (array_list->devices && devname &&
1062
!match_oneof(array_list->devices, devname)) {
1063
if (verbose >= 2 && array_list->devname)
1064
fprintf(stderr, Name
1065
": Not a listed device for %s.\n",
1066
array_list->devname);
1069
if (array_list->super_minor != UnSet &&
1070
array_list->super_minor != info->array.md_minor) {
1071
if (verbose >= 2 && array_list->devname)
1072
fprintf(stderr, Name
1073
": Different super-minor to %s.\n",
1074
array_list->devname);
1077
if (!array_list->uuid_set &&
1078
!array_list->name[0] &&
1079
!array_list->devices &&
1080
array_list->super_minor == UnSet) {
1081
if (verbose >= 2 && array_list->devname)
1082
fprintf(stderr, Name
1083
": %s doesn't have any identifying information.\n",
1084
array_list->devname);
1087
/* FIXME, should I check raid_disks and level too?? */
1091
if (match->devname && array_list->devname)
1092
fprintf(stderr, Name
1093
": we match both %s and %s - cannot decide which to use.\n",
1094
match->devname, array_list->devname);
1096
fprintf(stderr, Name
1097
": multiple lines in mdadm.conf match\n");