]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
1 /*#############################################################################
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2014 IPFire development team #
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. #
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. #
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/>. #
19 #############################################################################*/
26 #include <blkid/blkid.h>
30 #include <linux/loop.h>
35 #include <sys/ioctl.h>
36 #include <sys/mount.h>
39 #include <sys/sysinfo.h>
40 #include <sys/utsname.h>
45 #include <libsmooth.h>
49 const char* other_filesystems
[] = {
56 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
57 char chroot_cmd
[STRING_SIZE
];
59 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
61 return mysystem(output
, chroot_cmd
);
64 struct hw
* hw_init() {
65 struct hw
* hw
= calloc(1, sizeof(*hw
));
69 hw
->udev
= udev_new();
71 fprintf(stderr
, "Could not create udev instance\n");
75 // What architecture are we running on?
76 struct utsname uname_data
;
77 int ret
= uname(&uname_data
);
79 snprintf(hw
->arch
, sizeof(hw
->arch
), "%s", uname_data
.machine
);
81 // Should we install in EFI mode?
82 if ((strcmp(hw
->arch
, "x86_64") == 0) || (strcmp(hw
->arch
, "aarch64") == 0))
88 void hw_free(struct hw
* hw
) {
95 static int strstartswith(const char* a
, const char* b
) {
96 return (strncmp(a
, b
, strlen(b
)) == 0);
99 static char loop_device
[STRING_SIZE
];
101 static int setup_loop_device(const char* source
, const char* device
) {
102 int file_fd
= open(source
, O_RDWR
);
107 if ((device_fd
= open(device
, O_RDWR
)) < 0)
110 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
122 if (device_fd
>= 0) {
123 ioctl(device_fd
, LOOP_CLR_FD
, 0);
130 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
131 const char* loop_device
= "/dev/loop0";
133 // Create target if it does not exist
134 if (access(target
, X_OK
) != 0)
135 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
140 if (S_ISREG(st
.st_mode
)) {
141 int r
= setup_loop_device(source
, loop_device
);
143 source
= loop_device
;
149 int r
= mount(source
, target
, fs
, flags
, NULL
);
152 fprintf(stderr
, "Error mounting %s to %s (fs = %s, flags = %d): %s\n",
153 source
, target
, fs
, flags
, strerror(r
));
159 int hw_umount(const char* target
) {
160 int r
= umount2(target
, 0);
162 if (r
&& errno
== EBUSY
) {
163 // Give it a moment to settle
166 r
= umount2(target
, MNT_FORCE
);
172 static int hw_test_source_medium(const char* path
) {
173 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
175 // If the source could not be mounted we
180 // Check if the test file exists.
181 ret
= access(SOURCE_TEST_FILE
, R_OK
);
183 // Umount the test device.
184 hw_umount(SOURCE_MOUNT_PATH
);
189 char* hw_find_source_medium(struct hw
* hw
) {
192 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
194 udev_enumerate_add_match_subsystem(enumerate
, "block");
195 udev_enumerate_scan_devices(enumerate
);
197 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
199 struct udev_list_entry
* dev_list_entry
;
200 udev_list_entry_foreach(dev_list_entry
, devices
) {
201 const char* path
= udev_list_entry_get_name(dev_list_entry
);
202 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
204 const char* dev_path
= udev_device_get_devnode(dev
);
206 // Skip everything what we cannot work with
207 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
208 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
211 if (hw_test_source_medium(dev_path
) == 0) {
212 ret
= strdup(dev_path
);
215 udev_device_unref(dev
);
217 // If a suitable device was found the search will end.
222 udev_enumerate_unref(enumerate
);
227 static struct hw_disk
** hw_create_disks() {
228 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
233 static unsigned long long hw_block_device_get_size(const char* dev
) {
234 int fd
= open(dev
, O_RDONLY
);
238 unsigned long long size
= blkid_get_dev_size(fd
);
244 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
245 struct hw_disk
** ret
= hw_create_disks();
246 struct hw_disk
** disks
= ret
;
248 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
250 udev_enumerate_add_match_subsystem(enumerate
, "block");
251 udev_enumerate_scan_devices(enumerate
);
253 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
255 struct udev_list_entry
* dev_list_entry
;
256 unsigned int i
= HW_MAX_DISKS
;
257 udev_list_entry_foreach(dev_list_entry
, devices
) {
258 const char* path
= udev_list_entry_get_name(dev_list_entry
);
259 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
261 const char* dev_path
= udev_device_get_devnode(dev
);
263 // Skip everything what we cannot work with
264 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
265 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
266 strstartswith(dev_path
, "/dev/md")) {
267 udev_device_unref(dev
);
271 // Skip sourcedrive if we need to
272 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
273 udev_device_unref(dev
);
277 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
278 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
279 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
280 udev_device_unref(dev
);
284 // Skip devices with a size of zero
285 unsigned long long size
= hw_block_device_get_size(dev_path
);
287 udev_device_unref(dev
);
291 struct hw_disk
* disk
= malloc(sizeof(*disk
));
297 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
298 const char* p
= disk
->path
+ 5;
303 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
305 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
307 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
310 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
312 *disk
->vendor
= '\0';
315 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
317 model
= udev_device_get_sysattr_value(dev
, "model");
319 model
= udev_device_get_sysattr_value(dev
, "product");
322 strncpy(disk
->model
, model
, sizeof(disk
->model
));
326 // Format description
327 char size_str
[STRING_SIZE
];
328 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
330 if (*disk
->vendor
&& *disk
->model
) {
331 snprintf(disk
->description
, sizeof(disk
->description
),
332 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
334 } else if (*disk
->vendor
|| *disk
->model
) {
335 snprintf(disk
->description
, sizeof(disk
->description
),
336 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
339 snprintf(disk
->description
, sizeof(disk
->description
),
340 "%s - %s", size_str
, p
);
343 // Cut off the description string after 40 characters
344 disk
->description
[41] = '\0';
351 udev_device_unref(dev
);
354 udev_enumerate_unref(enumerate
);
361 void hw_free_disks(struct hw_disk
** disks
) {
362 struct hw_disk
** disk
= disks
;
364 while (*disk
!= NULL
) {
365 if (--(*disk
)->ref
== 0)
374 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
375 unsigned int ret
= 0;
383 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
384 struct hw_disk
** ret
= hw_create_disks();
385 struct hw_disk
** selected_disks
= ret
;
387 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
389 for (unsigned int i
= 0; i
< num_disks
; i
++) {
390 if (!selection
|| selection
[i
]) {
391 struct hw_disk
*selected_disk
= disks
[i
];
392 selected_disk
->ref
++;
394 *selected_disks
++ = selected_disk
;
399 *selected_disks
= NULL
;
404 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
405 struct hw_disk
** ret
= hw_create_disks();
406 struct hw_disk
** selected_disks
= ret
;
408 unsigned int num_disks
= hw_count_disks(disks
);
409 assert(num_disks
> 0);
411 for (unsigned int i
= 0; i
< num_disks
; i
++) {
412 struct hw_disk
*disk
= disks
[i
];
415 *selected_disks
++ = disk
;
420 *selected_disks
= NULL
;
425 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
426 unsigned long long memory
= hw_memory();
428 unsigned long long swap_size
= memory
/ 4;
430 // Min. swap size is 128MB
431 if (swap_size
< MB2BYTES(128))
432 swap_size
= MB2BYTES(128);
434 // Cap swap size to 1GB
435 else if (swap_size
> MB2BYTES(1024))
436 swap_size
= MB2BYTES(1024);
441 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
442 return MB2BYTES(128);
445 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
446 // All RAID devices have the p suffix.
450 // Devices with a number at the end have the p suffix, too.
451 // e.g. mmcblk0, cciss0
452 unsigned int last_char
= strlen(dest
->path
) - 1;
453 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
459 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
463 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
464 hw_device_has_p_suffix(dest
) ? "p" : "");
465 dest
->part_boot_idx
= 0;
467 // Determine the size of the target block device
469 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
470 dest
->disk2
->size
: dest
->disk1
->size
;
472 // The RAID will install some metadata at the end of the disk
473 // and we will save up some space for that.
474 dest
->size
-= MB2BYTES(2);
476 dest
->size
= dest
->disk1
->size
;
479 // As we add some extra space before the beginning of the first
480 // partition, we need to substract that here.
481 dest
->size
-= MB2BYTES(1);
483 // Add some more space for partition tables, etc.
484 dest
->size
-= MB2BYTES(1);
486 // The disk has to have at least 2GB
487 if (dest
->size
<= MB2BYTES(2048))
490 // Determine partition table
491 dest
->part_table
= HW_PART_TABLE_MSDOS
;
493 // Disks over 2TB need to use GPT
494 if (dest
->size
>= MB2BYTES(2047 * 1024))
495 dest
->part_table
= HW_PART_TABLE_GPT
;
497 // We also use GPT on raid disks by default
498 else if (dest
->is_raid
)
499 dest
->part_table
= HW_PART_TABLE_GPT
;
501 // When using GPT, GRUB2 needs a little bit of space to put
503 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
504 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
505 "%s%d", path
, part_idx
);
507 dest
->size_bootldr
= MB2BYTES(4);
509 dest
->part_boot_idx
= part_idx
++;
511 *dest
->part_bootldr
= '\0';
512 dest
->size_bootldr
= 0;
515 dest
->size_boot
= hw_boot_size(dest
);
517 // Create an EFI partition when running in EFI mode
519 dest
->size_boot_efi
= MB2BYTES(32);
521 dest
->size_boot_efi
= 0;
523 // Determine the size of the data partition.
524 unsigned long long space_left
= dest
->size
- \
525 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
527 // If we have less than 2GB left, we disable swap
528 if (space_left
<= MB2BYTES(2048))
531 // Should we use swap?
535 dest
->size_swap
= hw_swap_size(dest
);
538 space_left
-= dest
->size_swap
;
540 // Root is getting what ever is left
541 dest
->size_root
= space_left
;
543 // Set partition names
544 if (dest
->size_boot
> 0) {
545 if (dest
->part_boot_idx
== 0)
546 dest
->part_boot_idx
= part_idx
;
548 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
550 *dest
->part_boot
= '\0';
552 if (dest
->size_boot_efi
> 0) {
553 dest
->part_boot_efi_idx
= part_idx
;
555 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
556 "%s%d", path
, part_idx
++);
558 *dest
->part_boot_efi
= '\0';
560 if (dest
->size_swap
> 0)
561 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
563 *dest
->part_swap
= '\0';
565 // There is always a root partition
566 if (dest
->part_boot_idx
== 0)
567 dest
->part_boot_idx
= part_idx
;
569 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
574 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
575 struct hw_destination
* dest
= malloc(sizeof(*dest
));
577 if (part_type
== HW_PART_TYPE_NORMAL
) {
578 dest
->disk1
= *disks
;
581 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
583 } else if (part_type
== HW_PART_TYPE_RAID1
) {
584 dest
->disk1
= *disks
++;
585 dest
->disk2
= *disks
;
586 dest
->raid_level
= 1;
588 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
591 // Is this a RAID device?
592 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
594 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
598 // Set default filesystem
599 dest
->filesystem
= HW_FS_DEFAULT
;
604 unsigned long long hw_memory() {
607 int r
= sysinfo(&si
);
614 static int hw_zero_out_device(const char* path
, int bytes
) {
616 memset(block
, 0, sizeof(block
));
618 int blocks
= bytes
/ sizeof(block
);
620 int fd
= open(path
, O_WRONLY
);
624 unsigned int bytes_written
= 0;
625 while (blocks
-- > 0) {
626 bytes_written
+= write(fd
, block
, sizeof(block
));
632 return bytes_written
;
635 static int try_open(const char* path
) {
636 FILE* f
= fopen(path
, "r");
645 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
646 // Before we write a new partition table to the disk, we will erase
647 // the first couple of megabytes at the beginning of the device to
648 // get rid of all left other things like bootloaders and partition tables.
649 // This solves some problems when changing from MBR to GPT partitions or
650 // the other way around.
651 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
656 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
658 // Set partition type
659 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
660 asprintf(&cmd
, "%s mklabel msdos", cmd
);
661 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
662 asprintf(&cmd
, "%s mklabel gpt", cmd
);
664 unsigned long long part_start
= MB2BYTES(1);
666 if (*dest
->part_bootldr
) {
667 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
668 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
669 part_start
, part_start
+ dest
->size_bootldr
- 1);
671 part_start
+= dest
->size_bootldr
;
674 if (*dest
->part_boot
) {
675 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
676 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
677 part_start
, part_start
+ dest
->size_boot
- 1);
679 part_start
+= dest
->size_boot
;
682 if (*dest
->part_boot_efi
) {
683 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
684 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
685 part_start
, part_start
+ dest
->size_boot_efi
- 1);
687 part_start
+= dest
->size_boot_efi
;
690 if (*dest
->part_swap
) {
691 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
692 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
693 part_start
, part_start
+ dest
->size_swap
- 1);
695 part_start
+= dest
->size_swap
;
698 if (*dest
->part_root
) {
699 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
700 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
701 part_start
, part_start
+ dest
->size_root
- 1);
703 part_start
+= dest
->size_root
;
706 if (dest
->part_boot_idx
> 0)
707 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
709 if (dest
->part_boot_efi_idx
> 0)
710 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
712 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
713 if (*dest
->part_bootldr
) {
714 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
716 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
719 r
= mysystem(output
, cmd
);
721 // Wait until the system re-read the partition table
723 unsigned int counter
= 10;
725 while (counter
-- > 0) {
728 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
731 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
734 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
737 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
740 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
743 // All partitions do exist, exiting the loop.
754 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
755 char cmd
[STRING_SIZE
] = "\0";
758 if (fs
== HW_FS_SWAP
) {
759 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
761 } else if (fs
== HW_FS_REISERFS
) {
762 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
765 } else if (fs
== HW_FS_EXT4
) {
766 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
769 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
770 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
773 } else if (fs
== HW_FS_XFS
) {
774 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
777 } else if (fs
== HW_FS_FAT32
) {
778 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
783 int r
= mysystem(output
, cmd
);
788 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
792 if (*dest
->part_boot
) {
793 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
799 if (*dest
->part_boot_efi
) {
800 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
806 if (*dest
->part_swap
) {
807 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
813 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
820 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
821 char target
[STRING_SIZE
];
823 assert(*prefix
== '/');
825 const char* filesystem
;
826 switch (dest
->filesystem
) {
828 filesystem
= "reiserfs";
832 case HW_FS_EXT4_WO_JOURNAL
:
849 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
854 if (*dest
->part_boot
) {
855 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
856 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
858 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
860 hw_umount_filesystems(dest
, prefix
);
867 if (*dest
->part_boot_efi
) {
868 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
869 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
871 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
873 hw_umount_filesystems(dest
, prefix
);
880 if (*dest
->part_swap
) {
881 r
= swapon(dest
->part_swap
, 0);
883 hw_umount_filesystems(dest
, prefix
);
889 // bind-mount misc filesystems
890 char** otherfs
= other_filesystems
;
892 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
894 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
895 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
897 hw_umount_filesystems(dest
, prefix
);
908 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
910 char target
[STRING_SIZE
];
912 // Write all buffers to disk before umounting
916 if (*dest
->part_boot_efi
) {
917 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
918 r
= hw_umount(target
);
924 if (*dest
->part_boot
) {
925 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
926 r
= hw_umount(target
);
932 if (*dest
->part_swap
) {
933 swapoff(dest
->part_swap
);
937 char** otherfs
= other_filesystems
;
939 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
940 r
= hw_umount(target
);
946 r
= hw_umount(prefix
);
953 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
954 char cmd
[STRING_SIZE
];
956 hw_stop_all_raid_arrays(output
);
957 hw_stop_all_raid_arrays(output
);
960 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
961 mysystem(output
, cmd
);
965 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
966 mysystem(output
, cmd
);
972 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
976 assert(dest
->is_raid
);
978 // Stop all RAID arrays that might be around (again).
979 // It seems that there is some sort of race-condition with udev re-enabling
980 // the raid arrays and therefore locking the disks.
981 r
= hw_destroy_raid_superblocks(dest
, output
);
983 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
984 RAID_METADATA
, dest
->path
);
986 switch (dest
->raid_level
) {
988 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
996 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
998 // Clear all data at the beginning
999 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1005 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1007 // Clear all data at the beginning
1008 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1013 r
= mysystem(output
, cmd
);
1016 // Wait a moment until the device has been properly brought up
1018 unsigned int counter
= 10;
1019 while (counter
-- > 0) {
1022 // If the raid device has not yet been properly brought up,
1023 // opening it will fail with the message: Device or resource busy
1024 // Hence we will wait a bit until it becomes usable.
1025 if (try_open(dest
->path
) == 0)
1033 int hw_stop_all_raid_arrays(const char* output
) {
1034 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1037 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1038 char cmd
[STRING_SIZE
];
1040 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1041 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1050 static char* hw_get_uuid(const char* dev
) {
1051 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1052 const char* buffer
= NULL
;
1059 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1062 uuid
= strdup(buffer
);
1064 blkid_free_probe(p
);
1069 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1071 int hw_write_fstab(struct hw_destination
* dest
) {
1072 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1079 if (*dest
->part_boot
) {
1080 uuid
= hw_get_uuid(dest
->part_boot
);
1083 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1089 if (*dest
->part_boot_efi
) {
1090 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1093 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1100 if (*dest
->part_swap
) {
1101 uuid
= hw_get_uuid(dest
->part_swap
);
1104 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1110 uuid
= hw_get_uuid(dest
->part_root
);
1112 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1127 int hw_start_networking(const char* output
) {
1128 return mysystem(output
, "/usr/bin/start-networking.sh");
1131 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1132 char path
[STRING_SIZE
];
1134 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1135 int r
= access(path
, R_OK
);
1138 return strdup(path
);
1143 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1144 char command
[STRING_SIZE
];
1146 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1147 int rc
= mysystem(output
, command
);