]>
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 return mount(source
, target
, fs
, flags
, NULL
);
152 int hw_umount(const char* target
) {
153 int r
= umount2(target
, 0);
155 if (r
&& errno
== EBUSY
) {
156 // Give it a moment to settle
159 r
= umount2(target
, MNT_FORCE
);
165 static int hw_test_source_medium(const char* path
) {
166 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
168 // If the source could not be mounted we
173 // Check if the test file exists.
174 ret
= access(SOURCE_TEST_FILE
, R_OK
);
176 // Umount the test device.
177 hw_umount(SOURCE_MOUNT_PATH
);
182 char* hw_find_source_medium(struct hw
* hw
) {
185 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
187 udev_enumerate_add_match_subsystem(enumerate
, "block");
188 udev_enumerate_scan_devices(enumerate
);
190 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
192 struct udev_list_entry
* dev_list_entry
;
193 udev_list_entry_foreach(dev_list_entry
, devices
) {
194 const char* path
= udev_list_entry_get_name(dev_list_entry
);
195 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
197 const char* dev_path
= udev_device_get_devnode(dev
);
199 // Skip everything what we cannot work with
200 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
201 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
204 if (hw_test_source_medium(dev_path
) == 0) {
205 ret
= strdup(dev_path
);
208 udev_device_unref(dev
);
210 // If a suitable device was found the search will end.
215 udev_enumerate_unref(enumerate
);
220 static struct hw_disk
** hw_create_disks() {
221 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
226 static unsigned long long hw_block_device_get_size(const char* dev
) {
227 int fd
= open(dev
, O_RDONLY
);
231 unsigned long long size
= blkid_get_dev_size(fd
);
237 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
238 struct hw_disk
** ret
= hw_create_disks();
239 struct hw_disk
** disks
= ret
;
241 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
243 udev_enumerate_add_match_subsystem(enumerate
, "block");
244 udev_enumerate_scan_devices(enumerate
);
246 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
248 struct udev_list_entry
* dev_list_entry
;
249 unsigned int i
= HW_MAX_DISKS
;
250 udev_list_entry_foreach(dev_list_entry
, devices
) {
251 const char* path
= udev_list_entry_get_name(dev_list_entry
);
252 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
254 const char* dev_path
= udev_device_get_devnode(dev
);
256 // Skip everything what we cannot work with
257 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
258 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
259 strstartswith(dev_path
, "/dev/md")) {
260 udev_device_unref(dev
);
264 // Skip sourcedrive if we need to
265 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
266 udev_device_unref(dev
);
270 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
271 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
272 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
273 udev_device_unref(dev
);
277 // Skip devices with a size of zero
278 unsigned long long size
= hw_block_device_get_size(dev_path
);
280 udev_device_unref(dev
);
284 struct hw_disk
* disk
= malloc(sizeof(*disk
));
290 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
291 const char* p
= disk
->path
+ 5;
296 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
298 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
300 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
303 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
305 *disk
->vendor
= '\0';
308 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
310 model
= udev_device_get_sysattr_value(dev
, "model");
312 model
= udev_device_get_sysattr_value(dev
, "product");
315 strncpy(disk
->model
, model
, sizeof(disk
->model
));
319 // Format description
320 char size_str
[STRING_SIZE
];
321 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
323 if (*disk
->vendor
&& *disk
->model
) {
324 snprintf(disk
->description
, sizeof(disk
->description
),
325 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
327 } else if (*disk
->vendor
|| *disk
->model
) {
328 snprintf(disk
->description
, sizeof(disk
->description
),
329 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
332 snprintf(disk
->description
, sizeof(disk
->description
),
333 "%s - %s", size_str
, p
);
336 // Cut off the description string after 40 characters
337 disk
->description
[41] = '\0';
344 udev_device_unref(dev
);
347 udev_enumerate_unref(enumerate
);
354 void hw_free_disks(struct hw_disk
** disks
) {
355 struct hw_disk
** disk
= disks
;
357 while (*disk
!= NULL
) {
358 if (--(*disk
)->ref
== 0)
367 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
368 unsigned int ret
= 0;
376 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
377 struct hw_disk
** ret
= hw_create_disks();
378 struct hw_disk
** selected_disks
= ret
;
380 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
382 for (unsigned int i
= 0; i
< num_disks
; i
++) {
383 if (!selection
|| selection
[i
]) {
384 struct hw_disk
*selected_disk
= disks
[i
];
385 selected_disk
->ref
++;
387 *selected_disks
++ = selected_disk
;
392 *selected_disks
= NULL
;
397 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
398 struct hw_disk
** ret
= hw_create_disks();
399 struct hw_disk
** selected_disks
= ret
;
401 unsigned int num_disks
= hw_count_disks(disks
);
402 assert(num_disks
> 0);
404 for (unsigned int i
= 0; i
< num_disks
; i
++) {
405 struct hw_disk
*disk
= disks
[i
];
408 *selected_disks
++ = disk
;
413 *selected_disks
= NULL
;
418 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
419 unsigned long long memory
= hw_memory();
421 unsigned long long swap_size
= memory
/ 4;
423 // Min. swap size is 128MB
424 if (swap_size
< MB2BYTES(128))
425 swap_size
= MB2BYTES(128);
427 // Cap swap size to 1GB
428 else if (swap_size
> MB2BYTES(1024))
429 swap_size
= MB2BYTES(1024);
434 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
435 return MB2BYTES(128);
438 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
439 // All RAID devices have the p suffix.
443 // Devices with a number at the end have the p suffix, too.
444 // e.g. mmcblk0, cciss0
445 unsigned int last_char
= strlen(dest
->path
) - 1;
446 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
452 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
456 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
457 hw_device_has_p_suffix(dest
) ? "p" : "");
458 dest
->part_boot_idx
= 0;
460 // Determine the size of the target block device
462 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
463 dest
->disk2
->size
: dest
->disk1
->size
;
465 // The RAID will install some metadata at the end of the disk
466 // and we will save up some space for that.
467 dest
->size
-= MB2BYTES(2);
469 dest
->size
= dest
->disk1
->size
;
472 // As we add some extra space before the beginning of the first
473 // partition, we need to substract that here.
474 dest
->size
-= MB2BYTES(1);
476 // Add some more space for partition tables, etc.
477 dest
->size
-= MB2BYTES(1);
479 // The disk has to have at least 2GB
480 if (dest
->size
<= MB2BYTES(2048))
483 // Determine partition table
484 dest
->part_table
= HW_PART_TABLE_MSDOS
;
486 // Disks over 2TB need to use GPT
487 if (dest
->size
>= MB2BYTES(2047 * 1024))
488 dest
->part_table
= HW_PART_TABLE_GPT
;
490 // We also use GPT on raid disks by default
491 else if (dest
->is_raid
)
492 dest
->part_table
= HW_PART_TABLE_GPT
;
494 // When using GPT, GRUB2 needs a little bit of space to put
496 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
497 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
498 "%s%d", path
, part_idx
);
500 dest
->size_bootldr
= MB2BYTES(4);
502 dest
->part_boot_idx
= part_idx
++;
504 *dest
->part_bootldr
= '\0';
505 dest
->size_bootldr
= 0;
508 dest
->size_boot
= hw_boot_size(dest
);
510 // Create an EFI partition when running in EFI mode
512 dest
->size_boot_efi
= MB2BYTES(32);
514 dest
->size_boot_efi
= 0;
516 // Determine the size of the data partition.
517 unsigned long long space_left
= dest
->size
- \
518 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
520 // If we have less than 2GB left, we disable swap
521 if (space_left
<= MB2BYTES(2048))
524 // Should we use swap?
528 dest
->size_swap
= hw_swap_size(dest
);
531 space_left
-= dest
->size_swap
;
533 // Root is getting what ever is left
534 dest
->size_root
= space_left
;
536 // Set partition names
537 if (dest
->size_boot
> 0) {
538 if (dest
->part_boot_idx
== 0)
539 dest
->part_boot_idx
= part_idx
;
541 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
543 *dest
->part_boot
= '\0';
545 if (dest
->size_boot_efi
> 0) {
546 dest
->part_boot_efi_idx
= part_idx
;
548 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
549 "%s%d", path
, part_idx
++);
551 *dest
->part_boot_efi
= '\0';
552 dest
->part_boot_efi_idx
= 0;
555 if (dest
->size_swap
> 0)
556 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
558 *dest
->part_swap
= '\0';
560 // There is always a root partition
561 if (dest
->part_boot_idx
== 0)
562 dest
->part_boot_idx
= part_idx
;
564 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
569 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
570 struct hw_destination
* dest
= malloc(sizeof(*dest
));
572 if (part_type
== HW_PART_TYPE_NORMAL
) {
573 dest
->disk1
= *disks
;
576 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
578 } else if (part_type
== HW_PART_TYPE_RAID1
) {
579 dest
->disk1
= *disks
++;
580 dest
->disk2
= *disks
;
581 dest
->raid_level
= 1;
583 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
586 // Is this a RAID device?
587 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
589 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
593 // Set default filesystem
594 dest
->filesystem
= HW_FS_DEFAULT
;
599 unsigned long long hw_memory() {
602 int r
= sysinfo(&si
);
609 static int hw_zero_out_device(const char* path
, int bytes
) {
611 memset(block
, 0, sizeof(block
));
613 int blocks
= bytes
/ sizeof(block
);
615 int fd
= open(path
, O_WRONLY
);
619 unsigned int bytes_written
= 0;
620 while (blocks
-- > 0) {
621 bytes_written
+= write(fd
, block
, sizeof(block
));
627 return bytes_written
;
630 static int try_open(const char* path
) {
631 FILE* f
= fopen(path
, "r");
640 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
641 // Before we write a new partition table to the disk, we will erase
642 // the first couple of megabytes at the beginning of the device to
643 // get rid of all left other things like bootloaders and partition tables.
644 // This solves some problems when changing from MBR to GPT partitions or
645 // the other way around.
646 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
651 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
653 // Set partition type
654 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
655 asprintf(&cmd
, "%s mklabel msdos", cmd
);
656 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
657 asprintf(&cmd
, "%s mklabel gpt", cmd
);
659 unsigned long long part_start
= MB2BYTES(1);
661 if (*dest
->part_bootldr
) {
662 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
663 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
664 part_start
, part_start
+ dest
->size_bootldr
- 1);
666 part_start
+= dest
->size_bootldr
;
669 if (*dest
->part_boot
) {
670 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
671 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
672 part_start
, part_start
+ dest
->size_boot
- 1);
674 part_start
+= dest
->size_boot
;
677 if (*dest
->part_boot_efi
) {
678 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
679 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
680 part_start
, part_start
+ dest
->size_boot_efi
- 1);
682 part_start
+= dest
->size_boot_efi
;
685 if (*dest
->part_swap
) {
686 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
687 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
688 part_start
, part_start
+ dest
->size_swap
- 1);
690 part_start
+= dest
->size_swap
;
693 if (*dest
->part_root
) {
694 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
695 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
696 part_start
, part_start
+ dest
->size_root
- 1);
698 part_start
+= dest
->size_root
;
701 if (dest
->part_boot_idx
> 0)
702 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
704 if (dest
->part_boot_efi_idx
> 0)
705 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
707 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
708 if (*dest
->part_bootldr
) {
709 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
713 r
= mysystem(output
, cmd
);
715 // Wait until the system re-read the partition table
717 unsigned int counter
= 10;
719 while (counter
-- > 0) {
722 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
725 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
728 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
731 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
734 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
737 // All partitions do exist, exiting the loop.
748 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
749 char cmd
[STRING_SIZE
] = "\0";
752 if (fs
== HW_FS_SWAP
) {
753 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
755 } else if (fs
== HW_FS_REISERFS
) {
756 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
759 } else if (fs
== HW_FS_EXT4
) {
760 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
763 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
764 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
767 } else if (fs
== HW_FS_XFS
) {
768 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
771 } else if (fs
== HW_FS_FAT32
) {
772 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
777 int r
= mysystem(output
, cmd
);
782 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
786 if (*dest
->part_boot
) {
787 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
793 if (*dest
->part_boot_efi
) {
794 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
800 if (*dest
->part_swap
) {
801 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
807 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
814 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
815 char target
[STRING_SIZE
];
817 assert(*prefix
== '/');
819 const char* filesystem
;
820 switch (dest
->filesystem
) {
822 filesystem
= "reiserfs";
826 case HW_FS_EXT4_WO_JOURNAL
:
843 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
848 if (*dest
->part_boot
) {
849 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
850 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
852 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
854 hw_umount_filesystems(dest
, prefix
);
861 if (*dest
->part_boot_efi
) {
862 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
863 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
865 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
867 hw_umount_filesystems(dest
, prefix
);
874 if (*dest
->part_swap
) {
875 r
= swapon(dest
->part_swap
, 0);
877 hw_umount_filesystems(dest
, prefix
);
883 // bind-mount misc filesystems
884 char** otherfs
= other_filesystems
;
886 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
888 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
889 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
891 hw_umount_filesystems(dest
, prefix
);
902 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
904 char target
[STRING_SIZE
];
906 // Write all buffers to disk before umounting
910 if (*dest
->part_boot_efi
) {
911 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
912 r
= hw_umount(target
);
918 if (*dest
->part_boot
) {
919 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
920 r
= hw_umount(target
);
926 if (*dest
->part_swap
) {
927 swapoff(dest
->part_swap
);
931 char** otherfs
= other_filesystems
;
933 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
934 r
= hw_umount(target
);
940 r
= hw_umount(prefix
);
947 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
948 char cmd
[STRING_SIZE
];
950 hw_stop_all_raid_arrays(output
);
951 hw_stop_all_raid_arrays(output
);
954 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
955 mysystem(output
, cmd
);
959 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
960 mysystem(output
, cmd
);
966 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
970 assert(dest
->is_raid
);
972 // Stop all RAID arrays that might be around (again).
973 // It seems that there is some sort of race-condition with udev re-enabling
974 // the raid arrays and therefore locking the disks.
975 r
= hw_destroy_raid_superblocks(dest
, output
);
977 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
978 RAID_METADATA
, dest
->path
);
980 switch (dest
->raid_level
) {
982 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
990 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
992 // Clear all data at the beginning
993 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
999 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1001 // Clear all data at the beginning
1002 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1007 r
= mysystem(output
, cmd
);
1010 // Wait a moment until the device has been properly brought up
1012 unsigned int counter
= 10;
1013 while (counter
-- > 0) {
1016 // If the raid device has not yet been properly brought up,
1017 // opening it will fail with the message: Device or resource busy
1018 // Hence we will wait a bit until it becomes usable.
1019 if (try_open(dest
->path
) == 0)
1027 int hw_stop_all_raid_arrays(const char* output
) {
1028 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1031 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1032 char cmd
[STRING_SIZE
];
1034 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1035 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1044 static char* hw_get_uuid(const char* dev
) {
1045 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1046 const char* buffer
= NULL
;
1053 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1056 uuid
= strdup(buffer
);
1058 blkid_free_probe(p
);
1063 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1065 int hw_write_fstab(struct hw_destination
* dest
) {
1066 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1073 if (*dest
->part_boot
) {
1074 uuid
= hw_get_uuid(dest
->part_boot
);
1077 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1083 if (*dest
->part_boot_efi
) {
1084 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1087 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1094 if (*dest
->part_swap
) {
1095 uuid
= hw_get_uuid(dest
->part_swap
);
1098 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1104 uuid
= hw_get_uuid(dest
->part_root
);
1106 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1121 int hw_start_networking(const char* output
) {
1122 return mysystem(output
, "/usr/bin/start-networking.sh");
1125 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1126 char path
[STRING_SIZE
];
1128 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1129 int r
= access(path
, R_OK
);
1132 return strdup(path
);
1137 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1138 char command
[STRING_SIZE
];
1140 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1141 int rc
= mysystem(output
, command
);