]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
36 * Read the config file
38 * conf_get_uuids gets a list of devicename+uuid pairs
39 * conf_get_devs gets device names after expanding wildcards
41 * Each keeps the returned list and frees it when asked to make
44 * The format of the config file needs to be fairly extensible.
45 * Now, arrays only have names and uuids and devices merely are.
46 * But later arrays might want names, and devices might want superblock
47 * versions, and who knows what else.
48 * I like free format, abhore backslash line continuation, adore
49 * indentation for structure and am ok about # comments.
51 * So, each line that isn't blank or a #comment must either start
52 * with a key word, and not be indented, or must start with a
53 * non-key-word and must be indented.
55 * Keywords are DEVICE and ARRAY
56 * DEV{ICE} introduces some devices that might contain raid components.
58 * DEV style=0 /dev/sda* /dev/hd*
59 * DEV style=1 /dev/sd[b-f]*
60 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
62 * ARRAY /dev/md0 uuid=whatever name=something
63 * Spaces separate words on each line. Quoting, with "" or '' protects them,
64 * but may not wrap over lines
69 #define CONFFILE "/etc/mdadm.conf"
71 char DefaultConfFile
[] = CONFFILE
;
73 char *keywords
[] = { "device", "array", NULL
};
76 * match_keyword returns an index into the keywords array, or -1 for no match
77 * case is ignored, and at least three characters must be given
80 int match_keyword(char *word
)
82 int len
= strlen(word
);
85 if (len
< 3) return -1;
86 for (n
=0; keywords
[n
]; n
++) {
87 if (strncasecmp(word
, keywords
[n
], len
)==0)
93 /* conf_word gets one word from the conf file.
94 * if "allow_key", then accept words at the start of a line,
95 * otherwise stop when such a word is found.
96 * We assume that the file pointer is at the end of a word, so the
97 * next character is a space, or a newline. If not, it is the start of a line.
100 char *conf_word(FILE *file
, int allow_key
)
107 char *word
= malloc(wsize
);
111 while (wordfound
==0) {
112 /* at the end of a word.. */
115 while (c
!= EOF
&& c
!= '\n')
118 if (c
== '\n') continue;
120 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
124 /* looks like it is safe to get a word here, if there is one */
126 /* first, skip any spaces */
127 while (c
== ' ' || c
== '\t')
129 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
130 /* we really have a character of a word, so start saving it */
131 while (c
!= EOF
&& c
!= '\n' && (quote
|| (c
!=' ' && c
!= '\t'))) {
133 if (quote
&& c
== quote
) quote
= 0;
134 else if (quote
== 0 && (c
== '\'' || c
== '"'))
137 if (len
== wsize
-1) {
139 word
= realloc(word
, wsize
);
147 if (c
!= EOF
) ungetc(c
, file
);
150 /* printf("word is <%s>\n", word); */
159 * conf_line reads one logical line from the conffile.
160 * It skips comments and continues until it finds a line that starts
161 * with a non blank/comment. This character is pushed back for the next call
162 * A doubly linked list of words is returned.
163 * the first word will be a keyword. Other words will have had quotes removed.
166 char *conf_line(FILE *file
)
171 w
= conf_word(file
, 1);
172 if (w
== NULL
) return NULL
;
178 while ((w
= conf_word(file
,0))){
179 char *w2
= dl_strdup(w
);
183 /* printf("got a line\n");*/
187 void free_line(char *line
)
190 for (w
=dl_next(line
); w
!= line
; w
=dl_next(line
)) {
199 struct conf_dev
*next
;
205 int devline(char *line
)
210 for (w
=dl_next(line
); w
!= line
; w
=dl_next(w
)) {
212 cd
= malloc(sizeof(*cd
));
213 cd
->name
= strdup(w
);
217 fprintf(stderr
, Name
": unreconised word on DEVICE line: %s\n",
223 mddev_ident_t mddevlist
= NULL
;
224 mddev_ident_t
*mddevlp
= &mddevlist
;
226 void arrayline(char *line
)
230 struct mddev_ident_s mis
;
234 mis
.super_minor
= -1;
240 for (w
=dl_next(line
); w
!=line
; w
=dl_next(w
)) {
243 fprintf(stderr
, Name
": only give one device per ARRAY line: %s and %s\n",
245 else mis
.devname
= w
;
246 } else if (strncasecmp(w
, "uuid=", 5)==0 ) {
248 fprintf(stderr
, Name
": only specify uuid once, %s ignored.\n",
251 if (parse_uuid(w
+5, mis
.uuid
))
254 fprintf(stderr
, Name
": bad uuid: %s\n", w
);
256 } else if (strncasecmp(w
, "super-minor=", 12)==0 ) {
257 if (mis
.super_minor
>= 0)
258 fprintf(stderr
, Name
": only specify super-minor once, %s ignored.\n",
262 mis
.super_minor
= strtol(w
+12, &endptr
, 10);
263 if (w
[12]==0 || endptr
[0]!=0 || mis
.super_minor
< 0) {
264 fprintf(stderr
, Name
": invalid super-minor number: %s\n",
266 mis
.super_minor
= -1;
269 } else if (strncasecmp(w
, "devices=", 8 ) == 0 ) {
271 fprintf(stderr
, Name
": only specify devices once (use a comma separated list). %s ignored\n",
274 mis
.devices
= strdup(w
+8);
275 } else if (strncasecmp(w
, "spare-group=", 12) == 0 ) {
277 fprintf(stderr
, Name
": only specify one spare group per array. %s ignored.\n",
280 mis
.spare_group
= strdup(w
+12);
281 } else if (strncasecmp(w
, "level=", 6) == 0 ) {
282 /* this is mainly for compatability with --brief output */
283 mis
.level
= map_name(pers
, w
+6);
284 } else if (strncasecmp(w
, "disks=", 6) == 0 ) {
285 /* again, for compat */
286 mis
.raid_disks
= atoi(w
+6);
288 fprintf(stderr
, Name
": unrecognised word on ARRAY line: %s\n",
292 if (mis
.devname
== NULL
)
293 fprintf(stderr
, Name
": ARRAY line with a device\n");
294 else if (mis
.uuid_set
== 0 && mis
.devices
== NULL
&& mis
.super_minor
< 0)
295 fprintf(stderr
, Name
": ARRAY line %s has no identity information.\n", mis
.devname
);
297 mi
= malloc(sizeof(*mi
));
299 mi
->devname
= strdup(mis
.devname
);
308 void load_conffile(char *conffile
)
314 if (conffile
== NULL
)
315 conffile
= DefaultConfFile
;
317 f
= fopen(conffile
, "r");
322 while ((line
=conf_line(f
))) {
323 switch(match_keyword(line
)) {
331 fprintf(stderr
, Name
": Unknown keyword %s\n", line
);
337 /* printf("got file\n"); */
341 mddev_ident_t
conf_get_ident(char *conffile
, char *dev
)
344 load_conffile(conffile
);
346 while (dev
&& rv
&& strcmp(dev
, rv
->devname
)!=0)
351 mddev_dev_t
conf_get_devs(char *conffile
)
356 static mddev_dev_t dlist
= NULL
;
360 mddev_dev_t t
= dlist
;
366 load_conffile(conffile
);
368 for (cd
=cdevlist
; cd
; cd
=cd
->next
) {
369 glob(cd
->name
, flags
, NULL
, &globbuf
);
370 flags
|= GLOB_APPEND
;
373 for (i
=0; i
<globbuf
.gl_pathc
; i
++) {
374 mddev_dev_t t
= malloc(sizeof(*t
));
375 t
->devname
= strdup(globbuf
.gl_pathv
[i
]);
378 /* printf("one dev is %s\n", t->devname);*/
386 int match_oneof(char *devices
, char *devname
)
388 /* check if one of the comma separated patterns in devices
393 while (devices
&& *devices
) {
396 devices
= strchr(devices
, ',');
398 devices
= p
+ strlen(p
);
399 if (devices
-p
< 1024) {
400 strncpy(patn
, p
, devices
-p
);
402 if (fnmatch(patn
, devname
, FNM_PATHNAME
)==0)