]>
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 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
);
206 // 2nd try, ntfs for a rufus converted usb key
207 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "ntfs3", MS_RDONLY
);
210 // 3rd try, vfat for a rufus converted usb key
211 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "vfat", MS_RDONLY
);
214 // If the source could not be mounted we
219 // Check if the test file exists.
220 ret
= access(SOURCE_TEST_FILE
, R_OK
);
222 // Umount the test device.
223 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
228 char* hw_find_source_medium(struct hw
* hw
) {
231 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
233 udev_enumerate_add_match_subsystem(enumerate
, "block");
234 udev_enumerate_scan_devices(enumerate
);
236 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
238 struct udev_list_entry
* dev_list_entry
;
239 udev_list_entry_foreach(dev_list_entry
, devices
) {
240 const char* path
= udev_list_entry_get_name(dev_list_entry
);
241 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
243 const char* dev_path
= udev_device_get_devnode(dev
);
245 // Skip everything what we cannot work with
246 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
247 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
250 if (hw_test_source_medium(dev_path
) == 0) {
251 ret
= strdup(dev_path
);
254 udev_device_unref(dev
);
256 // If a suitable device was found the search will end.
261 udev_enumerate_unref(enumerate
);
266 static struct hw_disk
** hw_create_disks() {
267 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
272 static unsigned long long hw_block_device_get_size(const char* dev
) {
273 int fd
= open(dev
, O_RDONLY
);
277 unsigned long long size
= blkid_get_dev_size(fd
);
283 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
284 struct hw_disk
** ret
= hw_create_disks();
285 struct hw_disk
** disks
= ret
;
287 // Determine the disk device of source if it is a partition
288 const char* sourcedisk
= NULL
;
289 char syssource
[PATH_MAX
];
290 (void)snprintf(syssource
, sizeof(syssource
) - 1, "/sys/class/block/%s", sourcedrive
+ 5);
291 struct udev_device
* s_dev
= udev_device_new_from_syspath(hw
->udev
, syssource
);
292 const char* s_devtype
= udev_device_get_property_value(s_dev
, "DEVTYPE");
293 if (s_devtype
&& (strcmp(s_devtype
, "partition") == 0)) {
294 struct udev_device
* p_dev
= udev_device_get_parent_with_subsystem_devtype(s_dev
,"block","disk");
296 sourcedisk
= udev_device_get_devnode(p_dev
);
299 if (!sourcedisk
) sourcedisk
= sourcedrive
;
301 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
303 udev_enumerate_add_match_subsystem(enumerate
, "block");
304 udev_enumerate_scan_devices(enumerate
);
306 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
308 struct udev_list_entry
* dev_list_entry
;
309 unsigned int i
= HW_MAX_DISKS
;
310 udev_list_entry_foreach(dev_list_entry
, devices
) {
311 const char* path
= udev_list_entry_get_name(dev_list_entry
);
312 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
314 const char* dev_path
= udev_device_get_devnode(dev
);
316 // Skip everything what we cannot work with
317 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
318 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
319 strstartswith(dev_path
, "/dev/md")) {
320 udev_device_unref(dev
);
324 // Skip sourcedisk if we need to
325 if (sourcedisk
&& (strcmp(dev_path
, sourcedisk
) == 0)) {
326 udev_device_unref(dev
);
330 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
331 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
332 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
333 udev_device_unref(dev
);
337 // Skip devices with a size of zero
338 unsigned long long size
= hw_block_device_get_size(dev_path
);
340 udev_device_unref(dev
);
344 struct hw_disk
* disk
= malloc(sizeof(*disk
));
350 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
351 const char* p
= disk
->path
+ 5;
356 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
358 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
360 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
363 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
365 *disk
->vendor
= '\0';
368 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
370 model
= udev_device_get_sysattr_value(dev
, "model");
372 model
= udev_device_get_sysattr_value(dev
, "product");
375 strncpy(disk
->model
, model
, sizeof(disk
->model
));
379 // Format description
380 char size_str
[STRING_SIZE
];
381 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
383 if (*disk
->vendor
&& *disk
->model
) {
384 snprintf(disk
->description
, sizeof(disk
->description
),
385 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
387 } else if (*disk
->vendor
|| *disk
->model
) {
388 snprintf(disk
->description
, sizeof(disk
->description
),
389 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
392 snprintf(disk
->description
, sizeof(disk
->description
),
393 "%s - %s", size_str
, p
);
396 // Cut off the description string after 40 characters
397 disk
->description
[41] = '\0';
404 udev_device_unref(dev
);
407 udev_enumerate_unref(enumerate
);
414 void hw_free_disks(struct hw_disk
** disks
) {
415 struct hw_disk
** disk
= disks
;
417 while (*disk
!= NULL
) {
418 if (--(*disk
)->ref
== 0)
427 unsigned int hw_count_disks(struct hw_disk
** disks
) {
428 unsigned int ret
= 0;
436 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
437 struct hw_disk
** ret
= hw_create_disks();
438 struct hw_disk
** selected_disks
= ret
;
440 unsigned int num_disks
= hw_count_disks(disks
);
442 for (unsigned int i
= 0; i
< num_disks
; i
++) {
443 if (!selection
|| selection
[i
]) {
444 struct hw_disk
*selected_disk
= disks
[i
];
445 selected_disk
->ref
++;
447 *selected_disks
++ = selected_disk
;
452 *selected_disks
= NULL
;
457 struct hw_disk
** hw_select_first_disk(struct hw_disk
** disks
) {
458 struct hw_disk
** ret
= hw_create_disks();
459 struct hw_disk
** selected_disks
= ret
;
461 unsigned int num_disks
= hw_count_disks(disks
);
462 assert(num_disks
> 0);
464 for (unsigned int i
= 0; i
< num_disks
; i
++) {
465 struct hw_disk
*disk
= disks
[i
];
468 *selected_disks
++ = disk
;
473 *selected_disks
= NULL
;
478 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
479 unsigned long long memory
= hw_memory();
481 unsigned long long swap_size
= memory
/ 4;
483 // Min. swap size is 128MB
484 if (swap_size
< MB2BYTES(128))
485 swap_size
= MB2BYTES(128);
487 // Cap swap size to 1GB
488 else if (swap_size
> MB2BYTES(1024))
489 swap_size
= MB2BYTES(1024);
494 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
495 return MB2BYTES(512);
498 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
499 // All RAID devices have the p suffix.
503 // Devices with a number at the end have the p suffix, too.
504 // e.g. mmcblk0, cciss0
505 unsigned int last_char
= strlen(dest
->path
) - 1;
506 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
512 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
516 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
517 hw_device_has_p_suffix(dest
) ? "p" : "");
518 dest
->part_boot_idx
= 0;
520 // Determine the size of the target block device
522 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
523 dest
->disk2
->size
: dest
->disk1
->size
;
525 // The RAID will install some metadata at the end of the disk
526 // and we will save up some space for that.
527 dest
->size
-= MB2BYTES(2);
529 dest
->size
= dest
->disk1
->size
;
532 // As we add some extra space before the beginning of the first
533 // partition, we need to substract that here.
534 dest
->size
-= MB2BYTES(1);
536 // Add some more space for partition tables, etc.
537 dest
->size
-= MB2BYTES(1);
539 // The disk has to have at least 2GB
540 if (dest
->size
<= MB2BYTES(2048))
543 // Determine partition table
544 dest
->part_table
= HW_PART_TABLE_MSDOS
;
546 // Disks over 2TB need to use GPT
547 if (dest
->size
>= MB2BYTES(2047 * 1024))
548 dest
->part_table
= HW_PART_TABLE_GPT
;
550 // We also use GPT on raid disks by default
551 else if (dest
->is_raid
)
552 dest
->part_table
= HW_PART_TABLE_GPT
;
554 // When using GPT, GRUB2 needs a little bit of space to put
556 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
557 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
558 "%s%d", path
, part_idx
);
560 dest
->size_bootldr
= MB2BYTES(4);
562 dest
->part_boot_idx
= part_idx
++;
564 *dest
->part_bootldr
= '\0';
565 dest
->size_bootldr
= 0;
568 // Disable seperate boot partition for BTRFS installations.
569 if(dest
->filesystem
== HW_FS_BTRFS
) {
572 dest
->size_boot
= hw_boot_size(dest
);
575 // Create an EFI partition when running in EFI mode
577 dest
->size_boot_efi
= MB2BYTES(32);
579 dest
->size_boot_efi
= 0;
581 // Determine the size of the data partition.
582 unsigned long long space_left
= dest
->size
- \
583 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
585 // If we have less than 2GB left, we disable swap
586 if (space_left
<= MB2BYTES(2048))
589 // Should we use swap?
593 dest
->size_swap
= hw_swap_size(dest
);
596 space_left
-= dest
->size_swap
;
598 // Root is getting what ever is left
599 dest
->size_root
= space_left
;
601 // Set partition names
602 if (dest
->size_boot
> 0) {
603 if (dest
->part_boot_idx
== 0)
604 dest
->part_boot_idx
= part_idx
;
606 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
608 *dest
->part_boot
= '\0';
610 if (dest
->size_boot_efi
> 0) {
611 dest
->part_boot_efi_idx
= part_idx
;
613 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
614 "%s%d", path
, part_idx
++);
616 *dest
->part_boot_efi
= '\0';
617 dest
->part_boot_efi_idx
= 0;
620 if (dest
->size_swap
> 0)
621 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
623 *dest
->part_swap
= '\0';
625 // There is always a root partition
626 if (dest
->part_boot_idx
== 0)
627 dest
->part_boot_idx
= part_idx
;
629 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
634 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
,
635 int disable_swap
, int filesystem
) {
636 struct hw_destination
* dest
= malloc(sizeof(*dest
));
639 dest
->filesystem
= filesystem
;
641 if (part_type
== HW_PART_TYPE_NORMAL
) {
642 dest
->disk1
= *disks
;
645 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
647 } else if (part_type
== HW_PART_TYPE_RAID1
) {
648 dest
->disk1
= *disks
++;
649 dest
->disk2
= *disks
;
650 dest
->raid_level
= 1;
652 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
655 // Is this a RAID device?
656 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
658 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
665 unsigned long long hw_memory() {
668 int r
= sysinfo(&si
);
675 static int hw_zero_out_device(const char* path
, int bytes
) {
677 memset(block
, 0, sizeof(block
));
679 int blocks
= bytes
/ sizeof(block
);
681 int fd
= open(path
, O_WRONLY
);
685 unsigned int bytes_written
= 0;
686 while (blocks
-- > 0) {
687 bytes_written
+= write(fd
, block
, sizeof(block
));
693 return bytes_written
;
696 static int try_open(const char* path
) {
697 FILE* f
= fopen(path
, "r");
706 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
707 // Before we write a new partition table to the disk, we will erase
708 // the first couple of megabytes at the beginning of the device to
709 // get rid of all left other things like bootloaders and partition tables.
710 // This solves some problems when changing from MBR to GPT partitions or
711 // the other way around.
712 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
717 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
719 // Set partition type
720 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
721 asprintf(&cmd
, "%s mklabel msdos", cmd
);
722 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
723 asprintf(&cmd
, "%s mklabel gpt", cmd
);
725 unsigned long long part_start
= MB2BYTES(1);
727 if (*dest
->part_bootldr
) {
728 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
729 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
730 part_start
, part_start
+ dest
->size_bootldr
- 1);
732 part_start
+= dest
->size_bootldr
;
735 if (*dest
->part_boot
) {
736 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
737 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
738 part_start
, part_start
+ dest
->size_boot
- 1);
740 part_start
+= dest
->size_boot
;
743 if (*dest
->part_boot_efi
) {
744 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
745 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
746 part_start
, part_start
+ dest
->size_boot_efi
- 1);
748 part_start
+= dest
->size_boot_efi
;
751 if (*dest
->part_swap
) {
752 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
753 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
754 part_start
, part_start
+ dest
->size_swap
- 1);
756 part_start
+= dest
->size_swap
;
759 if (*dest
->part_root
) {
760 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
761 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
762 part_start
, part_start
+ dest
->size_root
- 1);
764 part_start
+= dest
->size_root
;
767 if (dest
->part_boot_idx
> 0)
768 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
770 if (dest
->part_boot_efi_idx
> 0)
771 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
773 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
774 if (*dest
->part_bootldr
) {
775 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
779 r
= mysystem(output
, cmd
);
781 // Wait until the system re-read the partition table
783 unsigned int counter
= 10;
785 while (counter
-- > 0) {
788 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
791 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
794 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
797 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
800 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
803 // All partitions do exist, exiting the loop.
814 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
815 char cmd
[STRING_SIZE
] = "\0";
819 if (fs
== HW_FS_SWAP
) {
820 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
823 } else if (fs
== HW_FS_EXT4
) {
824 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
827 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
828 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
831 } else if (fs
== HW_FS_XFS
) {
832 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
835 } else if (fs
== HW_FS_BTRFS
) {
836 r
= hw_create_btrfs_layout(path
, output
);
841 } else if (fs
== HW_FS_FAT32
) {
842 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
847 r
= mysystem(output
, cmd
);
852 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
856 if (*dest
->part_boot
) {
857 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
863 if (*dest
->part_boot_efi
) {
864 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
870 if (*dest
->part_swap
) {
871 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
877 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
884 int hw_create_btrfs_layout(const char* path
, const char* output
) {
885 const struct btrfs_subvolumes
* subvolume
= NULL
;
886 char cmd
[STRING_SIZE
];
887 char volume
[STRING_SIZE
];
890 r
= snprintf(cmd
, sizeof(cmd
), "/usr/bin/mkfs.btrfs -f %s", path
);
895 // Create the main BTRFS file system.
896 r
= mysystem(output
, cmd
);
903 // We need to mount the FS in order to create any subvolumes.
904 r
= hw_mount(path
, DESTINATION_MOUNT_PATH
, "btrfs", 0);
910 // Loop through the list of subvolumes to create.
911 for ( subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++ ) {
912 r
= snprintf(volume
, sizeof(volume
), "%s", subvolume
->name
);
914 // Abort if snprintf fails.
919 // Call function to create the subvolume
920 r
= hw_create_btrfs_subvolume(output
, volume
);
927 // Umount the main BTRFS after subvolume creation.
928 r
= hw_umount(DESTINATION_MOUNT_PATH
, 0);
937 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
938 char target
[STRING_SIZE
];
941 assert(*prefix
== '/');
943 const char* filesystem
;
944 switch (dest
->filesystem
) {
946 case HW_FS_EXT4_WO_JOURNAL
:
955 filesystem
= "btrfs";
967 if (dest
->filesystem
== HW_FS_BTRFS
) {
968 r
= hw_mount_btrfs_subvolumes(dest
->part_root
);
973 r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
980 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
981 r
= mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
984 hw_umount_filesystems(dest
, prefix
);
989 if (*dest
->part_boot
) {
990 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
992 hw_umount_filesystems(dest
, prefix
);
999 if (*dest
->part_boot_efi
) {
1000 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
1001 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1003 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
1005 hw_umount_filesystems(dest
, prefix
);
1012 if (*dest
->part_swap
) {
1013 r
= swapon(dest
->part_swap
, 0);
1015 hw_umount_filesystems(dest
, prefix
);
1021 // bind-mount misc filesystems
1022 r
= hw_bind_mount("/dev", prefix
);
1026 r
= hw_bind_mount("/proc", prefix
);
1030 r
= hw_bind_mount("/sys", prefix
);
1034 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
1035 if (r
&& errno
!= ENOENT
)
1041 int hw_mount_btrfs_subvolumes(const char* source
) {
1042 const struct btrfs_subvolumes
* subvolume
= NULL
;
1043 char path
[STRING_SIZE
];
1044 char options
[STRING_SIZE
];
1047 // Loop through the list of known subvolumes.
1048 for ( subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++ ) {
1049 // Assign subvolume path.
1050 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
1056 // Assign subvolume name.
1057 r
= snprintf(options
, sizeof(options
), "subvol=%s,%s", subvolume
->name
, BTRFS_MOUNT_OPTIONS
);
1062 // Create the directory.
1063 r
= hw_mkdir(path
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1065 // Abort if the directory could not be created.
1066 if(r
!= 0 && errno
!= EEXIST
)
1069 // Print log message
1070 fprintf(flog
, "Mounting subvolume %s to %s\n", subvolume
->name
, subvolume
->mount_path
);
1072 // Try to mount the subvolume.
1073 r
= mount(source
, path
, "btrfs", NULL
, options
);
1083 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1085 char target
[STRING_SIZE
];
1087 // Write all buffers to disk before umounting
1091 if (*dest
->part_boot_efi
) {
1092 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
1098 if (*dest
->part_boot
) {
1099 r
= hw_umount(HW_PATH_BOOT
, prefix
);
1105 if (*dest
->part_swap
) {
1106 swapoff(dest
->part_swap
);
1110 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
1114 r
= hw_umount("/sys", prefix
);
1118 r
= hw_umount("/proc", prefix
);
1122 r
= hw_umount("/dev", prefix
);
1127 if(dest
->filesystem
== HW_FS_BTRFS
) {
1128 r
= hw_umount_btrfs_layout();
1130 r
= hw_umount(prefix
, NULL
);
1139 int hw_umount_btrfs_layout() {
1140 const struct btrfs_subvolumes
* subvolume
= NULL
;
1141 char path
[STRING_SIZE
];
1147 // Reset the retry marker
1150 // Loop through the list of subvolumes
1151 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1152 // Abort if the subvolume path could not be assigned.
1153 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
1159 // Try to umount the subvolume.
1160 r
= umount2(path
, 0);
1162 // Handle return codes.
1166 // Set marker to retry the umount.
1169 // Ignore if the subvolume could not be unmounted yet,
1170 // because it is still used.
1174 // Ignore if the subvolume already has been unmounted
1177 // Ignore if the directory does not longer exist.
1180 fprintf(flog
, "Could not umount %s from %s - Error: %d\n", subvolume
->name
, path
, r
);
1185 // Print log message
1186 fprintf(flog
, "Umounted %s from %s\n", subvolume
->name
, path
);
1189 // Abort loop if all mountpoins got umounted
1194 // Abort after five failed umount attempts
1199 // Increment counter.
1204 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
1205 char cmd
[STRING_SIZE
];
1207 hw_stop_all_raid_arrays(output
);
1208 hw_stop_all_raid_arrays(output
);
1211 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
1212 mysystem(output
, cmd
);
1216 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
1217 mysystem(output
, cmd
);
1223 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1227 assert(dest
->is_raid
);
1229 // Stop all RAID arrays that might be around (again).
1230 // It seems that there is some sort of race-condition with udev re-enabling
1231 // the raid arrays and therefore locking the disks.
1232 r
= hw_destroy_raid_superblocks(dest
, output
);
1234 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1235 RAID_METADATA
, dest
->path
);
1237 switch (dest
->raid_level
) {
1239 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1247 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1249 // Clear all data at the beginning
1250 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1256 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1258 // Clear all data at the beginning
1259 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1264 r
= mysystem(output
, cmd
);
1267 // Wait a moment until the device has been properly brought up
1269 unsigned int counter
= 10;
1270 while (counter
-- > 0) {
1273 // If the raid device has not yet been properly brought up,
1274 // opening it will fail with the message: Device or resource busy
1275 // Hence we will wait a bit until it becomes usable.
1276 if (try_open(dest
->path
) == 0)
1284 int hw_stop_all_raid_arrays(const char* output
) {
1285 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1288 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1289 char cmd
[STRING_SIZE
];
1291 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1292 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1301 static char* hw_get_uuid(const char* dev
) {
1302 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1303 const char* buffer
= NULL
;
1310 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1313 uuid
= strdup(buffer
);
1315 blkid_free_probe(p
);
1320 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1322 int hw_write_fstab(struct hw_destination
* dest
) {
1323 const struct btrfs_subvolumes
* subvolume
= NULL
;
1324 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1329 char mount_options
[STRING_SIZE
];
1332 if (*dest
->part_boot
) {
1333 uuid
= hw_get_uuid(dest
->part_boot
);
1336 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
1342 if (*dest
->part_boot_efi
) {
1343 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1346 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1353 if (*dest
->part_swap
) {
1354 uuid
= hw_get_uuid(dest
->part_swap
);
1357 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1363 uuid
= hw_get_uuid(dest
->part_root
);
1365 if(dest
->filesystem
== HW_FS_BTRFS
) {
1366 // Loop through the list of subvolumes
1367 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1368 // Abort if the mount options could not be assigned
1369 int r
= snprintf(mount_options
, sizeof(mount_options
), "defaults,%s,subvol=%s", BTRFS_MOUNT_OPTIONS
, subvolume
->name
);
1374 // Write the entry to the file
1375 fprintf(f
, FSTAB_FMT
, uuid
, subvolume
->mount_path
, "btrfs", mount_options
, 1, 1);
1378 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1395 int hw_start_networking(const char* output
) {
1396 return mysystem(output
, "/usr/bin/start-networking.sh");
1399 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1400 char path
[STRING_SIZE
];
1402 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1403 int r
= access(path
, R_OK
);
1406 return strdup(path
);
1411 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1412 char command
[STRING_SIZE
];
1414 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s "
1415 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1416 backup_path
, destination
, destination
, destination
);
1417 int rc
= mysystem(output
, command
);
1425 int hw_mkdir(const char *dir
) {
1426 char tmp
[STRING_SIZE
];
1431 snprintf(tmp
, sizeof(tmp
),"%s",dir
);
1434 if (tmp
[len
- 1] == '/') {
1438 for (p
= tmp
+ 1; *p
; p
++) {
1442 // Create target if it does not exist
1443 if (access(tmp
, X_OK
) != 0) {
1444 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1455 // Create target if it does not exist
1456 if (access(tmp
, X_OK
) != 0) {
1457 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1468 int hw_create_btrfs_subvolume(const char* output
, const char* subvolume
) {
1469 char command
[STRING_SIZE
];
1472 // Abort if the command could not be assigned.
1473 r
= snprintf(command
, sizeof(command
), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH
, subvolume
);
1478 // Create the subvolume
1479 r
= mysystem(output
, command
);