]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Create.c
Allow /etc/mdadm/mdadm.conf as an alternate to /etc/mdadm.conf
[thirdparty/mdadm.git] / Create.c
CommitLineData
64c4757e 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
64c4757e 3 *
cd29a5c8 4 * Copyright (C) 2001-2002 Neil Brown <neilb@cse.unsw.edu.au>
64c4757e
NB
5 *
6 *
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.
11 *
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.
16 *
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
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
682c7051
NB
31#include "md_u.h"
32#include "md_p.h"
64c4757e 33
82d9eba6 34int Create(struct supertype *st, char *mddev, int mdfd,
aa88f531 35 int chunk, int level, int layout, unsigned long size, int raiddisks, int sparedisks,
947fd4dd 36 char *name,
cd29a5c8 37 int subdevs, mddev_dev_t devlist,
47d79ef8 38 int runstop, int verbose, int force, int assume_clean,
dfd4d8ee 39 char *bitmap_file, int bitmap_chunk, int write_behind, int delay)
64c4757e 40{
682c7051
NB
41 /*
42 * Create a new raid array.
43 *
44 * First check that necessary details are available
45 * (i.e. level, raid-disks)
46 *
47 * Then check each disk to see what might be on it
48 * and report anything interesting.
49 *
50 * If anything looks odd, and runstop not set,
51 * abort.
52 *
53 * SET_ARRAY_INFO and ADD_NEW_DISK, and
82d9eba6 54 * if runstop==run, or raiddisks disks were used,
682c7051
NB
55 * RUN_ARRAY
56 */
b5e64645 57 unsigned long long minsize=0, maxsize=0;
cd29a5c8
NB
58 char *mindisc = NULL;
59 char *maxdisc = NULL;
c913b90e 60 int dnum;
cd29a5c8 61 mddev_dev_t dv;
682c7051 62 int fail=0, warn=0;
82b27616 63 struct stat stb;
82d9eba6 64 int first_missing = subdevs * 2;
52826846 65 int missing_disks = 0;
82d9eba6 66 int insert_point = subdevs * 2; /* where to insert a missing drive */
4b1ac34b
NB
67 void *super;
68 int pass;
82d9eba6
NB
69 int vers;
70 int rv;
c82f047c 71 int bitmap_fd;
f9c25f1d 72 unsigned long long bitmapsize;
682c7051
NB
73
74 mdu_array_info_t array;
dcec9ee5
NB
75 int major = BITMAP_MAJOR_HI;
76
82d9eba6
NB
77 vers = md_get_version(mdfd);
78 if (vers < 9000) {
dd0781e5 79 fprintf(stderr, Name ": Create requires md driver version 0.90.0 or later\n");
52826846 80 return 1;
682c7051 81 }
98c6faba 82 if (level == UnSet) {
682c7051
NB
83 fprintf(stderr,
84 Name ": a RAID level is needed to create an array.\n");
85 return 1;
86 }
87 if (raiddisks < 1) {
88 fprintf(stderr,
b83d95f3 89 Name ": a number of --raid-devices must be given to create an array\n");
682c7051
NB
90 return 1;
91 }
98c6faba
NB
92 if (raiddisks < 4 && level == 6) {
93 fprintf(stderr,
94 Name ": at least 4 raid-devices needed for level 6\n");
95 return 1;
96 }
97 if (raiddisks > 256 && level == 6) {
98 fprintf(stderr,
99 Name ": no more than 256 raid-devices supported for level 6\n");
100 return 1;
101 }
52826846
NB
102 if (raiddisks < 2 && level >= 4) {
103 fprintf(stderr,
98c6faba 104 Name ": at least 2 raid-devices needed for level 4 or 5\n");
52826846
NB
105 return 1;
106 }
682c7051 107 if (subdevs > raiddisks+sparedisks) {
b83d95f3 108 fprintf(stderr, Name ": You have listed more devices (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
52826846 109 return 1;
682c7051 110 }
cd29a5c8 111 if (subdevs < raiddisks+sparedisks) {
52826846
NB
112 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
113 return 1;
114 }
115
682c7051 116 /* now set some defaults */
98c6faba 117 if (layout == UnSet)
682c7051
NB
118 switch(level) {
119 default: /* no layout */
120 layout = 0;
121 break;
e5329c37
NB
122 case 10:
123 layout = 0x102; /* near=2, far=1 */
dab6685f 124 if (verbose > 0)
e5329c37
NB
125 fprintf(stderr,
126 Name ": layout defaults to n1\n");
127 break;
682c7051 128 case 5:
98c6faba 129 case 6:
682c7051 130 layout = map_name(r5layout, "default");
dab6685f 131 if (verbose > 0)
682c7051
NB
132 fprintf(stderr,
133 Name ": layout defaults to %s\n", map_num(r5layout, layout));
134 break;
b5e64645
NB
135 case LEVEL_FAULTY:
136 layout = map_name(faultylayout, "default");
137
dab6685f 138 if (verbose > 0)
b5e64645
NB
139 fprintf(stderr,
140 Name ": layout defaults to %s\n", map_num(faultylayout, layout));
141 break;
682c7051
NB
142 }
143
e5329c37
NB
144 if (level == 10)
145 /* check layout fits in array*/
146 if ((layout&255) * ((layout>>8)&255) > raiddisks) {
147 fprintf(stderr, Name ": that layout requires at least %d devices\n",
148 (layout&255) * ((layout>>8)&255));
149 return 1;
150 }
151
aa88f531
NB
152 switch(level) {
153 case 4:
154 case 5:
e5329c37 155 case 10:
98c6faba 156 case 6:
aa88f531
NB
157 case 0:
158 case -1: /* linear */
159 if (chunk == 0) {
160 chunk = 64;
dab6685f 161 if (verbose > 0)
aa88f531
NB
162 fprintf(stderr, Name ": chunk size defaults to 64K\n");
163 }
164 break;
165 default: /* raid1, multipath */
166 if (chunk) {
167 chunk = 0;
dab6685f 168 if (verbose > 0)
aa88f531
NB
169 fprintf(stderr, Name ": chunk size ignored for this level\n");
170 }
171 break;
682c7051
NB
172 }
173
174 /* now look at the subdevs */
52826846
NB
175 array.active_disks = 0;
176 array.working_disks = 0;
c913b90e
NB
177 dnum = 0;
178 for (dv=devlist; dv; dv=dv->next, dnum++) {
cd29a5c8 179 char *dname = dv->devname;
b5e64645
NB
180 unsigned long dsize;
181 unsigned long long ldsize, freesize;
52826846 182 int fd;
cd29a5c8 183 if (strcasecmp(dname, "missing")==0) {
c913b90e
NB
184 if (first_missing > dnum)
185 first_missing = dnum;
52826846
NB
186 missing_disks ++;
187 continue;
188 }
189 array.working_disks++;
c913b90e 190 if (dnum < raiddisks)
52826846 191 array.active_disks++;
d7eaf49f 192 fd = open(dname, O_RDONLY|O_EXCL, 0);
682c7051
NB
193 if (fd <0 ) {
194 fprintf(stderr, Name ": Cannot open %s: %s\n",
195 dname, strerror(errno));
196 fail=1;
197 continue;
198 }
b5e64645
NB
199#ifdef BLKGETSIZE64
200 if (ioctl(fd, BLKGETSIZE64, &ldsize)==0)
201 ;
202 else
203#endif
682c7051
NB
204 if (ioctl(fd, BLKGETSIZE, &dsize)) {
205 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
206 dname, strerror(errno));
207 fail = 1;
208 close(fd);
209 continue;
210 }
b5e64645
NB
211 else {
212 ldsize = dsize;
570c0542 213 ldsize <<= 9;
b5e64645 214 }
1bf4e2d9 215 freesize = st->ss->avail_size(st, ldsize >> 9);
82d9eba6 216 if (freesize == 0) {
aa88f531 217 fprintf(stderr, Name ": %s is too small: %luK\n",
b5e64645 218 dname, (unsigned long)(ldsize>>10));
52826846
NB
219 fail = 1;
220 close(fd);
221 continue;
682c7051 222 }
82d9eba6
NB
223
224 freesize /= 2; /* convert to K */
682c7051
NB
225
226 if (size && freesize < size) {
52826846 227 fprintf(stderr, Name ": %s is smaller that given size."
b5e64645 228 " %lluK < %luK + superblock\n", dname, freesize, size);
52826846
NB
229 fail = 1;
230 close(fd);
231 continue;
682c7051 232 }
cd29a5c8
NB
233 if (maxdisc == NULL || (maxdisc && freesize > maxsize)) {
234 maxdisc = dname;
52826846 235 maxsize = freesize;
682c7051 236 }
cd29a5c8
NB
237 if (mindisc ==NULL || (mindisc && freesize < minsize)) {
238 mindisc = dname;
52826846 239 minsize = freesize;
682c7051 240 }
dab6685f
NB
241 if (runstop != 1 || verbose >= 0) {
242 warn |= check_ext2(fd, dname);
243 warn |= check_reiser(fd, dname);
244 warn |= check_raid(fd, dname);
245 }
682c7051
NB
246 close(fd);
247 }
248 if (fail) {
52826846
NB
249 fprintf(stderr, Name ": create aborted\n");
250 return 1;
682c7051
NB
251 }
252 if (size == 0) {
cd29a5c8 253 if (mindisc == NULL) {
52826846
NB
254 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
255 return 1;
256 }
c13c45e9 257 if (level > 0 || level == LEVEL_MULTIPATH || level == LEVEL_FAULTY) {
b5e64645
NB
258 /* size is meaningful */
259 if (minsize > 0x100000000ULL) {
260 fprintf(stderr, Name ": devices too large for RAID level %d\n", level);
261 return 1;
262 }
263 size = minsize;
dab6685f 264 if (verbose > 0)
b5e64645
NB
265 fprintf(stderr, Name ": size set to %luK\n", size);
266 }
682c7051 267 }
b5e64645 268 if (level > 0 && ((maxsize-size)*100 > maxsize)) {
dab6685f
NB
269 if (runstop != 1 || verbose >= 0)
270 fprintf(stderr, Name ": largest drive (%s) exceed size (%luK) by more than 1%%\n",
271 maxdisc, size);
52826846 272 warn = 1;
682c7051
NB
273 }
274
275 if (warn) {
52826846
NB
276 if (runstop!= 1) {
277 if (!ask("Continue creating array? ")) {
278 fprintf(stderr, Name ": create aborted.\n");
279 return 1;
280 }
281 } else {
dab6685f 282 if (verbose > 0)
52826846 283 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
682c7051 284 }
682c7051
NB
285 }
286
52826846
NB
287 /* If this is raid5, we want to configure the last active slot
288 * as missing, so that a reconstruct happens (faster than re-parity)
98c6faba 289 * FIX: Can we do this for raid6 as well?
52826846 290 */
47d79ef8 291 if (assume_clean==0 && force == 0 && first_missing >= raiddisks) {
98c6faba
NB
292 switch ( level ) {
293 case 5:
294 insert_point = raiddisks-1;
295 sparedisks++;
296 array.active_disks--;
297 missing_disks++;
298 break;
299 default:
300 break;
301 }
52826846
NB
302 }
303
682c7051
NB
304 /* Ok, lets try some ioctls */
305
306 array.level = level;
307 array.size = size;
682c7051 308 array.raid_disks = raiddisks;
82b27616
NB
309 /* The kernel should *know* what md_minor we are dealing
310 * with, but it chooses to trust me instead. Sigh
311 */
682c7051 312 array.md_minor = 0;
82b27616 313 if (fstat(mdfd, &stb)==0)
0df46c2a 314 array.md_minor = minor(stb.st_rdev);
682c7051 315 array.not_persistent = 0;
98c6faba 316 /*** FIX: Need to do something about RAID-6 here ***/
b5e64645
NB
317 if ( ( (level == 5) &&
318 (insert_point < raiddisks || first_missing < raiddisks) )
319 ||
320 ( level == 6 && missing_disks == 2)
47d79ef8
NB
321 ||
322 assume_clean
b5e64645 323 )
98c6faba 324 array.state = 1; /* clean, but one+ drive will be missing */
82b27616 325 else
52826846 326 array.state = 0; /* not clean, but no errors */
82b27616 327
f9c25f1d
NB
328 if (level == 10) {
329 /* for raid10, the bitmap size is the capacity of the array,
330 * which is array.size * raid_disks / ncopies;
331 * .. but convert to sectors.
332 */
333 int ncopies = (layout>>8) * (layout & 255);
334 bitmapsize = (unsigned long long)array.size * raiddisks / ncopies * 2;
335 printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, array.size, raiddisks, ncopies);
336 } else
337 bitmapsize = (unsigned long long)array.size * 2;
338
82b27616
NB
339 /* There is lots of redundancy in these disk counts,
340 * raid_disks is the most meaningful value
341 * it describes the geometry of the array
342 * it is constant
343 * nr_disks is total number of used slots.
344 * it should be raid_disks+spare_disks
345 * spare_disks is the number of extra disks present
346 * see above
347 * active_disks is the number of working disks in
348 * active slots. (With raid_disks)
349 * working_disks is the total number of working disks,
350 * including spares
351 * failed_disks is the number of disks marked failed
352 *
353 * Ideally, the kernel would keep these (except raid_disks)
354 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
355 * So for now, we assume that all raid and spare
356 * devices will be given.
357 */
52826846
NB
358 array.spare_disks=sparedisks;
359 array.failed_disks=missing_disks;
360 array.nr_disks = array.working_disks + array.failed_disks;
682c7051
NB
361 array.layout = layout;
362 array.chunk_size = chunk*1024;
55935d51 363
82d9eba6 364
947fd4dd 365 if (!st->ss->init_super(st, &super, &array, name))
82d9eba6 366 return 1;
682c7051 367
dcec9ee5
NB
368 if (bitmap_file && vers < 9003) {
369 major = BITMAP_MAJOR_HOSTENDIAN;
370#ifdef __BIG_ENDIAN
371 fprintf(stderr, Name ": Warning - bitmaps created on this kernel are not portable\n"
372 " between different architectured. Consider upgrading the Linux kernel.\n");
373#endif
374 }
375
55935d51
NB
376 if (bitmap_file && strcmp(bitmap_file, "internal")==0) {
377 if ((vers%100) < 2) {
378 fprintf(stderr, Name ": internal bitmaps not supported by this kernel.\n");
379 return 1;
380 }
34163fc7 381 if (!st->ss->add_internal_bitmap(st, super, bitmap_chunk, delay, write_behind,
f9c25f1d 382 bitmapsize, 1, major)) {
55935d51
NB
383 fprintf(stderr, Name ": Given bitmap chunk size not supported.\n");
384 return 1;
385 }
386 bitmap_file = NULL;
387 }
388
389
390
82d9eba6
NB
391 if ((vers % 100) >= 1) { /* can use different versions */
392 mdu_array_info_t inf;
393 memset(&inf, 0, sizeof(inf));
394 inf.major_version = st->ss->major;
395 inf.minor_version = st->minor_version;
396 rv = ioctl(mdfd, SET_ARRAY_INFO, &inf);
397 } else
398 rv = ioctl(mdfd, SET_ARRAY_INFO, NULL);
399 if (rv) {
52826846
NB
400 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
401 mddev, strerror(errno));
402 return 1;
682c7051 403 }
52826846 404
c82f047c
NB
405 if (bitmap_file) {
406 int uuid[4];
55935d51
NB
407
408 if (bitmap_chunk == UnSet)
409 bitmap_chunk = DEFAULT_BITMAP_CHUNK;
410
c82f047c 411 st->ss->uuid_from_super(uuid, super);
dfd4d8ee 412 if (CreateBitmap(bitmap_file, force, (char*)uuid, bitmap_chunk,
dcec9ee5 413 delay, write_behind,
f9c25f1d 414 bitmapsize,
dcec9ee5 415 major)) {
c82f047c
NB
416 return 1;
417 }
418 bitmap_fd = open(bitmap_file, O_RDWR);
419 if (bitmap_fd < 0) {
420 fprintf(stderr, Name ": weird: %s cannot be openned\n",
dcec9ee5 421 bitmap_file);
c82f047c
NB
422 return 1;
423 }
424 if (ioctl(mdfd, SET_BITMAP_FILE, bitmap_fd) < 0) {
425 fprintf(stderr, Name ": Cannot set bitmap file for %s: %s\n",
426 mddev, strerror(errno));
427 return 1;
428 }
429 }
430
4b1ac34b
NB
431
432
433 for (pass=1; pass <=2 ; pass++) {
434 mddev_dev_t moved_disk = NULL; /* the disk that was moved out of the insert point */
435
436 for (dnum=0, dv = devlist ; dv ;
437 dv=(dv->next)?(dv->next):moved_disk, dnum++) {
438 int fd;
439 struct stat stb;
440 mdu_disk_info_t disk;
441
442 disk.number = dnum;
443 if (dnum == insert_point) {
444 moved_disk = dv;
52826846 445 }
4b1ac34b
NB
446 disk.raid_disk = disk.number;
447 if (disk.raid_disk < raiddisks)
dfd4d8ee
NB
448 disk.state = (1<<MD_DISK_ACTIVE) |
449 (1<<MD_DISK_SYNC);
4b1ac34b
NB
450 else
451 disk.state = 0;
dfd4d8ee
NB
452 if (dv->writemostly)
453 disk.state |= (1<<MD_DISK_WRITEMOSTLY);
454
4b1ac34b
NB
455 if (dnum == insert_point ||
456 strcasecmp(dv->devname, "missing")==0) {
457 disk.major = 0;
458 disk.minor = 0;
dfd4d8ee 459 disk.state = (1<<MD_DISK_FAULTY);
4b1ac34b
NB
460 } else {
461 fd = open(dv->devname, O_RDONLY|O_EXCL, 0);
462 if (fd < 0) {
463 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
464 dv->devname);
465 return 1;
466 }
467 fstat(fd, &stb);
468 disk.major = major(stb.st_rdev);
469 disk.minor = minor(stb.st_rdev);
470 close(fd);
471 }
472 switch(pass){
473 case 1:
82d9eba6 474 st->ss->add_to_super(super, &disk);
4b1ac34b
NB
475 break;
476 case 2:
eaac7dde 477 if (disk.state == 1) break;
1bf4e2d9 478 st->ss->write_init_super(st, super, &disk, dv->devname);
4b1ac34b
NB
479
480 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
481 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n",
482 dv->devname, strerror(errno));
483 free(super);
484 return 1;
485 }
486
487 break;
488 }
489 if (dv == moved_disk && dnum != insert_point) break;
52826846 490 }
5e52ae9e 491 }
4b1ac34b 492 free(super);
5e52ae9e 493
682c7051
NB
494 /* param is not actually used */
495 if (runstop == 1 || subdevs >= raiddisks) {
82b27616 496 mdu_param_t param;
682c7051
NB
497 if (ioctl(mdfd, RUN_ARRAY, &param)) {
498 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
499 strerror(errno));
91f068bf 500 Manage_runstop(mddev, mdfd, -1, 0);
682c7051
NB
501 return 1;
502 }
dab6685f
NB
503 if (verbose >= 0)
504 fprintf(stderr, Name ": array %s started.\n", mddev);
682c7051 505 } else {
b83d95f3 506 fprintf(stderr, Name ": not starting array - not enough devices.\n");
682c7051
NB
507 }
508 return 0;
64c4757e 509}