]>
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 */
74 int have_container
= 0;
77 unsigned long long bitmapsize
;
79 struct mdinfo info
, *infos
;
82 int major_num
= BITMAP_MAJOR_HI
;
84 memset(&info
, 0, sizeof(info
));
86 vers
= md_get_version(mdfd
);
88 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
92 memset(&inf
, 0, sizeof(inf
));
93 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
94 if (inf
.working_disks
!= 0) {
95 fprintf(stderr
, Name
": another array by this name"
96 " is already running.\n");
100 if (level
== UnSet
) {
101 /* "ddf" and "imsm" metadata only supports one level - should possibly
102 * push this into metadata handler??
104 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
105 level
= LEVEL_CONTAINER
;
108 if (level
== UnSet
) {
110 Name
": a RAID level is needed to create an array.\n");
113 if (raiddisks
< 4 && level
== 6) {
115 Name
": at least 4 raid-devices needed for level 6\n");
118 if (raiddisks
> 256 && level
== 6) {
120 Name
": no more than 256 raid-devices supported for level 6\n");
123 if (raiddisks
< 2 && level
>= 4) {
125 Name
": at least 2 raid-devices needed for level 4 or 5\n");
128 if (level
<= 0 && sparedisks
) {
130 Name
": This level does not support spare devices\n");
134 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
135 /* If given a single device, it might be a container, and we can
136 * extract a device list from there
138 mdu_array_info_t inf
;
141 memset(&inf
, 0, sizeof(inf
));
142 fd
= open(devlist
->devname
, O_RDONLY
, 0);
144 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
145 inf
.raid_disks
== 0) {
146 /* yep, looks like a container */
148 rv
= st
->ss
->load_super(st
, fd
,
153 st
= guess_super(fd
);
154 if (st
&& !(rv
= st
->ss
->
164 if (have_container
) {
169 if (subdevs
> raiddisks
+sparedisks
) {
170 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
173 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
174 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
177 if (bitmap_file
&& level
<= 0) {
178 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
179 map_num(pers
, level
)?:"given");
183 /* now set some defaults */
186 default: /* no layout */
190 layout
= 0x102; /* near=2, far=1 */
193 Name
": layout defaults to n1\n");
197 layout
= map_name(r5layout
, "default");
200 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
203 layout
= map_name(faultylayout
, "default");
207 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
212 /* check layout fits in array*/
213 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
214 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
215 (layout
&255) * ((layout
>>8)&255));
225 case LEVEL_LINEAR
: /* linear */
229 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
234 case LEVEL_MULTIPATH
:
235 case LEVEL_CONTAINER
:
239 fprintf(stderr
, Name
": chunk size ignored for this level\n");
243 fprintf(stderr
, Name
": unknown level %d\n", level
);
247 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
248 chunk
, size
, NULL
, NULL
, verbose
>=0))
251 /* now look at the subdevs */
252 info
.array
.active_disks
= 0;
253 info
.array
.working_disks
= 0;
255 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
256 char *dname
= dv
->devname
;
257 unsigned long long freesize
;
258 if (strcasecmp(dname
, "missing")==0) {
259 if (first_missing
> dnum
)
260 first_missing
= dnum
;
261 if (second_missing
> dnum
&& dnum
> first_missing
)
262 second_missing
= dnum
;
266 info
.array
.working_disks
++;
267 if (dnum
< raiddisks
)
268 info
.array
.active_disks
++;
270 struct createinfo
*ci
= conf_get_create_info();
275 /* Need to choose a default metadata, which is different
276 * depending on geometry of array.
279 char *name
= "default";
280 for(i
=0; !st
&& superlist
[i
]; i
++) {
281 st
= superlist
[i
]->match_metadata_desc(name
);
282 if (st
&& !st
->ss
->validate_geometry
283 (st
, level
, layout
, raiddisks
,
284 chunk
, size
, dname
, &freesize
,
290 fprintf(stderr
, Name
": device %s not suitable "
291 "for any style of array\n",
295 if (st
->ss
!= &super0
||
296 st
->minor_version
!= 90)
299 if (!st
->ss
->validate_geometry(st
, level
, layout
,
306 Name
": %s is not suitable for "
314 freesize
/= 2; /* convert to K */
316 /* round to chunk size */
317 freesize
= freesize
& ~(chunk
-1);
320 if (size
&& freesize
< size
) {
321 fprintf(stderr
, Name
": %s is smaller that given size."
322 " %lluK < %lluK + metadata\n",
323 dname
, freesize
, size
);
327 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
331 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
335 if (runstop
!= 1 || verbose
>= 0) {
336 int fd
= open(dname
, O_RDONLY
, 0);
338 fprintf(stderr
, Name
": Cannot open %s: %s\n",
339 dname
, strerror(errno
));
343 warn
|= check_ext2(fd
, dname
);
344 warn
|= check_reiser(fd
, dname
);
345 warn
|= check_raid(fd
, dname
);
350 fprintf(stderr
, Name
": create aborted\n");
354 if (mindisc
== NULL
&& !have_container
) {
355 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
358 if (level
> 0 || level
== LEVEL_MULTIPATH
359 || level
== LEVEL_FAULTY
360 || st
->ss
->external
) {
361 /* size is meaningful */
362 if (!st
->ss
->validate_geometry(st
, level
, layout
,
366 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
371 fprintf(stderr
, Name
": size set to %lluK\n", size
);
374 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
375 if (runstop
!= 1 || verbose
>= 0)
376 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
383 if (!ask("Continue creating array? ")) {
384 fprintf(stderr
, Name
": create aborted.\n");
389 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
393 /* If this is raid4/5, we want to configure the last active slot
394 * as missing, so that a reconstruct happens (faster than re-parity)
395 * FIX: Can we do this for raid6 as well?
397 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
401 insert_point
= raiddisks
-1;
403 info
.array
.active_disks
--;
410 /* For raid6, if creating with 1 missing drive, make a good drive
411 * into a spare, else the create will fail
413 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
414 second_missing
>= raiddisks
&& level
== 6) {
415 insert_point
= raiddisks
- 1;
416 if (insert_point
== first_missing
)
419 info
.array
.active_disks
--;
423 if (level
<= 0 && first_missing
< subdevs
* 2) {
425 Name
": This level does not support missing devices\n");
429 /* Ok, lets try some ioctls */
431 info
.array
.level
= level
;
432 info
.array
.size
= size
;
433 info
.array
.raid_disks
= raiddisks
;
434 /* The kernel should *know* what md_minor we are dealing
435 * with, but it chooses to trust me instead. Sigh
437 info
.array
.md_minor
= 0;
438 if (fstat(mdfd
, &stb
)==0)
439 info
.array
.md_minor
= minor(stb
.st_rdev
);
440 info
.array
.not_persistent
= 0;
442 if ( ( (level
== 4 || level
== 5) &&
443 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
445 ( level
== 6 && (insert_point
< raiddisks
446 || second_missing
< raiddisks
))
452 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
453 info
.resync_start
= ~0ULL;
455 info
.array
.state
= 0; /* not clean, but no errors */
456 info
.resync_start
= 0;
459 /* for raid10, the bitmap size is the capacity of the array,
460 * which is array.size * raid_disks / ncopies;
461 * .. but convert to sectors.
463 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
464 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
465 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
467 bitmapsize
= (unsigned long long)size
* 2;
469 /* There is lots of redundancy in these disk counts,
470 * raid_disks is the most meaningful value
471 * it describes the geometry of the array
473 * nr_disks is total number of used slots.
474 * it should be raid_disks+spare_disks
475 * spare_disks is the number of extra disks present
477 * active_disks is the number of working disks in
478 * active slots. (With raid_disks)
479 * working_disks is the total number of working disks,
481 * failed_disks is the number of disks marked failed
483 * Ideally, the kernel would keep these (except raid_disks)
484 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
485 * So for now, we assume that all raid and spare
486 * devices will be given.
488 info
.array
.spare_disks
=sparedisks
;
489 info
.array
.failed_disks
=missing_disks
;
490 info
.array
.nr_disks
= info
.array
.working_disks
491 + info
.array
.failed_disks
;
492 info
.array
.layout
= layout
;
493 info
.array
.chunk_size
= chunk
*1024;
495 if (name
== NULL
|| *name
== 0) {
496 /* base name on mddev */
501 * /dev/md/home -> home
502 * /dev/mdhome -> home
504 name
= strrchr(mddev
, '/');
507 if (strncmp(name
, "md_d", 4)==0 &&
510 (name
-mddev
) == 5 /* /dev/ */)
512 else if (strncmp(name
, "md", 2)==0 &&
515 (name
-mddev
) == 5 /* /dev/ */)
519 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
522 total_slots
= info
.array
.nr_disks
;
523 st
->ss
->getinfo_super(st
, &info
);
525 if (did_default
&& verbose
>= 0) {
526 if (info
.text_version
[0] == '/') {
527 int dnum
= devname2devnum(info
.text_version
+1);
529 int mdp
= get_mdp_major();
532 path
= map_dev(MD_MAJOR
, dnum
, 1);
534 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
536 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
538 fprintf(stderr
, Name
": Creating array inside "
540 mdi
?mdi
->text_version
:"managed", path
);
543 fprintf(stderr
, Name
": Defaulting to version"
544 " %s metadata\n", info
.text_version
);
547 if (bitmap_file
&& vers
< 9003) {
548 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
550 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
551 " between different architectured. Consider upgrading the Linux kernel.\n");
555 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
556 if ((vers
%100) < 2) {
557 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
560 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
562 bitmapsize
, 1, major_num
)) {
563 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
570 sra
= sysfs_read(mdfd
, 0, 0);
572 if (st
->ss
->external
) {
574 strcat(strcpy(ver
, "external:"),
576 if (st
->ss
->external
&& st
->subarray
[0]) {
579 /* When creating a member, we need to be careful
580 * to negotiate with mdmon properly.
581 * If it is already running, we cannot write to
582 * the devices and must ask it to do that part.
583 * If it isn't running, we write to the devices,
585 * We hold an exclusive open on the container
586 * device to make sure mdmon doesn't exit after
587 * we checked that it is running.
589 * For now, fail if it is already running.
591 container_fd
= open_dev_excl(st
->container_dev
);
592 if (container_fd
< 0) {
593 fprintf(stderr
, Name
": Cannot get exclusive "
594 "open on container - weird.\n");
597 if (mdmon_running(st
->container_dev
)) {
599 fprintf(stderr
, Name
": reusing mdmon "
601 devnum2devname(st
->container_dev
));
602 st
->update_tail
= &st
->updates
;
606 if ((vers
% 100) < 2 ||
608 sysfs_set_str(sra
, NULL
, "metadata_version",
610 fprintf(stderr
, Name
": This kernel does not "
611 "support external metadata.\n");
614 rv
= sysfs_set_array(sra
, &info
);
615 } else if ((vers
% 100) >= 1) { /* can use different versions */
616 mdu_array_info_t inf
;
617 memset(&inf
, 0, sizeof(inf
));
618 inf
.major_version
= info
.array
.major_version
;
619 inf
.minor_version
= info
.array
.minor_version
;
620 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
622 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
624 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
625 mddev
, strerror(errno
));
632 st
->ss
->uuid_from_super(st
, uuid
);
633 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
639 bitmap_fd
= open(bitmap_file
, O_RDWR
);
641 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
645 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
646 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
647 mddev
, strerror(errno
));
652 infos
= malloc(sizeof(*infos
) * total_slots
);
654 for (pass
=1; pass
<=2 ; pass
++) {
655 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
657 for (dnum
=0, dv
= devlist
; dv
;
658 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
661 struct mdinfo
*inf
= &infos
[dnum
];
663 if (dnum
>= total_slots
)
665 if (dnum
== insert_point
) {
668 if (dnum
== insert_point
||
669 strcasecmp(dv
->devname
, "missing")==0)
676 inf
->disk
.number
= dnum
;
677 inf
->disk
.raid_disk
= dnum
;
678 if (inf
->disk
.raid_disk
< raiddisks
)
679 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
685 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
687 if (st
->ss
->external
&& st
->subarray
[0])
688 fd
= open(dv
->devname
, O_RDWR
, 0);
690 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
,0);
693 fprintf(stderr
, Name
": failed to open %s "
694 "after earlier success - aborting\n",
699 inf
->disk
.major
= major(stb
.st_rdev
);
700 inf
->disk
.minor
= minor(stb
.st_rdev
);
702 remove_partitions(fd
);
703 st
->ss
->add_to_super(st
, &inf
->disk
,
705 st
->ss
->getinfo_super(st
, inf
);
707 /* getinfo_super might have lost these ... */
708 inf
->disk
.major
= major(stb
.st_rdev
);
709 inf
->disk
.minor
= minor(stb
.st_rdev
);
715 if (st
->ss
->external
)
716 rv
= sysfs_add_disk(sra
, inf
);
718 rv
= ioctl(mdfd
, ADD_NEW_DISK
,
723 Name
": ADD_NEW_DISK for %s "
725 dv
->devname
, strerror(errno
));
726 st
->ss
->free_super(st
);
731 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
734 st
->ss
->write_init_super(st
);
735 flush_metadata_updates(st
);
739 st
->ss
->free_super(st
);
741 /* param is not actually used */
742 if (level
== LEVEL_CONTAINER
)
743 /* No need to start */
745 else if (runstop
== 1 || subdevs
>= raiddisks
) {
746 if (st
->ss
->external
) {
749 case LEVEL_MULTIPATH
:
751 sysfs_set_str(sra
, NULL
, "array_state",
756 sysfs_set_str(sra
, NULL
, "array_state",
762 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
763 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
765 Manage_runstop(mddev
, mdfd
, -1, 0);
770 fprintf(stderr
, Name
": array %s started.\n", mddev
);
771 if (st
->ss
->external
&& st
->subarray
[0]) {
773 start_mdmon(st
->container_dev
);
775 ping_monitor(devnum2devname(st
->container_dev
));
779 fprintf(stderr
, Name
": not starting array - not enough devices.\n");