]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - Build.c
2 * mdadm - manage Linux "md" devices aka RAID arrays.
4 * Copyright (C) 2001-2009 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@suse.de>
27 #define REGISTER_DEV _IO (MD_MAJOR, 1)
28 #define START_MD _IO (MD_MAJOR, 2)
29 #define STOP_MD _IO (MD_MAJOR, 3)
31 int Build(char *mddev
, int chunk
, int level
, int layout
,
32 int raiddisks
, struct mddev_dev
*devlist
, int assume_clean
,
33 char *bitmap_file
, int bitmap_chunk
, int write_behind
,
34 int delay
, int verbose
, int autof
, unsigned long long size
)
36 /* Build a linear or raid0 arrays without superblocks
37 * We cannot really do any checks, we just do it.
38 * For md_version < 0.90.0, we call REGISTER_DEV
39 * with the device numbers, and then
40 * START_MD giving the "geometry"
41 * geometry is 0xpp00cc
42 * where pp is personality: 1==linear, 2=raid0
43 * cc = chunk size factor: 0==4k, 1==8k etc.
45 * For md_version >= 0.90.0 we call
46 * SET_ARRAY_INFO, ADD_NEW_DISK, RUN_ARRAY
52 int subdevs
= 0, missing_disks
= 0;
55 unsigned long long bitmapsize
;
57 char chosen_name
[1024];
58 int uuid
[4] = {0,0,0,0};
59 struct map_ent
*map
= NULL
;
61 /* scan all devices, make sure they really are block devices */
62 for (dv
= devlist
; dv
; dv
=dv
->next
) {
64 if (strcmp("missing", dv
->devname
) == 0) {
68 if (stat(dv
->devname
, &stb
)) {
69 fprintf(stderr
, Name
": Cannot find %s: %s\n",
70 dv
->devname
, strerror(errno
));
73 if ((stb
.st_mode
& S_IFMT
) != S_IFBLK
) {
74 fprintf(stderr
, Name
": %s is not a block device.\n",
80 if (raiddisks
!= subdevs
) {
81 fprintf(stderr
, Name
": requested %d devices in array but listed %d\n",
88 default: /* no layout */
92 layout
= 0x102; /* near=2, far=1 */
95 Name
": layout defaults to n1\n");
99 layout
= map_name(r5layout
, "default");
102 Name
": layout defaults to %s\n", map_num(r5layout
, layout
));
105 layout
= map_name(faultylayout
, "default");
109 Name
": layout defaults to %s\n", map_num(faultylayout
, layout
));
113 /* We need to create the device. It can have no name. */
115 mdfd
= create_mddev(mddev
, NULL
, autof
, LOCAL
,
123 map_update(&map
, fd2devnum(mdfd
), "none", uuid
, chosen_name
);
126 vers
= md_get_version(mdfd
);
128 /* looks Ok, go for it */
130 mdu_array_info_t array
;
133 array
.nr_disks
= raiddisks
;
134 array
.raid_disks
= raiddisks
;
136 if (fstat(mdfd
, &stb
)==0)
137 array
.md_minor
= minor(stb
.st_rdev
);
138 array
.not_persistent
= 1;
139 array
.state
= 0; /* not clean, but no errors */
142 array
.active_disks
= raiddisks
- missing_disks
;
143 array
.working_disks
= raiddisks
- missing_disks
;
144 array
.spare_disks
= 0;
145 array
.failed_disks
= missing_disks
;
146 if (chunk
== 0 && (level
==0 || level
==LEVEL_LINEAR
))
148 array
.chunk_size
= chunk
*1024;
149 array
.layout
= layout
;
150 if (ioctl(mdfd
, SET_ARRAY_INFO
, &array
)) {
151 fprintf(stderr
, Name
": SET_ARRAY_INFO failed for %s: %s\n",
152 mddev
, strerror(errno
));
155 } else if (bitmap_file
) {
156 fprintf(stderr
, Name
": bitmaps not supported with this kernel\n");
160 if (bitmap_file
&& level
<= 0) {
161 fprintf(stderr
, Name
": bitmaps not meaningful with level %s\n",
162 map_num(pers
, level
)?:"given");
165 /* now add the devices */
166 for ((i
=0), (dv
= devlist
) ; dv
; i
++, dv
=dv
->next
) {
167 unsigned long long dsize
;
169 if (strcmp("missing", dv
->devname
) == 0)
171 if (stat(dv
->devname
, &stb
)) {
172 fprintf(stderr
, Name
": Weird: %s has disappeared.\n",
176 if ((stb
.st_mode
& S_IFMT
)!= S_IFBLK
) {
177 fprintf(stderr
, Name
": Wierd: %s is no longer a block device.\n",
181 fd
= open(dv
->devname
, O_RDONLY
|O_EXCL
);
183 fprintf(stderr
, Name
": Cannot open %s: %s\n",
184 dv
->devname
, strerror(errno
));
187 if (get_dev_size(fd
, NULL
, &dsize
) &&
188 (size
== 0 || dsize
< size
))
192 mdu_disk_info_t disk
;
195 disk
.state
= (1<<MD_DISK_SYNC
) | (1<<MD_DISK_ACTIVE
);
196 if (dv
->writemostly
== 1)
197 disk
.state
|= 1<<MD_DISK_WRITEMOSTLY
;
198 disk
.major
= major(stb
.st_rdev
);
199 disk
.minor
= minor(stb
.st_rdev
);
200 if (ioctl(mdfd
, ADD_NEW_DISK
, &disk
)) {
201 fprintf(stderr
, Name
": ADD_NEW_DISK failed for %s: %s\n",
202 dv
->devname
, strerror(errno
));
206 if (ioctl(mdfd
, REGISTER_DEV
, &stb
.st_rdev
)) {
207 fprintf(stderr
, Name
": REGISTER_DEV failed for %s: %s.\n",
208 dv
->devname
, strerror(errno
));
213 /* now to start it */
215 mdu_param_t param
; /* not used by syscall */
217 bitmap_fd
= open(bitmap_file
, O_RDWR
);
219 int major
= BITMAP_MAJOR_HI
;
221 if (bitmap_chunk
== UnSet
) {
222 fprintf(stderr
, Name
": %s cannot be openned.",
228 major
= BITMAP_MAJOR_HOSTENDIAN
;
230 fprintf(stderr
, Name
": Warning - bitmaps created on this kernel are not portable\n"
231 " between different architectures. Consider upgrading the Linux kernel.\n");
234 bitmapsize
= size
>>9; /* FIXME wrong for RAID10 */
235 if (CreateBitmap(bitmap_file
, 1, NULL
, bitmap_chunk
,
236 delay
, write_behind
, bitmapsize
, major
)) {
239 bitmap_fd
= open(bitmap_file
, O_RDWR
);
241 fprintf(stderr
, Name
": %s cannot be openned.",
246 if (bitmap_fd
>= 0) {
247 if (ioctl(mdfd
, SET_BITMAP_FILE
, bitmap_fd
) < 0) {
248 fprintf(stderr
, Name
": Cannot set bitmap file for %s: %s\n",
249 mddev
, strerror(errno
));
254 if (ioctl(mdfd
, RUN_ARRAY
, ¶m
)) {
255 fprintf(stderr
, Name
": RUN_ARRAY failed: %s\n",
262 while (chunk
> 4096) {
268 else chunk
|= 0x10000;
269 if (ioctl(mdfd
, START_MD
, arg
)) {
270 fprintf(stderr
, Name
": START_MD failed: %s\n",
276 fprintf(stderr
, Name
": array %s built and started.\n",
278 wait_for(mddev
, mdfd
);
284 ioctl(mdfd
, STOP_ARRAY
, 0);
286 ioctl(mdfd
, STOP_MD
, 0);