]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
1 /*#############################################################################
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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>
43 #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 static int hw_mkdir(const char *dir
) {
58 char tmp
[STRING_SIZE
];
63 snprintf(tmp
, sizeof(tmp
),"%s",dir
);
66 if (tmp
[len
- 1] == '/') {
70 for (p
= tmp
+ 1; *p
; p
++) {
74 // Create target if it does not exist
75 if (access(tmp
, X_OK
) != 0) {
76 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
87 // Create target if it does not exist
88 if (access(tmp
, X_OK
) != 0) {
89 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
99 struct hw
* hw_init() {
100 struct hw
* hw
= calloc(1, sizeof(*hw
));
103 // Initialize libudev
104 hw
->udev
= udev_new();
106 fprintf(stderr
, "Could not create udev instance\n");
110 // What architecture are we running on?
111 struct utsname uname_data
;
112 int ret
= uname(&uname_data
);
114 snprintf(hw
->arch
, sizeof(hw
->arch
), "%s", uname_data
.machine
);
116 // Should we install in EFI mode?
117 if ((strcmp(hw
->arch
, "x86_64") == 0) || (strcmp(hw
->arch
, "aarch64") == 0))
123 void hw_free(struct hw
* hw
) {
125 udev_unref(hw
->udev
);
130 static int strstartswith(const char* a
, const char* b
) {
131 return (strncmp(a
, b
, strlen(b
)) == 0);
134 static int setup_loop_device(const char* source
, const char* device
) {
135 int file_fd
= open(source
, O_RDWR
);
140 if ((device_fd
= open(device
, O_RDWR
)) < 0)
143 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
155 if (device_fd
>= 0) {
156 ioctl(device_fd
, LOOP_CLR_FD
, 0);
163 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
164 const char* loop_device
= "/dev/loop0";
166 // Create target if it does not exist
167 if (access(target
, X_OK
) != 0)
168 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
173 if (S_ISREG(st
.st_mode
)) {
174 int r
= setup_loop_device(source
, loop_device
);
176 source
= loop_device
;
182 return mount(source
, target
, fs
, flags
, NULL
);
185 static int hw_bind_mount(const char* source
, const char* prefix
) {
186 if (!source
|| !prefix
) {
191 char target
[PATH_MAX
];
195 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
199 // Ensure target exists
200 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
202 return hw_mount(source
, target
, NULL
, MS_BIND
);
205 int hw_umount(const char* source
, const char* prefix
) {
206 char target
[PATH_MAX
];
210 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
212 r
= snprintf(target
, sizeof(target
) - 1, "%s", source
);
217 r
= umount2(target
, 0);
220 // Try again with force if umount wasn't successful
224 r
= umount2(target
, MNT_FORCE
);
227 // target wasn't a mountpoint. Ignore.
232 // target doesn't exist
242 static int hw_test_source_medium(const char* path
) {
243 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
246 // 2nd try, ntfs for a rufus converted usb key
247 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "ntfs3", MS_RDONLY
);
250 // 3rd try, vfat for a rufus converted usb key
251 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "vfat", MS_RDONLY
);
254 // If the source could not be mounted we
259 // Check if the test file exists.
260 ret
= access(SOURCE_TEST_FILE
, R_OK
);
262 // Umount the test device.
263 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
268 char* hw_find_source_medium(struct hw
* hw
) {
271 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
273 udev_enumerate_add_match_subsystem(enumerate
, "block");
274 udev_enumerate_scan_devices(enumerate
);
276 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
278 struct udev_list_entry
* dev_list_entry
;
279 udev_list_entry_foreach(dev_list_entry
, devices
) {
280 const char* path
= udev_list_entry_get_name(dev_list_entry
);
281 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
283 const char* dev_path
= udev_device_get_devnode(dev
);
285 // Skip everything what we cannot work with
286 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
287 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
290 if (hw_test_source_medium(dev_path
) == 0) {
291 ret
= strdup(dev_path
);
294 udev_device_unref(dev
);
296 // If a suitable device was found the search will end.
301 udev_enumerate_unref(enumerate
);
306 static struct hw_disk
** hw_create_disks() {
307 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
312 static unsigned long long hw_block_device_get_size(const char* dev
) {
313 int fd
= open(dev
, O_RDONLY
);
317 unsigned long long size
= blkid_get_dev_size(fd
);
323 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
324 struct hw_disk
** ret
= hw_create_disks();
325 struct hw_disk
** disks
= ret
;
327 // Determine the disk device of source if it is a partition
328 const char* sourcedisk
= NULL
;
329 char syssource
[PATH_MAX
];
330 (void)snprintf(syssource
, sizeof(syssource
) - 1, "/sys/class/block/%s", sourcedrive
+ 5);
331 struct udev_device
* s_dev
= udev_device_new_from_syspath(hw
->udev
, syssource
);
332 const char* s_devtype
= udev_device_get_property_value(s_dev
, "DEVTYPE");
333 if (s_devtype
&& (strcmp(s_devtype
, "partition") == 0)) {
334 struct udev_device
* p_dev
= udev_device_get_parent_with_subsystem_devtype(s_dev
,"block","disk");
336 sourcedisk
= udev_device_get_devnode(p_dev
);
339 if (!sourcedisk
) sourcedisk
= sourcedrive
;
341 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
343 udev_enumerate_add_match_subsystem(enumerate
, "block");
344 udev_enumerate_scan_devices(enumerate
);
346 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
348 struct udev_list_entry
* dev_list_entry
;
349 unsigned int i
= HW_MAX_DISKS
;
350 udev_list_entry_foreach(dev_list_entry
, devices
) {
351 const char* path
= udev_list_entry_get_name(dev_list_entry
);
352 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
354 const char* dev_path
= udev_device_get_devnode(dev
);
356 // Skip everything what we cannot work with
357 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
358 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
359 strstartswith(dev_path
, "/dev/md")) {
360 udev_device_unref(dev
);
364 // Skip sourcedisk if we need to
365 if (sourcedisk
&& (strcmp(dev_path
, sourcedisk
) == 0)) {
366 udev_device_unref(dev
);
370 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
371 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
372 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
373 udev_device_unref(dev
);
377 // Skip devices with a size of zero
378 unsigned long long size
= hw_block_device_get_size(dev_path
);
380 udev_device_unref(dev
);
384 struct hw_disk
* disk
= malloc(sizeof(*disk
));
390 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
391 const char* p
= disk
->path
+ 5;
396 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
398 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
400 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
403 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
405 *disk
->vendor
= '\0';
408 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
410 model
= udev_device_get_sysattr_value(dev
, "model");
412 model
= udev_device_get_sysattr_value(dev
, "product");
415 strncpy(disk
->model
, model
, sizeof(disk
->model
));
419 // Format description
420 char size_str
[STRING_SIZE
];
421 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
423 if (*disk
->vendor
&& *disk
->model
) {
424 snprintf(disk
->description
, sizeof(disk
->description
),
425 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
427 } else if (*disk
->vendor
|| *disk
->model
) {
428 snprintf(disk
->description
, sizeof(disk
->description
),
429 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
432 snprintf(disk
->description
, sizeof(disk
->description
),
433 "%s - %s", size_str
, p
);
436 // Cut off the description string after 40 characters
437 disk
->description
[41] = '\0';
444 udev_device_unref(dev
);
447 udev_enumerate_unref(enumerate
);
454 void hw_free_disks(struct hw_disk
** disks
) {
455 struct hw_disk
** disk
= disks
;
457 while (*disk
!= NULL
) {
458 if (--(*disk
)->ref
== 0)
467 unsigned int hw_count_disks(struct hw_disk
** disks
) {
468 unsigned int ret
= 0;
476 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
477 struct hw_disk
** ret
= hw_create_disks();
478 struct hw_disk
** selected_disks
= ret
;
480 unsigned int num_disks
= hw_count_disks(disks
);
482 for (unsigned int i
= 0; i
< num_disks
; i
++) {
483 if (!selection
|| selection
[i
]) {
484 struct hw_disk
*selected_disk
= disks
[i
];
485 selected_disk
->ref
++;
487 *selected_disks
++ = selected_disk
;
492 *selected_disks
= NULL
;
497 struct hw_disk
** hw_select_first_disk(struct hw_disk
** disks
) {
498 struct hw_disk
** ret
= hw_create_disks();
499 struct hw_disk
** selected_disks
= ret
;
501 unsigned int num_disks
= hw_count_disks(disks
);
502 assert(num_disks
> 0);
504 for (unsigned int i
= 0; i
< num_disks
; i
++) {
505 struct hw_disk
*disk
= disks
[i
];
508 *selected_disks
++ = disk
;
513 *selected_disks
= NULL
;
518 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
519 unsigned long long memory
= hw_memory();
521 unsigned long long swap_size
= memory
/ 4;
523 // Min. swap size is 128MB
524 if (swap_size
< MB2BYTES(128))
525 swap_size
= MB2BYTES(128);
527 // Cap swap size to 1GB
528 else if (swap_size
> MB2BYTES(1024))
529 swap_size
= MB2BYTES(1024);
534 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
535 return MB2BYTES(512);
538 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
539 // All RAID devices have the p suffix.
543 // Devices with a number at the end have the p suffix, too.
544 // e.g. mmcblk0, cciss0
545 unsigned int last_char
= strlen(dest
->path
) - 1;
546 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
552 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
556 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
557 hw_device_has_p_suffix(dest
) ? "p" : "");
558 dest
->part_boot_idx
= 0;
560 // Determine the size of the target block device
562 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
563 dest
->disk2
->size
: dest
->disk1
->size
;
565 // The RAID will install some metadata at the end of the disk
566 // and we will save up some space for that.
567 dest
->size
-= MB2BYTES(2);
569 dest
->size
= dest
->disk1
->size
;
572 // As we add some extra space before the beginning of the first
573 // partition, we need to substract that here.
574 dest
->size
-= MB2BYTES(1);
576 // Add some more space for partition tables, etc.
577 dest
->size
-= MB2BYTES(1);
579 // The disk has to have at least 2GB
580 if (dest
->size
<= MB2BYTES(2048))
583 // Determine partition table
584 dest
->part_table
= HW_PART_TABLE_MSDOS
;
586 // Disks over 2TB need to use GPT
587 if (dest
->size
>= MB2BYTES(2047 * 1024))
588 dest
->part_table
= HW_PART_TABLE_GPT
;
590 // We also use GPT on raid disks by default
591 else if (dest
->is_raid
)
592 dest
->part_table
= HW_PART_TABLE_GPT
;
594 // When using GPT, GRUB2 needs a little bit of space to put
596 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
597 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
598 "%s%d", path
, part_idx
);
600 dest
->size_bootldr
= MB2BYTES(4);
602 dest
->part_boot_idx
= part_idx
++;
604 *dest
->part_bootldr
= '\0';
605 dest
->size_bootldr
= 0;
608 // Disable seperate boot partition for BTRFS installations.
609 if(dest
->filesystem
== HW_FS_BTRFS
) {
612 dest
->size_boot
= hw_boot_size(dest
);
615 // Create an EFI partition when running in EFI mode
617 dest
->size_boot_efi
= MB2BYTES(32);
619 dest
->size_boot_efi
= 0;
621 // Determine the size of the data partition.
622 unsigned long long space_left
= dest
->size
- \
623 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
625 // If we have less than 2GB left, we disable swap
626 if (space_left
<= MB2BYTES(2048))
629 // Should we use swap?
633 dest
->size_swap
= hw_swap_size(dest
);
636 space_left
-= dest
->size_swap
;
638 // Root is getting what ever is left
639 dest
->size_root
= space_left
;
641 // Set partition names
642 if (dest
->size_boot
> 0) {
643 if (dest
->part_boot_idx
== 0)
644 dest
->part_boot_idx
= part_idx
;
646 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
648 *dest
->part_boot
= '\0';
650 if (dest
->size_boot_efi
> 0) {
651 dest
->part_boot_efi_idx
= part_idx
;
653 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
654 "%s%d", path
, part_idx
++);
656 *dest
->part_boot_efi
= '\0';
657 dest
->part_boot_efi_idx
= 0;
660 if (dest
->size_swap
> 0)
661 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
663 *dest
->part_swap
= '\0';
665 // There is always a root partition
666 if (dest
->part_boot_idx
== 0)
667 dest
->part_boot_idx
= part_idx
;
669 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
674 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
,
675 int disable_swap
, int filesystem
) {
676 struct hw_destination
* dest
= malloc(sizeof(*dest
));
679 dest
->filesystem
= filesystem
;
681 if (part_type
== HW_PART_TYPE_NORMAL
) {
682 dest
->disk1
= *disks
;
685 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
687 } else if (part_type
== HW_PART_TYPE_RAID1
) {
688 dest
->disk1
= *disks
++;
689 dest
->disk2
= *disks
;
690 dest
->raid_level
= 1;
692 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
695 // Is this a RAID device?
696 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
698 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
705 unsigned long long hw_memory() {
708 int r
= sysinfo(&si
);
715 static int hw_zero_out_device(const char* path
, int bytes
) {
717 memset(block
, 0, sizeof(block
));
719 int blocks
= bytes
/ sizeof(block
);
721 int fd
= open(path
, O_WRONLY
);
725 unsigned int bytes_written
= 0;
726 while (blocks
-- > 0) {
727 bytes_written
+= write(fd
, block
, sizeof(block
));
733 return bytes_written
;
736 static int try_open(const char* path
) {
737 FILE* f
= fopen(path
, "r");
746 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
747 // Before we write a new partition table to the disk, we will erase
748 // the first couple of megabytes at the beginning of the device to
749 // get rid of all left other things like bootloaders and partition tables.
750 // This solves some problems when changing from MBR to GPT partitions or
751 // the other way around.
752 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
757 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
759 // Set partition type
760 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
761 asprintf(&cmd
, "%s mklabel msdos", cmd
);
762 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
763 asprintf(&cmd
, "%s mklabel gpt", cmd
);
765 unsigned long long part_start
= MB2BYTES(1);
767 if (*dest
->part_bootldr
) {
768 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
769 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
770 part_start
, part_start
+ dest
->size_bootldr
- 1);
772 part_start
+= dest
->size_bootldr
;
775 if (*dest
->part_boot
) {
776 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
777 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
778 part_start
, part_start
+ dest
->size_boot
- 1);
780 part_start
+= dest
->size_boot
;
783 if (*dest
->part_boot_efi
) {
784 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
785 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
786 part_start
, part_start
+ dest
->size_boot_efi
- 1);
788 part_start
+= dest
->size_boot_efi
;
791 if (*dest
->part_swap
) {
792 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
793 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
794 part_start
, part_start
+ dest
->size_swap
- 1);
796 part_start
+= dest
->size_swap
;
799 if (*dest
->part_root
) {
800 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
801 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
802 part_start
, part_start
+ dest
->size_root
- 1);
804 part_start
+= dest
->size_root
;
807 if (dest
->part_boot_idx
> 0)
808 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
810 if (dest
->part_boot_efi_idx
> 0)
811 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
813 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
814 if (*dest
->part_bootldr
) {
815 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
819 r
= mysystem(output
, cmd
);
821 // Wait until the system re-read the partition table
823 unsigned int counter
= 10;
825 while (counter
-- > 0) {
828 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
831 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
834 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
837 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
840 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
843 // All partitions do exist, exiting the loop.
854 static int hw_create_btrfs_subvolume(const char* output
, const char* subvolume
) {
855 char command
[STRING_SIZE
];
858 // Abort if the command could not be assigned.
859 r
= snprintf(command
, sizeof(command
), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH
, subvolume
);
863 // Create the subvolume
864 r
= mysystem(output
, command
);
871 static int hw_create_btrfs_layout(const char* path
, const char* output
) {
872 const struct btrfs_subvolumes
* subvolume
= NULL
;
873 char cmd
[STRING_SIZE
];
874 char volume
[STRING_SIZE
];
877 r
= snprintf(cmd
, sizeof(cmd
), "/usr/bin/mkfs.btrfs -f %s", path
);
881 // Create the main BTRFS file system.
882 r
= mysystem(output
, cmd
);
886 // We need to mount the FS in order to create any subvolumes.
887 r
= hw_mount(path
, DESTINATION_MOUNT_PATH
, "btrfs", 0);
891 // Loop through the list of subvolumes to create.
892 for ( subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++ ) {
893 r
= snprintf(volume
, sizeof(volume
), "%s", subvolume
->name
);
895 // Abort if snprintf fails.
899 // Call function to create the subvolume
900 r
= hw_create_btrfs_subvolume(output
, volume
);
905 // Umount the main BTRFS after subvolume creation.
906 r
= hw_umount(DESTINATION_MOUNT_PATH
, 0);
913 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
914 char cmd
[STRING_SIZE
] = "\0";
918 if (fs
== HW_FS_SWAP
) {
919 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
922 } else if (fs
== HW_FS_EXT4
) {
923 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
926 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
927 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
930 } else if (fs
== HW_FS_XFS
) {
931 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
934 } else if (fs
== HW_FS_BTRFS
) {
935 r
= hw_create_btrfs_layout(path
, output
);
940 } else if (fs
== HW_FS_FAT32
) {
941 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
946 r
= mysystem(output
, cmd
);
951 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
955 if (*dest
->part_boot
) {
956 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
962 if (*dest
->part_boot_efi
) {
963 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
969 if (*dest
->part_swap
) {
970 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
976 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
983 static int hw_mount_btrfs_subvolumes(const char* source
) {
984 const struct btrfs_subvolumes
* subvolume
= NULL
;
985 char path
[STRING_SIZE
];
986 char options
[STRING_SIZE
];
989 // Loop through the list of known subvolumes.
990 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
991 // Assign subvolume path.
992 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
996 // Assign subvolume name.
997 r
= snprintf(options
, sizeof(options
), "subvol=%s,%s", subvolume
->name
, BTRFS_MOUNT_OPTIONS
);
1001 // Create the directory.
1004 // Abort if the directory could not be created.
1005 if(r
!= 0 && errno
!= EEXIST
)
1008 // Print log message
1009 fprintf(flog
, "Mounting subvolume %s to %s\n", subvolume
->name
, subvolume
->mount_path
);
1011 // Try to mount the subvolume.
1012 r
= mount(source
, path
, "btrfs", NULL
, options
);
1020 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1021 char target
[STRING_SIZE
];
1024 assert(*prefix
== '/');
1026 const char* filesystem
;
1027 switch (dest
->filesystem
) {
1029 case HW_FS_EXT4_WO_JOURNAL
:
1030 filesystem
= "ext4";
1038 filesystem
= "btrfs";
1042 filesystem
= "vfat";
1050 if (dest
->filesystem
== HW_FS_BTRFS
) {
1051 r
= hw_mount_btrfs_subvolumes(dest
->part_root
);
1055 r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
1061 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
1062 r
= mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1065 hw_umount_filesystems(dest
, prefix
);
1070 if (*dest
->part_boot
) {
1071 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
1073 hw_umount_filesystems(dest
, prefix
);
1080 if (*dest
->part_boot_efi
) {
1081 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
1082 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1084 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
1086 hw_umount_filesystems(dest
, prefix
);
1093 if (*dest
->part_swap
) {
1094 r
= swapon(dest
->part_swap
, 0);
1096 hw_umount_filesystems(dest
, prefix
);
1102 // bind-mount misc filesystems
1103 r
= hw_bind_mount("/dev", prefix
);
1107 r
= hw_bind_mount("/proc", prefix
);
1111 r
= hw_bind_mount("/sys", prefix
);
1115 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
1116 if (r
&& errno
!= ENOENT
)
1122 static int hw_umount_btrfs_layout() {
1123 const struct btrfs_subvolumes
* subvolume
= NULL
;
1124 char path
[STRING_SIZE
];
1130 // Reset the retry marker
1133 // Loop through the list of subvolumes
1134 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1135 // Abort if the subvolume path could not be assigned.
1136 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
1140 // Try to umount the subvolume.
1141 r
= umount2(path
, 0);
1143 // Handle return codes.
1147 // Set marker to retry the umount.
1150 // Ignore if the subvolume could not be unmounted yet,
1151 // because it is still used.
1155 // Ignore if the subvolume already has been unmounted
1158 // Ignore if the directory does not longer exist.
1161 fprintf(flog
, "Could not umount %s from %s - Error: %d\n", subvolume
->name
, path
, r
);
1166 // Print log message
1167 fprintf(flog
, "Umounted %s from %s\n", subvolume
->name
, path
);
1170 // Abort loop if all mountpoins got umounted
1174 // Abort after five failed umount attempts
1178 // Increment counter.
1183 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1186 // Write all buffers to disk before umounting
1190 if (*dest
->part_boot_efi
) {
1191 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
1197 if (*dest
->part_boot
) {
1198 r
= hw_umount(HW_PATH_BOOT
, prefix
);
1204 if (*dest
->part_swap
) {
1205 swapoff(dest
->part_swap
);
1209 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
1213 r
= hw_umount("/sys", prefix
);
1217 r
= hw_umount("/proc", prefix
);
1221 r
= hw_umount("/dev", prefix
);
1226 if(dest
->filesystem
== HW_FS_BTRFS
) {
1227 r
= hw_umount_btrfs_layout();
1229 r
= hw_umount(prefix
, NULL
);
1238 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
1239 char cmd
[STRING_SIZE
];
1241 hw_stop_all_raid_arrays(output
);
1242 hw_stop_all_raid_arrays(output
);
1245 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
1246 mysystem(output
, cmd
);
1250 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
1251 mysystem(output
, cmd
);
1257 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1261 assert(dest
->is_raid
);
1263 // Stop all RAID arrays that might be around (again).
1264 // It seems that there is some sort of race-condition with udev re-enabling
1265 // the raid arrays and therefore locking the disks.
1266 r
= hw_destroy_raid_superblocks(dest
, output
);
1268 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1269 RAID_METADATA
, dest
->path
);
1271 switch (dest
->raid_level
) {
1273 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1281 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1283 // Clear all data at the beginning
1284 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1290 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1292 // Clear all data at the beginning
1293 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1298 r
= mysystem(output
, cmd
);
1301 // Wait a moment until the device has been properly brought up
1303 unsigned int counter
= 10;
1304 while (counter
-- > 0) {
1307 // If the raid device has not yet been properly brought up,
1308 // opening it will fail with the message: Device or resource busy
1309 // Hence we will wait a bit until it becomes usable.
1310 if (try_open(dest
->path
) == 0)
1318 int hw_stop_all_raid_arrays(const char* output
) {
1319 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1322 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1323 char cmd
[STRING_SIZE
];
1325 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1326 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1335 static char* hw_get_uuid(const char* dev
) {
1336 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1337 const char* buffer
= NULL
;
1344 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1347 uuid
= strdup(buffer
);
1349 blkid_free_probe(p
);
1354 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1356 int hw_write_fstab(struct hw_destination
* dest
) {
1357 const struct btrfs_subvolumes
* subvolume
= NULL
;
1358 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1363 char mount_options
[STRING_SIZE
];
1366 if (*dest
->part_boot
) {
1367 uuid
= hw_get_uuid(dest
->part_boot
);
1370 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
1376 if (*dest
->part_boot_efi
) {
1377 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1380 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1387 if (*dest
->part_swap
) {
1388 uuid
= hw_get_uuid(dest
->part_swap
);
1391 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1397 uuid
= hw_get_uuid(dest
->part_root
);
1399 if(dest
->filesystem
== HW_FS_BTRFS
) {
1400 // Loop through the list of subvolumes
1401 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1402 // Abort if the mount options could not be assigned
1403 int r
= snprintf(mount_options
, sizeof(mount_options
), "defaults,%s,subvol=%s", BTRFS_MOUNT_OPTIONS
, subvolume
->name
);
1408 // Write the entry to the file
1409 fprintf(f
, FSTAB_FMT
, uuid
, subvolume
->mount_path
, "btrfs", mount_options
, 1, 1);
1412 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1429 int hw_start_networking(const char* output
) {
1430 return mysystem(output
, "/usr/bin/start-networking.sh");
1433 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1434 char path
[STRING_SIZE
];
1436 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1437 int r
= access(path
, R_OK
);
1440 return strdup(path
);
1445 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1446 char command
[STRING_SIZE
];
1448 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s "
1449 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1450 backup_path
, destination
, destination
, destination
);
1451 int rc
= mysystem(output
, command
);