]> git.ipfire.org Git - thirdparty/mdadm.git/blob - Create.c
c7d35979a443c22f625069869d68ab9cddba1266
[thirdparty/mdadm.git] / Create.c
1 /*
2 * mdctl - manage Linux "md" devices aka RAID arrays.
3 *
4 * Copyright (C) 2001 Neil Brown <neilb@cse.unsw.edu.au>
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
30 #include "mdctl.h"
31 #include "md_u.h"
32 #include "md_p.h"
33
34 int Create(char *mddev, int mdfd,
35 int chunk, int level, int layout, int size, int raiddisks, int sparedisks,
36 int subdevs, char *subdev[],
37 int runstop, int verbose, int force)
38 {
39 /*
40 * Create a new raid array.
41 *
42 * First check that necessary details are available
43 * (i.e. level, raid-disks)
44 *
45 * Then check each disk to see what might be on it
46 * and report anything interesting.
47 *
48 * If anything looks odd, and runstop not set,
49 * abort.
50 *
51 * SET_ARRAY_INFO and ADD_NEW_DISK, and
52 * if runstop==run, or raiddisks diskswere used,
53 * RUN_ARRAY
54 */
55 int minsize, maxsize;
56 int maxdisc= -1, mindisc = -1;
57 int i;
58 int fail=0, warn=0;
59 struct stat stb;
60 int first_missing = MD_SB_DISKS*2;
61 int missing_disks = 0;
62 int insert_point = MD_SB_DISKS*2; /* where to insert a missing drive */
63
64 mdu_array_info_t array;
65
66
67 if (md_get_version(mdfd) < 9000) {
68 fprintf(stderr, Name ": Create requires md driver verison 0.90.0 or later\n");
69 return 1;
70 }
71 if (level == -10) {
72 fprintf(stderr,
73 Name ": a RAID level is needed to create an array.\n");
74 return 1;
75 }
76 if (raiddisks < 1) {
77 fprintf(stderr,
78 Name ": a number of --raid-disks must be given to create an array\n");
79 return 1;
80 }
81 if (raiddisks < 2 && level >= 4) {
82 fprintf(stderr,
83 Name ": atleast 2 raid-disks needed for level 4 or 5\n");
84 return 1;
85 }
86 if (raiddisks+sparedisks > MD_SB_DISKS) {
87 fprintf(stderr,
88 Name ": too many discs requested: %d+%d > %d\n",
89 raiddisks, sparedisks, MD_SB_DISKS);
90 return 1;
91 }
92 if (subdevs > raiddisks+sparedisks) {
93 fprintf(stderr, Name ": You have listed more disks (%d) than are in the array(%d)!\n", subdevs, raiddisks+sparedisks);
94 return 1;
95 }
96 if (subdevs < raiddisks) {
97 fprintf(stderr, Name ": You haven't given enough devices (real or missing) to create this array\n");
98 return 1;
99 }
100
101 /* now set some defaults */
102 if (layout == -1)
103 switch(level) {
104 default: /* no layout */
105 layout = 0;
106 break;
107 case 5:
108 layout = map_name(r5layout, "default");
109 if (verbose)
110 fprintf(stderr,
111 Name ": layout defaults to %s\n", map_num(r5layout, layout));
112 break;
113 }
114
115 if (chunk == 0) {
116 chunk = 64;
117 if (verbose)
118 fprintf(stderr, Name ": chunk size defaults to 64K\n");
119 }
120
121 /* now look at the subdevs */
122 array.active_disks = 0;
123 array.working_disks = 0;
124 for (i=0; i<subdevs; i++) {
125 char *dname = subdev[i];
126 int dsize, freesize;
127 int fd;
128 if (strcasecmp(subdev[i], "missing")==0) {
129 if (first_missing > i)
130 first_missing = i;
131 missing_disks ++;
132 continue;
133 }
134 array.working_disks++;
135 if (i < raiddisks)
136 array.active_disks++;
137 fd = open(dname, O_RDONLY, 0);
138 if (fd <0 ) {
139 fprintf(stderr, Name ": Cannot open %s: %s\n",
140 dname, strerror(errno));
141 fail=1;
142 continue;
143 }
144 if (ioctl(fd, BLKGETSIZE, &dsize)) {
145 fprintf(stderr, Name ": Cannot get size of %s: %s\n",
146 dname, strerror(errno));
147 fail = 1;
148 close(fd);
149 continue;
150 }
151 if (dsize < MD_RESERVED_SECTORS*2) {
152 fprintf(stderr, Name ": %s is too small: %dK\n",
153 dname, dsize/2);
154 fail = 1;
155 close(fd);
156 continue;
157 }
158 freesize = MD_NEW_SIZE_SECTORS(dsize);
159 freesize /= 2;
160
161 if (size && freesize < size) {
162 fprintf(stderr, Name ": %s is smaller that given size."
163 " %dK < %dK + superblock\n", dname, freesize, size);
164 fail = 1;
165 close(fd);
166 continue;
167 }
168 if (maxdisc< 0 || (maxdisc>=0 && freesize > maxsize)) {
169 maxdisc = i;
170 maxsize = freesize;
171 }
172 if (mindisc < 0 || (mindisc >=0 && freesize < minsize)) {
173 mindisc = i;
174 minsize = freesize;
175 }
176 warn |= check_ext2(fd, dname);
177 warn |= check_reiser(fd, dname);
178 warn |= check_raid(fd, dname);
179 close(fd);
180 }
181 if (fail) {
182 fprintf(stderr, Name ": create aborted\n");
183 return 1;
184 }
185 if (size == 0) {
186 if (mindisc == -1) {
187 fprintf(stderr, Name ": no size and no drives given - aborting create.\n");
188 return 1;
189 }
190 size = minsize;
191 if (verbose && level>0)
192 fprintf(stderr, Name ": size set to %dK\n", size);
193 }
194 if ((maxsize-size)*100 > maxsize) {
195 fprintf(stderr, Name ": largest drive (%s) exceed size (%dK) by more than 1%\n",
196 subdev[maxdisc], size);
197 warn = 1;
198 }
199
200 if (warn) {
201 if (runstop!= 1) {
202 if (!ask("Continue creating array? ")) {
203 fprintf(stderr, Name ": create aborted.\n");
204 return 1;
205 }
206 } else {
207 if (verbose)
208 fprintf(stderr, Name ": creation continuing despite oddities due to --run\n");
209 }
210 }
211
212 /* If this is raid5, we want to configure the last active slot
213 * as missing, so that a reconstruct happens (faster than re-parity)
214 */
215 if (force == 0 && level == 5 && first_missing >= raiddisks) {
216 insert_point = raiddisks-1;
217 sparedisks++;
218 array.active_disks--;
219 missing_disks++;
220 }
221
222 /* Ok, lets try some ioctls */
223
224 array.level = level;
225 array.size = size;
226 array.raid_disks = raiddisks;
227 /* The kernel should *know* what md_minor we are dealing
228 * with, but it chooses to trust me instead. Sigh
229 */
230 array.md_minor = 0;
231 if (fstat(mdfd, &stb)==0)
232 array.md_minor = MINOR(stb.st_rdev);
233 array.not_persistent = 0;
234 if (level == 5 && (insert_point < raiddisks || first_missing < raiddisks))
235 array.state = 1; /* clean, but one drive will be missing */
236 else
237 array.state = 0; /* not clean, but no errors */
238
239 /* There is lots of redundancy in these disk counts,
240 * raid_disks is the most meaningful value
241 * it describes the geometry of the array
242 * it is constant
243 * nr_disks is total number of used slots.
244 * it should be raid_disks+spare_disks
245 * spare_disks is the number of extra disks present
246 * see above
247 * active_disks is the number of working disks in
248 * active slots. (With raid_disks)
249 * working_disks is the total number of working disks,
250 * including spares
251 * failed_disks is the number of disks marked failed
252 *
253 * Ideally, the kernel would keep these (except raid_disks)
254 * up-to-date as we ADD_NEW_DISK, but it doesn't (yet).
255 * So for now, we assume that all raid and spare
256 * devices will be given.
257 */
258 array.spare_disks=sparedisks;
259 array.failed_disks=missing_disks;
260 array.nr_disks = array.working_disks + array.failed_disks;
261 array.layout = layout;
262 array.chunk_size = chunk*1024;
263
264 if (ioctl(mdfd, SET_ARRAY_INFO, &array)) {
265 fprintf(stderr, Name ": SET_ARRAY_INFO failed for %s: %s\n",
266 mddev, strerror(errno));
267 return 1;
268 }
269
270 for (i=0; i<subdevs; i++) {
271 int fd;
272 struct stat stb;
273 mdu_disk_info_t disk;
274
275 disk.number = i;
276 if (i >= insert_point)
277 disk.number++;
278 disk.raid_disk = disk.number;
279 if (disk.raid_disk < raiddisks)
280 disk.state = 6; /* active and in sync */
281 else
282 disk.state = 0;
283 if (strcasecmp(subdev[i], "missing")==0) {
284 disk.major = 0;
285 disk.minor = 0;
286 disk.state = 1; /* faulty */
287 } else {
288 fd = open(subdev[i], O_RDONLY, 0);
289 if (fd < 0) {
290 fprintf(stderr, Name ": failed to open %s after earlier success - aborting\n",
291 subdev[i]);
292 return 1;
293 }
294 fstat(fd, &stb);
295 disk.major = MAJOR(stb.st_rdev);
296 disk.minor = MINOR(stb.st_rdev);
297 close(fd);
298 }
299 if (ioctl(mdfd, ADD_NEW_DISK, &disk)) {
300 fprintf(stderr, Name ": ADD_NEW_DISK for %s failed: %s\n",
301 subdev[i], strerror(errno));
302 return 1;
303 }
304 }
305
306 if (insert_point < MD_SB_DISKS) {
307 mdu_disk_info_t disk;
308 disk.number = insert_point;
309 disk.raid_disk = disk.number;
310 disk.state = 1; /* faulty */
311 disk.major = disk.minor = 0;
312 ioctl(mdfd,ADD_NEW_DISK, &disk);
313 }
314
315 /* param is not actually used */
316 if (runstop == 1 || subdevs >= raiddisks) {
317 mdu_param_t param;
318 if (ioctl(mdfd, RUN_ARRAY, &param)) {
319 fprintf(stderr, Name ": RUN_ARRAY failed: %s\n",
320 strerror(errno));
321 return 1;
322 }
323 fprintf(stderr, Name ": array %s started.\n", mddev);
324 } else {
325 fprintf(stderr, Name ": not starting array - not enough discs.\n");
326 }
327 return 0;
328 }