]>
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?
92 void hw_free(struct hw
* hw
) {
99 static int strstartswith(const char* a
, const char* b
) {
100 return (strncmp(a
, b
, strlen(b
)) == 0);
103 static char loop_device
[STRING_SIZE
];
105 static int setup_loop_device(const char* source
, const char* device
) {
106 int file_fd
= open(source
, O_RDWR
);
111 if ((device_fd
= open(device
, O_RDWR
)) < 0)
114 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
126 if (device_fd
>= 0) {
127 ioctl(device_fd
, LOOP_CLR_FD
, 0);
134 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
135 const char* loop_device
= "/dev/loop0";
137 // Create target if it does not exist
138 if (access(target
, X_OK
) != 0)
139 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
144 if (S_ISREG(st
.st_mode
)) {
145 int r
= setup_loop_device(source
, loop_device
);
147 source
= loop_device
;
153 int r
= mount(source
, target
, fs
, flags
, NULL
);
156 fprintf(stderr
, "Error mounting %s to %s (fs = %s, flags = %d): %s\n",
157 source
, target
, fs
, flags
, strerror(r
));
163 int hw_umount(const char* target
) {
164 int r
= umount2(target
, 0);
166 if (r
&& errno
== EBUSY
) {
167 // Give it a moment to settle
170 r
= umount2(target
, MNT_FORCE
);
176 static int hw_test_source_medium(const char* path
) {
177 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
179 // If the source could not be mounted we
184 // Check if the test file exists.
185 ret
= access(SOURCE_TEST_FILE
, R_OK
);
187 // Umount the test device.
188 hw_umount(SOURCE_MOUNT_PATH
);
193 char* hw_find_source_medium(struct hw
* hw
) {
196 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
198 udev_enumerate_add_match_subsystem(enumerate
, "block");
199 udev_enumerate_scan_devices(enumerate
);
201 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
203 struct udev_list_entry
* dev_list_entry
;
204 udev_list_entry_foreach(dev_list_entry
, devices
) {
205 const char* path
= udev_list_entry_get_name(dev_list_entry
);
206 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
208 const char* dev_path
= udev_device_get_devnode(dev
);
210 // Skip everything what we cannot work with
211 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
212 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
215 if (hw_test_source_medium(dev_path
) == 0) {
216 ret
= strdup(dev_path
);
219 udev_device_unref(dev
);
221 // If a suitable device was found the search will end.
226 udev_enumerate_unref(enumerate
);
231 static struct hw_disk
** hw_create_disks() {
232 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
237 static unsigned long long hw_block_device_get_size(const char* dev
) {
238 int fd
= open(dev
, O_RDONLY
);
242 unsigned long long size
= blkid_get_dev_size(fd
);
248 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
249 struct hw_disk
** ret
= hw_create_disks();
250 struct hw_disk
** disks
= ret
;
252 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
254 udev_enumerate_add_match_subsystem(enumerate
, "block");
255 udev_enumerate_scan_devices(enumerate
);
257 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
259 struct udev_list_entry
* dev_list_entry
;
260 unsigned int i
= HW_MAX_DISKS
;
261 udev_list_entry_foreach(dev_list_entry
, devices
) {
262 const char* path
= udev_list_entry_get_name(dev_list_entry
);
263 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
265 const char* dev_path
= udev_device_get_devnode(dev
);
267 // Skip everything what we cannot work with
268 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
269 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
270 strstartswith(dev_path
, "/dev/md")) {
271 udev_device_unref(dev
);
275 // Skip sourcedrive if we need to
276 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
277 udev_device_unref(dev
);
281 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
282 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
283 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
284 udev_device_unref(dev
);
288 // Skip devices with a size of zero
289 unsigned long long size
= hw_block_device_get_size(dev_path
);
291 udev_device_unref(dev
);
295 struct hw_disk
* disk
= malloc(sizeof(*disk
));
301 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
302 const char* p
= disk
->path
+ 5;
307 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
309 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
311 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
314 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
316 *disk
->vendor
= '\0';
319 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
321 model
= udev_device_get_sysattr_value(dev
, "model");
323 model
= udev_device_get_sysattr_value(dev
, "product");
326 strncpy(disk
->model
, model
, sizeof(disk
->model
));
330 // Format description
331 char size_str
[STRING_SIZE
];
332 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
334 if (*disk
->vendor
&& *disk
->model
) {
335 snprintf(disk
->description
, sizeof(disk
->description
),
336 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
338 } else if (*disk
->vendor
|| *disk
->model
) {
339 snprintf(disk
->description
, sizeof(disk
->description
),
340 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
343 snprintf(disk
->description
, sizeof(disk
->description
),
344 "%s - %s", size_str
, p
);
347 // Cut off the description string after 40 characters
348 disk
->description
[41] = '\0';
355 udev_device_unref(dev
);
358 udev_enumerate_unref(enumerate
);
365 void hw_free_disks(struct hw_disk
** disks
) {
366 struct hw_disk
** disk
= disks
;
368 while (*disk
!= NULL
) {
369 if (--(*disk
)->ref
== 0)
378 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
379 unsigned int ret
= 0;
387 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
388 struct hw_disk
** ret
= hw_create_disks();
389 struct hw_disk
** selected_disks
= ret
;
391 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
393 for (unsigned int i
= 0; i
< num_disks
; i
++) {
394 if (!selection
|| selection
[i
]) {
395 struct hw_disk
*selected_disk
= disks
[i
];
396 selected_disk
->ref
++;
398 *selected_disks
++ = selected_disk
;
403 *selected_disks
= NULL
;
408 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
409 struct hw_disk
** ret
= hw_create_disks();
410 struct hw_disk
** selected_disks
= ret
;
412 unsigned int num_disks
= hw_count_disks(disks
);
413 assert(num_disks
> 0);
415 for (unsigned int i
= 0; i
< num_disks
; i
++) {
416 struct hw_disk
*disk
= disks
[i
];
419 *selected_disks
++ = disk
;
424 *selected_disks
= NULL
;
429 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
430 unsigned long long memory
= hw_memory();
432 unsigned long long swap_size
= memory
/ 4;
434 // Min. swap size is 128MB
435 if (swap_size
< MB2BYTES(128))
436 swap_size
= MB2BYTES(128);
438 // Cap swap size to 1GB
439 else if (swap_size
> MB2BYTES(1024))
440 swap_size
= MB2BYTES(1024);
445 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
446 return MB2BYTES(128);
449 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
450 // All RAID devices have the p suffix.
454 // Devices with a number at the end have the p suffix, too.
455 // e.g. mmcblk0, cciss0
456 unsigned int last_char
= strlen(dest
->path
) - 1;
457 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
463 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
467 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
468 hw_device_has_p_suffix(dest
) ? "p" : "");
469 dest
->part_boot_idx
= 0;
471 // Determine the size of the target block device
473 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
474 dest
->disk2
->size
: dest
->disk1
->size
;
476 // The RAID will install some metadata at the end of the disk
477 // and we will save up some space for that.
478 dest
->size
-= MB2BYTES(2);
480 dest
->size
= dest
->disk1
->size
;
483 // As we add some extra space before the beginning of the first
484 // partition, we need to substract that here.
485 dest
->size
-= MB2BYTES(1);
487 // Add some more space for partition tables, etc.
488 dest
->size
-= MB2BYTES(1);
490 // The disk has to have at least 2GB
491 if (dest
->size
<= MB2BYTES(2048))
494 // Determine partition table
495 dest
->part_table
= HW_PART_TABLE_MSDOS
;
497 // Disks over 2TB need to use GPT
498 if (dest
->size
>= MB2BYTES(2047 * 1024))
499 dest
->part_table
= HW_PART_TABLE_GPT
;
501 // We also use GPT on raid disks by default
502 else if (dest
->is_raid
)
503 dest
->part_table
= HW_PART_TABLE_GPT
;
505 // When using GPT, GRUB2 needs a little bit of space to put
507 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
508 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
509 "%s%d", path
, part_idx
);
511 dest
->size_bootldr
= MB2BYTES(4);
513 dest
->part_boot_idx
= part_idx
++;
515 *dest
->part_bootldr
= '\0';
516 dest
->size_bootldr
= 0;
519 dest
->size_boot
= hw_boot_size(dest
);
521 // Create an EFI partition when running in EFI mode
523 dest
->size_boot_efi
= MB2BYTES(32);
525 dest
->size_boot_efi
= 0;
527 // Determine the size of the data partition.
528 unsigned long long space_left
= dest
->size
- \
529 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
531 // If we have less than 2GB left, we disable swap
532 if (space_left
<= MB2BYTES(2048))
535 // Should we use swap?
539 dest
->size_swap
= hw_swap_size(dest
);
542 space_left
-= dest
->size_swap
;
544 // Root is getting what ever is left
545 dest
->size_root
= space_left
;
547 // Set partition names
548 if (dest
->size_boot
> 0) {
549 if (dest
->part_boot_idx
== 0)
550 dest
->part_boot_idx
= part_idx
;
552 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
554 *dest
->part_boot
= '\0';
556 if (dest
->size_boot_efi
> 0) {
557 dest
->part_boot_efi_idx
= part_idx
;
559 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
560 "%s%d", path
, part_idx
++);
562 *dest
->part_boot_efi
= '\0';
564 if (dest
->size_swap
> 0)
565 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
567 *dest
->part_swap
= '\0';
569 // There is always a root partition
570 if (dest
->part_boot_idx
== 0)
571 dest
->part_boot_idx
= part_idx
;
573 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
578 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
579 struct hw_destination
* dest
= malloc(sizeof(*dest
));
581 if (part_type
== HW_PART_TYPE_NORMAL
) {
582 dest
->disk1
= *disks
;
585 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
587 } else if (part_type
== HW_PART_TYPE_RAID1
) {
588 dest
->disk1
= *disks
++;
589 dest
->disk2
= *disks
;
590 dest
->raid_level
= 1;
592 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
595 // Is this a RAID device?
596 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
598 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
602 // Set default filesystem
603 dest
->filesystem
= HW_FS_DEFAULT
;
608 unsigned long long hw_memory() {
611 int r
= sysinfo(&si
);
618 static int hw_zero_out_device(const char* path
, int bytes
) {
620 memset(block
, 0, sizeof(block
));
622 int blocks
= bytes
/ sizeof(block
);
624 int fd
= open(path
, O_WRONLY
);
628 unsigned int bytes_written
= 0;
629 while (blocks
-- > 0) {
630 bytes_written
+= write(fd
, block
, sizeof(block
));
636 return bytes_written
;
639 static int try_open(const char* path
) {
640 FILE* f
= fopen(path
, "r");
649 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
650 // Before we write a new partition table to the disk, we will erase
651 // the first couple of megabytes at the beginning of the device to
652 // get rid of all left other things like bootloaders and partition tables.
653 // This solves some problems when changing from MBR to GPT partitions or
654 // the other way around.
655 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
660 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
662 // Set partition type
663 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
664 asprintf(&cmd
, "%s mklabel msdos", cmd
);
665 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
666 asprintf(&cmd
, "%s mklabel gpt", cmd
);
668 unsigned long long part_start
= MB2BYTES(1);
670 if (*dest
->part_bootldr
) {
671 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
672 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
673 part_start
, part_start
+ dest
->size_bootldr
- 1);
675 part_start
+= dest
->size_bootldr
;
678 if (*dest
->part_boot
) {
679 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
680 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
681 part_start
, part_start
+ dest
->size_boot
- 1);
683 part_start
+= dest
->size_boot
;
686 if (*dest
->part_boot_efi
) {
687 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
688 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
689 part_start
, part_start
+ dest
->size_boot_efi
- 1);
691 part_start
+= dest
->size_boot_efi
;
694 if (*dest
->part_swap
) {
695 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
696 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
697 part_start
, part_start
+ dest
->size_swap
- 1);
699 part_start
+= dest
->size_swap
;
702 if (*dest
->part_root
) {
703 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
704 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
705 part_start
, part_start
+ dest
->size_root
- 1);
707 part_start
+= dest
->size_root
;
710 if (dest
->part_boot_idx
> 0)
711 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
713 if (dest
->part_boot_efi_idx
> 0)
714 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
716 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
717 if (*dest
->part_bootldr
) {
718 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
720 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
723 r
= mysystem(output
, cmd
);
725 // Wait until the system re-read the partition table
727 unsigned int counter
= 10;
729 while (counter
-- > 0) {
732 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
735 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
738 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
741 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
744 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
747 // All partitions do exist, exiting the loop.
758 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
759 char cmd
[STRING_SIZE
] = "\0";
762 if (fs
== HW_FS_SWAP
) {
763 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
765 } else if (fs
== HW_FS_REISERFS
) {
766 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
769 } else if (fs
== HW_FS_EXT4
) {
770 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
773 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
774 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
777 } else if (fs
== HW_FS_XFS
) {
778 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
781 } else if (fs
== HW_FS_FAT32
) {
782 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
787 int r
= mysystem(output
, cmd
);
792 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
796 if (*dest
->part_boot
) {
797 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
803 if (*dest
->part_boot_efi
) {
804 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
810 if (*dest
->part_swap
) {
811 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
817 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
824 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
825 char target
[STRING_SIZE
];
827 assert(*prefix
== '/');
829 const char* filesystem
;
830 switch (dest
->filesystem
) {
832 filesystem
= "reiserfs";
836 case HW_FS_EXT4_WO_JOURNAL
:
853 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
858 if (*dest
->part_boot
) {
859 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
860 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
862 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
864 hw_umount_filesystems(dest
, prefix
);
871 if (*dest
->part_boot_efi
) {
872 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
873 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
875 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
877 hw_umount_filesystems(dest
, prefix
);
884 if (*dest
->part_swap
) {
885 r
= swapon(dest
->part_swap
, 0);
887 hw_umount_filesystems(dest
, prefix
);
893 // bind-mount misc filesystems
894 char** otherfs
= other_filesystems
;
896 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
898 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
899 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
901 hw_umount_filesystems(dest
, prefix
);
912 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
914 char target
[STRING_SIZE
];
916 // Write all buffers to disk before umounting
920 if (*dest
->part_boot_efi
) {
921 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
922 r
= hw_umount(target
);
928 if (*dest
->part_boot
) {
929 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
930 r
= hw_umount(target
);
936 if (*dest
->part_swap
) {
937 swapoff(dest
->part_swap
);
941 char** otherfs
= other_filesystems
;
943 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
944 r
= hw_umount(target
);
950 r
= hw_umount(prefix
);
957 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
958 char cmd
[STRING_SIZE
];
960 hw_stop_all_raid_arrays(output
);
961 hw_stop_all_raid_arrays(output
);
964 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
965 mysystem(output
, cmd
);
969 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
970 mysystem(output
, cmd
);
976 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
980 assert(dest
->is_raid
);
982 // Stop all RAID arrays that might be around (again).
983 // It seems that there is some sort of race-condition with udev re-enabling
984 // the raid arrays and therefore locking the disks.
985 r
= hw_destroy_raid_superblocks(dest
, output
);
987 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
988 RAID_METADATA
, dest
->path
);
990 switch (dest
->raid_level
) {
992 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
1000 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
1002 // Clear all data at the beginning
1003 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1009 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1011 // Clear all data at the beginning
1012 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1017 r
= mysystem(output
, cmd
);
1020 // Wait a moment until the device has been properly brought up
1022 unsigned int counter
= 10;
1023 while (counter
-- > 0) {
1026 // If the raid device has not yet been properly brought up,
1027 // opening it will fail with the message: Device or resource busy
1028 // Hence we will wait a bit until it becomes usable.
1029 if (try_open(dest
->path
) == 0)
1037 int hw_stop_all_raid_arrays(const char* output
) {
1038 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1041 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1042 char cmd
[STRING_SIZE
];
1045 char cmd_grub
[STRING_SIZE
];
1046 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --target=i386-pc"
1047 " --no-floppy --recheck");
1049 if (dest
->is_raid
) {
1050 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
1051 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1055 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
1056 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1060 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
1061 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1066 // Install GRUB in EFI mode
1068 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-install"
1069 " --target=%s-efi --efi-directory=%s %s", hw
->arch
, HW_PATH_BOOT_EFI
,
1070 (hw
->efi_supported
) ? "" : "--no-nvram");
1072 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1077 // Generate configuration file
1078 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
1079 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1088 static char* hw_get_uuid(const char* dev
) {
1089 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1090 const char* buffer
= NULL
;
1097 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1100 uuid
= strdup(buffer
);
1102 blkid_free_probe(p
);
1107 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1109 int hw_write_fstab(struct hw_destination
* dest
) {
1110 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1117 if (*dest
->part_boot
) {
1118 uuid
= hw_get_uuid(dest
->part_boot
);
1121 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1127 if (*dest
->part_boot_efi
) {
1128 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1131 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1138 if (*dest
->part_swap
) {
1139 uuid
= hw_get_uuid(dest
->part_swap
);
1142 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1148 uuid
= hw_get_uuid(dest
->part_root
);
1150 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1165 int hw_start_networking(const char* output
) {
1166 return mysystem(output
, "/usr/bin/start-networking.sh");
1169 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1170 char path
[STRING_SIZE
];
1172 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1173 int r
= access(path
, R_OK
);
1176 return strdup(path
);
1181 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1182 char command
[STRING_SIZE
];
1184 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1185 int rc
= mysystem(output
, command
);