]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - config.c
2 * mdctl - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001 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
34 * Read the config file
36 * conf_get_uuids gets a list of devicename+uuid pairs
37 * conf_get_devs gets device names after expanding wildcards
39 * Each keeps the returned list and frees it when asked to make
42 * The format of the config file needs to be fairly extensible.
43 * Now, arrays only have names and uuids and devices merely are.
44 * But later arrays might want names, and devices might want superblock
45 * versions, and who knows what else.
46 * I like free format, abhore backslash line continuation, adore
47 * indentation for structure and am ok about # comments.
49 * So, each line that isn't blank or a #comment must either start
50 * with a key word, and not be indented, or must start with a
51 * non-key-word and must be indented.
53 * Keywords are DEVICE and ARRAY
54 * DEV{ICE} introduces some devices that might contain raid components.
56 * DEV style=0 /dev/sda* /dev/hd*
57 * DEV style=1 /dev/sd[b-f]*
58 * ARR{AY} describes an array giving md device and attributes like uuid=whatever
60 * ARRAY /dev/md0 uuid=whatever name=something
61 * Spaces separate words on each line. Quoting, with "" or '' protects them,
62 * but may not wrap over lines
66 char DefaultConfFile
[] = "/etc/mdctl.conf";
68 char *keywords
[] = { "device", "array", NULL
};
71 * match_keyword returns an index into the keywords array, or -1 for no match
72 * case is ignored, and at least three characters must be given
75 int match_keyword(char *word
)
77 int len
= strlen(word
);
80 if (len
< 3) return -1;
81 for (n
=0; keywords
[n
]; n
++) {
82 if (strncasecmp(word
, keywords
[n
], len
)==0)
88 /* conf_word gets one word from the conf file.
89 * if "allow_key", then accept words at the start of a line,
90 * otherwise stop when such a word is found.
91 * We assume that the file pointer is at the end of a word, so the
92 * next character is a space, or a newline. If not, it is the start of a line.
95 char *conf_word(FILE *file
, int allow_key
)
102 char *word
= malloc(wsize
);
106 while (wordfound
==0) {
107 /* at the end of a word.. */
110 while (c
!= EOF
&& c
!= '\n')
113 if (c
== '\n') continue;
115 if (c
!= ' ' && c
!= '\t' && ! allow_key
) {
119 /* looks like it is safe to get a word here, if there is one */
121 /* first, skip any spaces */
122 while (c
== ' ' || c
== '\t')
124 if (c
!= EOF
&& c
!= '\n' && c
!= '#') {
125 /* we really have a character of a word, so start saving it */
126 while (c
!= EOF
&& c
!= '\n' && (quote
|| (c
!=' ' && c
!= '\t'))) {
128 if (quote
&& c
== quote
) quote
= 0;
129 else if (quote
== 0 && (c
== '\'' || c
== '"'))
132 if (len
== wsize
-1) {
134 word
= realloc(word
, wsize
);
142 if (c
!= EOF
) ungetc(c
, file
);
145 /* printf("word is <%s>\n", word); */
154 * conf_line reads one logical line from the conffile.
155 * It skips comments and continues until it finds a line that starts
156 * with a non blank/comment. This character is pushed back for the next call
157 * A doubly linked list of words is returned.
158 * the first word will be a keyword. Other words will have had quotes removed.
161 char *conf_line(FILE *file
)
166 w
= conf_word(file
, 1);
167 if (w
== NULL
) return NULL
;
173 while ((w
= conf_word(file
,0))){
174 char *w2
= dl_strdup(w
);
178 /* printf("got a line\n");*/
182 void free_line(char *line
)
185 for (w
=dl_next(line
); w
!= line
; w
=dl_next(line
)) {
194 struct conf_dev
*next
;
200 int devline(char *line
)
205 for (w
=dl_next(line
); w
!= line
; w
=dl_next(w
)) {
207 cd
= malloc(sizeof(*cd
));
208 cd
->name
= strdup(w
);
212 fprintf(stderr
, Name
": unreconised word on DEVICE line: %s\n",
218 mddev_uuid_t uuidlist
= NULL
;
220 void arrayline(char *line
)
228 for (w
=dl_next(line
); w
!=line
; w
=dl_next(w
)) {
231 fprintf(stderr
, Name
": only give one device per ARRAY line: %s and %s\n",
234 } else if (strncasecmp(w
, "uuid=", 5)==0 ) {
236 fprintf(stderr
, Name
": only specify uuid once, %s ignored.\n",
239 if (parse_uuid(w
+5, uuid
))
242 fprintf(stderr
, Name
": bad uuid: %s\n", w
);
245 fprintf(stderr
, Name
": unrecognised word on ARRAY line: %s\n",
250 fprintf(stderr
, Name
": ARRAY line with a device\n");
251 else if (uidset
== 0)
252 fprintf(stderr
, Name
": ARRAY line %s has no uuid\n", dev
);
254 mu
= malloc(sizeof(*mu
));
255 mu
->devname
= strdup(dev
);
256 memcpy(mu
->uuid
, uuid
, sizeof(uuid
));
264 void load_conffile(char *conffile
)
270 if (conffile
== NULL
)
271 conffile
= DefaultConfFile
;
273 f
= fopen(conffile
, "r");
278 while ((line
=conf_line(f
))) {
279 switch(match_keyword(line
)) {
287 fprintf(stderr
, Name
": Unknown keyword %s\n", line
);
293 /* printf("got file\n"); */
297 mddev_uuid_t
conf_get_uuids(char *conffile
)
299 load_conffile(conffile
);
303 mddev_dev_t
conf_get_devs(char *conffile
)
308 static mddev_dev_t dlist
= NULL
;
312 mddev_dev_t t
= dlist
;
318 load_conffile(conffile
);
320 for (cd
=cdevlist
; cd
; cd
=cd
->next
) {
321 glob(cd
->name
, flags
, NULL
, &globbuf
);
322 flags
|= GLOB_APPEND
;
325 for (i
=0; i
<globbuf
.gl_pathc
; i
++) {
326 mddev_dev_t t
= malloc(sizeof(*t
));
327 t
->devname
= strdup(globbuf
.gl_pathv
[i
]);
330 /* printf("one dev is %s\n", t->devname);*/