File: mkraid.c

package info (click to toggle)
raidtools2 0.90.990824-5
  • links: PTS
  • area: main
  • in suites: potato
  • size: 688 kB
  • ctags: 468
  • sloc: ansic: 2,631; makefile: 143; sh: 114
file content (311 lines) | stat: -rw-r--r-- 8,060 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * mkraid.c : Utility for the Linux Multiple Devices driver
 *            Copyright (C) 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
 *            Copyright (C) 1998 Erik Troan
 *
 * This utility reads a Linux MD raid1/4/5 configuration file, writes a
 * raid superblock on the devices in the raid set, and initializes the
 * set for first time use, depending on command line switches and
 * configuration file settings.
 *
 * This source is covered by the GNU GPL, the same as all Linux kernel
 * sources.
 */

#include "common.h"
#include "parser.h"
#include "raid_io.h"
#include "raidlib.h"
#include "popt.h"
#include "version.h"

void printcfg (md_cfg_entry_t * cfg);

void usage (void) {
    printf("usage: mkraid [--configfile] [--version] [--force] [--upgrade]\n");
    printf("       [--dangerous-no-resync] [-acfhuv] </dev/md?>*\n");
}

static int makeOneRaid(struct md_version * ver, enum mkraidFunc func,
	 md_cfg_entry_t * cfg, int force, int upgrade, int resync) {
int i, ret, file;

    if (check_active(cfg)) 
	return 1;

    if (analyze_sb(ver, func, cfg, force, upgrade, resync))
	return 1;

    if (upgrade)
	return 0;
#if 0
    printcfg(cfg);
#endif

    file = open(cfg->md_name,O_RDONLY);
    ret = ioctl(file, SET_ARRAY_INFO, (unsigned long)&cfg->array.param);
    if (ret)
        return 1;

    for (i = 0; i < cfg->array.param.nr_disks; i++) {
            ret = ioctl(file, ADD_NEW_DISK,(unsigned long)(cfg->array.disks+i));
            if (ret)
                 return 1;
    }
    close(file);

    handleOneConfig(raidrun, cfg);

    return 0;
}

int main (int argc, char *argv[])
{
    char *namestart=argv[0];
    FILE *fp = NULL;
    md_cfg_entry_t *p;
    int exit_status=0;
    int version = 0, help = 0, debug = 0;
    char * configFile = RAID_CONFIG;
    int force_flag = 0;
    int old_force_flag = 0;
    int upgrade_flag = 0;
    int no_resync_flag = 0;
    int all_flag = 0;
    char ** args;
    struct md_version ver;
    poptContext optCon;
    int i;
    enum mkraidFunc func;
    struct poptOption optionsTable[] = {
	{ "configfile", 'c', POPT_ARG_STRING, &configFile, 0 },
	{ "force", 'f', 0, &old_force_flag, 0 },
	{ "really-force", 'R', 0, &force_flag, 0 },
	{ "upgrade", 'u', 0, &upgrade_flag, 0 },
	{ "dangerous-no-resync", 'r', 0, &no_resync_flag, 0 },
	{ "help", 'h', 0, &help, 0 },
	{ "version", 'V', 0, &version, 0 },
	{ "debug", 0, 0, &debug, 0 },
	{ "all", 'a', 0, &all_flag, 0 },
	{ NULL, 0, 0, NULL, 0 }
    } ;

    optCon = poptGetContext("mkraid", argc, argv, optionsTable, 0);
    if ((i = poptGetNextOpt(optCon)) < -1) {
	fprintf(stderr, "%s: %s\n", 
		poptBadOption(optCon, POPT_BADOPTION_NOALIAS),
		poptStrerror(i));
	usage();
	return EXIT_FAILURE;
    }

    if (prepare_raidlib())
         return EXIT_FAILURE;

    namestart = strrchr(argv[0], '/');
    if (!namestart)
        namestart = argv[0];
    else
        namestart++;

    if (!strcmp (namestart, "mkraid")) {
        func = mkraid;
	if (all_flag) {
        	fprintf (stderr, "--all not possible for mkraid!\n");
        	return (EXIT_FAILURE);
	}
    } else if (!strcmp (namestart, "raid0run")) {
        func = raid0run;
	if (old_force_flag) {
        	fprintf (stderr, "--force not possible for raid0run!\n");
        	return (EXIT_FAILURE);
	}
	if (force_flag) {
        	fprintf (stderr, "--really-force not possible for raid0run!\n");
        	return (EXIT_FAILURE);
	}
	if (upgrade_flag) {
        	fprintf (stderr, "--upgrade not possible for raid0run!\n");
        	return (EXIT_FAILURE);
	}
	if (no_resync_flag) {
        	fprintf (stderr, "--dangerous-no-resync not possible for raid0run!\n");
        	return (EXIT_FAILURE);
	}
    } else {
        fprintf (stderr, "Unknown command %s\n", argv[0]);
        usage();
        return (EXIT_FAILURE);
    }

    if (help) {
	usage();
	return EXIT_FAILURE;
    } else if (version) {
	printf("mkraid version %d.%d.%d\n", MKRAID_MAJOR_VERSION,
			MKRAID_MINOR_VERSION, MKRAID_PATCHLEVEL_VERSION);
	return EXIT_VERSION;
    }

    fp = fopen(configFile, "r");
    if (fp == NULL) {
	fprintf(stderr, "Couldn't open %s -- %s\n", configFile, 
	        strerror(errno));
	goto abort;
    }

    srand((unsigned int) time(NULL));
    if (parse_config(fp))
	goto abort;

    args = poptGetArgs(optCon);
    if (!all_flag && !args) {
	fprintf(stderr, "nothing to do!\n");
	usage();
	return EXIT_FAILURE;
    }

    if (getMdVersion(&ver)) {
	fprintf(stderr, "cannot determine md version: %s\n", strerror(errno));
	return EXIT_FAILURE;
    }

    if (old_force_flag && (func == mkraid)) {
	fprintf(stderr, 

"--force and the new RAID 0.90 hot-add/hot-remove functionality should be\n"
" used with extreme care! If /etc/raidtab is not in sync with the real array\n"
" configuration, then a --force will DESTROY ALL YOUR DATA. It's especially\n"
" dangerous to use -f if the array is in degraded mode. \n\n"
" PLEASE dont mention the --really-force flag in any email, documentation or\n"
" HOWTO, just suggest the --force flag instead. Thus everybody will read\n"
" this warning at least once :) It really sucks to LOSE DATA. If you are\n"
" confident that everything will go ok then you can use the --really-force\n"
" flag. Also, if you are unsure what this is all about, dont hesitate to\n"
" ask questions on linux-raid@vger.rutgers.edu\n");

	return EXIT_FAILURE;
    }

    if (debug) {
    	int file, ret;

	file = open(cfg_head->md_name, O_RDONLY);
	if (file == -1) {
		perror("could not open file");
		return EXIT_FAILURE;
	}
		
	ret = ioctl(file, PRINT_RAID_DEBUG, 0UL);
	if (ret) {
		perror("debug ioctl failed");
		return EXIT_FAILURE;
	}

	printf("dumped RAID status into the syslog.\n");
	return 0;
    }

    if (all_flag) {
	for (p = cfg_head; p; p = p->next) {
	    printf("considering %s...\n", p->md_name);
	    if ((p->array.param.level != 0) &&
			(p->array.param.level != -1)) {
		fprintf(stderr, "%s is not a RAID0 or LINEAR array, skipping.\n", p->md_name);
		continue;
	    }
	    if (!p->array.param.not_persistent) {
		fprintf(stderr, "%s is persistent, skipping.\n", p->md_name);
		return EXIT_FAILURE;
	    }
	    /*
	     * Invert the meaning of no_resync_flag from this point on
	     * to avoid confusion.
	     */
	    if (makeOneRaid(&ver, func, p, force_flag, upgrade_flag,
			!no_resync_flag))
		goto abort;
	}
    } else {

    while (*args) {
	if (force_flag) {
		fprintf(stderr, 
		"DESTROYING the contents of %s in 5 seconds, Ctrl-C if unsure!\n", *args);
		sleep(5);
	}
	for (p = cfg_head; p; p = p->next) {
	    if (strcmp(p->md_name, *args)) continue;
	    if (func == raid0run) {
		if ((p->array.param.level != 0) &&
			(p->array.param.level != -1)) {
			fprintf(stderr, "%s is not a RAID0 or LINEAR array!\n",
					p->md_name);
			return EXIT_FAILURE;
		}
		if (!p->array.param.not_persistent) {
			fprintf(stderr, "%s must be a nonpersistent RAID0 or LINEAR array!\n", p->md_name);
			return EXIT_FAILURE;
		}
	    }
	    if (makeOneRaid(&ver, func, p, force_flag, upgrade_flag, !no_resync_flag))
		goto abort;
	    break;
	}
	if (!p) {
	    fprintf(stderr, "device %s is not described in config file\n", *args);
	    exit_status++;
	}
	args++;
    }
    }

    fclose(fp);
    return 0;

abort:
    fprintf(stderr, "mkraid: aborted, see the syslog and /proc/mdstat for potential clues.\n");
    exit_status = 1;
    if (fp)
	fclose(fp);
    return exit_status;
}


#define P(x) printf("%18s: \t %d\n",#x,cfg->array.param.x)
#define DP(x) printf("%18s: \t %d\n",#x,cfg->array.disks[i].x)

void printcfg (md_cfg_entry_t * cfg)
{
	int i;

	P(major_version);
	P(minor_version);
	P(patch_version);
        P(ctime);
        P(level);
        P(size);
        P(nr_disks);
        P(raid_disks);
        P(md_minor);

        P(utime);
        P(state);
        P(active_disks);
        P(working_disks);
        P(failed_disks);
        P(spare_disks);

        P(layout);
        P(chunk_size);

	for (i = 0; i < cfg->array.param.nr_disks; i++) {
		printf("\n");

		DP(number);
		DP(major);
		DP(minor);
		DP(raid_disk);
		DP(state);
	}
}