]>
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 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.
192 // target doesn't exist
202 static int hw_test_source_medium(const char* path
) {
203 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
205 // If the source could not be mounted we
210 // Check if the test file exists.
211 ret
= access(SOURCE_TEST_FILE
, R_OK
);
213 // Umount the test device.
214 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
219 char* hw_find_source_medium(struct hw
* hw
) {
222 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
224 udev_enumerate_add_match_subsystem(enumerate
, "block");
225 udev_enumerate_scan_devices(enumerate
);
227 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
229 struct udev_list_entry
* dev_list_entry
;
230 udev_list_entry_foreach(dev_list_entry
, devices
) {
231 const char* path
= udev_list_entry_get_name(dev_list_entry
);
232 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
234 const char* dev_path
= udev_device_get_devnode(dev
);
236 // Skip everything what we cannot work with
237 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
238 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
241 if (hw_test_source_medium(dev_path
) == 0) {
242 ret
= strdup(dev_path
);
245 udev_device_unref(dev
);
247 // If a suitable device was found the search will end.
252 udev_enumerate_unref(enumerate
);
257 static struct hw_disk
** hw_create_disks() {
258 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
263 static unsigned long long hw_block_device_get_size(const char* dev
) {
264 int fd
= open(dev
, O_RDONLY
);
268 unsigned long long size
= blkid_get_dev_size(fd
);
274 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
275 struct hw_disk
** ret
= hw_create_disks();
276 struct hw_disk
** disks
= ret
;
278 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
280 udev_enumerate_add_match_subsystem(enumerate
, "block");
281 udev_enumerate_scan_devices(enumerate
);
283 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
285 struct udev_list_entry
* dev_list_entry
;
286 unsigned int i
= HW_MAX_DISKS
;
287 udev_list_entry_foreach(dev_list_entry
, devices
) {
288 const char* path
= udev_list_entry_get_name(dev_list_entry
);
289 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
291 const char* dev_path
= udev_device_get_devnode(dev
);
293 // Skip everything what we cannot work with
294 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
295 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
296 strstartswith(dev_path
, "/dev/md")) {
297 udev_device_unref(dev
);
301 // Skip sourcedrive if we need to
302 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
303 udev_device_unref(dev
);
307 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
308 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
309 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
310 udev_device_unref(dev
);
314 // Skip devices with a size of zero
315 unsigned long long size
= hw_block_device_get_size(dev_path
);
317 udev_device_unref(dev
);
321 struct hw_disk
* disk
= malloc(sizeof(*disk
));
327 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
328 const char* p
= disk
->path
+ 5;
333 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
335 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
337 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
340 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
342 *disk
->vendor
= '\0';
345 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
347 model
= udev_device_get_sysattr_value(dev
, "model");
349 model
= udev_device_get_sysattr_value(dev
, "product");
352 strncpy(disk
->model
, model
, sizeof(disk
->model
));
356 // Format description
357 char size_str
[STRING_SIZE
];
358 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
360 if (*disk
->vendor
&& *disk
->model
) {
361 snprintf(disk
->description
, sizeof(disk
->description
),
362 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
364 } else if (*disk
->vendor
|| *disk
->model
) {
365 snprintf(disk
->description
, sizeof(disk
->description
),
366 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
369 snprintf(disk
->description
, sizeof(disk
->description
),
370 "%s - %s", size_str
, p
);
373 // Cut off the description string after 40 characters
374 disk
->description
[41] = '\0';
381 udev_device_unref(dev
);
384 udev_enumerate_unref(enumerate
);
391 void hw_free_disks(struct hw_disk
** disks
) {
392 struct hw_disk
** disk
= disks
;
394 while (*disk
!= NULL
) {
395 if (--(*disk
)->ref
== 0)
404 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
405 unsigned int ret
= 0;
413 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
414 struct hw_disk
** ret
= hw_create_disks();
415 struct hw_disk
** selected_disks
= ret
;
417 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
419 for (unsigned int i
= 0; i
< num_disks
; i
++) {
420 if (!selection
|| selection
[i
]) {
421 struct hw_disk
*selected_disk
= disks
[i
];
422 selected_disk
->ref
++;
424 *selected_disks
++ = selected_disk
;
429 *selected_disks
= NULL
;
434 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
435 struct hw_disk
** ret
= hw_create_disks();
436 struct hw_disk
** selected_disks
= ret
;
438 unsigned int num_disks
= hw_count_disks(disks
);
439 assert(num_disks
> 0);
441 for (unsigned int i
= 0; i
< num_disks
; i
++) {
442 struct hw_disk
*disk
= disks
[i
];
445 *selected_disks
++ = disk
;
450 *selected_disks
= NULL
;
455 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
456 unsigned long long memory
= hw_memory();
458 unsigned long long swap_size
= memory
/ 4;
460 // Min. swap size is 128MB
461 if (swap_size
< MB2BYTES(128))
462 swap_size
= MB2BYTES(128);
464 // Cap swap size to 1GB
465 else if (swap_size
> MB2BYTES(1024))
466 swap_size
= MB2BYTES(1024);
471 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
472 return MB2BYTES(128);
475 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
476 // All RAID devices have the p suffix.
480 // Devices with a number at the end have the p suffix, too.
481 // e.g. mmcblk0, cciss0
482 unsigned int last_char
= strlen(dest
->path
) - 1;
483 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
489 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
493 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
494 hw_device_has_p_suffix(dest
) ? "p" : "");
495 dest
->part_boot_idx
= 0;
497 // Determine the size of the target block device
499 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
500 dest
->disk2
->size
: dest
->disk1
->size
;
502 // The RAID will install some metadata at the end of the disk
503 // and we will save up some space for that.
504 dest
->size
-= MB2BYTES(2);
506 dest
->size
= dest
->disk1
->size
;
509 // As we add some extra space before the beginning of the first
510 // partition, we need to substract that here.
511 dest
->size
-= MB2BYTES(1);
513 // Add some more space for partition tables, etc.
514 dest
->size
-= MB2BYTES(1);
516 // The disk has to have at least 2GB
517 if (dest
->size
<= MB2BYTES(2048))
520 // Determine partition table
521 dest
->part_table
= HW_PART_TABLE_MSDOS
;
523 // Disks over 2TB need to use GPT
524 if (dest
->size
>= MB2BYTES(2047 * 1024))
525 dest
->part_table
= HW_PART_TABLE_GPT
;
527 // We also use GPT on raid disks by default
528 else if (dest
->is_raid
)
529 dest
->part_table
= HW_PART_TABLE_GPT
;
531 // When using GPT, GRUB2 needs a little bit of space to put
533 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
534 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
535 "%s%d", path
, part_idx
);
537 dest
->size_bootldr
= MB2BYTES(4);
539 dest
->part_boot_idx
= part_idx
++;
541 *dest
->part_bootldr
= '\0';
542 dest
->size_bootldr
= 0;
545 dest
->size_boot
= hw_boot_size(dest
);
547 // Create an EFI partition when running in EFI mode
549 dest
->size_boot_efi
= MB2BYTES(32);
551 dest
->size_boot_efi
= 0;
553 // Determine the size of the data partition.
554 unsigned long long space_left
= dest
->size
- \
555 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
557 // If we have less than 2GB left, we disable swap
558 if (space_left
<= MB2BYTES(2048))
561 // Should we use swap?
565 dest
->size_swap
= hw_swap_size(dest
);
568 space_left
-= dest
->size_swap
;
570 // Root is getting what ever is left
571 dest
->size_root
= space_left
;
573 // Set partition names
574 if (dest
->size_boot
> 0) {
575 if (dest
->part_boot_idx
== 0)
576 dest
->part_boot_idx
= part_idx
;
578 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
580 *dest
->part_boot
= '\0';
582 if (dest
->size_boot_efi
> 0) {
583 dest
->part_boot_efi_idx
= part_idx
;
585 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
586 "%s%d", path
, part_idx
++);
588 *dest
->part_boot_efi
= '\0';
589 dest
->part_boot_efi_idx
= 0;
592 if (dest
->size_swap
> 0)
593 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
595 *dest
->part_swap
= '\0';
597 // There is always a root partition
598 if (dest
->part_boot_idx
== 0)
599 dest
->part_boot_idx
= part_idx
;
601 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
606 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
607 struct hw_destination
* dest
= malloc(sizeof(*dest
));
609 if (part_type
== HW_PART_TYPE_NORMAL
) {
610 dest
->disk1
= *disks
;
613 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
615 } else if (part_type
== HW_PART_TYPE_RAID1
) {
616 dest
->disk1
= *disks
++;
617 dest
->disk2
= *disks
;
618 dest
->raid_level
= 1;
620 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
623 // Is this a RAID device?
624 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
626 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
630 // Set default filesystem
631 dest
->filesystem
= HW_FS_DEFAULT
;
636 unsigned long long hw_memory() {
639 int r
= sysinfo(&si
);
646 static int hw_zero_out_device(const char* path
, int bytes
) {
648 memset(block
, 0, sizeof(block
));
650 int blocks
= bytes
/ sizeof(block
);
652 int fd
= open(path
, O_WRONLY
);
656 unsigned int bytes_written
= 0;
657 while (blocks
-- > 0) {
658 bytes_written
+= write(fd
, block
, sizeof(block
));
664 return bytes_written
;
667 static int try_open(const char* path
) {
668 FILE* f
= fopen(path
, "r");
677 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
678 // Before we write a new partition table to the disk, we will erase
679 // the first couple of megabytes at the beginning of the device to
680 // get rid of all left other things like bootloaders and partition tables.
681 // This solves some problems when changing from MBR to GPT partitions or
682 // the other way around.
683 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
688 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
690 // Set partition type
691 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
692 asprintf(&cmd
, "%s mklabel msdos", cmd
);
693 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
694 asprintf(&cmd
, "%s mklabel gpt", cmd
);
696 unsigned long long part_start
= MB2BYTES(1);
698 if (*dest
->part_bootldr
) {
699 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
700 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
701 part_start
, part_start
+ dest
->size_bootldr
- 1);
703 part_start
+= dest
->size_bootldr
;
706 if (*dest
->part_boot
) {
707 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
708 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
709 part_start
, part_start
+ dest
->size_boot
- 1);
711 part_start
+= dest
->size_boot
;
714 if (*dest
->part_boot_efi
) {
715 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
716 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
717 part_start
, part_start
+ dest
->size_boot_efi
- 1);
719 part_start
+= dest
->size_boot_efi
;
722 if (*dest
->part_swap
) {
723 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
724 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
725 part_start
, part_start
+ dest
->size_swap
- 1);
727 part_start
+= dest
->size_swap
;
730 if (*dest
->part_root
) {
731 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
732 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
733 part_start
, part_start
+ dest
->size_root
- 1);
735 part_start
+= dest
->size_root
;
738 if (dest
->part_boot_idx
> 0)
739 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
741 if (dest
->part_boot_efi_idx
> 0)
742 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
744 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
745 if (*dest
->part_bootldr
) {
746 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
750 r
= mysystem(output
, cmd
);
752 // Wait until the system re-read the partition table
754 unsigned int counter
= 10;
756 while (counter
-- > 0) {
759 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
762 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
765 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
768 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
771 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
774 // All partitions do exist, exiting the loop.
785 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
786 char cmd
[STRING_SIZE
] = "\0";
789 if (fs
== HW_FS_SWAP
) {
790 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
792 } else if (fs
== HW_FS_REISERFS
) {
793 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
796 } else if (fs
== HW_FS_EXT4
) {
797 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
800 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
801 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
804 } else if (fs
== HW_FS_XFS
) {
805 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
808 } else if (fs
== HW_FS_FAT32
) {
809 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
814 int r
= mysystem(output
, cmd
);
819 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
823 if (*dest
->part_boot
) {
824 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
830 if (*dest
->part_boot_efi
) {
831 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
837 if (*dest
->part_swap
) {
838 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
844 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
851 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
852 char target
[STRING_SIZE
];
854 assert(*prefix
== '/');
856 const char* filesystem
;
857 switch (dest
->filesystem
) {
859 filesystem
= "reiserfs";
863 case HW_FS_EXT4_WO_JOURNAL
:
880 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
885 if (*dest
->part_boot
) {
886 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
887 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
889 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
891 hw_umount_filesystems(dest
, prefix
);
898 if (*dest
->part_boot_efi
) {
899 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
900 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
902 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
904 hw_umount_filesystems(dest
, prefix
);
911 if (*dest
->part_swap
) {
912 r
= swapon(dest
->part_swap
, 0);
914 hw_umount_filesystems(dest
, prefix
);
920 // bind-mount misc filesystems
921 r
= hw_bind_mount("/dev", prefix
);
925 r
= hw_bind_mount("/proc", prefix
);
929 r
= hw_bind_mount("/sys", prefix
);
933 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
934 if (r
&& errno
!= ENOENT
)
940 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
942 char target
[STRING_SIZE
];
944 // Write all buffers to disk before umounting
948 if (*dest
->part_boot_efi
) {
949 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
955 if (*dest
->part_boot
) {
956 r
= hw_umount(HW_PATH_BOOT
, prefix
);
962 if (*dest
->part_swap
) {
963 swapoff(dest
->part_swap
);
967 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
971 r
= hw_umount("/sys", prefix
);
975 r
= hw_umount("/proc", prefix
);
979 r
= hw_umount("/dev", prefix
);
984 r
= hw_umount(prefix
, NULL
);
991 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
992 char cmd
[STRING_SIZE
];
994 hw_stop_all_raid_arrays(output
);
995 hw_stop_all_raid_arrays(output
);
998 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
999 mysystem(output
, cmd
);
1003 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
1004 mysystem(output
, cmd
);
1010 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1014 assert(dest
->is_raid
);
1016 // Stop all RAID arrays that might be around (again).
1017 // It seems that there is some sort of race-condition with udev re-enabling
1018 // the raid arrays and therefore locking the disks.
1019 r
= hw_destroy_raid_superblocks(dest
, output
);
1021 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1022 RAID_METADATA
, dest
->path
);
1024 switch (dest
->raid_level
) {
1026 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1034 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1036 // Clear all data at the beginning
1037 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1043 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1045 // Clear all data at the beginning
1046 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1051 r
= mysystem(output
, cmd
);
1054 // Wait a moment until the device has been properly brought up
1056 unsigned int counter
= 10;
1057 while (counter
-- > 0) {
1060 // If the raid device has not yet been properly brought up,
1061 // opening it will fail with the message: Device or resource busy
1062 // Hence we will wait a bit until it becomes usable.
1063 if (try_open(dest
->path
) == 0)
1071 int hw_stop_all_raid_arrays(const char* output
) {
1072 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1075 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1076 char cmd
[STRING_SIZE
];
1078 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1079 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1088 static char* hw_get_uuid(const char* dev
) {
1089 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1090 const char* buffer
= NULL
;
1097 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1100 uuid
= strdup(buffer
);
1102 blkid_free_probe(p
);
1107 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1109 int hw_write_fstab(struct hw_destination
* dest
) {
1110 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1117 if (*dest
->part_boot
) {
1118 uuid
= hw_get_uuid(dest
->part_boot
);
1121 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1127 if (*dest
->part_boot_efi
) {
1128 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1131 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1138 if (*dest
->part_swap
) {
1139 uuid
= hw_get_uuid(dest
->part_swap
);
1142 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1148 uuid
= hw_get_uuid(dest
->part_root
);
1150 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1165 int hw_start_networking(const char* output
) {
1166 return mysystem(output
, "/usr/bin/start-networking.sh");
1169 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1170 char path
[STRING_SIZE
];
1172 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1173 int r
= access(path
, R_OK
);
1176 return strdup(path
);
1181 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1182 char command
[STRING_SIZE
];
1184 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1185 int rc
= mysystem(output
, command
);