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