]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
6bf05b185a6fd509783fba0e694ed867045fdd98
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
) {
137 int file_fd
= open(source
, O_RDWR
);
141 if ((device_fd
= open(device
, O_RDWR
)) < 0)
144 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
156 if (device_fd
>= 0) {
157 ioctl(device_fd
, LOOP_CLR_FD
, 0);
164 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
165 const char* loop_device
= "/dev/loop0";
167 // Create target if it does not exist
168 if (access(target
, X_OK
) != 0)
169 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
174 if (S_ISREG(st
.st_mode
)) {
175 int r
= setup_loop_device(source
, loop_device
);
177 source
= loop_device
;
183 return mount(source
, target
, fs
, flags
, NULL
);
186 static int hw_bind_mount(const char* source
, const char* prefix
) {
187 if (!source
|| !prefix
) {
192 char target
[PATH_MAX
];
196 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
200 // Ensure target exists
201 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
203 return hw_mount(source
, target
, NULL
, MS_BIND
);
206 int hw_umount(const char* source
, const char* prefix
) {
207 char target
[PATH_MAX
];
211 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
213 r
= snprintf(target
, sizeof(target
) - 1, "%s", source
);
218 r
= umount2(target
, 0);
221 // Try again with force if umount wasn't successful
225 r
= umount2(target
, MNT_FORCE
);
228 // target wasn't a mountpoint. Ignore.
233 // target doesn't exist
243 static int hw_test_source_medium(const char* path
) {
244 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
247 // 2nd try, ntfs for a rufus converted usb key
248 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "ntfs3", MS_RDONLY
);
251 // 3rd try, vfat for a rufus converted usb key
252 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "vfat", MS_RDONLY
);
255 // If the source could not be mounted we
260 // Check if the test file exists.
261 ret
= access(SOURCE_TEST_FILE
, R_OK
);
263 // Umount the test device.
264 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
269 char* hw_find_source_medium(struct hw
* hw
) {
272 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
274 udev_enumerate_add_match_subsystem(enumerate
, "block");
275 udev_enumerate_scan_devices(enumerate
);
277 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
279 struct udev_list_entry
* dev_list_entry
;
280 udev_list_entry_foreach(dev_list_entry
, devices
) {
281 const char* path
= udev_list_entry_get_name(dev_list_entry
);
282 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
284 const char* dev_path
= udev_device_get_devnode(dev
);
286 // Skip everything what we cannot work with
287 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
288 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
291 if (hw_test_source_medium(dev_path
) == 0) {
292 ret
= strdup(dev_path
);
295 udev_device_unref(dev
);
297 // If a suitable device was found the search will end.
302 udev_enumerate_unref(enumerate
);
307 static struct hw_disk
** hw_create_disks() {
308 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
313 static unsigned long long hw_block_device_get_size(const char* dev
) {
314 int fd
= open(dev
, O_RDONLY
);
318 unsigned long long size
= blkid_get_dev_size(fd
);
324 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
325 struct hw_disk
** ret
= hw_create_disks();
326 struct hw_disk
** disks
= ret
;
329 // Determine the disk device of source if it is a partition
330 const char* sourcedisk
= NULL
;
331 char syssource
[PATH_MAX
];
332 (void)snprintf(syssource
, sizeof(syssource
) - 1, "/sys/class/block/%s", sourcedrive
+ 5);
333 struct udev_device
* s_dev
= udev_device_new_from_syspath(hw
->udev
, syssource
);
334 const char* s_devtype
= udev_device_get_property_value(s_dev
, "DEVTYPE");
335 if (s_devtype
&& (strcmp(s_devtype
, "partition") == 0)) {
336 struct udev_device
* p_dev
= udev_device_get_parent_with_subsystem_devtype(s_dev
,"block","disk");
338 sourcedisk
= udev_device_get_devnode(p_dev
);
341 if (!sourcedisk
) sourcedisk
= sourcedrive
;
343 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
345 udev_enumerate_add_match_subsystem(enumerate
, "block");
346 udev_enumerate_scan_devices(enumerate
);
348 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
350 struct udev_list_entry
* dev_list_entry
;
351 unsigned int i
= HW_MAX_DISKS
;
352 udev_list_entry_foreach(dev_list_entry
, devices
) {
353 const char* path
= udev_list_entry_get_name(dev_list_entry
);
354 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
356 const char* dev_path
= udev_device_get_devnode(dev
);
358 // Skip everything what we cannot work with
359 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
360 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
361 strstartswith(dev_path
, "/dev/md")) {
362 udev_device_unref(dev
);
366 // Skip sourcedisk if we need to
367 if (sourcedisk
&& (strcmp(dev_path
, sourcedisk
) == 0)) {
368 udev_device_unref(dev
);
372 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
373 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
374 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
375 udev_device_unref(dev
);
379 // Skip devices with a size of zero
380 unsigned long long size
= hw_block_device_get_size(dev_path
);
382 udev_device_unref(dev
);
386 struct hw_disk
* disk
= malloc(sizeof(*disk
));
392 snprintf(disk
->path
, sizeof(disk
->path
), "%s", dev_path
);
393 const char* p
= disk
->path
+ 5;
398 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
400 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
402 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
405 snprintf(disk
->vendor
, sizeof(disk
->vendor
), "%s", vendor
);
407 *disk
->vendor
= '\0';
410 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
412 model
= udev_device_get_sysattr_value(dev
, "model");
414 model
= udev_device_get_sysattr_value(dev
, "product");
417 snprintf(disk
->model
, sizeof(disk
->model
), "%s", model
);
421 // Format description
422 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
424 if (*disk
->vendor
&& *disk
->model
) {
425 snprintf(disk
->description
, sizeof(disk
->description
),
426 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
428 } else if (*disk
->vendor
|| *disk
->model
) {
429 snprintf(disk
->description
, sizeof(disk
->description
),
430 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
433 snprintf(disk
->description
, sizeof(disk
->description
),
434 "%s - %s", size_str
, p
);
437 // Cut off the description string after 40 characters
438 disk
->description
[41] = '\0';
445 udev_device_unref(dev
);
448 udev_enumerate_unref(enumerate
);
455 void hw_free_disks(struct hw_disk
** disks
) {
456 struct hw_disk
** disk
= disks
;
458 while (*disk
!= NULL
) {
459 if (--(*disk
)->ref
== 0)
468 unsigned int hw_count_disks(struct hw_disk
** disks
) {
469 unsigned int ret
= 0;
477 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
478 struct hw_disk
** ret
= hw_create_disks();
479 struct hw_disk
** selected_disks
= ret
;
481 unsigned int num_disks
= hw_count_disks(disks
);
483 for (unsigned int i
= 0; i
< num_disks
; i
++) {
484 if (!selection
|| selection
[i
]) {
485 struct hw_disk
*selected_disk
= disks
[i
];
486 selected_disk
->ref
++;
488 *selected_disks
++ = selected_disk
;
493 *selected_disks
= NULL
;
498 struct hw_disk
** hw_select_first_disk(struct hw_disk
** disks
) {
499 struct hw_disk
** ret
= hw_create_disks();
500 struct hw_disk
** selected_disks
= ret
;
502 unsigned int num_disks
= hw_count_disks(disks
);
503 assert(num_disks
> 0);
505 for (unsigned int i
= 0; i
< num_disks
; i
++) {
506 struct hw_disk
*disk
= disks
[i
];
509 *selected_disks
++ = disk
;
514 *selected_disks
= NULL
;
519 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
520 unsigned long long memory
= hw_memory();
522 unsigned long long swap_size
= memory
/ 4;
524 // Min. swap size is 128MB
525 if (swap_size
< MB2BYTES(128))
526 swap_size
= MB2BYTES(128);
528 // Cap swap size to 1GB
529 else if (swap_size
> MB2BYTES(1024))
530 swap_size
= MB2BYTES(1024);
535 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
536 return MB2BYTES(512);
539 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
540 // All RAID devices have the p suffix.
544 // Devices with a number at the end have the p suffix, too.
545 // e.g. mmcblk0, cciss0
546 unsigned int last_char
= strlen(dest
->path
) - 1;
547 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
553 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
557 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
558 hw_device_has_p_suffix(dest
) ? "p" : "");
559 dest
->part_boot_idx
= 0;
561 // Determine the size of the target block device
563 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
564 dest
->disk2
->size
: dest
->disk1
->size
;
566 // The RAID will install some metadata at the end of the disk
567 // and we will save up some space for that.
568 dest
->size
-= MB2BYTES(2);
570 dest
->size
= dest
->disk1
->size
;
573 // As we add some extra space before the beginning of the first
574 // partition, we need to substract that here.
575 dest
->size
-= MB2BYTES(1);
577 // Add some more space for partition tables, etc.
578 dest
->size
-= MB2BYTES(1);
580 // The disk has to have at least 2GB
581 if (dest
->size
<= MB2BYTES(2048))
584 // Determine partition table
585 dest
->part_table
= HW_PART_TABLE_MSDOS
;
587 // Disks over 2TB need to use GPT
588 if (dest
->size
>= MB2BYTES(2047 * 1024))
589 dest
->part_table
= HW_PART_TABLE_GPT
;
591 // We also use GPT on raid disks by default
592 else if (dest
->is_raid
)
593 dest
->part_table
= HW_PART_TABLE_GPT
;
595 // When using GPT, GRUB2 needs a little bit of space to put
597 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
598 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
599 "%s%d", path
, part_idx
);
601 dest
->size_bootldr
= MB2BYTES(4);
603 dest
->part_boot_idx
= part_idx
++;
605 *dest
->part_bootldr
= '\0';
606 dest
->size_bootldr
= 0;
609 // Disable seperate boot partition for BTRFS installations.
610 if(dest
->filesystem
== HW_FS_BTRFS
) {
613 dest
->size_boot
= hw_boot_size(dest
);
616 // Create an EFI partition when running in EFI mode
618 dest
->size_boot_efi
= MB2BYTES(32);
620 dest
->size_boot_efi
= 0;
622 // Determine the size of the data partition.
623 unsigned long long space_left
= dest
->size
- \
624 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
626 // If we have less than 2GB left, we disable swap
627 if (space_left
<= MB2BYTES(2048))
630 // Should we use swap?
634 dest
->size_swap
= hw_swap_size(dest
);
637 space_left
-= dest
->size_swap
;
639 // Root is getting what ever is left
640 dest
->size_root
= space_left
;
642 // Set partition names
643 if (dest
->size_boot
> 0) {
644 if (dest
->part_boot_idx
== 0)
645 dest
->part_boot_idx
= part_idx
;
647 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
649 *dest
->part_boot
= '\0';
651 if (dest
->size_boot_efi
> 0) {
652 dest
->part_boot_efi_idx
= part_idx
;
654 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
655 "%s%d", path
, part_idx
++);
657 *dest
->part_boot_efi
= '\0';
658 dest
->part_boot_efi_idx
= 0;
661 if (dest
->size_swap
> 0)
662 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
664 *dest
->part_swap
= '\0';
666 // There is always a root partition
667 if (dest
->part_boot_idx
== 0)
668 dest
->part_boot_idx
= part_idx
;
670 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
675 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
,
676 int disable_swap
, int filesystem
) {
677 struct hw_destination
* dest
= malloc(sizeof(*dest
));
680 dest
->filesystem
= filesystem
;
682 if (part_type
== HW_PART_TYPE_NORMAL
) {
683 dest
->disk1
= *disks
;
686 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
688 } else if (part_type
== HW_PART_TYPE_RAID1
) {
689 dest
->disk1
= *disks
++;
690 dest
->disk2
= *disks
;
691 dest
->raid_level
= 1;
693 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
696 // Is this a RAID device?
697 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
699 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
706 unsigned long long hw_memory() {
709 int r
= sysinfo(&si
);
716 static int hw_zero_out_device(const char* path
, int bytes
) {
718 memset(block
, 0, sizeof(block
));
720 int blocks
= bytes
/ sizeof(block
);
722 int fd
= open(path
, O_WRONLY
);
726 unsigned int bytes_written
= 0;
727 while (blocks
-- > 0) {
728 bytes_written
+= write(fd
, block
, sizeof(block
));
734 return bytes_written
;
737 static int try_open(const char* path
) {
738 FILE* f
= fopen(path
, "r");
747 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
748 // Before we write a new partition table to the disk, we will erase
749 // the first couple of megabytes at the beginning of the device to
750 // get rid of all left other things like bootloaders and partition tables.
751 // This solves some problems when changing from MBR to GPT partitions or
752 // the other way around.
753 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
758 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
760 // Set partition type
761 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
762 asprintf(&cmd
, "%s mklabel msdos", cmd
);
763 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
764 asprintf(&cmd
, "%s mklabel gpt", cmd
);
766 unsigned long long part_start
= MB2BYTES(1);
768 if (*dest
->part_bootldr
) {
769 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
770 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
771 part_start
, part_start
+ dest
->size_bootldr
- 1);
773 part_start
+= dest
->size_bootldr
;
776 if (*dest
->part_boot
) {
777 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
778 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
779 part_start
, part_start
+ dest
->size_boot
- 1);
781 part_start
+= dest
->size_boot
;
784 if (*dest
->part_boot_efi
) {
785 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
786 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
787 part_start
, part_start
+ dest
->size_boot_efi
- 1);
789 part_start
+= dest
->size_boot_efi
;
792 if (*dest
->part_swap
) {
793 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
794 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
795 part_start
, part_start
+ dest
->size_swap
- 1);
797 part_start
+= dest
->size_swap
;
800 if (*dest
->part_root
) {
801 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
802 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
803 part_start
, part_start
+ dest
->size_root
- 1);
805 part_start
+= dest
->size_root
;
808 if (dest
->part_boot_idx
> 0)
809 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
811 if (dest
->part_boot_efi_idx
> 0)
812 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
814 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
815 if (*dest
->part_bootldr
) {
816 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
820 r
= mysystem(output
, cmd
);
822 // Wait until the system re-read the partition table
824 unsigned int counter
= 10;
826 while (counter
-- > 0) {
829 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
832 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
835 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
838 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
841 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
844 // All partitions do exist, exiting the loop.
855 static int hw_create_btrfs_subvolume(const char* output
, const char* subvolume
) {
856 char command
[STRING_SIZE
];
859 // Abort if the command could not be assigned.
860 r
= snprintf(command
, sizeof(command
), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH
, subvolume
);
864 // Create the subvolume
865 r
= mysystem(output
, command
);
872 static int hw_create_btrfs_layout(const char* path
, const char* output
) {
873 const struct btrfs_subvolumes
* subvolume
= NULL
;
874 char cmd
[STRING_SIZE
];
875 char volume
[STRING_SIZE
];
878 r
= snprintf(cmd
, sizeof(cmd
), "/usr/bin/mkfs.btrfs -f %s", path
);
882 // Create the main BTRFS file system.
883 r
= mysystem(output
, cmd
);
887 // We need to mount the FS in order to create any subvolumes.
888 r
= hw_mount(path
, DESTINATION_MOUNT_PATH
, "btrfs", 0);
892 // Loop through the list of subvolumes to create.
893 for ( subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++ ) {
894 r
= snprintf(volume
, sizeof(volume
), "%s", subvolume
->name
);
896 // Abort if snprintf fails.
900 // Call function to create the subvolume
901 r
= hw_create_btrfs_subvolume(output
, volume
);
906 // Umount the main BTRFS after subvolume creation.
907 r
= hw_umount(DESTINATION_MOUNT_PATH
, 0);
914 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
915 char cmd
[STRING_SIZE
] = "\0";
919 if (fs
== HW_FS_SWAP
) {
920 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
923 } else if (fs
== HW_FS_EXT4
) {
924 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
927 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
928 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
931 } else if (fs
== HW_FS_XFS
) {
932 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
935 } else if (fs
== HW_FS_BTRFS
) {
936 r
= hw_create_btrfs_layout(path
, output
);
941 } else if (fs
== HW_FS_FAT32
) {
942 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
947 r
= mysystem(output
, cmd
);
952 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
956 if (*dest
->part_boot
) {
957 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
963 if (*dest
->part_boot_efi
) {
964 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
970 if (*dest
->part_swap
) {
971 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
977 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
984 static int hw_mount_btrfs_subvolumes(const char* source
) {
985 const struct btrfs_subvolumes
* subvolume
= NULL
;
986 char path
[STRING_SIZE
];
987 char options
[STRING_SIZE
];
990 // Loop through the list of known subvolumes.
991 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
992 // Assign subvolume path.
993 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
997 // Assign subvolume name.
998 r
= snprintf(options
, sizeof(options
), "subvol=%s,%s", subvolume
->name
, BTRFS_MOUNT_OPTIONS
);
1002 // Create the directory.
1005 // Abort if the directory could not be created.
1006 if(r
!= 0 && errno
!= EEXIST
)
1009 // Print log message
1010 fprintf(flog
, "Mounting subvolume %s to %s\n", subvolume
->name
, subvolume
->mount_path
);
1012 // Try to mount the subvolume.
1013 r
= mount(source
, path
, "btrfs", 0, options
);
1021 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1022 char target
[STRING_SIZE
];
1025 assert(*prefix
== '/');
1027 const char* filesystem
;
1028 switch (dest
->filesystem
) {
1030 case HW_FS_EXT4_WO_JOURNAL
:
1031 filesystem
= "ext4";
1039 filesystem
= "btrfs";
1043 filesystem
= "vfat";
1051 if (dest
->filesystem
== HW_FS_BTRFS
) {
1052 r
= hw_mount_btrfs_subvolumes(dest
->part_root
);
1056 r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
1062 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
1063 r
= mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1066 hw_umount_filesystems(dest
, prefix
);
1071 if (*dest
->part_boot
) {
1072 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
1074 hw_umount_filesystems(dest
, prefix
);
1081 if (*dest
->part_boot_efi
) {
1082 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
1083 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1085 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
1087 hw_umount_filesystems(dest
, prefix
);
1094 if (*dest
->part_swap
) {
1095 r
= swapon(dest
->part_swap
, 0);
1097 hw_umount_filesystems(dest
, prefix
);
1103 // bind-mount misc filesystems
1104 r
= hw_bind_mount("/dev", prefix
);
1108 r
= hw_bind_mount("/proc", prefix
);
1112 r
= hw_bind_mount("/sys", prefix
);
1116 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
1117 if (r
&& errno
!= ENOENT
)
1123 static int hw_umount_btrfs_layout() {
1124 const struct btrfs_subvolumes
* subvolume
= NULL
;
1125 char path
[STRING_SIZE
];
1131 // Reset the retry marker
1134 // Loop through the list of subvolumes
1135 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1136 // Abort if the subvolume path could not be assigned.
1137 r
= snprintf(path
, sizeof(path
), "%s%s", DESTINATION_MOUNT_PATH
, subvolume
->mount_path
);
1141 // Try to umount the subvolume.
1142 r
= umount2(path
, 0);
1144 // Handle return codes.
1148 // Set marker to retry the umount.
1151 // Ignore if the subvolume could not be unmounted yet,
1152 // because it is still used.
1156 // Ignore if the subvolume already has been unmounted
1159 // Ignore if the directory does not longer exist.
1162 fprintf(flog
, "Could not umount %s from %s - Error: %d\n", subvolume
->name
, path
, r
);
1167 // Print log message
1168 fprintf(flog
, "Umounted %s from %s\n", subvolume
->name
, path
);
1171 // Abort loop if all mountpoins got umounted
1175 // Abort after five failed umount attempts
1179 // Increment counter.
1184 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1187 // Write all buffers to disk before umounting
1191 if (*dest
->part_boot_efi
) {
1192 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
1198 if (*dest
->part_boot
) {
1199 r
= hw_umount(HW_PATH_BOOT
, prefix
);
1205 if (*dest
->part_swap
) {
1206 swapoff(dest
->part_swap
);
1210 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
1214 r
= hw_umount("/sys", prefix
);
1218 r
= hw_umount("/proc", prefix
);
1222 r
= hw_umount("/dev", prefix
);
1227 if(dest
->filesystem
== HW_FS_BTRFS
) {
1228 r
= hw_umount_btrfs_layout();
1230 r
= hw_umount(prefix
, NULL
);
1239 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
1240 char cmd
[STRING_SIZE
];
1242 hw_stop_all_raid_arrays(output
);
1243 hw_stop_all_raid_arrays(output
);
1246 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
1247 mysystem(output
, cmd
);
1251 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
1252 mysystem(output
, cmd
);
1258 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1262 assert(dest
->is_raid
);
1264 // Stop all RAID arrays that might be around (again).
1265 // It seems that there is some sort of race-condition with udev re-enabling
1266 // the raid arrays and therefore locking the disks.
1267 r
= hw_destroy_raid_superblocks(dest
, output
);
1269 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1270 RAID_METADATA
, dest
->path
);
1272 switch (dest
->raid_level
) {
1274 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1282 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1284 // Clear all data at the beginning
1285 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1291 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1293 // Clear all data at the beginning
1294 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1299 r
= mysystem(output
, cmd
);
1302 // Wait a moment until the device has been properly brought up
1304 unsigned int counter
= 10;
1305 while (counter
-- > 0) {
1308 // If the raid device has not yet been properly brought up,
1309 // opening it will fail with the message: Device or resource busy
1310 // Hence we will wait a bit until it becomes usable.
1311 if (try_open(dest
->path
) == 0)
1319 int hw_stop_all_raid_arrays(const char* output
) {
1320 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1323 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1324 char cmd
[STRING_SIZE
];
1326 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1327 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1336 static char* hw_get_uuid(const char* dev
) {
1337 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1338 const char* buffer
= NULL
;
1345 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1348 uuid
= strdup(buffer
);
1350 blkid_free_probe(p
);
1355 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1357 int hw_write_fstab(struct hw_destination
* dest
) {
1358 const struct btrfs_subvolumes
* subvolume
= NULL
;
1359 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1364 char mount_options
[STRING_SIZE
];
1367 if (*dest
->part_boot
) {
1368 uuid
= hw_get_uuid(dest
->part_boot
);
1371 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
1377 if (*dest
->part_boot_efi
) {
1378 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1381 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1388 if (*dest
->part_swap
) {
1389 uuid
= hw_get_uuid(dest
->part_swap
);
1392 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1398 uuid
= hw_get_uuid(dest
->part_root
);
1400 if(dest
->filesystem
== HW_FS_BTRFS
) {
1401 // Loop through the list of subvolumes
1402 for (subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++) {
1403 // Abort if the mount options could not be assigned
1404 int r
= snprintf(mount_options
, sizeof(mount_options
), "defaults,%s,subvol=%s", BTRFS_MOUNT_OPTIONS
, subvolume
->name
);
1409 // Write the entry to the file
1410 fprintf(f
, FSTAB_FMT
, uuid
, subvolume
->mount_path
, "btrfs", mount_options
, 1, 1);
1413 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1430 int hw_start_networking(const char* output
) {
1431 return mysystem(output
, "/usr/bin/start-networking.sh");
1434 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1435 char path
[STRING_SIZE
];
1437 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1438 int r
= access(path
, R_OK
);
1441 return strdup(path
);
1446 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1447 char command
[STRING_SIZE
];
1449 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s "
1450 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1451 backup_path
, destination
, destination
, destination
);
1452 int rc
= mysystem(output
, command
);