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