]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
1 /*#############################################################################
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2014 IPFire development team #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 #############################################################################*/
26 #include <blkid/blkid.h>
30 #include <linux/loop.h>
35 #include <sys/ioctl.h>
36 #include <sys/mount.h>
39 #include <sys/sysinfo.h>
40 #include <sys/utsname.h>
45 #include <libsmooth.h>
49 const char* other_filesystems
[] = {
56 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
57 char chroot_cmd
[STRING_SIZE
];
59 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
61 return mysystem(output
, chroot_cmd
);
64 struct hw
* hw_init() {
65 struct hw
* hw
= calloc(1, sizeof(*hw
));
69 hw
->udev
= udev_new();
71 fprintf(stderr
, "Could not create udev instance\n");
75 // What architecture are we running on?
76 struct utsname uname_data
;
77 int ret
= uname(&uname_data
);
79 snprintf(hw
->arch
, sizeof(hw
->arch
), "%s", uname_data
.machine
);
81 // Detect if we are running in EFI mode
82 ret
= access("/sys/firmware/efi", R_OK
);
84 hw
->efi_supported
= 1;
86 // Should we install in EFI mode?
87 if ((strcmp(hw
->arch
, "x86_64") == 0) || (strcmp(hw
->arch
, "aarch64") == 0))
93 void hw_free(struct hw
* hw
) {
100 static int strstartswith(const char* a
, const char* b
) {
101 return (strncmp(a
, b
, strlen(b
)) == 0);
104 static char loop_device
[STRING_SIZE
];
106 static int setup_loop_device(const char* source
, const char* device
) {
107 int file_fd
= open(source
, O_RDWR
);
112 if ((device_fd
= open(device
, O_RDWR
)) < 0)
115 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
127 if (device_fd
>= 0) {
128 ioctl(device_fd
, LOOP_CLR_FD
, 0);
135 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
136 const char* loop_device
= "/dev/loop0";
138 // Create target if it does not exist
139 if (access(target
, X_OK
) != 0)
140 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
145 if (S_ISREG(st
.st_mode
)) {
146 int r
= setup_loop_device(source
, loop_device
);
148 source
= loop_device
;
154 int r
= mount(source
, target
, fs
, flags
, NULL
);
157 fprintf(stderr
, "Error mounting %s to %s (fs = %s, flags = %d): %s\n",
158 source
, target
, fs
, flags
, strerror(r
));
164 int hw_umount(const char* target
) {
165 int r
= umount2(target
, 0);
167 if (r
&& errno
== EBUSY
) {
168 // Give it a moment to settle
171 r
= umount2(target
, MNT_FORCE
);
177 static int hw_test_source_medium(const char* path
) {
178 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
180 // If the source could not be mounted we
185 // Check if the test file exists.
186 ret
= access(SOURCE_TEST_FILE
, R_OK
);
188 // Umount the test device.
189 hw_umount(SOURCE_MOUNT_PATH
);
194 char* hw_find_source_medium(struct hw
* hw
) {
197 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
199 udev_enumerate_add_match_subsystem(enumerate
, "block");
200 udev_enumerate_scan_devices(enumerate
);
202 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
204 struct udev_list_entry
* dev_list_entry
;
205 udev_list_entry_foreach(dev_list_entry
, devices
) {
206 const char* path
= udev_list_entry_get_name(dev_list_entry
);
207 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
209 const char* dev_path
= udev_device_get_devnode(dev
);
211 // Skip everything what we cannot work with
212 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
213 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
216 if (hw_test_source_medium(dev_path
) == 0) {
217 ret
= strdup(dev_path
);
220 udev_device_unref(dev
);
222 // If a suitable device was found the search will end.
227 udev_enumerate_unref(enumerate
);
232 static struct hw_disk
** hw_create_disks() {
233 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
238 static unsigned long long hw_block_device_get_size(const char* dev
) {
239 int fd
= open(dev
, O_RDONLY
);
243 unsigned long long size
= blkid_get_dev_size(fd
);
249 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
250 struct hw_disk
** ret
= hw_create_disks();
251 struct hw_disk
** disks
= ret
;
253 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
255 udev_enumerate_add_match_subsystem(enumerate
, "block");
256 udev_enumerate_scan_devices(enumerate
);
258 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
260 struct udev_list_entry
* dev_list_entry
;
261 unsigned int i
= HW_MAX_DISKS
;
262 udev_list_entry_foreach(dev_list_entry
, devices
) {
263 const char* path
= udev_list_entry_get_name(dev_list_entry
);
264 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
266 const char* dev_path
= udev_device_get_devnode(dev
);
268 // Skip everything what we cannot work with
269 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
270 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
271 strstartswith(dev_path
, "/dev/md")) {
272 udev_device_unref(dev
);
276 // Skip sourcedrive if we need to
277 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
278 udev_device_unref(dev
);
282 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
283 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
284 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
285 udev_device_unref(dev
);
289 // Skip devices with a size of zero
290 unsigned long long size
= hw_block_device_get_size(dev_path
);
292 udev_device_unref(dev
);
296 struct hw_disk
* disk
= malloc(sizeof(*disk
));
302 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
303 const char* p
= disk
->path
+ 5;
308 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
310 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
312 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
315 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
317 *disk
->vendor
= '\0';
320 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
322 model
= udev_device_get_sysattr_value(dev
, "model");
324 model
= udev_device_get_sysattr_value(dev
, "product");
327 strncpy(disk
->model
, model
, sizeof(disk
->model
));
331 // Format description
332 char size_str
[STRING_SIZE
];
333 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
335 if (*disk
->vendor
&& *disk
->model
) {
336 snprintf(disk
->description
, sizeof(disk
->description
),
337 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
339 } else if (*disk
->vendor
|| *disk
->model
) {
340 snprintf(disk
->description
, sizeof(disk
->description
),
341 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
344 snprintf(disk
->description
, sizeof(disk
->description
),
345 "%s - %s", size_str
, p
);
348 // Cut off the description string after 40 characters
349 disk
->description
[41] = '\0';
356 udev_device_unref(dev
);
359 udev_enumerate_unref(enumerate
);
366 void hw_free_disks(struct hw_disk
** disks
) {
367 struct hw_disk
** disk
= disks
;
369 while (*disk
!= NULL
) {
370 if (--(*disk
)->ref
== 0)
379 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
380 unsigned int ret
= 0;
388 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
389 struct hw_disk
** ret
= hw_create_disks();
390 struct hw_disk
** selected_disks
= ret
;
392 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
394 for (unsigned int i
= 0; i
< num_disks
; i
++) {
395 if (!selection
|| selection
[i
]) {
396 struct hw_disk
*selected_disk
= disks
[i
];
397 selected_disk
->ref
++;
399 *selected_disks
++ = selected_disk
;
404 *selected_disks
= NULL
;
409 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
410 struct hw_disk
** ret
= hw_create_disks();
411 struct hw_disk
** selected_disks
= ret
;
413 unsigned int num_disks
= hw_count_disks(disks
);
414 assert(num_disks
> 0);
416 for (unsigned int i
= 0; i
< num_disks
; i
++) {
417 struct hw_disk
*disk
= disks
[i
];
420 *selected_disks
++ = disk
;
425 *selected_disks
= NULL
;
430 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
431 unsigned long long memory
= hw_memory();
433 unsigned long long swap_size
= memory
/ 4;
435 // Min. swap size is 128MB
436 if (swap_size
< MB2BYTES(128))
437 swap_size
= MB2BYTES(128);
439 // Cap swap size to 1GB
440 else if (swap_size
> MB2BYTES(1024))
441 swap_size
= MB2BYTES(1024);
446 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
447 return MB2BYTES(128);
450 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
451 // All RAID devices have the p suffix.
455 // Devices with a number at the end have the p suffix, too.
456 // e.g. mmcblk0, cciss0
457 unsigned int last_char
= strlen(dest
->path
) - 1;
458 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
464 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
468 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
469 hw_device_has_p_suffix(dest
) ? "p" : "");
470 dest
->part_boot_idx
= 0;
472 // Determine the size of the target block device
474 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
475 dest
->disk2
->size
: dest
->disk1
->size
;
477 // The RAID will install some metadata at the end of the disk
478 // and we will save up some space for that.
479 dest
->size
-= MB2BYTES(2);
481 dest
->size
= dest
->disk1
->size
;
484 // As we add some extra space before the beginning of the first
485 // partition, we need to substract that here.
486 dest
->size
-= MB2BYTES(1);
488 // Add some more space for partition tables, etc.
489 dest
->size
-= MB2BYTES(1);
491 // The disk has to have at least 2GB
492 if (dest
->size
<= MB2BYTES(2048))
495 // Determine partition table
496 dest
->part_table
= HW_PART_TABLE_MSDOS
;
498 // Disks over 2TB need to use GPT
499 if (dest
->size
>= MB2BYTES(2047 * 1024))
500 dest
->part_table
= HW_PART_TABLE_GPT
;
502 // We also use GPT on raid disks by default
503 else if (dest
->is_raid
)
504 dest
->part_table
= HW_PART_TABLE_GPT
;
506 // When using GPT, GRUB2 needs a little bit of space to put
508 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
509 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
510 "%s%d", path
, part_idx
);
512 dest
->size_bootldr
= MB2BYTES(4);
514 dest
->part_boot_idx
= part_idx
++;
516 *dest
->part_bootldr
= '\0';
517 dest
->size_bootldr
= 0;
520 dest
->size_boot
= hw_boot_size(dest
);
522 // Create an EFI partition when running in EFI mode
524 dest
->size_boot_efi
= MB2BYTES(32);
526 dest
->size_boot_efi
= 0;
528 // Determine the size of the data partition.
529 unsigned long long space_left
= dest
->size
- \
530 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
532 // If we have less than 2GB left, we disable swap
533 if (space_left
<= MB2BYTES(2048))
536 // Should we use swap?
540 dest
->size_swap
= hw_swap_size(dest
);
543 space_left
-= dest
->size_swap
;
545 // Root is getting what ever is left
546 dest
->size_root
= space_left
;
548 // Set partition names
549 if (dest
->size_boot
> 0) {
550 if (dest
->part_boot_idx
== 0)
551 dest
->part_boot_idx
= part_idx
;
553 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
555 *dest
->part_boot
= '\0';
557 if (dest
->size_boot_efi
> 0) {
558 dest
->part_boot_efi_idx
= part_idx
;
560 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
561 "%s%d", path
, part_idx
++);
563 *dest
->part_boot_efi
= '\0';
565 if (dest
->size_swap
> 0)
566 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
568 *dest
->part_swap
= '\0';
570 // There is always a root partition
571 if (dest
->part_boot_idx
== 0)
572 dest
->part_boot_idx
= part_idx
;
574 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
579 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
580 struct hw_destination
* dest
= malloc(sizeof(*dest
));
582 if (part_type
== HW_PART_TYPE_NORMAL
) {
583 dest
->disk1
= *disks
;
586 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
588 } else if (part_type
== HW_PART_TYPE_RAID1
) {
589 dest
->disk1
= *disks
++;
590 dest
->disk2
= *disks
;
591 dest
->raid_level
= 1;
593 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
596 // Is this a RAID device?
597 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
599 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
603 // Set default filesystem
604 dest
->filesystem
= HW_FS_DEFAULT
;
609 unsigned long long hw_memory() {
612 int r
= sysinfo(&si
);
619 static int hw_zero_out_device(const char* path
, int bytes
) {
621 memset(block
, 0, sizeof(block
));
623 int blocks
= bytes
/ sizeof(block
);
625 int fd
= open(path
, O_WRONLY
);
629 unsigned int bytes_written
= 0;
630 while (blocks
-- > 0) {
631 bytes_written
+= write(fd
, block
, sizeof(block
));
637 return bytes_written
;
640 static int try_open(const char* path
) {
641 FILE* f
= fopen(path
, "r");
650 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
651 // Before we write a new partition table to the disk, we will erase
652 // the first couple of megabytes at the beginning of the device to
653 // get rid of all left other things like bootloaders and partition tables.
654 // This solves some problems when changing from MBR to GPT partitions or
655 // the other way around.
656 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
661 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
663 // Set partition type
664 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
665 asprintf(&cmd
, "%s mklabel msdos", cmd
);
666 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
667 asprintf(&cmd
, "%s mklabel gpt", cmd
);
669 unsigned long long part_start
= MB2BYTES(1);
671 if (*dest
->part_bootldr
) {
672 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
673 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
674 part_start
, part_start
+ dest
->size_bootldr
- 1);
676 part_start
+= dest
->size_bootldr
;
679 if (*dest
->part_boot
) {
680 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
681 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
682 part_start
, part_start
+ dest
->size_boot
- 1);
684 part_start
+= dest
->size_boot
;
687 if (*dest
->part_boot_efi
) {
688 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
689 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
690 part_start
, part_start
+ dest
->size_boot_efi
- 1);
692 part_start
+= dest
->size_boot_efi
;
695 if (*dest
->part_swap
) {
696 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
697 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
698 part_start
, part_start
+ dest
->size_swap
- 1);
700 part_start
+= dest
->size_swap
;
703 if (*dest
->part_root
) {
704 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
705 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
706 part_start
, part_start
+ dest
->size_root
- 1);
708 part_start
+= dest
->size_root
;
711 if (dest
->part_boot_idx
> 0)
712 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
714 if (dest
->part_boot_efi_idx
> 0)
715 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
717 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
718 if (*dest
->part_bootldr
) {
719 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
721 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
724 r
= mysystem(output
, cmd
);
726 // Wait until the system re-read the partition table
728 unsigned int counter
= 10;
730 while (counter
-- > 0) {
733 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
736 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
739 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
742 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
745 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
748 // All partitions do exist, exiting the loop.
759 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
760 char cmd
[STRING_SIZE
] = "\0";
763 if (fs
== HW_FS_SWAP
) {
764 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
766 } else if (fs
== HW_FS_REISERFS
) {
767 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
770 } else if (fs
== HW_FS_EXT4
) {
771 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
774 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
775 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
778 } else if (fs
== HW_FS_XFS
) {
779 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
782 } else if (fs
== HW_FS_FAT32
) {
783 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
788 int r
= mysystem(output
, cmd
);
793 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
797 if (*dest
->part_boot
) {
798 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
804 if (*dest
->part_boot_efi
) {
805 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
811 if (*dest
->part_swap
) {
812 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
818 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
825 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
826 char target
[STRING_SIZE
];
828 assert(*prefix
== '/');
830 const char* filesystem
;
831 switch (dest
->filesystem
) {
833 filesystem
= "reiserfs";
837 case HW_FS_EXT4_WO_JOURNAL
:
854 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
859 if (*dest
->part_boot
) {
860 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
861 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
863 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
865 hw_umount_filesystems(dest
, prefix
);
872 if (*dest
->part_boot_efi
) {
873 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
874 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
876 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
878 hw_umount_filesystems(dest
, prefix
);
885 if (*dest
->part_swap
) {
886 r
= swapon(dest
->part_swap
, 0);
888 hw_umount_filesystems(dest
, prefix
);
894 // bind-mount misc filesystems
895 char** otherfs
= other_filesystems
;
897 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
899 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
900 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
902 hw_umount_filesystems(dest
, prefix
);
913 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
915 char target
[STRING_SIZE
];
917 // Write all buffers to disk before umounting
921 if (*dest
->part_boot_efi
) {
922 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
923 r
= hw_umount(target
);
929 if (*dest
->part_boot
) {
930 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
931 r
= hw_umount(target
);
937 if (*dest
->part_swap
) {
938 swapoff(dest
->part_swap
);
942 char** otherfs
= other_filesystems
;
944 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
945 r
= hw_umount(target
);
951 r
= hw_umount(prefix
);
958 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
959 char cmd
[STRING_SIZE
];
961 hw_stop_all_raid_arrays(output
);
962 hw_stop_all_raid_arrays(output
);
965 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
966 mysystem(output
, cmd
);
970 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
971 mysystem(output
, cmd
);
977 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
981 assert(dest
->is_raid
);
983 // Stop all RAID arrays that might be around (again).
984 // It seems that there is some sort of race-condition with udev re-enabling
985 // the raid arrays and therefore locking the disks.
986 r
= hw_destroy_raid_superblocks(dest
, output
);
988 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
989 RAID_METADATA
, dest
->path
);
991 switch (dest
->raid_level
) {
993 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1001 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1003 // Clear all data at the beginning
1004 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1010 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1012 // Clear all data at the beginning
1013 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1018 r
= mysystem(output
, cmd
);
1021 // Wait a moment until the device has been properly brought up
1023 unsigned int counter
= 10;
1024 while (counter
-- > 0) {
1027 // If the raid device has not yet been properly brought up,
1028 // opening it will fail with the message: Device or resource busy
1029 // Hence we will wait a bit until it becomes usable.
1030 if (try_open(dest
->path
) == 0)
1038 int hw_stop_all_raid_arrays(const char* output
) {
1039 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1042 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1043 char cmd
[STRING_SIZE
];
1046 char cmd_grub
[STRING_SIZE
];
1047 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --target=i386-pc"
1048 " --no-floppy --recheck");
1050 if (dest
->is_raid
) {
1051 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
1052 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1056 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
1057 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1061 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
1062 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1067 // Install GRUB in EFI mode
1069 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-install"
1070 " --target=%s-efi --efi-directory=%s %s", hw
->arch
, HW_PATH_BOOT_EFI
,
1071 (hw
->efi_supported
) ? "" : "--no-nvram");
1073 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1078 // Generate configuration file
1079 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
1080 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1089 static char* hw_get_uuid(const char* dev
) {
1090 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1091 const char* buffer
= NULL
;
1098 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1101 uuid
= strdup(buffer
);
1103 blkid_free_probe(p
);
1108 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1110 int hw_write_fstab(struct hw_destination
* dest
) {
1111 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1118 if (*dest
->part_boot
) {
1119 uuid
= hw_get_uuid(dest
->part_boot
);
1122 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1128 if (*dest
->part_boot_efi
) {
1129 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1132 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1139 if (*dest
->part_swap
) {
1140 uuid
= hw_get_uuid(dest
->part_swap
);
1143 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1149 uuid
= hw_get_uuid(dest
->part_root
);
1151 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1166 int hw_start_networking(const char* output
) {
1167 return mysystem(output
, "/usr/bin/start-networking.sh");
1170 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1171 char path
[STRING_SIZE
];
1173 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1174 int r
= access(path
, R_OK
);
1177 return strdup(path
);
1182 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1183 char command
[STRING_SIZE
];
1185 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1186 int rc
= mysystem(output
, command
);