]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2006 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@cse.unsw.edu.au>
24 * School of Computer Science and Engineering
25 * The University of New South Wales
35 int Create(struct supertype
*st
, char *mddev
, int mdfd
,
36 int chunk
, int level
, int layout
, unsigned long long size
, int raiddisks
, int sparedisks
,
37 char *name
, char *homehost
, int *uuid
,
38 int subdevs
, mddev_dev_t devlist
,
39 int runstop
, int verbose
, int force
, int assume_clean
,
40 char *bitmap_file
, int bitmap_chunk
, int write_behind
, int delay
)
43 * Create a new raid array.
45 * First check that necessary details are available
46 * (i.e. level, raid-disks)
48 * Then check each disk to see what might be on it
49 * and report anything interesting.
51 * If anything looks odd, and runstop not set,
54 * SET_ARRAY_INFO and ADD_NEW_DISK, and
55 * if runstop==run, or raiddisks disks were used,
58 unsigned long long minsize
=0, maxsize
=0;
65 int first_missing
= subdevs
* 2;
66 int second_missing
= subdevs
* 2;
67 int missing_disks
= 0;
68 int insert_point
= subdevs
* 2; /* where to insert a missing drive */
73 unsigned long long bitmapsize
;
76 int major_num
= BITMAP_MAJOR_HI
;
78 memset(&info
, 0, sizeof(info
));
80 vers
= md_get_version(mdfd
);
82 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
86 memset(&inf
, 0, sizeof(inf
));
87 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
88 if (inf
.working_disks
!= 0) {
89 fprintf(stderr
, Name
": another array by this name"
90 " is already running.\n");
96 Name
": a RAID level is needed to create an array.\n");
99 if (raiddisks
< 4 && level
== 6) {
101 Name
": at least 4 raid-devices needed for level 6\n");
104 if (raiddisks
> 256 && level
== 6) {
106 Name
": no more than 256 raid-devices supported for level 6\n");
109 if (raiddisks
< 2 && level
>= 4) {
111 Name
": at least 2 raid-devices needed for level 4 or 5\n");
114 if (level
<= 0 && sparedisks
) {
116 Name
": This level does not support spare devices\n");
119 if (subdevs
> raiddisks
+sparedisks
) {
120 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
123 if (subdevs
< raiddisks
+sparedisks
) {
124 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
127 if (bitmap_file
&& level
<= 0) {
128 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
129 map_num(pers
, level
)?:"given");
133 /* now set some defaults */
136 default: /* no layout */
140 layout
= 0x102; /* near=2, far=1 */
143 Name
": layout defaults to n1\n");
147 layout
= map_name(r5layout
, "default");
150 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
153 layout
= map_name(faultylayout
, "default");
157 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
162 /* check layout fits in array*/
163 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
164 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
165 (layout
&255) * ((layout
>>8)&255));
175 case LEVEL_LINEAR
: /* linear */
179 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
184 case LEVEL_MULTIPATH
:
185 case LEVEL_CONTAINER
:
189 fprintf(stderr
, Name
": chunk size ignored for this level\n");
193 fprintf(stderr
, Name
": unknown level %d\n", level
);
197 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
198 chunk
, size
, NULL
, NULL
))
201 /* now look at the subdevs */
202 info
.array
.active_disks
= 0;
203 info
.array
.working_disks
= 0;
205 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
206 char *dname
= dv
->devname
;
207 unsigned long long freesize
;
209 if (strcasecmp(dname
, "missing")==0) {
210 if (first_missing
> dnum
)
211 first_missing
= dnum
;
212 if (second_missing
> dnum
&& dnum
> first_missing
)
213 second_missing
= dnum
;
217 info
.array
.working_disks
++;
218 if (dnum
< raiddisks
)
219 info
.array
.active_disks
++;
220 fd
= open(dname
, O_RDONLY
|O_EXCL
, 0);
222 fprintf(stderr
, Name
": Cannot open %s: %s\n",
223 dname
, strerror(errno
));
228 struct createinfo
*ci
= conf_get_create_info();
233 /* Need to choose a default metadata, which is different
234 * depending on geometry of array.
237 char *name
= "default";
238 for(i
=0; !st
&& superlist
[i
]; i
++) {
239 st
= superlist
[i
]->match_metadata_desc(name
);
240 if (st
&& !st
->ss
->validate_geometry
241 (st
, level
, layout
, raiddisks
,
242 chunk
, size
, dname
, &freesize
))
247 fprintf(stderr
, Name
": device %s not suitable "
248 "for any style of array\n",
252 if (st
->ss
->major
!= 0 ||
253 st
->minor_version
!= 90)
254 fprintf(stderr
, Name
": Defaulting to version"
259 if (!st
->ss
->validate_geometry(st
, level
, layout
,
265 Name
": %s is not suitable for "
273 freesize
/= 2; /* convert to K */
275 /* round to chunk size */
276 freesize
= freesize
& ~(chunk
-1);
279 if (size
&& freesize
< size
) {
280 fprintf(stderr
, Name
": %s is smaller that given size."
281 " %lluK < %lluK + metadata\n",
282 dname
, freesize
, size
);
287 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
291 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
295 if (runstop
!= 1 || verbose
>= 0) {
296 warn
|= check_ext2(fd
, dname
);
297 warn
|= check_reiser(fd
, dname
);
298 warn
|= check_raid(fd
, dname
);
303 fprintf(stderr
, Name
": create aborted\n");
307 if (mindisc
== NULL
) {
308 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
311 if (level
> 0 || level
== LEVEL_MULTIPATH
|| level
== LEVEL_FAULTY
) {
312 /* size is meaningful */
313 if (minsize
> 0x100000000ULL
&& st
->ss
->major
== 0) {
314 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
319 fprintf(stderr
, Name
": size set to %lluK\n", size
);
322 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
323 if (runstop
!= 1 || verbose
>= 0)
324 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
331 if (!ask("Continue creating array? ")) {
332 fprintf(stderr
, Name
": create aborted.\n");
337 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
341 /* If this is raid4/5, we want to configure the last active slot
342 * as missing, so that a reconstruct happens (faster than re-parity)
343 * FIX: Can we do this for raid6 as well?
345 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
349 insert_point
= raiddisks
-1;
351 info
.array
.active_disks
--;
358 /* For raid6, if creating with 1 missing drive, make a good drive
359 * into a spare, else the create will fail
361 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
362 second_missing
>= raiddisks
&& level
== 6) {
363 insert_point
= raiddisks
- 1;
364 if (insert_point
== first_missing
)
367 info
.array
.active_disks
--;
371 if (level
<= 0 && first_missing
!= subdevs
* 2) {
373 Name
": This level does not support missing devices\n");
377 /* Ok, lets try some ioctls */
379 info
.array
.level
= level
;
380 info
.array
.size
= size
;
381 info
.array
.raid_disks
= raiddisks
;
382 /* The kernel should *know* what md_minor we are dealing
383 * with, but it chooses to trust me instead. Sigh
385 info
.array
.md_minor
= 0;
386 if (fstat(mdfd
, &stb
)==0)
387 info
.array
.md_minor
= minor(stb
.st_rdev
);
388 info
.array
.not_persistent
= 0;
390 if ( ( (level
== 4 || level
== 5) &&
391 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
393 ( level
== 6 && (insert_point
< raiddisks
394 || second_missing
< raiddisks
))
398 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
400 info
.array
.state
= 0; /* not clean, but no errors */
403 /* for raid10, the bitmap size is the capacity of the array,
404 * which is array.size * raid_disks / ncopies;
405 * .. but convert to sectors.
407 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
408 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
409 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
411 bitmapsize
= (unsigned long long)size
* 2;
413 /* There is lots of redundancy in these disk counts,
414 * raid_disks is the most meaningful value
415 * it describes the geometry of the array
417 * nr_disks is total number of used slots.
418 * it should be raid_disks+spare_disks
419 * spare_disks is the number of extra disks present
421 * active_disks is the number of working disks in
422 * active slots. (With raid_disks)
423 * working_disks is the total number of working disks,
425 * failed_disks is the number of disks marked failed
427 * Ideally, the kernel would keep these (except raid_disks)
428 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
429 * So for now, we assume that all raid and spare
430 * devices will be given.
432 info
.array
.spare_disks
=sparedisks
;
433 info
.array
.failed_disks
=missing_disks
;
434 info
.array
.nr_disks
= info
.array
.working_disks
435 + info
.array
.failed_disks
;
436 info
.array
.layout
= layout
;
437 info
.array
.chunk_size
= chunk
*1024;
438 info
.array
.major_version
= st
->ss
->major
;
440 if (name
== NULL
|| *name
== 0) {
441 /* base name on mddev */
446 * /dev/md/home -> home
447 * /dev/mdhome -> home
449 name
= strrchr(mddev
, '/');
452 if (strncmp(name
, "md_d", 4)==0 &&
455 (name
-mddev
) == 5 /* /dev/ */)
457 else if (strncmp(name
, "md", 2)==0 &&
460 (name
-mddev
) == 5 /* /dev/ */)
464 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
467 if (bitmap_file
&& vers
< 9003) {
468 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
470 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
471 " between different architectured. Consider upgrading the Linux kernel.\n");
475 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
476 if ((vers
%100) < 2) {
477 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
480 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
482 bitmapsize
, 1, major_num
)) {
483 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
491 if ((vers
% 100) >= 1) { /* can use different versions */
492 mdu_array_info_t inf
;
493 memset(&inf
, 0, sizeof(inf
));
494 inf
.major_version
= st
->ss
->major
;
495 inf
.minor_version
= st
->minor_version
;
496 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
498 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
500 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
501 mddev
, strerror(errno
));
508 st
->ss
->uuid_from_super(st
, uuid
);
509 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
515 bitmap_fd
= open(bitmap_file
, O_RDWR
);
517 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
521 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
522 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
523 mddev
, strerror(errno
));
530 for (pass
=1; pass
<=2 ; pass
++) {
531 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
533 for (dnum
=0, dv
= devlist
; dv
;
534 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
538 info
.disk
.number
= dnum
;
539 if (dnum
== insert_point
) {
542 info
.disk
.raid_disk
= info
.disk
.number
;
543 if (info
.disk
.raid_disk
< raiddisks
)
544 info
.disk
.state
= (1<<MD_DISK_ACTIVE
) |
549 info
.disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
551 if (dnum
== insert_point
||
552 strcasecmp(dv
->devname
, "missing")==0) {
555 info
.disk
.state
= (1<<MD_DISK_FAULTY
);
557 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
, 0);
559 fprintf(stderr
, Name
": failed to open %s after earlier success - aborting\n",
564 info
.disk
.major
= major(stb
.st_rdev
);
565 info
.disk
.minor
= minor(stb
.st_rdev
);
566 remove_partitions(fd
);
571 st
->ss
->add_to_super(st
, &info
.disk
);
574 if (info
.disk
.state
== 1) break;
575 Kill(dv
->devname
, 0, 1); /* Just be sure it is clean */
576 Kill(dv
->devname
, 0, 1); /* and again, there could be two superblocks */
577 st
->ss
->write_init_super(st
, &info
.disk
,
580 if (ioctl(mdfd
, ADD_NEW_DISK
, &info
.disk
)) {
581 fprintf(stderr
, Name
": ADD_NEW_DISK for %s failed: %s\n",
582 dv
->devname
, strerror(errno
));
583 st
->ss
->free_super(st
);
589 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
592 st
->ss
->free_super(st
);
594 /* param is not actually used */
595 if (level
== LEVEL_CONTAINER
)
596 /* No need to start */
598 else if (runstop
== 1 || subdevs
>= raiddisks
) {
600 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
601 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
603 Manage_runstop(mddev
, mdfd
, -1, 0);
607 fprintf(stderr
, Name
": array %s started.\n", mddev
);
609 fprintf(stderr
, Name
": not starting array - not enough devices.\n");