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