]>
Commit | Line | Data |
---|---|---|
f0fa1795 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # IPFire - An Open Source Firewall Distribution # | |
4 | # Copyright (C) 2014 IPFire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
25fcce25 MT |
21 | #ifndef _GNU_SOURCE |
22 | #define _GNU_SOURCE | |
23 | #endif | |
24 | ||
f0fa1795 | 25 | #include <assert.h> |
d7dd283b MT |
26 | #include <blkid/blkid.h> |
27 | #include <fcntl.h> | |
f0fa1795 | 28 | #include <libudev.h> |
d7dd283b | 29 | #include <math.h> |
f0fa1795 MT |
30 | #include <stdio.h> |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
d7dd283b | 33 | #include <sys/ioctl.h> |
f0fa1795 | 34 | #include <sys/mount.h> |
25fcce25 | 35 | #include <sys/swap.h> |
f0fa1795 MT |
36 | #include <unistd.h> |
37 | ||
d7dd283b MT |
38 | #include <linux/fs.h> |
39 | ||
f0fa1795 | 40 | #include "hw.h" |
25fcce25 MT |
41 | #include "../libsmooth/libsmooth.h" |
42 | ||
43 | const char* other_filesystems[] = { | |
44 | "/dev", | |
45 | "/proc", | |
46 | "/sys", | |
47 | NULL | |
48 | }; | |
f0fa1795 MT |
49 | |
50 | struct hw* hw_init() { | |
51 | struct hw* hw = malloc(sizeof(*hw)); | |
52 | assert(hw); | |
53 | ||
54 | // Initialize libudev | |
55 | hw->udev = udev_new(); | |
56 | if (!hw->udev) { | |
57 | fprintf(stderr, "Could not create udev instance\n"); | |
58 | exit(1); | |
59 | } | |
60 | ||
61 | return hw; | |
62 | } | |
63 | ||
64 | void hw_free(struct hw* hw) { | |
65 | if (hw->udev) | |
66 | udev_unref(hw->udev); | |
67 | ||
68 | free(hw); | |
69 | } | |
70 | ||
71 | static int strstartswith(const char* a, const char* b) { | |
72 | return (strncmp(a, b, strlen(b)) == 0); | |
73 | } | |
74 | ||
25fcce25 MT |
75 | int hw_mount(const char* source, const char* target, const char* fs, int flags) { |
76 | return mount(source, target, fs, flags, NULL); | |
f0fa1795 MT |
77 | } |
78 | ||
79 | int hw_umount(const char* target) { | |
80 | return umount2(target, MNT_DETACH); | |
81 | } | |
82 | ||
83 | static int hw_test_source_medium(const char* path) { | |
25fcce25 | 84 | int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY); |
f0fa1795 MT |
85 | |
86 | // If the source could not be mounted we | |
87 | // cannot proceed. | |
88 | if (ret) | |
89 | return ret; | |
90 | ||
91 | // Check if the test file exists. | |
92 | ret = access(SOURCE_TEST_FILE, F_OK); | |
93 | ||
94 | // Umount the test device. | |
95 | hw_umount(SOURCE_MOUNT_PATH); | |
96 | ||
97 | return ret; | |
98 | } | |
99 | ||
100 | char* hw_find_source_medium(struct hw* hw) { | |
101 | char* ret = NULL; | |
102 | ||
103 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); | |
104 | ||
105 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
106 | udev_enumerate_scan_devices(enumerate); | |
107 | ||
108 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
109 | ||
110 | struct udev_list_entry* dev_list_entry; | |
111 | udev_list_entry_foreach(dev_list_entry, devices) { | |
112 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
113 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
114 | ||
115 | const char* dev_path = udev_device_get_devnode(dev); | |
116 | ||
117 | // Skip everything what we cannot work with | |
118 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef | 119 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md")) |
f0fa1795 MT |
120 | continue; |
121 | ||
122 | if (hw_test_source_medium(dev_path)) { | |
123 | ret = strdup(dev_path); | |
124 | } | |
125 | ||
126 | udev_device_unref(dev); | |
127 | ||
128 | // If a suitable device was found the search will end. | |
129 | if (ret) | |
130 | break; | |
131 | } | |
132 | ||
133 | udev_enumerate_unref(enumerate); | |
134 | ||
135 | return ret; | |
136 | } | |
d7dd283b MT |
137 | |
138 | static struct hw_disk** hw_create_disks() { | |
139 | struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1)); | |
140 | ||
141 | return ret; | |
142 | } | |
143 | ||
144 | static unsigned long long hw_block_device_get_size(const char* dev) { | |
145 | int fd = open(dev, O_RDONLY); | |
146 | if (fd < 0) | |
147 | return 0; | |
148 | ||
149 | unsigned long long size = blkid_get_dev_size(fd); | |
150 | close(fd); | |
151 | ||
152 | return size; | |
153 | } | |
154 | ||
155 | struct hw_disk** hw_find_disks(struct hw* hw) { | |
156 | struct hw_disk** ret = hw_create_disks(); | |
157 | struct hw_disk** disks = ret; | |
158 | ||
159 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); | |
160 | ||
161 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
162 | udev_enumerate_scan_devices(enumerate); | |
163 | ||
164 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
165 | ||
166 | struct udev_list_entry* dev_list_entry; | |
167 | unsigned int i = HW_MAX_DISKS; | |
168 | udev_list_entry_foreach(dev_list_entry, devices) { | |
169 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
170 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
171 | ||
172 | const char* dev_path = udev_device_get_devnode(dev); | |
173 | ||
174 | // Skip everything what we cannot work with | |
175 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef MT |
176 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") || |
177 | strstartswith(dev_path, "/dev/md")) { | |
d7dd283b MT |
178 | udev_device_unref(dev); |
179 | continue; | |
180 | } | |
181 | ||
182 | // DEVTYPE must be disk (otherwise we will see all sorts of partitions here) | |
183 | const char* devtype = udev_device_get_property_value(dev, "DEVTYPE"); | |
184 | if (devtype && (strcmp(devtype, "disk") != 0)) { | |
185 | udev_device_unref(dev); | |
186 | continue; | |
187 | } | |
188 | ||
189 | // Skip all source mediums | |
190 | if (hw_test_source_medium(dev_path) == 0) { | |
191 | udev_device_unref(dev); | |
192 | continue; | |
193 | } | |
194 | ||
195 | // Skip devices with a size of zero | |
196 | unsigned long long size = hw_block_device_get_size(dev_path); | |
197 | if (size == 0) { | |
198 | udev_device_unref(dev); | |
199 | continue; | |
200 | } | |
201 | ||
202 | struct hw_disk* disk = malloc(sizeof(*disk)); | |
203 | if (disk == NULL) | |
204 | return NULL; | |
205 | ||
206 | disk->ref = 1; | |
207 | ||
208 | strncpy(disk->path, dev_path, sizeof(disk->path)); | |
209 | ||
210 | disk->size = size; | |
211 | ||
212 | // Vendor | |
213 | const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR"); | |
214 | if (!vendor) | |
215 | vendor = udev_device_get_sysattr_value(dev, "vendor"); | |
216 | if (!vendor) | |
217 | vendor = udev_device_get_sysattr_value(dev, "manufacturer"); | |
218 | if (!vendor) | |
219 | vendor = "N/A"; | |
220 | ||
221 | strncpy(disk->vendor, vendor, sizeof(disk->vendor)); | |
222 | ||
223 | // Model | |
224 | const char* model = udev_device_get_property_value(dev, "ID_MODEL"); | |
225 | if (!model) | |
226 | model = udev_device_get_sysattr_value(dev, "model"); | |
227 | if (!model) | |
228 | model = udev_device_get_sysattr_value(dev, "product"); | |
229 | if (!model) | |
230 | model = "N/A"; | |
231 | ||
232 | strncpy(disk->model, model, sizeof(disk->model)); | |
233 | ||
234 | snprintf(disk->description, sizeof(disk->description), | |
235 | "%4.1fGB %s - %s", (double)disk->size / pow(1024, 3), | |
236 | disk->vendor, disk->model); | |
237 | ||
238 | *disks++ = disk; | |
239 | ||
240 | if (--i == 0) | |
241 | break; | |
242 | ||
243 | udev_device_unref(dev); | |
244 | } | |
245 | ||
246 | udev_enumerate_unref(enumerate); | |
247 | ||
248 | *disks = NULL; | |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
253 | void hw_free_disks(struct hw_disk** disks) { | |
254 | struct hw_disk** disk = disks; | |
255 | ||
256 | while (*disk != NULL) { | |
257 | if (--(*disk)->ref == 0) | |
258 | free(*disk); | |
259 | ||
260 | disk++; | |
261 | } | |
262 | ||
263 | free(disks); | |
264 | } | |
265 | ||
266 | unsigned int hw_count_disks(struct hw_disk** disks) { | |
267 | unsigned int ret = 0; | |
268 | ||
269 | while (*disks++) | |
270 | ret++; | |
271 | ||
272 | return ret; | |
273 | } | |
274 | ||
275 | struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) { | |
276 | struct hw_disk** ret = hw_create_disks(); | |
277 | struct hw_disk** selected_disks = ret; | |
278 | ||
279 | unsigned int num_disks = hw_count_disks(disks); | |
280 | ||
281 | for (unsigned int i = 0; i < num_disks; i++) { | |
282 | if (selection && selection[i]) { | |
283 | struct hw_disk *selected_disk = disks[i]; | |
284 | selected_disk->ref++; | |
285 | ||
286 | *selected_disks++ = selected_disk; | |
287 | } | |
288 | } | |
289 | ||
290 | // Set sentinel | |
291 | *selected_disks = NULL; | |
292 | ||
293 | return ret; | |
294 | } | |
295 | ||
25fcce25 MT |
296 | static unsigned long long hw_swap_size(struct hw_destination* dest) { |
297 | unsigned long long memory = hw_memory(); | |
298 | ||
299 | unsigned long long swap_size = memory / 4; | |
300 | ||
301 | // Min. swap size is 128MB | |
302 | if (swap_size < MB2BYTES(128)) | |
303 | swap_size = MB2BYTES(128); | |
304 | ||
305 | // Cap swap size to 1GB | |
306 | else if (swap_size > MB2BYTES(1024)) | |
307 | swap_size = MB2BYTES(1024); | |
308 | ||
309 | return swap_size; | |
310 | } | |
311 | ||
312 | static unsigned long long hw_root_size(struct hw_destination* dest) { | |
313 | unsigned long long root_size; | |
314 | ||
315 | if (dest->size < MB2BYTES(2048)) | |
316 | root_size = MB2BYTES(1024); | |
317 | ||
318 | else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072)) | |
319 | root_size = MB2BYTES(1536); | |
320 | ||
321 | else | |
322 | root_size = MB2BYTES(2048); | |
323 | ||
324 | return root_size; | |
325 | } | |
326 | ||
327 | static unsigned long long hw_boot_size(struct hw_destination* dest) { | |
328 | return MB2BYTES(64); | |
329 | } | |
330 | ||
331 | static int hw_calculate_partition_table(struct hw_destination* dest) { | |
332 | // Determine the size of the target block device | |
333 | if (dest->is_raid) { | |
334 | dest->size = (dest->disk1->size >= dest->disk2->size) ? | |
335 | dest->disk1->size : dest->disk2->size; | |
336 | } else { | |
337 | dest->size = dest->disk1->size; | |
338 | } | |
339 | ||
340 | dest->size_boot = hw_boot_size(dest); | |
341 | dest->size_swap = hw_swap_size(dest); | |
342 | dest->size_root = hw_root_size(dest); | |
343 | ||
344 | // Determine the size of the data partition. | |
345 | unsigned long long used_space = dest->size_boot + dest->size_swap + dest->size_root; | |
346 | ||
347 | // Disk is way too small | |
348 | if (used_space >= dest->size) | |
349 | return -1; | |
350 | ||
351 | dest->size_data = dest->size - used_space; | |
352 | ||
353 | // If it gets too small, we remove the swap space. | |
354 | if (dest->size_data <= MB2BYTES(256)) { | |
355 | dest->size_data += dest->size_swap; | |
356 | dest->size_swap = 0; | |
357 | } | |
358 | ||
359 | // Set partition names | |
360 | char path[DEV_SIZE]; | |
361 | int part_idx = 1; | |
362 | ||
363 | snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : ""); | |
364 | ||
365 | if (dest->size_boot > 0) { | |
366 | dest->part_boot_idx = part_idx; | |
367 | ||
368 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
369 | } else | |
370 | *dest->part_boot = '\0'; | |
371 | ||
372 | if (dest->size_swap > 0) | |
373 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
374 | else | |
375 | *dest->part_swap = '\0'; | |
376 | ||
377 | // There is always a root partition | |
378 | if (!*dest->part_boot) | |
379 | dest->part_boot_idx = part_idx; | |
380 | ||
381 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
382 | ||
383 | if (dest->size_data > 0) | |
384 | snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++); | |
385 | else | |
386 | *dest->part_data = '\0'; | |
387 | ||
388 | // Determine partition table | |
389 | dest->part_table = HW_PART_TABLE_MSDOS; | |
390 | ||
391 | // Disks over 2TB need to use GPT | |
392 | if (dest->size >= MB2BYTES(2047 * 1024)) | |
393 | dest->part_table = HW_PART_TABLE_GPT; | |
394 | ||
395 | // We also use GPT on raid disks by default | |
396 | else if (dest->is_raid) | |
397 | dest->part_table = HW_PART_TABLE_GPT; | |
398 | ||
399 | return 0; | |
400 | } | |
401 | ||
d7dd283b MT |
402 | struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) { |
403 | struct hw_destination* dest = malloc(sizeof(*dest)); | |
404 | ||
405 | if (part_type == HW_PART_TYPE_NORMAL) { | |
406 | dest->disk1 = *disks; | |
407 | dest->disk2 = NULL; | |
408 | ||
409 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
410 | ||
411 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
412 | dest->disk1 = *disks++; | |
413 | dest->disk2 = *disks; | |
4a0d9bef | 414 | dest->raid_level = 1; |
d7dd283b MT |
415 | |
416 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
417 | } | |
418 | ||
419 | // Is this a RAID device? | |
420 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
421 | ||
25fcce25 MT |
422 | int r = hw_calculate_partition_table(dest); |
423 | if (r) | |
424 | return NULL; | |
d7dd283b | 425 | |
25fcce25 MT |
426 | // Set default filesystem |
427 | dest->filesystem = HW_FS_DEFAULT; | |
d7dd283b MT |
428 | |
429 | return dest; | |
430 | } | |
c4e96674 MT |
431 | |
432 | unsigned long long hw_memory() { | |
433 | FILE* handle = NULL; | |
434 | char line[STRING_SIZE]; | |
435 | ||
436 | unsigned long long memory = 0; | |
437 | ||
438 | /* Calculate amount of memory in machine */ | |
439 | if ((handle = fopen("/proc/meminfo", "r"))) { | |
440 | while (fgets(line, sizeof(line), handle)) { | |
25fcce25 | 441 | if (!sscanf (line, "MemTotal: %llu kB", &memory)) { |
c4e96674 MT |
442 | memory = 0; |
443 | } | |
444 | } | |
445 | ||
446 | fclose(handle); | |
447 | } | |
448 | ||
449 | return memory * 1024; | |
450 | } | |
25fcce25 MT |
451 | |
452 | int hw_create_partitions(struct hw_destination* dest) { | |
453 | char* cmd = NULL; | |
454 | ||
455 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); | |
456 | ||
457 | // Set partition type | |
458 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
459 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
460 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
461 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
462 | ||
463 | unsigned long long part_start = 0 * 1024 * 1024; // 1MB | |
464 | ||
465 | if (*dest->part_boot) { | |
466 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
467 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", | |
468 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_boot)); | |
469 | ||
470 | part_start += dest->size_boot; | |
471 | } | |
472 | ||
473 | if (*dest->part_swap) { | |
474 | asprintf(&cmd, "%s mkpart %s linux-swap %lluMB %lluMB", cmd, | |
475 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", | |
476 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_swap)); | |
477 | ||
478 | part_start += dest->size_swap; | |
479 | } | |
480 | ||
481 | if (*dest->part_root) { | |
482 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
483 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", | |
484 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_root)); | |
485 | ||
486 | part_start += dest->size_root; | |
487 | } | |
488 | ||
489 | if (*dest->part_data) { | |
490 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
491 | (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary", | |
492 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_data)); | |
493 | ||
494 | part_start += dest->size_data; | |
495 | } | |
496 | ||
497 | if (dest->part_table == HW_PART_TABLE_MSDOS && dest->part_boot_idx > 0) { | |
498 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); | |
499 | ||
500 | } else if (dest->part_table == HW_PART_TABLE_GPT) { | |
501 | asprintf(&cmd, "%s disk_set pmbr_boot on", cmd); | |
502 | } | |
503 | ||
504 | int r = mysystem(cmd); | |
505 | ||
506 | if (cmd) | |
507 | free(cmd); | |
508 | ||
509 | return r; | |
510 | } | |
511 | ||
512 | static int hw_format_filesystem(const char* path, int fs) { | |
513 | char cmd[STRING_SIZE] = "\0"; | |
514 | ||
515 | // Swap | |
516 | if (fs == HW_FS_SWAP) { | |
517 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
518 | // ReiserFS | |
519 | } else if (fs == HW_FS_REISERFS) { | |
520 | snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path); | |
521 | ||
522 | // EXT4 | |
523 | } else if (fs == HW_FS_EXT4) { | |
524 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path); | |
525 | ||
526 | // EXT4 w/o journal | |
527 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
528 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path); | |
529 | } | |
530 | ||
531 | assert(*cmd); | |
532 | ||
533 | int r = mysystem(cmd); | |
534 | ||
535 | return r; | |
536 | } | |
537 | ||
538 | int hw_create_filesystems(struct hw_destination* dest) { | |
539 | int r; | |
540 | ||
541 | // boot | |
542 | if (*dest->part_boot) { | |
543 | r = hw_format_filesystem(dest->part_boot, dest->filesystem); | |
544 | if (r) | |
545 | return r; | |
546 | } | |
547 | ||
548 | // swap | |
549 | if (*dest->part_swap) { | |
550 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP); | |
551 | if (r) | |
552 | return r; | |
553 | } | |
554 | ||
555 | // root | |
556 | r = hw_format_filesystem(dest->part_root, dest->filesystem); | |
557 | if (r) | |
558 | return r; | |
559 | ||
560 | // data | |
561 | if (*dest->part_data) { | |
562 | r = hw_format_filesystem(dest->part_data, dest->filesystem); | |
563 | if (r) | |
564 | return r; | |
565 | } | |
566 | ||
567 | return 0; | |
568 | } | |
569 | ||
570 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
571 | char target[STRING_SIZE]; | |
572 | ||
573 | assert(*prefix == '/'); | |
574 | ||
575 | const char* filesystem; | |
576 | switch (dest->filesystem) { | |
577 | case HW_FS_REISERFS: | |
578 | filesystem = "reiserfs"; | |
579 | break; | |
580 | ||
581 | case HW_FS_EXT4: | |
582 | case HW_FS_EXT4_WO_JOURNAL: | |
583 | filesystem = "ext4"; | |
584 | break; | |
585 | ||
586 | default: | |
587 | assert(0); | |
588 | } | |
589 | ||
590 | // root | |
591 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
592 | if (r) | |
593 | return r; | |
594 | ||
595 | // boot | |
596 | if (*dest->part_boot) { | |
597 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
598 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
599 | ||
600 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
601 | if (r) { | |
602 | hw_umount_filesystems(dest, prefix); | |
603 | ||
604 | return r; | |
605 | } | |
606 | } | |
607 | ||
608 | // data | |
609 | if (*dest->part_data) { | |
610 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA); | |
611 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
612 | ||
613 | r = hw_mount(dest->part_data, target, filesystem, 0); | |
614 | if (r) { | |
615 | hw_umount_filesystems(dest, prefix); | |
616 | ||
617 | return r; | |
618 | } | |
619 | } | |
620 | ||
621 | // swap | |
622 | if (*dest->part_swap) { | |
623 | r = swapon(dest->part_swap, 0); | |
624 | if (r) { | |
625 | hw_umount_filesystems(dest, prefix); | |
626 | ||
627 | return r; | |
628 | } | |
629 | } | |
630 | ||
631 | // bind-mount misc filesystems | |
632 | char** otherfs = other_filesystems; | |
633 | while (*otherfs) { | |
634 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs); | |
635 | ||
636 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
637 | r = hw_mount(*otherfs, target, NULL, MS_BIND); | |
638 | if (r) { | |
639 | hw_umount_filesystems(dest, prefix); | |
640 | ||
641 | return r; | |
642 | } | |
643 | ||
644 | otherfs++; | |
645 | } | |
646 | ||
647 | return 0; | |
648 | } | |
649 | ||
650 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
651 | // boot | |
652 | if (*dest->part_boot) { | |
653 | hw_umount(dest->part_boot); | |
654 | } | |
655 | ||
656 | // data | |
657 | if (*dest->part_data) { | |
658 | hw_umount(dest->part_data); | |
659 | } | |
660 | ||
661 | // root | |
662 | hw_umount(dest->part_root); | |
663 | ||
664 | // swap | |
665 | if (*dest->part_swap) { | |
666 | swapoff(dest->part_swap); | |
667 | } | |
668 | ||
669 | // misc filesystems | |
670 | char target[STRING_SIZE]; | |
671 | char** otherfs = other_filesystems; | |
672 | ||
673 | while (*otherfs) { | |
674 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++); | |
675 | hw_umount(target); | |
676 | } | |
677 | ||
678 | return 0; | |
679 | } | |
4a0d9bef MT |
680 | |
681 | int hw_setup_raid(struct hw_destination* dest) { | |
682 | char* cmd = NULL; | |
683 | ||
684 | assert(dest->is_raid); | |
685 | ||
686 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=0.9 %s", dest->path); | |
687 | ||
688 | switch (dest->raid_level) { | |
689 | case 1: | |
690 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
691 | break; | |
692 | ||
693 | default: | |
694 | assert(0); | |
695 | } | |
696 | ||
697 | if (dest->disk1) { | |
698 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
699 | } | |
700 | ||
701 | if (dest->disk2) { | |
702 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
703 | } | |
704 | ||
705 | int r = mysystem(cmd); | |
706 | free(cmd); | |
707 | ||
708 | // Wait a moment until the device has been properly brought up | |
709 | if (r == 0) { | |
710 | unsigned int counter = 10; | |
711 | while (counter-- > 0) { | |
712 | sleep(1); | |
713 | ||
714 | if (access(dest->path, R_OK) == 0) | |
715 | break; | |
716 | } | |
717 | } | |
718 | ||
719 | return r; | |
720 | } | |
721 | ||
722 | int hw_stop_all_raid_arrays() { | |
723 | return mysystem("/sbin/mdadm --stop --scan"); | |
724 | } |