]>
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>
47 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
48 char chroot_cmd
[STRING_SIZE
];
50 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
52 return mysystem(output
, chroot_cmd
);
55 struct hw
* hw_init() {
56 struct hw
* hw
= calloc(1, sizeof(*hw
));
60 hw
->udev
= udev_new();
62 fprintf(stderr
, "Could not create udev instance\n");
66 // What architecture are we running on?
67 struct utsname uname_data
;
68 int ret
= uname(&uname_data
);
70 snprintf(hw
->arch
, sizeof(hw
->arch
), "%s", uname_data
.machine
);
72 // Should we install in EFI mode?
73 if ((strcmp(hw
->arch
, "x86_64") == 0) || (strcmp(hw
->arch
, "aarch64") == 0))
79 void hw_free(struct hw
* hw
) {
86 static int strstartswith(const char* a
, const char* b
) {
87 return (strncmp(a
, b
, strlen(b
)) == 0);
90 static char loop_device
[STRING_SIZE
];
92 static int setup_loop_device(const char* source
, const char* device
) {
93 int file_fd
= open(source
, O_RDWR
);
98 if ((device_fd
= open(device
, O_RDWR
)) < 0)
101 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
113 if (device_fd
>= 0) {
114 ioctl(device_fd
, LOOP_CLR_FD
, 0);
121 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
122 const char* loop_device
= "/dev/loop0";
124 // Create target if it does not exist
125 if (access(target
, X_OK
) != 0)
126 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
131 if (S_ISREG(st
.st_mode
)) {
132 int r
= setup_loop_device(source
, loop_device
);
134 source
= loop_device
;
140 return mount(source
, target
, fs
, flags
, NULL
);
143 static int hw_bind_mount(const char* source
, const char* prefix
) {
144 if (!source
|| !prefix
) {
149 char target
[PATH_MAX
];
153 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
157 // Ensure target exists
158 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
160 return hw_mount(source
, target
, NULL
, MS_BIND
);
163 int hw_umount(const char* source
, const char* prefix
) {
164 char target
[PATH_MAX
];
168 r
= snprintf(target
, sizeof(target
) - 1, "%s/%s", prefix
, source
);
170 r
= snprintf(target
, sizeof(target
) - 1, "%s", source
);
175 r
= umount2(target
, 0);
178 // Try again with force if umount wasn't successful
182 r
= umount2(target
, MNT_FORCE
);
185 // target wasn't a mountpoint. Ignore.
190 // target doesn't exist
200 static int hw_test_source_medium(const char* path
) {
201 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
204 // 2nd try, ntfs for a rufus converted usb key
205 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "ntfs3", MS_RDONLY
);
208 // 3rd try, vfat for a rufus converted usb key
209 ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "vfat", MS_RDONLY
);
212 // If the source could not be mounted we
217 // Check if the test file exists.
218 ret
= access(SOURCE_TEST_FILE
, R_OK
);
220 // Umount the test device.
221 hw_umount(SOURCE_MOUNT_PATH
, NULL
);
226 char* hw_find_source_medium(struct hw
* hw
) {
229 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
231 udev_enumerate_add_match_subsystem(enumerate
, "block");
232 udev_enumerate_scan_devices(enumerate
);
234 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
236 struct udev_list_entry
* dev_list_entry
;
237 udev_list_entry_foreach(dev_list_entry
, devices
) {
238 const char* path
= udev_list_entry_get_name(dev_list_entry
);
239 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
241 const char* dev_path
= udev_device_get_devnode(dev
);
243 // Skip everything what we cannot work with
244 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
245 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
248 if (hw_test_source_medium(dev_path
) == 0) {
249 ret
= strdup(dev_path
);
252 udev_device_unref(dev
);
254 // If a suitable device was found the search will end.
259 udev_enumerate_unref(enumerate
);
264 static struct hw_disk
** hw_create_disks() {
265 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
270 static unsigned long long hw_block_device_get_size(const char* dev
) {
271 int fd
= open(dev
, O_RDONLY
);
275 unsigned long long size
= blkid_get_dev_size(fd
);
281 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
282 struct hw_disk
** ret
= hw_create_disks();
283 struct hw_disk
** disks
= ret
;
285 // Determine the disk device of source if it is a partition
286 char* sourcedisk
= NULL
;
287 char syssource
[PATH_MAX
];
288 (void)snprintf(syssource
, sizeof(syssource
) - 1, "/sys/class/block/%s", sourcedrive
+ 5);
289 struct udev_device
* s_dev
= udev_device_new_from_syspath(hw
->udev
, syssource
);
290 const char* s_devtype
= udev_device_get_property_value(s_dev
, "DEVTYPE");
291 if (s_devtype
&& (strcmp(s_devtype
, "partition") == 0)) {
292 struct udev_device
* p_dev
= udev_device_get_parent_with_subsystem_devtype(s_dev
,"block","disk");
294 sourcedisk
= udev_device_get_devnode(p_dev
);
297 if (!sourcedisk
) sourcedisk
= sourcedrive
;
299 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
301 udev_enumerate_add_match_subsystem(enumerate
, "block");
302 udev_enumerate_scan_devices(enumerate
);
304 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
306 struct udev_list_entry
* dev_list_entry
;
307 unsigned int i
= HW_MAX_DISKS
;
308 udev_list_entry_foreach(dev_list_entry
, devices
) {
309 const char* path
= udev_list_entry_get_name(dev_list_entry
);
310 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
312 const char* dev_path
= udev_device_get_devnode(dev
);
314 // Skip everything what we cannot work with
315 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
316 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
317 strstartswith(dev_path
, "/dev/md")) {
318 udev_device_unref(dev
);
322 // Skip sourcedisk if we need to
323 if (sourcedisk
&& (strcmp(dev_path
, sourcedisk
) == 0)) {
324 udev_device_unref(dev
);
328 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
329 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
330 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
331 udev_device_unref(dev
);
335 // Skip devices with a size of zero
336 unsigned long long size
= hw_block_device_get_size(dev_path
);
338 udev_device_unref(dev
);
342 struct hw_disk
* disk
= malloc(sizeof(*disk
));
348 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
349 const char* p
= disk
->path
+ 5;
354 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
356 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
358 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
361 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
363 *disk
->vendor
= '\0';
366 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
368 model
= udev_device_get_sysattr_value(dev
, "model");
370 model
= udev_device_get_sysattr_value(dev
, "product");
373 strncpy(disk
->model
, model
, sizeof(disk
->model
));
377 // Format description
378 char size_str
[STRING_SIZE
];
379 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
381 if (*disk
->vendor
&& *disk
->model
) {
382 snprintf(disk
->description
, sizeof(disk
->description
),
383 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
385 } else if (*disk
->vendor
|| *disk
->model
) {
386 snprintf(disk
->description
, sizeof(disk
->description
),
387 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
390 snprintf(disk
->description
, sizeof(disk
->description
),
391 "%s - %s", size_str
, p
);
394 // Cut off the description string after 40 characters
395 disk
->description
[41] = '\0';
402 udev_device_unref(dev
);
405 udev_enumerate_unref(enumerate
);
412 void hw_free_disks(struct hw_disk
** disks
) {
413 struct hw_disk
** disk
= disks
;
415 while (*disk
!= NULL
) {
416 if (--(*disk
)->ref
== 0)
425 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
426 unsigned int ret
= 0;
434 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
435 struct hw_disk
** ret
= hw_create_disks();
436 struct hw_disk
** selected_disks
= ret
;
438 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
440 for (unsigned int i
= 0; i
< num_disks
; i
++) {
441 if (!selection
|| selection
[i
]) {
442 struct hw_disk
*selected_disk
= disks
[i
];
443 selected_disk
->ref
++;
445 *selected_disks
++ = selected_disk
;
450 *selected_disks
= NULL
;
455 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
456 struct hw_disk
** ret
= hw_create_disks();
457 struct hw_disk
** selected_disks
= ret
;
459 unsigned int num_disks
= hw_count_disks(disks
);
460 assert(num_disks
> 0);
462 for (unsigned int i
= 0; i
< num_disks
; i
++) {
463 struct hw_disk
*disk
= disks
[i
];
466 *selected_disks
++ = disk
;
471 *selected_disks
= NULL
;
476 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
477 unsigned long long memory
= hw_memory();
479 unsigned long long swap_size
= memory
/ 4;
481 // Min. swap size is 128MB
482 if (swap_size
< MB2BYTES(128))
483 swap_size
= MB2BYTES(128);
485 // Cap swap size to 1GB
486 else if (swap_size
> MB2BYTES(1024))
487 swap_size
= MB2BYTES(1024);
492 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
493 return MB2BYTES(512);
496 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
497 // All RAID devices have the p suffix.
501 // Devices with a number at the end have the p suffix, too.
502 // e.g. mmcblk0, cciss0
503 unsigned int last_char
= strlen(dest
->path
) - 1;
504 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
510 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
514 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
515 hw_device_has_p_suffix(dest
) ? "p" : "");
516 dest
->part_boot_idx
= 0;
518 // Determine the size of the target block device
520 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
521 dest
->disk2
->size
: dest
->disk1
->size
;
523 // The RAID will install some metadata at the end of the disk
524 // and we will save up some space for that.
525 dest
->size
-= MB2BYTES(2);
527 dest
->size
= dest
->disk1
->size
;
530 // As we add some extra space before the beginning of the first
531 // partition, we need to substract that here.
532 dest
->size
-= MB2BYTES(1);
534 // Add some more space for partition tables, etc.
535 dest
->size
-= MB2BYTES(1);
537 // The disk has to have at least 2GB
538 if (dest
->size
<= MB2BYTES(2048))
541 // Determine partition table
542 dest
->part_table
= HW_PART_TABLE_MSDOS
;
544 // Disks over 2TB need to use GPT
545 if (dest
->size
>= MB2BYTES(2047 * 1024))
546 dest
->part_table
= HW_PART_TABLE_GPT
;
548 // We also use GPT on raid disks by default
549 else if (dest
->is_raid
)
550 dest
->part_table
= HW_PART_TABLE_GPT
;
552 // When using GPT, GRUB2 needs a little bit of space to put
554 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
555 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
556 "%s%d", path
, part_idx
);
558 dest
->size_bootldr
= MB2BYTES(4);
560 dest
->part_boot_idx
= part_idx
++;
562 *dest
->part_bootldr
= '\0';
563 dest
->size_bootldr
= 0;
566 // Disable seperate boot partition for BTRFS installations.
567 if(dest
->filesystem
== HW_FS_BTRFS
) {
570 dest
->size_boot
= hw_boot_size(dest
);
573 // Create an EFI partition when running in EFI mode
575 dest
->size_boot_efi
= MB2BYTES(32);
577 dest
->size_boot_efi
= 0;
579 // Determine the size of the data partition.
580 unsigned long long space_left
= dest
->size
- \
581 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
583 // If we have less than 2GB left, we disable swap
584 if (space_left
<= MB2BYTES(2048))
587 // Should we use swap?
591 dest
->size_swap
= hw_swap_size(dest
);
594 space_left
-= dest
->size_swap
;
596 // Root is getting what ever is left
597 dest
->size_root
= space_left
;
599 // Set partition names
600 if (dest
->size_boot
> 0) {
601 if (dest
->part_boot_idx
== 0)
602 dest
->part_boot_idx
= part_idx
;
604 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
606 *dest
->part_boot
= '\0';
608 if (dest
->size_boot_efi
> 0) {
609 dest
->part_boot_efi_idx
= part_idx
;
611 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
612 "%s%d", path
, part_idx
++);
614 *dest
->part_boot_efi
= '\0';
615 dest
->part_boot_efi_idx
= 0;
618 if (dest
->size_swap
> 0)
619 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
621 *dest
->part_swap
= '\0';
623 // There is always a root partition
624 if (dest
->part_boot_idx
== 0)
625 dest
->part_boot_idx
= part_idx
;
627 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
632 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
633 struct hw_destination
* dest
= malloc(sizeof(*dest
));
635 if (part_type
== HW_PART_TYPE_NORMAL
) {
636 dest
->disk1
= *disks
;
639 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
641 } else if (part_type
== HW_PART_TYPE_RAID1
) {
642 dest
->disk1
= *disks
++;
643 dest
->disk2
= *disks
;
644 dest
->raid_level
= 1;
646 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
649 // Is this a RAID device?
650 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
652 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
656 // Set default filesystem
657 dest
->filesystem
= HW_FS_DEFAULT
;
662 unsigned long long hw_memory() {
665 int r
= sysinfo(&si
);
672 static int hw_zero_out_device(const char* path
, int bytes
) {
674 memset(block
, 0, sizeof(block
));
676 int blocks
= bytes
/ sizeof(block
);
678 int fd
= open(path
, O_WRONLY
);
682 unsigned int bytes_written
= 0;
683 while (blocks
-- > 0) {
684 bytes_written
+= write(fd
, block
, sizeof(block
));
690 return bytes_written
;
693 static int try_open(const char* path
) {
694 FILE* f
= fopen(path
, "r");
703 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
704 // Before we write a new partition table to the disk, we will erase
705 // the first couple of megabytes at the beginning of the device to
706 // get rid of all left other things like bootloaders and partition tables.
707 // This solves some problems when changing from MBR to GPT partitions or
708 // the other way around.
709 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
714 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
716 // Set partition type
717 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
718 asprintf(&cmd
, "%s mklabel msdos", cmd
);
719 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
720 asprintf(&cmd
, "%s mklabel gpt", cmd
);
722 unsigned long long part_start
= MB2BYTES(1);
724 if (*dest
->part_bootldr
) {
725 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
726 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
727 part_start
, part_start
+ dest
->size_bootldr
- 1);
729 part_start
+= dest
->size_bootldr
;
732 if (*dest
->part_boot
) {
733 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
734 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
735 part_start
, part_start
+ dest
->size_boot
- 1);
737 part_start
+= dest
->size_boot
;
740 if (*dest
->part_boot_efi
) {
741 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
742 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
743 part_start
, part_start
+ dest
->size_boot_efi
- 1);
745 part_start
+= dest
->size_boot_efi
;
748 if (*dest
->part_swap
) {
749 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
750 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
751 part_start
, part_start
+ dest
->size_swap
- 1);
753 part_start
+= dest
->size_swap
;
756 if (*dest
->part_root
) {
757 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
758 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
759 part_start
, part_start
+ dest
->size_root
- 1);
761 part_start
+= dest
->size_root
;
764 if (dest
->part_boot_idx
> 0)
765 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
767 if (dest
->part_boot_efi_idx
> 0)
768 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
770 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
771 if (*dest
->part_bootldr
) {
772 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
776 r
= mysystem(output
, cmd
);
778 // Wait until the system re-read the partition table
780 unsigned int counter
= 10;
782 while (counter
-- > 0) {
785 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
788 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
791 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
794 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
797 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
800 // All partitions do exist, exiting the loop.
811 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
812 char cmd
[STRING_SIZE
] = "\0";
816 if (fs
== HW_FS_SWAP
) {
817 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
820 } else if (fs
== HW_FS_EXT4
) {
821 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
824 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
825 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
828 } else if (fs
== HW_FS_XFS
) {
829 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
832 } else if (fs
== HW_FS_BTRFS
) {
833 r
= hw_create_btrfs_layout(path
, output
);
838 } else if (fs
== HW_FS_FAT32
) {
839 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
844 r
= mysystem(output
, cmd
);
849 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
853 if (*dest
->part_boot
) {
854 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
860 if (*dest
->part_boot_efi
) {
861 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
867 if (*dest
->part_swap
) {
868 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
874 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
881 int hw_create_btrfs_layout(const char* path
, const char* output
) {
882 const struct btrfs_subvolumes
* subvolume
= NULL
;
883 char cmd
[STRING_SIZE
];
884 char volume
[STRING_SIZE
];
887 r
= snprintf(cmd
, sizeof(cmd
), "/usr/bin/mkfs.btrfs -f %s", path
);
892 // Create the main BTRFS file system.
893 r
= mysystem(output
, cmd
);
900 // We need to mount the FS in order to create any subvolumes.
901 r
= hw_mount(path
, DESTINATION_MOUNT_PATH
, "btrfs", 0);
907 // Loop through the list of subvolumes to create.
908 for ( subvolume
= btrfs_subvolumes
; subvolume
->name
; subvolume
++ ) {
909 r
= snprintf(volume
, sizeof(volume
), "%s", subvolume
->name
);
911 // Abort if snprintf fails.
916 // Call function to create the subvolume
917 r
= hw_create_btrfs_subvolume(output
, volume
);
924 // Umount the main BTRFS after subvolume creation.
925 r
= hw_umount(DESTINATION_MOUNT_PATH
, 0);
934 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
935 char target
[STRING_SIZE
];
937 assert(*prefix
== '/');
939 const char* filesystem
;
940 switch (dest
->filesystem
) {
942 case HW_FS_EXT4_WO_JOURNAL
:
951 filesystem
= "btrfs";
963 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
968 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
969 r
= mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
972 hw_umount_filesystems(dest
, prefix
);
977 if (*dest
->part_boot
) {
978 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
980 hw_umount_filesystems(dest
, prefix
);
987 if (*dest
->part_boot_efi
) {
988 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
989 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
991 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
993 hw_umount_filesystems(dest
, prefix
);
1000 if (*dest
->part_swap
) {
1001 r
= swapon(dest
->part_swap
, 0);
1003 hw_umount_filesystems(dest
, prefix
);
1009 // bind-mount misc filesystems
1010 r
= hw_bind_mount("/dev", prefix
);
1014 r
= hw_bind_mount("/proc", prefix
);
1018 r
= hw_bind_mount("/sys", prefix
);
1022 r
= hw_bind_mount("/sys/firmware/efi/efivars", prefix
);
1023 if (r
&& errno
!= ENOENT
)
1029 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
1031 char target
[STRING_SIZE
];
1033 // Write all buffers to disk before umounting
1037 if (*dest
->part_boot_efi
) {
1038 r
= hw_umount(HW_PATH_BOOT_EFI
, prefix
);
1044 if (*dest
->part_boot
) {
1045 r
= hw_umount(HW_PATH_BOOT
, prefix
);
1051 if (*dest
->part_swap
) {
1052 swapoff(dest
->part_swap
);
1056 r
= hw_umount("/sys/firmware/efi/efivars", prefix
);
1060 r
= hw_umount("/sys", prefix
);
1064 r
= hw_umount("/proc", prefix
);
1068 r
= hw_umount("/dev", prefix
);
1073 r
= hw_umount(prefix
, NULL
);
1080 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
1081 char cmd
[STRING_SIZE
];
1083 hw_stop_all_raid_arrays(output
);
1084 hw_stop_all_raid_arrays(output
);
1087 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
1088 mysystem(output
, cmd
);
1092 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
1093 mysystem(output
, cmd
);
1099 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
1103 assert(dest
->is_raid
);
1105 // Stop all RAID arrays that might be around (again).
1106 // It seems that there is some sort of race-condition with udev re-enabling
1107 // the raid arrays and therefore locking the disks.
1108 r
= hw_destroy_raid_superblocks(dest
, output
);
1110 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1111 RAID_METADATA
, dest
->path
);
1113 switch (dest
->raid_level
) {
1115 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1123 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1125 // Clear all data at the beginning
1126 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1132 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1134 // Clear all data at the beginning
1135 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1140 r
= mysystem(output
, cmd
);
1143 // Wait a moment until the device has been properly brought up
1145 unsigned int counter
= 10;
1146 while (counter
-- > 0) {
1149 // If the raid device has not yet been properly brought up,
1150 // opening it will fail with the message: Device or resource busy
1151 // Hence we will wait a bit until it becomes usable.
1152 if (try_open(dest
->path
) == 0)
1160 int hw_stop_all_raid_arrays(const char* output
) {
1161 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1164 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1165 char cmd
[STRING_SIZE
];
1167 snprintf(cmd
, sizeof(cmd
), "/usr/bin/install-bootloader %s", dest
->path
);
1168 int r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1177 static char* hw_get_uuid(const char* dev
) {
1178 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1179 const char* buffer
= NULL
;
1186 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1189 uuid
= strdup(buffer
);
1191 blkid_free_probe(p
);
1196 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1198 int hw_write_fstab(struct hw_destination
* dest
) {
1199 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1206 if (*dest
->part_boot
) {
1207 uuid
= hw_get_uuid(dest
->part_boot
);
1210 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
1216 if (*dest
->part_boot_efi
) {
1217 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1220 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1227 if (*dest
->part_swap
) {
1228 uuid
= hw_get_uuid(dest
->part_swap
);
1231 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1237 uuid
= hw_get_uuid(dest
->part_root
);
1239 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1254 int hw_start_networking(const char* output
) {
1255 return mysystem(output
, "/usr/bin/start-networking.sh");
1258 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1259 char path
[STRING_SIZE
];
1261 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1262 int r
= access(path
, R_OK
);
1265 return strdup(path
);
1270 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1271 char command
[STRING_SIZE
];
1273 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s "
1274 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1275 backup_path
, destination
, destination
, destination
);
1276 int rc
= mysystem(output
, command
);
1284 int hw_mkdir(const char *dir
) {
1285 char tmp
[STRING_SIZE
];
1290 snprintf(tmp
, sizeof(tmp
),"%s",dir
);
1293 if (tmp
[len
- 1] == '/') {
1297 for (p
= tmp
+ 1; *p
; p
++) {
1301 // Create target if it does not exist
1302 if (access(tmp
, X_OK
) != 0) {
1303 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1314 // Create target if it does not exist
1315 if (access(tmp
, X_OK
) != 0) {
1316 r
= mkdir(tmp
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
1327 int hw_create_btrfs_subvolume(const char* output
, const char* subvolume
) {
1328 char command
[STRING_SIZE
];
1331 // Abort if the command could not be assigned.
1332 r
= snprintf(command
, sizeof(command
), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH
, subvolume
);
1337 // Create the subvolume
1338 r
= mysystem(output
, command
);