]>
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; | |
22581f51 MT |
344 | |
345 | // The RAID will install some metadata at the end of the disk | |
346 | // and we will save up some space for that. | |
347 | dest->size -= MB2BYTES(2); | |
25fcce25 MT |
348 | } else { |
349 | dest->size = dest->disk1->size; | |
350 | } | |
351 | ||
352 | dest->size_boot = hw_boot_size(dest); | |
353 | dest->size_swap = hw_swap_size(dest); | |
354 | dest->size_root = hw_root_size(dest); | |
355 | ||
356 | // Determine the size of the data partition. | |
357 | unsigned long long used_space = dest->size_boot + dest->size_swap + dest->size_root; | |
358 | ||
359 | // Disk is way too small | |
360 | if (used_space >= dest->size) | |
361 | return -1; | |
362 | ||
363 | dest->size_data = dest->size - used_space; | |
364 | ||
365 | // If it gets too small, we remove the swap space. | |
366 | if (dest->size_data <= MB2BYTES(256)) { | |
367 | dest->size_data += dest->size_swap; | |
368 | dest->size_swap = 0; | |
369 | } | |
370 | ||
371 | // Set partition names | |
372 | char path[DEV_SIZE]; | |
373 | int part_idx = 1; | |
374 | ||
375 | snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : ""); | |
376 | ||
377 | if (dest->size_boot > 0) { | |
378 | dest->part_boot_idx = part_idx; | |
379 | ||
380 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
381 | } else | |
382 | *dest->part_boot = '\0'; | |
383 | ||
384 | if (dest->size_swap > 0) | |
385 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
386 | else | |
387 | *dest->part_swap = '\0'; | |
388 | ||
389 | // There is always a root partition | |
390 | if (!*dest->part_boot) | |
391 | dest->part_boot_idx = part_idx; | |
392 | ||
393 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
394 | ||
395 | if (dest->size_data > 0) | |
396 | snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++); | |
397 | else | |
398 | *dest->part_data = '\0'; | |
399 | ||
400 | // Determine partition table | |
401 | dest->part_table = HW_PART_TABLE_MSDOS; | |
402 | ||
403 | // Disks over 2TB need to use GPT | |
404 | if (dest->size >= MB2BYTES(2047 * 1024)) | |
405 | dest->part_table = HW_PART_TABLE_GPT; | |
406 | ||
407 | // We also use GPT on raid disks by default | |
408 | else if (dest->is_raid) | |
409 | dest->part_table = HW_PART_TABLE_GPT; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
d7dd283b MT |
414 | struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) { |
415 | struct hw_destination* dest = malloc(sizeof(*dest)); | |
416 | ||
417 | if (part_type == HW_PART_TYPE_NORMAL) { | |
418 | dest->disk1 = *disks; | |
419 | dest->disk2 = NULL; | |
420 | ||
421 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
422 | ||
423 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
424 | dest->disk1 = *disks++; | |
425 | dest->disk2 = *disks; | |
4a0d9bef | 426 | dest->raid_level = 1; |
d7dd283b MT |
427 | |
428 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
429 | } | |
430 | ||
431 | // Is this a RAID device? | |
432 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
433 | ||
25fcce25 MT |
434 | int r = hw_calculate_partition_table(dest); |
435 | if (r) | |
436 | return NULL; | |
d7dd283b | 437 | |
25fcce25 MT |
438 | // Set default filesystem |
439 | dest->filesystem = HW_FS_DEFAULT; | |
d7dd283b MT |
440 | |
441 | return dest; | |
442 | } | |
c4e96674 MT |
443 | |
444 | unsigned long long hw_memory() { | |
445 | FILE* handle = NULL; | |
446 | char line[STRING_SIZE]; | |
447 | ||
448 | unsigned long long memory = 0; | |
449 | ||
450 | /* Calculate amount of memory in machine */ | |
451 | if ((handle = fopen("/proc/meminfo", "r"))) { | |
452 | while (fgets(line, sizeof(line), handle)) { | |
25fcce25 | 453 | if (!sscanf (line, "MemTotal: %llu kB", &memory)) { |
c4e96674 MT |
454 | memory = 0; |
455 | } | |
456 | } | |
457 | ||
458 | fclose(handle); | |
459 | } | |
460 | ||
461 | return memory * 1024; | |
462 | } | |
25fcce25 MT |
463 | |
464 | int hw_create_partitions(struct hw_destination* dest) { | |
465 | char* cmd = NULL; | |
466 | ||
467 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); | |
468 | ||
469 | // Set partition type | |
470 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
471 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
472 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
473 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
474 | ||
f5007e9c | 475 | unsigned long long part_start = 1 * 1024 * 1024; // 1MB |
25fcce25 MT |
476 | |
477 | if (*dest->part_boot) { | |
478 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
479 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", | |
480 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_boot)); | |
481 | ||
482 | part_start += dest->size_boot; | |
483 | } | |
484 | ||
485 | if (*dest->part_swap) { | |
486 | asprintf(&cmd, "%s mkpart %s linux-swap %lluMB %lluMB", cmd, | |
487 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", | |
488 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_swap)); | |
489 | ||
490 | part_start += dest->size_swap; | |
491 | } | |
492 | ||
493 | if (*dest->part_root) { | |
494 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
495 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", | |
496 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_root)); | |
497 | ||
498 | part_start += dest->size_root; | |
499 | } | |
500 | ||
501 | if (*dest->part_data) { | |
502 | asprintf(&cmd, "%s mkpart %s ext2 %lluMB %lluMB", cmd, | |
503 | (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary", | |
504 | BYTES2MB(part_start), BYTES2MB(part_start + dest->size_data)); | |
505 | ||
506 | part_start += dest->size_data; | |
507 | } | |
508 | ||
509 | if (dest->part_table == HW_PART_TABLE_MSDOS && dest->part_boot_idx > 0) { | |
510 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); | |
511 | ||
512 | } else if (dest->part_table == HW_PART_TABLE_GPT) { | |
513 | asprintf(&cmd, "%s disk_set pmbr_boot on", cmd); | |
514 | } | |
515 | ||
516 | int r = mysystem(cmd); | |
517 | ||
268090a8 MT |
518 | // Wait until the system re-read the partition table |
519 | if (r == 0) { | |
520 | unsigned int counter = 10; | |
521 | ||
522 | while (counter-- > 0) { | |
523 | sleep(1); | |
524 | ||
525 | if (*dest->part_boot && (access(dest->part_boot, R_OK) != 0)) | |
526 | continue; | |
527 | ||
528 | if (*dest->part_swap && (access(dest->part_swap, R_OK) != 0)) | |
529 | continue; | |
530 | ||
531 | if (*dest->part_root && (access(dest->part_root, R_OK) != 0)) | |
532 | continue; | |
533 | ||
534 | if (*dest->part_data && (access(dest->part_data, R_OK) != 0)) | |
535 | continue; | |
536 | ||
537 | // All partitions do exist, exiting the loop. | |
538 | break; | |
539 | } | |
540 | } | |
541 | ||
25fcce25 MT |
542 | if (cmd) |
543 | free(cmd); | |
544 | ||
545 | return r; | |
546 | } | |
547 | ||
548 | static int hw_format_filesystem(const char* path, int fs) { | |
549 | char cmd[STRING_SIZE] = "\0"; | |
550 | ||
551 | // Swap | |
552 | if (fs == HW_FS_SWAP) { | |
553 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
554 | // ReiserFS | |
555 | } else if (fs == HW_FS_REISERFS) { | |
556 | snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path); | |
557 | ||
558 | // EXT4 | |
559 | } else if (fs == HW_FS_EXT4) { | |
560 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path); | |
561 | ||
562 | // EXT4 w/o journal | |
563 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
564 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path); | |
565 | } | |
566 | ||
567 | assert(*cmd); | |
568 | ||
569 | int r = mysystem(cmd); | |
570 | ||
571 | return r; | |
572 | } | |
573 | ||
574 | int hw_create_filesystems(struct hw_destination* dest) { | |
575 | int r; | |
576 | ||
577 | // boot | |
578 | if (*dest->part_boot) { | |
579 | r = hw_format_filesystem(dest->part_boot, dest->filesystem); | |
580 | if (r) | |
581 | return r; | |
582 | } | |
583 | ||
584 | // swap | |
585 | if (*dest->part_swap) { | |
586 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP); | |
587 | if (r) | |
588 | return r; | |
589 | } | |
590 | ||
591 | // root | |
592 | r = hw_format_filesystem(dest->part_root, dest->filesystem); | |
593 | if (r) | |
594 | return r; | |
595 | ||
596 | // data | |
597 | if (*dest->part_data) { | |
598 | r = hw_format_filesystem(dest->part_data, dest->filesystem); | |
599 | if (r) | |
600 | return r; | |
601 | } | |
602 | ||
603 | return 0; | |
604 | } | |
605 | ||
606 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
607 | char target[STRING_SIZE]; | |
608 | ||
609 | assert(*prefix == '/'); | |
610 | ||
611 | const char* filesystem; | |
612 | switch (dest->filesystem) { | |
613 | case HW_FS_REISERFS: | |
614 | filesystem = "reiserfs"; | |
615 | break; | |
616 | ||
617 | case HW_FS_EXT4: | |
618 | case HW_FS_EXT4_WO_JOURNAL: | |
619 | filesystem = "ext4"; | |
620 | break; | |
621 | ||
622 | default: | |
623 | assert(0); | |
624 | } | |
625 | ||
626 | // root | |
627 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
628 | if (r) | |
629 | return r; | |
630 | ||
631 | // boot | |
632 | if (*dest->part_boot) { | |
633 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
634 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
635 | ||
636 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
637 | if (r) { | |
638 | hw_umount_filesystems(dest, prefix); | |
639 | ||
640 | return r; | |
641 | } | |
642 | } | |
643 | ||
644 | // data | |
645 | if (*dest->part_data) { | |
646 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA); | |
647 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
648 | ||
649 | r = hw_mount(dest->part_data, target, filesystem, 0); | |
650 | if (r) { | |
651 | hw_umount_filesystems(dest, prefix); | |
652 | ||
653 | return r; | |
654 | } | |
655 | } | |
656 | ||
657 | // swap | |
658 | if (*dest->part_swap) { | |
659 | r = swapon(dest->part_swap, 0); | |
660 | if (r) { | |
661 | hw_umount_filesystems(dest, prefix); | |
662 | ||
663 | return r; | |
664 | } | |
665 | } | |
666 | ||
667 | // bind-mount misc filesystems | |
668 | char** otherfs = other_filesystems; | |
669 | while (*otherfs) { | |
670 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs); | |
671 | ||
672 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
673 | r = hw_mount(*otherfs, target, NULL, MS_BIND); | |
674 | if (r) { | |
675 | hw_umount_filesystems(dest, prefix); | |
676 | ||
677 | return r; | |
678 | } | |
679 | ||
680 | otherfs++; | |
681 | } | |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
686 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
687 | // boot | |
688 | if (*dest->part_boot) { | |
689 | hw_umount(dest->part_boot); | |
690 | } | |
691 | ||
692 | // data | |
693 | if (*dest->part_data) { | |
694 | hw_umount(dest->part_data); | |
695 | } | |
696 | ||
697 | // root | |
698 | hw_umount(dest->part_root); | |
699 | ||
700 | // swap | |
701 | if (*dest->part_swap) { | |
702 | swapoff(dest->part_swap); | |
703 | } | |
704 | ||
705 | // misc filesystems | |
706 | char target[STRING_SIZE]; | |
707 | char** otherfs = other_filesystems; | |
708 | ||
709 | while (*otherfs) { | |
710 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++); | |
711 | hw_umount(target); | |
712 | } | |
713 | ||
714 | return 0; | |
715 | } | |
4a0d9bef MT |
716 | |
717 | int hw_setup_raid(struct hw_destination* dest) { | |
718 | char* cmd = NULL; | |
719 | ||
720 | assert(dest->is_raid); | |
721 | ||
f5007e9c | 722 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest->path); |
4a0d9bef MT |
723 | |
724 | switch (dest->raid_level) { | |
725 | case 1: | |
726 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
727 | break; | |
728 | ||
729 | default: | |
730 | assert(0); | |
731 | } | |
732 | ||
733 | if (dest->disk1) { | |
734 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
735 | } | |
736 | ||
737 | if (dest->disk2) { | |
738 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
739 | } | |
740 | ||
741 | int r = mysystem(cmd); | |
742 | free(cmd); | |
743 | ||
744 | // Wait a moment until the device has been properly brought up | |
745 | if (r == 0) { | |
746 | unsigned int counter = 10; | |
747 | while (counter-- > 0) { | |
748 | sleep(1); | |
749 | ||
fde37387 MT |
750 | // If the raid device has not yet been properly brought up, |
751 | // opening it will fail with the message: Device or resource busy | |
752 | // Hence we will wait a bit until it becomes usable. | |
753 | FILE* f = fopen(dest->path, "r"); | |
754 | if (f) { | |
755 | fclose(f); | |
4a0d9bef | 756 | break; |
fde37387 | 757 | } |
4a0d9bef MT |
758 | } |
759 | } | |
760 | ||
761 | return r; | |
762 | } | |
763 | ||
764 | int hw_stop_all_raid_arrays() { | |
765 | return mysystem("/sbin/mdadm --stop --scan"); | |
766 | } | |
f5007e9c MT |
767 | |
768 | int hw_install_bootloader(struct hw_destination* dest) { | |
769 | char cmd[STRING_SIZE]; | |
770 | int r; | |
771 | ||
772 | // Generate configuration file | |
773 | snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg"); | |
774 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
775 | if (r) | |
776 | return r; | |
777 | ||
778 | char cmd_grub[STRING_SIZE]; | |
779 | snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck"); | |
780 | ||
781 | if (dest->is_raid) { | |
782 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path); | |
783 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
784 | if (r) | |
785 | return r; | |
786 | ||
787 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path); | |
788 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
789 | } else { | |
790 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path); | |
791 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
792 | } | |
793 | ||
794 | return r; | |
795 | } |