]>
git.ipfire.org Git - people/pmueller/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 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
50 char chroot_cmd
[STRING_SIZE
];
52 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
54 return mysystem(output
, chroot_cmd
);
57 struct hw
* hw_init() {
58 struct hw
* hw
= calloc(1, sizeof(*hw
));
62 hw
->udev
= udev_new();
64 fprintf(stderr
, "Could not create udev instance\n");
68 // What architecture are we running on?
69 struct utsname uname_data
;
70 int ret
= uname(&uname_data
);
72 snprintf(hw
->arch
, sizeof(hw
->arch
), "%s", uname_data
.machine
);
74 // Should we install in EFI mode?
75 if ((strcmp(hw
->arch
, "x86_64") == 0) || (strcmp(hw
->arch
, "aarch64") == 0))
81 void hw_free(struct hw
* hw
) {
88 static int strstartswith(const char* a
, const char* b
) {
89 return (strncmp(a
, b
, strlen(b
)) == 0);
92 static char loop_device
[STRING_SIZE
];
94 static int setup_loop_device(const char* source
, const char* device
) {
95 int file_fd
= open(source
, O_RDWR
);
100 if ((device_fd
= open(device
, O_RDWR
)) < 0)
103 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
115 if (device_fd
>= 0) {
116 ioctl(device_fd
, LOOP_CLR_FD
, 0);
123 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
124 const char* loop_device
= "/dev/loop0";
126 // Create target if it does not exist
127 if (access(target
, X_OK
) != 0)
128 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
133 if (S_ISREG(st
.st_mode
)) {
134 int r
= setup_loop_device(source
, loop_device
);
136 source
= loop_device
;
142 return mount(source
, target
, fs
, flags
, NULL
);
145 static int hw_bind_mount(const char* source
, const char* prefix
) {
146 if (!source
|| !prefix
) {
151 char target
[PATH_MAX
];
155 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
159 // Ensure target exists
160 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
162 return hw_mount(source
, target
, NULL
, MS_BIND
);
165 int hw_umount(const char* source
, const char* prefix
) {
166 char target
[PATH_MAX
];
170 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
172 r
= snprintf(target
, sizeof(target
) - 1, "%s", source
);
177 r
= umount2(target
, 0);
180 // Try again with force if umount wasn't successful
184 r
= umount2(target
, MNT_FORCE
);
187 // target wasn't a mountpoint. Ignore.
197 static int hw_test_source_medium(const char* path
) {
198 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
200 // If the source could not be mounted we
205 // Check if the test file exists.
206 ret
= access(SOURCE_TEST_FILE
, R_OK
);
208 // Umount the test device.
209 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
214 char* hw_find_source_medium(struct hw
* hw
) {
217 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
219 udev_enumerate_add_match_subsystem(enumerate
, "block");
220 udev_enumerate_scan_devices(enumerate
);
222 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
224 struct udev_list_entry
* dev_list_entry
;
225 udev_list_entry_foreach(dev_list_entry
, devices
) {
226 const char* path
= udev_list_entry_get_name(dev_list_entry
);
227 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
229 const char* dev_path
= udev_device_get_devnode(dev
);
231 // Skip everything what we cannot work with
232 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
233 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
236 if (hw_test_source_medium(dev_path
) == 0) {
237 ret
= strdup(dev_path
);
240 udev_device_unref(dev
);
242 // If a suitable device was found the search will end.
247 udev_enumerate_unref(enumerate
);
252 static struct hw_disk
** hw_create_disks() {
253 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
258 static unsigned long long hw_block_device_get_size(const char* dev
) {
259 int fd
= open(dev
, O_RDONLY
);
263 unsigned long long size
= blkid_get_dev_size(fd
);
269 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
270 struct hw_disk
** ret
= hw_create_disks();
271 struct hw_disk
** disks
= ret
;
273 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
275 udev_enumerate_add_match_subsystem(enumerate
, "block");
276 udev_enumerate_scan_devices(enumerate
);
278 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
280 struct udev_list_entry
* dev_list_entry
;
281 unsigned int i
= HW_MAX_DISKS
;
282 udev_list_entry_foreach(dev_list_entry
, devices
) {
283 const char* path
= udev_list_entry_get_name(dev_list_entry
);
284 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
286 const char* dev_path
= udev_device_get_devnode(dev
);
288 // Skip everything what we cannot work with
289 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
290 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
291 strstartswith(dev_path
, "/dev/md")) {
292 udev_device_unref(dev
);
296 // Skip sourcedrive if we need to
297 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
298 udev_device_unref(dev
);
302 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
303 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
304 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
305 udev_device_unref(dev
);
309 // Skip devices with a size of zero
310 unsigned long long size
= hw_block_device_get_size(dev_path
);
312 udev_device_unref(dev
);
316 struct hw_disk
* disk
= malloc(sizeof(*disk
));
322 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
323 const char* p
= disk
->path
+ 5;
328 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
330 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
332 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
335 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
337 *disk
->vendor
= '\0';
340 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
342 model
= udev_device_get_sysattr_value(dev
, "model");
344 model
= udev_device_get_sysattr_value(dev
, "product");
347 strncpy(disk
->model
, model
, sizeof(disk
->model
));
351 // Format description
352 char size_str
[STRING_SIZE
];
353 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
355 if (*disk
->vendor
&& *disk
->model
) {
356 snprintf(disk
->description
, sizeof(disk
->description
),
357 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
359 } else if (*disk
->vendor
|| *disk
->model
) {
360 snprintf(disk
->description
, sizeof(disk
->description
),
361 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
364 snprintf(disk
->description
, sizeof(disk
->description
),
365 "%s - %s", size_str
, p
);
368 // Cut off the description string after 40 characters
369 disk
->description
[41] = '\0';
376 udev_device_unref(dev
);
379 udev_enumerate_unref(enumerate
);
386 void hw_free_disks(struct hw_disk
** disks
) {
387 struct hw_disk
** disk
= disks
;
389 while (*disk
!= NULL
) {
390 if (--(*disk
)->ref
== 0)
399 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
400 unsigned int ret
= 0;
408 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
409 struct hw_disk
** ret
= hw_create_disks();
410 struct hw_disk
** selected_disks
= ret
;
412 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
414 for (unsigned int i
= 0; i
< num_disks
; i
++) {
415 if (!selection
|| selection
[i
]) {
416 struct hw_disk
*selected_disk
= disks
[i
];
417 selected_disk
->ref
++;
419 *selected_disks
++ = selected_disk
;
424 *selected_disks
= NULL
;
429 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
430 struct hw_disk
** ret
= hw_create_disks();
431 struct hw_disk
** selected_disks
= ret
;
433 unsigned int num_disks
= hw_count_disks(disks
);
434 assert(num_disks
> 0);
436 for (unsigned int i
= 0; i
< num_disks
; i
++) {
437 struct hw_disk
*disk
= disks
[i
];
440 *selected_disks
++ = disk
;
445 *selected_disks
= NULL
;
450 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
451 unsigned long long memory
= hw_memory();
453 unsigned long long swap_size
= memory
/ 4;
455 // Min. swap size is 128MB
456 if (swap_size
< MB2BYTES(128))
457 swap_size
= MB2BYTES(128);
459 // Cap swap size to 1GB
460 else if (swap_size
> MB2BYTES(1024))
461 swap_size
= MB2BYTES(1024);
466 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
467 return MB2BYTES(128);
470 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
471 // All RAID devices have the p suffix.
475 // Devices with a number at the end have the p suffix, too.
476 // e.g. mmcblk0, cciss0
477 unsigned int last_char
= strlen(dest
->path
) - 1;
478 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
484 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
488 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
489 hw_device_has_p_suffix(dest
) ? "p" : "");
490 dest
->part_boot_idx
= 0;
492 // Determine the size of the target block device
494 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
495 dest
->disk2
->size
: dest
->disk1
->size
;
497 // The RAID will install some metadata at the end of the disk
498 // and we will save up some space for that.
499 dest
->size
-= MB2BYTES(2);
501 dest
->size
= dest
->disk1
->size
;
504 // As we add some extra space before the beginning of the first
505 // partition, we need to substract that here.
506 dest
->size
-= MB2BYTES(1);
508 // Add some more space for partition tables, etc.
509 dest
->size
-= MB2BYTES(1);
511 // The disk has to have at least 2GB
512 if (dest
->size
<= MB2BYTES(2048))
515 // Determine partition table
516 dest
->part_table
= HW_PART_TABLE_MSDOS
;
518 // Disks over 2TB need to use GPT
519 if (dest
->size
>= MB2BYTES(2047 * 1024))
520 dest
->part_table
= HW_PART_TABLE_GPT
;
522 // We also use GPT on raid disks by default
523 else if (dest
->is_raid
)
524 dest
->part_table
= HW_PART_TABLE_GPT
;
526 // When using GPT, GRUB2 needs a little bit of space to put
528 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
529 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
530 "%s%d", path
, part_idx
);
532 dest
->size_bootldr
= MB2BYTES(4);
534 dest
->part_boot_idx
= part_idx
++;
536 *dest
->part_bootldr
= '\0';
537 dest
->size_bootldr
= 0;
540 dest
->size_boot
= hw_boot_size(dest
);
542 // Create an EFI partition when running in EFI mode
544 dest
->size_boot_efi
= MB2BYTES(32);
546 dest
->size_boot_efi
= 0;
548 // Determine the size of the data partition.
549 unsigned long long space_left
= dest
->size
- \
550 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
552 // If we have less than 2GB left, we disable swap
553 if (space_left
<= MB2BYTES(2048))
556 // Should we use swap?
560 dest
->size_swap
= hw_swap_size(dest
);
563 space_left
-= dest
->size_swap
;
565 // Root is getting what ever is left
566 dest
->size_root
= space_left
;
568 // Set partition names
569 if (dest
->size_boot
> 0) {
570 if (dest
->part_boot_idx
== 0)
571 dest
->part_boot_idx
= part_idx
;
573 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
575 *dest
->part_boot
= '\0';
577 if (dest
->size_boot_efi
> 0) {
578 dest
->part_boot_efi_idx
= part_idx
;
580 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
581 "%s%d", path
, part_idx
++);
583 *dest
->part_boot_efi
= '\0';
584 dest
->part_boot_efi_idx
= 0;
587 if (dest
->size_swap
> 0)
588 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
590 *dest
->part_swap
= '\0';
592 // There is always a root partition
593 if (dest
->part_boot_idx
== 0)
594 dest
->part_boot_idx
= part_idx
;
596 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
601 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
602 struct hw_destination
* dest
= malloc(sizeof(*dest
));
604 if (part_type
== HW_PART_TYPE_NORMAL
) {
605 dest
->disk1
= *disks
;
608 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
610 } else if (part_type
== HW_PART_TYPE_RAID1
) {
611 dest
->disk1
= *disks
++;
612 dest
->disk2
= *disks
;
613 dest
->raid_level
= 1;
615 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
618 // Is this a RAID device?
619 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
621 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
625 // Set default filesystem
626 dest
->filesystem
= HW_FS_DEFAULT
;
631 unsigned long long hw_memory() {
634 int r
= sysinfo(&si
);
641 static int hw_zero_out_device(const char* path
, int bytes
) {
643 memset(block
, 0, sizeof(block
));
645 int blocks
= bytes
/ sizeof(block
);
647 int fd
= open(path
, O_WRONLY
);
651 unsigned int bytes_written
= 0;
652 while (blocks
-- > 0) {
653 bytes_written
+= write(fd
, block
, sizeof(block
));
659 return bytes_written
;
662 static int try_open(const char* path
) {
663 FILE* f
= fopen(path
, "r");
672 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
673 // Before we write a new partition table to the disk, we will erase
674 // the first couple of megabytes at the beginning of the device to
675 // get rid of all left other things like bootloaders and partition tables.
676 // This solves some problems when changing from MBR to GPT partitions or
677 // the other way around.
678 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
683 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
685 // Set partition type
686 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
687 asprintf(&cmd
, "%s mklabel msdos", cmd
);
688 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
689 asprintf(&cmd
, "%s mklabel gpt", cmd
);
691 unsigned long long part_start
= MB2BYTES(1);
693 if (*dest
->part_bootldr
) {
694 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
695 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
696 part_start
, part_start
+ dest
->size_bootldr
- 1);
698 part_start
+= dest
->size_bootldr
;
701 if (*dest
->part_boot
) {
702 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
703 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
704 part_start
, part_start
+ dest
->size_boot
- 1);
706 part_start
+= dest
->size_boot
;
709 if (*dest
->part_boot_efi
) {
710 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
711 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
712 part_start
, part_start
+ dest
->size_boot_efi
- 1);
714 part_start
+= dest
->size_boot_efi
;
717 if (*dest
->part_swap
) {
718 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
719 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
720 part_start
, part_start
+ dest
->size_swap
- 1);
722 part_start
+= dest
->size_swap
;
725 if (*dest
->part_root
) {
726 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
727 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
728 part_start
, part_start
+ dest
->size_root
- 1);
730 part_start
+= dest
->size_root
;
733 if (dest
->part_boot_idx
> 0)
734 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
736 if (dest
->part_boot_efi_idx
> 0)
737 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
739 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
740 if (*dest
->part_bootldr
) {
741 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
745 r
= mysystem(output
, cmd
);
747 // Wait until the system re-read the partition table
749 unsigned int counter
= 10;
751 while (counter
-- > 0) {
754 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
757 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
760 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
763 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
766 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
769 // All partitions do exist, exiting the loop.
780 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
781 char cmd
[STRING_SIZE
] = "\0";
784 if (fs
== HW_FS_SWAP
) {
785 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
787 } else if (fs
== HW_FS_REISERFS
) {
788 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
791 } else if (fs
== HW_FS_EXT4
) {
792 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
795 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
796 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
799 } else if (fs
== HW_FS_XFS
) {
800 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
803 } else if (fs
== HW_FS_FAT32
) {
804 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
809 int r
= mysystem(output
, cmd
);
814 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
818 if (*dest
->part_boot
) {
819 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
825 if (*dest
->part_boot_efi
) {
826 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
832 if (*dest
->part_swap
) {
833 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
839 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
846 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
847 char target
[STRING_SIZE
];
849 assert(*prefix
== '/');
851 const char* filesystem
;
852 switch (dest
->filesystem
) {
854 filesystem
= "reiserfs";
858 case HW_FS_EXT4_WO_JOURNAL
:
875 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
880 if (*dest
->part_boot
) {
881 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
882 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
884 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
886 hw_umount_filesystems(dest
, prefix
);
893 if (*dest
->part_boot_efi
) {
894 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
895 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
897 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
899 hw_umount_filesystems(dest
, prefix
);
906 if (*dest
->part_swap
) {
907 r
= swapon(dest
->part_swap
, 0);
909 hw_umount_filesystems(dest
, prefix
);
915 // bind-mount misc filesystems
916 r
= hw_bind_mount("/dev", prefix
);
920 r
= hw_bind_mount("/proc", prefix
);
924 r
= hw_bind_mount("/sys", prefix
);
928 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
929 if (r
&& errno
!= ENOENT
)
935 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
937 char target
[STRING_SIZE
];
939 // Write all buffers to disk before umounting
943 if (*dest
->part_boot_efi
) {
944 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
950 if (*dest
->part_boot
) {
951 r
= hw_umount(HW_PATH_BOOT
, prefix
);
957 if (*dest
->part_swap
) {
958 swapoff(dest
->part_swap
);
962 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
966 r
= hw_umount("/sys", prefix
);
970 r
= hw_umount("/proc", prefix
);
974 r
= hw_umount("/dev", prefix
);
979 r
= hw_umount(prefix
, NULL
);
986 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
987 char cmd
[STRING_SIZE
];
989 hw_stop_all_raid_arrays(output
);
990 hw_stop_all_raid_arrays(output
);
993 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
994 mysystem(output
, cmd
);
998 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
999 mysystem(output
, cmd
);
1005 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1009 assert(dest
->is_raid
);
1011 // Stop all RAID arrays that might be around (again).
1012 // It seems that there is some sort of race-condition with udev re-enabling
1013 // the raid arrays and therefore locking the disks.
1014 r
= hw_destroy_raid_superblocks(dest
, output
);
1016 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1017 RAID_METADATA
, dest
->path
);
1019 switch (dest
->raid_level
) {
1021 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1029 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1031 // Clear all data at the beginning
1032 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1038 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1040 // Clear all data at the beginning
1041 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1046 r
= mysystem(output
, cmd
);
1049 // Wait a moment until the device has been properly brought up
1051 unsigned int counter
= 10;
1052 while (counter
-- > 0) {
1055 // If the raid device has not yet been properly brought up,
1056 // opening it will fail with the message: Device or resource busy
1057 // Hence we will wait a bit until it becomes usable.
1058 if (try_open(dest
->path
) == 0)
1066 int hw_stop_all_raid_arrays(const char* output
) {
1067 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1070 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1071 char cmd
[STRING_SIZE
];
1073 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1074 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1083 static char* hw_get_uuid(const char* dev
) {
1084 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1085 const char* buffer
= NULL
;
1092 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1095 uuid
= strdup(buffer
);
1097 blkid_free_probe(p
);
1102 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1104 int hw_write_fstab(struct hw_destination
* dest
) {
1105 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1112 if (*dest
->part_boot
) {
1113 uuid
= hw_get_uuid(dest
->part_boot
);
1116 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1122 if (*dest
->part_boot_efi
) {
1123 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1126 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1133 if (*dest
->part_swap
) {
1134 uuid
= hw_get_uuid(dest
->part_swap
);
1137 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1143 uuid
= hw_get_uuid(dest
->part_root
);
1145 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1160 int hw_start_networking(const char* output
) {
1161 return mysystem(output
, "/usr/bin/start-networking.sh");
1164 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1165 char path
[STRING_SIZE
];
1167 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1168 int r
= access(path
, R_OK
);
1171 return strdup(path
);
1176 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1177 char command
[STRING_SIZE
];
1179 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1180 int rc
= mysystem(output
, command
);