]>
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;
75 int container_fd
= -1;
77 unsigned long long bitmapsize
;
79 struct mdinfo info
, *infos
;
81 unsigned long safe_mode_delay
= 0;
83 int major_num
= BITMAP_MAJOR_HI
;
85 memset(&info
, 0, sizeof(info
));
87 vers
= md_get_version(mdfd
);
89 fprintf(stderr
, Name
": Create requires md driver version 0.90.0 or later\n");
93 memset(&inf
, 0, sizeof(inf
));
94 ioctl(mdfd
, GET_ARRAY_INFO
, &inf
);
95 if (inf
.working_disks
!= 0) {
96 fprintf(stderr
, Name
": another array by this name"
97 " is already running.\n");
101 if (level
== UnSet
) {
102 /* "ddf" and "imsm" metadata only supports one level - should possibly
103 * push this into metadata handler??
105 if (st
&& (st
->ss
== &super_ddf
|| st
->ss
== &super_imsm
))
106 level
= LEVEL_CONTAINER
;
109 if (level
== UnSet
) {
111 Name
": a RAID level is needed to create an array.\n");
114 if (raiddisks
< 4 && level
== 6) {
116 Name
": at least 4 raid-devices needed for level 6\n");
119 if (raiddisks
> 256 && level
== 6) {
121 Name
": no more than 256 raid-devices supported for level 6\n");
124 if (raiddisks
< 2 && level
>= 4) {
126 Name
": at least 2 raid-devices needed for level 4 or 5\n");
129 if (level
<= 0 && sparedisks
) {
131 Name
": This level does not support spare devices\n");
135 if (subdevs
== 1 && strcmp(devlist
->devname
, "missing") != 0) {
136 /* If given a single device, it might be a container, and we can
137 * extract a device list from there
139 mdu_array_info_t inf
;
142 memset(&inf
, 0, sizeof(inf
));
143 fd
= open(devlist
->devname
, O_RDONLY
);
145 ioctl(fd
, GET_ARRAY_INFO
, &inf
) == 0 &&
146 inf
.raid_disks
== 0) {
147 /* yep, looks like a container */
149 rv
= st
->ss
->load_super(st
, fd
,
154 st
= guess_super(fd
);
155 if (st
&& !(rv
= st
->ss
->
165 if (have_container
) {
170 if (subdevs
> raiddisks
+sparedisks
) {
171 fprintf(stderr
, Name
": You have listed more devices (%d) than are in the array(%d)!\n", subdevs
, raiddisks
+sparedisks
);
174 if (!have_container
&& subdevs
< raiddisks
+sparedisks
) {
175 fprintf(stderr
, Name
": You haven't given enough devices (real or missing) to create this array\n");
178 if (bitmap_file
&& level
<= 0) {
179 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
180 map_num(pers
, level
)?:"given");
184 /* now set some defaults */
187 default: /* no layout */
191 layout
= 0x102; /* near=2, far=1 */
194 Name
": layout defaults to n1\n");
198 layout
= map_name(r5layout
, "default");
201 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
204 layout
= map_name(faultylayout
, "default");
208 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
213 /* check layout fits in array*/
214 if ((layout
&255) * ((layout
>>8)&255) > raiddisks
) {
215 fprintf(stderr
, Name
": that layout requires at least %d devices\n",
216 (layout
&255) * ((layout
>>8)&255));
226 case LEVEL_LINEAR
: /* linear */
230 fprintf(stderr
, Name
": chunk size defaults to 64K\n");
235 case LEVEL_MULTIPATH
:
236 case LEVEL_CONTAINER
:
240 fprintf(stderr
, Name
": chunk size ignored for this level\n");
244 fprintf(stderr
, Name
": unknown level %d\n", level
);
248 if (st
&& ! st
->ss
->validate_geometry(st
, level
, layout
, raiddisks
,
249 chunk
, size
, NULL
, NULL
, verbose
>=0))
252 /* now look at the subdevs */
253 info
.array
.active_disks
= 0;
254 info
.array
.working_disks
= 0;
256 for (dv
=devlist
; dv
; dv
=dv
->next
, dnum
++) {
257 char *dname
= dv
->devname
;
258 unsigned long long freesize
;
259 if (strcasecmp(dname
, "missing")==0) {
260 if (first_missing
> dnum
)
261 first_missing
= dnum
;
262 if (second_missing
> dnum
&& dnum
> first_missing
)
263 second_missing
= dnum
;
267 info
.array
.working_disks
++;
268 if (dnum
< raiddisks
)
269 info
.array
.active_disks
++;
271 struct createinfo
*ci
= conf_get_create_info();
276 /* Need to choose a default metadata, which is different
277 * depending on geometry of array.
280 char *name
= "default";
281 for(i
=0; !st
&& superlist
[i
]; i
++) {
282 st
= superlist
[i
]->match_metadata_desc(name
);
283 if (st
&& !st
->ss
->validate_geometry
284 (st
, level
, layout
, raiddisks
,
285 chunk
, size
, dname
, &freesize
,
291 fprintf(stderr
, Name
": device %s not suitable "
292 "for any style of array\n",
296 if (st
->ss
!= &super0
||
297 st
->minor_version
!= 90)
300 if (!st
->ss
->validate_geometry(st
, level
, layout
,
307 Name
": %s is not suitable for "
315 freesize
/= 2; /* convert to K */
317 /* round to chunk size */
318 freesize
= freesize
& ~(chunk
-1);
321 if (size
&& freesize
< size
) {
322 fprintf(stderr
, Name
": %s is smaller that given size."
323 " %lluK < %lluK + metadata\n",
324 dname
, freesize
, size
);
328 if (maxdisc
== NULL
|| (maxdisc
&& freesize
> maxsize
)) {
332 if (mindisc
==NULL
|| (mindisc
&& freesize
< minsize
)) {
336 if (runstop
!= 1 || verbose
>= 0) {
337 int fd
= open(dname
, O_RDONLY
);
339 fprintf(stderr
, Name
": Cannot open %s: %s\n",
340 dname
, strerror(errno
));
344 warn
|= check_ext2(fd
, dname
);
345 warn
|= check_reiser(fd
, dname
);
346 warn
|= check_raid(fd
, dname
);
351 fprintf(stderr
, Name
": create aborted\n");
355 if (mindisc
== NULL
&& !have_container
) {
356 fprintf(stderr
, Name
": no size and no drives given - aborting create.\n");
359 if (level
> 0 || level
== LEVEL_MULTIPATH
360 || level
== LEVEL_FAULTY
361 || st
->ss
->external
) {
362 /* size is meaningful */
363 if (!st
->ss
->validate_geometry(st
, level
, layout
,
367 fprintf(stderr
, Name
": devices too large for RAID level %d\n", level
);
372 fprintf(stderr
, Name
": size set to %lluK\n", size
);
375 if (level
> 0 && ((maxsize
-size
)*100 > maxsize
)) {
376 if (runstop
!= 1 || verbose
>= 0)
377 fprintf(stderr
, Name
": largest drive (%s) exceed size (%lluK) by more than 1%%\n",
384 if (!ask("Continue creating array? ")) {
385 fprintf(stderr
, Name
": create aborted.\n");
390 fprintf(stderr
, Name
": creation continuing despite oddities due to --run\n");
394 /* If this is raid4/5, we want to configure the last active slot
395 * as missing, so that a reconstruct happens (faster than re-parity)
396 * FIX: Can we do this for raid6 as well?
398 if (assume_clean
==0 && force
== 0 && first_missing
>= raiddisks
) {
402 insert_point
= raiddisks
-1;
404 info
.array
.active_disks
--;
411 /* For raid6, if creating with 1 missing drive, make a good drive
412 * into a spare, else the create will fail
414 if (assume_clean
== 0 && force
== 0 && first_missing
< raiddisks
&&
415 second_missing
>= raiddisks
&& level
== 6) {
416 insert_point
= raiddisks
- 1;
417 if (insert_point
== first_missing
)
420 info
.array
.active_disks
--;
424 if (level
<= 0 && first_missing
< subdevs
* 2) {
426 Name
": This level does not support missing devices\n");
430 /* Ok, lets try some ioctls */
432 info
.array
.level
= level
;
433 info
.array
.size
= size
;
434 info
.array
.raid_disks
= raiddisks
;
435 /* The kernel should *know* what md_minor we are dealing
436 * with, but it chooses to trust me instead. Sigh
438 info
.array
.md_minor
= 0;
439 if (fstat(mdfd
, &stb
)==0)
440 info
.array
.md_minor
= minor(stb
.st_rdev
);
441 info
.array
.not_persistent
= 0;
443 if ( ( (level
== 4 || level
== 5) &&
444 (insert_point
< raiddisks
|| first_missing
< raiddisks
) )
446 ( level
== 6 && (insert_point
< raiddisks
447 || second_missing
< raiddisks
))
453 info
.array
.state
= 1; /* clean, but one+ drive will be missing*/
454 info
.resync_start
= ~0ULL;
456 info
.array
.state
= 0; /* not clean, but no errors */
457 info
.resync_start
= 0;
460 /* for raid10, the bitmap size is the capacity of the array,
461 * which is array.size * raid_disks / ncopies;
462 * .. but convert to sectors.
464 int ncopies
= ((layout
>>8) & 255) * (layout
& 255);
465 bitmapsize
= (unsigned long long)size
* raiddisks
/ ncopies
* 2;
466 /* printf("bms=%llu as=%d rd=%d nc=%d\n", bitmapsize, size, raiddisks, ncopies);*/
468 bitmapsize
= (unsigned long long)size
* 2;
470 /* There is lots of redundancy in these disk counts,
471 * raid_disks is the most meaningful value
472 * it describes the geometry of the array
474 * nr_disks is total number of used slots.
475 * it should be raid_disks+spare_disks
476 * spare_disks is the number of extra disks present
478 * active_disks is the number of working disks in
479 * active slots. (With raid_disks)
480 * working_disks is the total number of working disks,
482 * failed_disks is the number of disks marked failed
484 * Ideally, the kernel would keep these (except raid_disks)
485 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
486 * So for now, we assume that all raid and spare
487 * devices will be given.
489 info
.array
.spare_disks
=sparedisks
;
490 info
.array
.failed_disks
=missing_disks
;
491 info
.array
.nr_disks
= info
.array
.working_disks
492 + info
.array
.failed_disks
;
493 info
.array
.layout
= layout
;
494 info
.array
.chunk_size
= chunk
*1024;
496 if (name
== NULL
|| *name
== 0) {
497 /* base name on mddev */
502 * /dev/md/home -> home
503 * /dev/mdhome -> home
505 name
= strrchr(mddev
, '/');
508 if (strncmp(name
, "md_d", 4)==0 &&
511 (name
-mddev
) == 5 /* /dev/ */)
513 else if (strncmp(name
, "md", 2)==0 &&
516 (name
-mddev
) == 5 /* /dev/ */)
520 if (!st
->ss
->init_super(st
, &info
.array
, size
, name
, homehost
, uuid
))
523 total_slots
= info
.array
.nr_disks
;
524 st
->ss
->getinfo_super(st
, &info
);
526 if (did_default
&& verbose
>= 0) {
527 if (is_subarray(info
.text_version
)) {
528 int dnum
= devname2devnum(info
.text_version
+1);
530 int mdp
= get_mdp_major();
533 path
= map_dev(MD_MAJOR
, dnum
, 1);
535 path
= map_dev(mdp
, (-1-dnum
)<< 6, 1);
537 mdi
= sysfs_read(-1, dnum
, GET_VERSION
);
539 fprintf(stderr
, Name
": Creating array inside "
541 mdi
?mdi
->text_version
:"managed", path
);
544 fprintf(stderr
, Name
": Defaulting to version"
545 " %s metadata\n", info
.text_version
);
548 if (bitmap_file
&& vers
< 9003) {
549 major_num
= BITMAP_MAJOR_HOSTENDIAN
;
551 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
552 " between different architectured. Consider upgrading the Linux kernel.\n");
556 if (bitmap_file
&& strcmp(bitmap_file
, "internal")==0) {
557 if ((vers
%100) < 2) {
558 fprintf(stderr
, Name
": internal bitmaps not supported by this kernel.\n");
561 if (!st
->ss
->add_internal_bitmap(st
, &bitmap_chunk
,
563 bitmapsize
, 1, major_num
)) {
564 fprintf(stderr
, Name
": Given bitmap chunk size not supported.\n");
571 sra
= sysfs_read(mdfd
, 0, 0);
573 if (st
->ss
->external
) {
575 strcat(strcpy(ver
, "external:"),
577 if (st
->ss
->external
&& st
->subarray
[0]) {
580 /* When creating a member, we need to be careful
581 * to negotiate with mdmon properly.
582 * If it is already running, we cannot write to
583 * the devices and must ask it to do that part.
584 * If it isn't running, we write to the devices,
586 * We hold an exclusive open on the container
587 * device to make sure mdmon doesn't exit after
588 * we checked that it is running.
590 * For now, fail if it is already running.
592 container_fd
= open_dev_excl(st
->container_dev
);
593 if (container_fd
< 0) {
594 fprintf(stderr
, Name
": Cannot get exclusive "
595 "open on container - weird.\n");
598 if (mdmon_running(st
->container_dev
)) {
600 fprintf(stderr
, Name
": reusing mdmon "
602 devnum2devname(st
->container_dev
));
603 st
->update_tail
= &st
->updates
;
607 if ((vers
% 100) < 2 ||
609 sysfs_set_str(sra
, NULL
, "metadata_version",
611 fprintf(stderr
, Name
": This kernel does not "
612 "support external metadata.\n");
615 rv
= sysfs_set_array(sra
, &info
);
616 } else if ((vers
% 100) >= 1) { /* can use different versions */
617 mdu_array_info_t inf
;
618 memset(&inf
, 0, sizeof(inf
));
619 inf
.major_version
= info
.array
.major_version
;
620 inf
.minor_version
= info
.array
.minor_version
;
621 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, &inf
);
623 rv
= ioctl(mdfd
, SET_ARRAY_INFO
, NULL
);
625 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
626 mddev
, strerror(errno
));
633 st
->ss
->uuid_from_super(st
, uuid
);
634 if (CreateBitmap(bitmap_file
, force
, (char*)uuid
, bitmap_chunk
,
640 bitmap_fd
= open(bitmap_file
, O_RDWR
);
642 fprintf(stderr
, Name
": weird: %s cannot be openned\n",
646 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
647 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
648 mddev
, strerror(errno
));
653 infos
= malloc(sizeof(*infos
) * total_slots
);
655 for (pass
=1; pass
<=2 ; pass
++) {
656 mddev_dev_t moved_disk
= NULL
; /* the disk that was moved out of the insert point */
658 for (dnum
=0, dv
= devlist
; dv
;
659 dv
=(dv
->next
)?(dv
->next
):moved_disk
, dnum
++) {
662 struct mdinfo
*inf
= &infos
[dnum
];
664 if (dnum
>= total_slots
)
666 if (dnum
== insert_point
) {
669 if (dnum
== insert_point
||
670 strcasecmp(dv
->devname
, "missing")==0)
677 inf
->disk
.number
= dnum
;
678 inf
->disk
.raid_disk
= dnum
;
679 if (inf
->disk
.raid_disk
< raiddisks
)
680 inf
->disk
.state
= (1<<MD_DISK_ACTIVE
) |
686 inf
->disk
.state
|= (1<<MD_DISK_WRITEMOSTLY
);
688 if (st
->ss
->external
&& st
->subarray
[0])
689 fd
= open(dv
->devname
, O_RDWR
);
691 fd
= open(dv
->devname
, O_RDWR
|O_EXCL
);
694 fprintf(stderr
, Name
": failed to open %s "
695 "after earlier success - aborting\n",
700 inf
->disk
.major
= major(stb
.st_rdev
);
701 inf
->disk
.minor
= minor(stb
.st_rdev
);
703 remove_partitions(fd
);
704 st
->ss
->add_to_super(st
, &inf
->disk
,
706 st
->ss
->getinfo_super(st
, inf
);
707 safe_mode_delay
= inf
->safe_mode_delay
;
709 /* getinfo_super might have lost these ... */
710 inf
->disk
.major
= major(stb
.st_rdev
);
711 inf
->disk
.minor
= minor(stb
.st_rdev
);
717 rv
= add_disk(mdfd
, st
, sra
, inf
);
721 Name
": ADD_NEW_DISK for %s "
723 dv
->devname
, strerror(errno
));
724 st
->ss
->free_super(st
);
729 if (dv
== moved_disk
&& dnum
!= insert_point
) break;
732 st
->ss
->write_init_super(st
);
733 flush_metadata_updates(st
);
737 st
->ss
->free_super(st
);
739 /* param is not actually used */
740 if (level
== LEVEL_CONTAINER
)
741 /* No need to start */
743 else if (runstop
== 1 || subdevs
>= raiddisks
) {
744 if (st
->ss
->external
) {
747 case LEVEL_MULTIPATH
:
749 sysfs_set_str(sra
, NULL
, "array_state",
754 sysfs_set_str(sra
, NULL
, "array_state",
758 sysfs_set_safemode(sra
, safe_mode_delay
);
761 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
762 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
764 Manage_runstop(mddev
, mdfd
, -1, 0);
769 fprintf(stderr
, Name
": array %s started.\n", mddev
);
770 if (st
->ss
->external
&& st
->subarray
[0]) {
772 start_mdmon(st
->container_dev
);
774 ping_monitor(devnum2devname(st
->container_dev
));
778 fprintf(stderr
, Name
": not starting array - not enough devices.\n");