]>
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
);
89 void hw_free(struct hw
* hw
) {
96 static int strstartswith(const char* a
, const char* b
) {
97 return (strncmp(a
, b
, strlen(b
)) == 0);
100 static char loop_device
[STRING_SIZE
];
102 static int setup_loop_device(const char* source
, const char* device
) {
103 int file_fd
= open(source
, O_RDWR
);
108 if ((device_fd
= open(device
, O_RDWR
)) < 0)
111 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
123 if (device_fd
>= 0) {
124 ioctl(device_fd
, LOOP_CLR_FD
, 0);
131 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
132 const char* loop_device
= "/dev/loop0";
134 // Create target if it does not exist
135 if (access(target
, X_OK
) != 0)
136 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
141 if (S_ISREG(st
.st_mode
)) {
142 int r
= setup_loop_device(source
, loop_device
);
144 source
= loop_device
;
150 int r
= mount(source
, target
, fs
, flags
, NULL
);
153 fprintf(stderr
, "Error mounting %s to %s (fs = %s, flags = %d): %s\n",
154 source
, target
, fs
, flags
, strerror(r
));
160 int hw_umount(const char* target
) {
161 int r
= umount2(target
, 0);
163 if (r
&& errno
== EBUSY
) {
164 // Give it a moment to settle
167 r
= umount2(target
, MNT_FORCE
);
173 static int hw_test_source_medium(const char* path
) {
174 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
176 // If the source could not be mounted we
181 // Check if the test file exists.
182 ret
= access(SOURCE_TEST_FILE
, R_OK
);
184 // Umount the test device.
185 hw_umount(SOURCE_MOUNT_PATH
);
190 char* hw_find_source_medium(struct hw
* hw
) {
193 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
195 udev_enumerate_add_match_subsystem(enumerate
, "block");
196 udev_enumerate_scan_devices(enumerate
);
198 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
200 struct udev_list_entry
* dev_list_entry
;
201 udev_list_entry_foreach(dev_list_entry
, devices
) {
202 const char* path
= udev_list_entry_get_name(dev_list_entry
);
203 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
205 const char* dev_path
= udev_device_get_devnode(dev
);
207 // Skip everything what we cannot work with
208 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
209 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
212 if (hw_test_source_medium(dev_path
) == 0) {
213 ret
= strdup(dev_path
);
216 udev_device_unref(dev
);
218 // If a suitable device was found the search will end.
223 udev_enumerate_unref(enumerate
);
228 static struct hw_disk
** hw_create_disks() {
229 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
234 static unsigned long long hw_block_device_get_size(const char* dev
) {
235 int fd
= open(dev
, O_RDONLY
);
239 unsigned long long size
= blkid_get_dev_size(fd
);
245 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
246 struct hw_disk
** ret
= hw_create_disks();
247 struct hw_disk
** disks
= ret
;
249 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
251 udev_enumerate_add_match_subsystem(enumerate
, "block");
252 udev_enumerate_scan_devices(enumerate
);
254 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
256 struct udev_list_entry
* dev_list_entry
;
257 unsigned int i
= HW_MAX_DISKS
;
258 udev_list_entry_foreach(dev_list_entry
, devices
) {
259 const char* path
= udev_list_entry_get_name(dev_list_entry
);
260 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
262 const char* dev_path
= udev_device_get_devnode(dev
);
264 // Skip everything what we cannot work with
265 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
266 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
267 strstartswith(dev_path
, "/dev/md")) {
268 udev_device_unref(dev
);
272 // Skip sourcedrive if we need to
273 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
274 udev_device_unref(dev
);
278 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
279 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
280 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
281 udev_device_unref(dev
);
285 // Skip devices with a size of zero
286 unsigned long long size
= hw_block_device_get_size(dev_path
);
288 udev_device_unref(dev
);
292 struct hw_disk
* disk
= malloc(sizeof(*disk
));
298 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
299 const char* p
= disk
->path
+ 5;
304 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
306 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
308 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
311 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
313 *disk
->vendor
= '\0';
316 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
318 model
= udev_device_get_sysattr_value(dev
, "model");
320 model
= udev_device_get_sysattr_value(dev
, "product");
323 strncpy(disk
->model
, model
, sizeof(disk
->model
));
327 // Format description
328 char size_str
[STRING_SIZE
];
329 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
331 if (*disk
->vendor
&& *disk
->model
) {
332 snprintf(disk
->description
, sizeof(disk
->description
),
333 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
335 } else if (*disk
->vendor
|| *disk
->model
) {
336 snprintf(disk
->description
, sizeof(disk
->description
),
337 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
340 snprintf(disk
->description
, sizeof(disk
->description
),
341 "%s - %s", size_str
, p
);
344 // Cut off the description string after 40 characters
345 disk
->description
[41] = '\0';
352 udev_device_unref(dev
);
355 udev_enumerate_unref(enumerate
);
362 void hw_free_disks(struct hw_disk
** disks
) {
363 struct hw_disk
** disk
= disks
;
365 while (*disk
!= NULL
) {
366 if (--(*disk
)->ref
== 0)
375 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
376 unsigned int ret
= 0;
384 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
385 struct hw_disk
** ret
= hw_create_disks();
386 struct hw_disk
** selected_disks
= ret
;
388 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
390 for (unsigned int i
= 0; i
< num_disks
; i
++) {
391 if (!selection
|| selection
[i
]) {
392 struct hw_disk
*selected_disk
= disks
[i
];
393 selected_disk
->ref
++;
395 *selected_disks
++ = selected_disk
;
400 *selected_disks
= NULL
;
405 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
406 struct hw_disk
** ret
= hw_create_disks();
407 struct hw_disk
** selected_disks
= ret
;
409 unsigned int num_disks
= hw_count_disks(disks
);
410 assert(num_disks
> 0);
412 for (unsigned int i
= 0; i
< num_disks
; i
++) {
413 struct hw_disk
*disk
= disks
[i
];
416 *selected_disks
++ = disk
;
421 *selected_disks
= NULL
;
426 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
427 unsigned long long memory
= hw_memory();
429 unsigned long long swap_size
= memory
/ 4;
431 // Min. swap size is 128MB
432 if (swap_size
< MB2BYTES(128))
433 swap_size
= MB2BYTES(128);
435 // Cap swap size to 1GB
436 else if (swap_size
> MB2BYTES(1024))
437 swap_size
= MB2BYTES(1024);
442 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
443 return MB2BYTES(128);
446 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
447 // All RAID devices have the p suffix.
451 // Devices with a number at the end have the p suffix, too.
452 // e.g. mmcblk0, cciss0
453 unsigned int last_char
= strlen(dest
->path
) - 1;
454 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
460 static int hw_calculate_partition_table(struct hw
* hw
, struct hw_destination
* dest
, int disable_swap
) {
464 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
465 hw_device_has_p_suffix(dest
) ? "p" : "");
466 dest
->part_boot_idx
= 0;
468 // Determine the size of the target block device
470 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
471 dest
->disk2
->size
: dest
->disk1
->size
;
473 // The RAID will install some metadata at the end of the disk
474 // and we will save up some space for that.
475 dest
->size
-= MB2BYTES(2);
477 dest
->size
= dest
->disk1
->size
;
480 // As we add some extra space before the beginning of the first
481 // partition, we need to substract that here.
482 dest
->size
-= MB2BYTES(1);
484 // Add some more space for partition tables, etc.
485 dest
->size
-= MB2BYTES(1);
487 // The disk has to have at least 2GB
488 if (dest
->size
<= MB2BYTES(2048))
491 // Determine partition table
492 dest
->part_table
= HW_PART_TABLE_MSDOS
;
494 // Disks over 2TB need to use GPT
495 if (dest
->size
>= MB2BYTES(2047 * 1024))
496 dest
->part_table
= HW_PART_TABLE_GPT
;
498 // We also use GPT on raid disks by default
499 else if (dest
->is_raid
)
500 dest
->part_table
= HW_PART_TABLE_GPT
;
502 // When using GPT, GRUB2 needs a little bit of space to put
504 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
505 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
506 "%s%d", path
, part_idx
);
508 dest
->size_bootldr
= MB2BYTES(4);
510 dest
->part_boot_idx
= part_idx
++;
512 *dest
->part_bootldr
= '\0';
513 dest
->size_bootldr
= 0;
516 dest
->size_boot
= hw_boot_size(dest
);
518 // Create an EFI partition when running in EFI mode
520 dest
->size_boot_efi
= MB2BYTES(32);
522 dest
->size_boot_efi
= 0;
524 // Determine the size of the data partition.
525 unsigned long long space_left
= dest
->size
- \
526 (dest
->size_bootldr
+ dest
->size_boot
+ dest
->size_boot_efi
);
528 // If we have less than 2GB left, we disable swap
529 if (space_left
<= MB2BYTES(2048))
532 // Should we use swap?
536 dest
->size_swap
= hw_swap_size(dest
);
539 space_left
-= dest
->size_swap
;
541 // Root is getting what ever is left
542 dest
->size_root
= space_left
;
544 // Set partition names
545 if (dest
->size_boot
> 0) {
546 if (dest
->part_boot_idx
== 0)
547 dest
->part_boot_idx
= part_idx
;
549 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
551 *dest
->part_boot
= '\0';
553 if (dest
->size_boot_efi
> 0) {
554 dest
->part_boot_efi_idx
= part_idx
;
556 snprintf(dest
->part_boot_efi
, sizeof(dest
->part_boot_efi
),
557 "%s%d", path
, part_idx
++);
559 *dest
->part_boot_efi
= '\0';
561 if (dest
->size_swap
> 0)
562 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
564 *dest
->part_swap
= '\0';
566 // There is always a root partition
567 if (dest
->part_boot_idx
== 0)
568 dest
->part_boot_idx
= part_idx
;
570 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
575 struct hw_destination
* hw_make_destination(struct hw
* hw
, int part_type
, struct hw_disk
** disks
, int disable_swap
) {
576 struct hw_destination
* dest
= malloc(sizeof(*dest
));
578 if (part_type
== HW_PART_TYPE_NORMAL
) {
579 dest
->disk1
= *disks
;
582 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
584 } else if (part_type
== HW_PART_TYPE_RAID1
) {
585 dest
->disk1
= *disks
++;
586 dest
->disk2
= *disks
;
587 dest
->raid_level
= 1;
589 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
592 // Is this a RAID device?
593 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
595 int r
= hw_calculate_partition_table(hw
, dest
, disable_swap
);
599 // Set default filesystem
600 dest
->filesystem
= HW_FS_DEFAULT
;
605 unsigned long long hw_memory() {
608 int r
= sysinfo(&si
);
615 static int hw_zero_out_device(const char* path
, int bytes
) {
617 memset(block
, 0, sizeof(block
));
619 int blocks
= bytes
/ sizeof(block
);
621 int fd
= open(path
, O_WRONLY
);
625 unsigned int bytes_written
= 0;
626 while (blocks
-- > 0) {
627 bytes_written
+= write(fd
, block
, sizeof(block
));
633 return bytes_written
;
636 static int try_open(const char* path
) {
637 FILE* f
= fopen(path
, "r");
646 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
647 // Before we write a new partition table to the disk, we will erase
648 // the first couple of megabytes at the beginning of the device to
649 // get rid of all left other things like bootloaders and partition tables.
650 // This solves some problems when changing from MBR to GPT partitions or
651 // the other way around.
652 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
657 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
659 // Set partition type
660 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
661 asprintf(&cmd
, "%s mklabel msdos", cmd
);
662 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
663 asprintf(&cmd
, "%s mklabel gpt", cmd
);
665 unsigned long long part_start
= MB2BYTES(1);
667 if (*dest
->part_bootldr
) {
668 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
669 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
670 part_start
, part_start
+ dest
->size_bootldr
- 1);
672 part_start
+= dest
->size_bootldr
;
675 if (*dest
->part_boot
) {
676 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
677 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
678 part_start
, part_start
+ dest
->size_boot
- 1);
680 part_start
+= dest
->size_boot
;
683 if (*dest
->part_boot_efi
) {
684 asprintf(&cmd
, "%s mkpart %s fat32 %lluB %lluB", cmd
,
685 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ESP" : "primary",
686 part_start
, part_start
+ dest
->size_boot_efi
- 1);
688 part_start
+= dest
->size_boot_efi
;
691 if (*dest
->part_swap
) {
692 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
693 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
694 part_start
, part_start
+ dest
->size_swap
- 1);
696 part_start
+= dest
->size_swap
;
699 if (*dest
->part_root
) {
700 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
701 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
702 part_start
, part_start
+ dest
->size_root
- 1);
704 part_start
+= dest
->size_root
;
707 if (dest
->part_boot_idx
> 0)
708 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
710 if (dest
->part_boot_efi_idx
> 0)
711 asprintf(&cmd
, "%s set %d esp on", cmd
, dest
->part_boot_efi_idx
);
713 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
714 if (*dest
->part_bootldr
) {
715 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
717 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
720 r
= mysystem(output
, cmd
);
722 // Wait until the system re-read the partition table
724 unsigned int counter
= 10;
726 while (counter
-- > 0) {
729 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
732 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
735 if (*dest
->part_boot_efi
&& (try_open(dest
->part_boot_efi
) != 0))
738 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
741 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
744 // All partitions do exist, exiting the loop.
755 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
756 char cmd
[STRING_SIZE
] = "\0";
759 if (fs
== HW_FS_SWAP
) {
760 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
762 } else if (fs
== HW_FS_REISERFS
) {
763 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
766 } else if (fs
== HW_FS_EXT4
) {
767 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 %s", path
);
770 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
771 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path
);
774 } else if (fs
== HW_FS_XFS
) {
775 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
778 } else if (fs
== HW_FS_FAT32
) {
779 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.vfat %s", path
);
784 int r
= mysystem(output
, cmd
);
789 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
793 if (*dest
->part_boot
) {
794 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
800 if (*dest
->part_boot_efi
) {
801 r
= hw_format_filesystem(dest
->part_boot_efi
, HW_FS_FAT32
, output
);
807 if (*dest
->part_swap
) {
808 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
814 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
821 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
822 char target
[STRING_SIZE
];
824 assert(*prefix
== '/');
826 const char* filesystem
;
827 switch (dest
->filesystem
) {
829 filesystem
= "reiserfs";
833 case HW_FS_EXT4_WO_JOURNAL
:
850 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
855 if (*dest
->part_boot
) {
856 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
857 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
859 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
861 hw_umount_filesystems(dest
, prefix
);
868 if (*dest
->part_boot_efi
) {
869 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
870 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
872 r
= hw_mount(dest
->part_boot_efi
, target
, "vfat", 0);
874 hw_umount_filesystems(dest
, prefix
);
881 if (*dest
->part_swap
) {
882 r
= swapon(dest
->part_swap
, 0);
884 hw_umount_filesystems(dest
, prefix
);
890 // bind-mount misc filesystems
891 char** otherfs
= other_filesystems
;
893 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
895 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
896 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
898 hw_umount_filesystems(dest
, prefix
);
909 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
911 char target
[STRING_SIZE
];
913 // Write all buffers to disk before umounting
917 if (*dest
->part_boot_efi
) {
918 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT_EFI
);
919 r
= hw_umount(target
);
925 if (*dest
->part_boot
) {
926 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
927 r
= hw_umount(target
);
933 if (*dest
->part_swap
) {
934 swapoff(dest
->part_swap
);
938 char** otherfs
= other_filesystems
;
940 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
941 r
= hw_umount(target
);
947 r
= hw_umount(prefix
);
954 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
955 char cmd
[STRING_SIZE
];
957 hw_stop_all_raid_arrays(output
);
958 hw_stop_all_raid_arrays(output
);
961 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
962 mysystem(output
, cmd
);
966 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
967 mysystem(output
, cmd
);
973 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
977 assert(dest
->is_raid
);
979 // Stop all RAID arrays that might be around (again).
980 // It seems that there is some sort of race-condition with udev re-enabling
981 // the raid arrays and therefore locking the disks.
982 r
= hw_destroy_raid_superblocks(dest
, output
);
984 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
985 RAID_METADATA
, dest
->path
);
987 switch (dest
->raid_level
) {
989 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
997 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
999 // Clear all data at the beginning
1000 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
1006 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
1008 // Clear all data at the beginning
1009 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
1014 r
= mysystem(output
, cmd
);
1017 // Wait a moment until the device has been properly brought up
1019 unsigned int counter
= 10;
1020 while (counter
-- > 0) {
1023 // If the raid device has not yet been properly brought up,
1024 // opening it will fail with the message: Device or resource busy
1025 // Hence we will wait a bit until it becomes usable.
1026 if (try_open(dest
->path
) == 0)
1034 int hw_stop_all_raid_arrays(const char* output
) {
1035 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
1038 int hw_install_bootloader(struct hw
* hw
, struct hw_destination
* dest
, const char* output
) {
1039 char cmd
[STRING_SIZE
];
1042 char cmd_grub
[STRING_SIZE
];
1043 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --target=i386-pc"
1044 " --no-floppy --recheck");
1046 if (dest
->is_raid
) {
1047 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
1048 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1052 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
1053 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1057 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
1058 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1063 // Install GRUB in EFI mode
1065 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-install"
1066 " --target=%s-efi --efi-directory=%s", hw
->arch
, HW_PATH_BOOT_EFI
);
1068 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1073 // Generate configuration file
1074 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
1075 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1084 static char* hw_get_uuid(const char* dev
) {
1085 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1086 const char* buffer
= NULL
;
1093 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1096 uuid
= strdup(buffer
);
1098 blkid_free_probe(p
);
1103 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1105 int hw_write_fstab(struct hw_destination
* dest
) {
1106 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1113 if (*dest
->part_boot
) {
1114 uuid
= hw_get_uuid(dest
->part_boot
);
1117 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1123 if (*dest
->part_boot_efi
) {
1124 uuid
= hw_get_uuid(dest
->part_boot_efi
);
1127 fprintf(f
, FSTAB_FMT
, uuid
, "/boot/efi", "auto", "defaults", 1, 2);
1134 if (*dest
->part_swap
) {
1135 uuid
= hw_get_uuid(dest
->part_swap
);
1138 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1144 uuid
= hw_get_uuid(dest
->part_root
);
1146 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1161 int hw_start_networking(const char* output
) {
1162 return mysystem(output
, "/usr/bin/start-networking.sh");
1165 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1166 char path
[STRING_SIZE
];
1168 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1169 int r
= access(path
, R_OK
);
1172 return strdup(path
);
1177 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1178 char command
[STRING_SIZE
];
1180 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1181 int rc
= mysystem(output
, command
);