]>
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>
33 #include <sys/ioctl.h>
34 #include <sys/mount.h>
36 #include <sys/sysinfo.h>
41 #include <libsmooth.h>
45 const char* other_filesystems
[] = {
52 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
53 char chroot_cmd
[STRING_SIZE
];
55 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
57 return mysystem(output
, chroot_cmd
);
60 struct hw
* hw_init() {
61 struct hw
* hw
= malloc(sizeof(*hw
));
65 hw
->udev
= udev_new();
67 fprintf(stderr
, "Could not create udev instance\n");
74 void hw_free(struct hw
* hw
) {
81 static int strstartswith(const char* a
, const char* b
) {
82 return (strncmp(a
, b
, strlen(b
)) == 0);
85 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
86 // Create target if it does not exist
87 if (access(target
, X_OK
) != 0)
88 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
90 return mount(source
, target
, fs
, flags
, NULL
);
93 int hw_umount(const char* target
) {
94 return umount2(target
, 0);
97 static int hw_test_source_medium(const char* path
) {
98 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
100 // If the source could not be mounted we
105 // Check if the test file exists.
106 ret
= access(SOURCE_TEST_FILE
, R_OK
);
108 // Umount the test device.
109 hw_umount(SOURCE_MOUNT_PATH
);
114 char* hw_find_source_medium(struct hw
* hw
) {
117 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
119 udev_enumerate_add_match_subsystem(enumerate
, "block");
120 udev_enumerate_scan_devices(enumerate
);
122 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
124 struct udev_list_entry
* dev_list_entry
;
125 udev_list_entry_foreach(dev_list_entry
, devices
) {
126 const char* path
= udev_list_entry_get_name(dev_list_entry
);
127 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
129 const char* dev_path
= udev_device_get_devnode(dev
);
131 // Skip everything what we cannot work with
132 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
133 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
136 if (hw_test_source_medium(dev_path
) == 0) {
137 ret
= strdup(dev_path
);
140 udev_device_unref(dev
);
142 // If a suitable device was found the search will end.
147 udev_enumerate_unref(enumerate
);
152 static struct hw_disk
** hw_create_disks() {
153 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
158 static unsigned long long hw_block_device_get_size(const char* dev
) {
159 int fd
= open(dev
, O_RDONLY
);
163 unsigned long long size
= blkid_get_dev_size(fd
);
169 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
170 struct hw_disk
** ret
= hw_create_disks();
171 struct hw_disk
** disks
= ret
;
173 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
175 udev_enumerate_add_match_subsystem(enumerate
, "block");
176 udev_enumerate_scan_devices(enumerate
);
178 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
180 struct udev_list_entry
* dev_list_entry
;
181 unsigned int i
= HW_MAX_DISKS
;
182 udev_list_entry_foreach(dev_list_entry
, devices
) {
183 const char* path
= udev_list_entry_get_name(dev_list_entry
);
184 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
186 const char* dev_path
= udev_device_get_devnode(dev
);
188 // Skip everything what we cannot work with
189 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
190 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
191 strstartswith(dev_path
, "/dev/md")) {
192 udev_device_unref(dev
);
196 // Skip sourcedrive if we need to
197 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
198 udev_device_unref(dev
);
202 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
203 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
204 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
205 udev_device_unref(dev
);
209 // Skip devices with a size of zero
210 unsigned long long size
= hw_block_device_get_size(dev_path
);
212 udev_device_unref(dev
);
216 struct hw_disk
* disk
= malloc(sizeof(*disk
));
222 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
223 const char* p
= disk
->path
+ 5;
228 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
230 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
232 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
235 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
237 *disk
->vendor
= '\0';
240 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
242 model
= udev_device_get_sysattr_value(dev
, "model");
244 model
= udev_device_get_sysattr_value(dev
, "product");
247 strncpy(disk
->model
, model
, sizeof(disk
->model
));
251 // Format description
252 char size_str
[STRING_SIZE
];
253 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
255 if (*disk
->vendor
&& *disk
->model
) {
256 snprintf(disk
->description
, sizeof(disk
->description
),
257 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
259 } else if (*disk
->vendor
|| *disk
->model
) {
260 snprintf(disk
->description
, sizeof(disk
->description
),
261 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
264 snprintf(disk
->description
, sizeof(disk
->description
),
265 "%s - %s", size_str
, p
);
273 udev_device_unref(dev
);
276 udev_enumerate_unref(enumerate
);
283 void hw_free_disks(struct hw_disk
** disks
) {
284 struct hw_disk
** disk
= disks
;
286 while (*disk
!= NULL
) {
287 if (--(*disk
)->ref
== 0)
296 unsigned int hw_count_disks(struct hw_disk
** disks
) {
297 unsigned int ret
= 0;
305 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
306 struct hw_disk
** ret
= hw_create_disks();
307 struct hw_disk
** selected_disks
= ret
;
309 unsigned int num_disks
= hw_count_disks(disks
);
311 for (unsigned int i
= 0; i
< num_disks
; i
++) {
312 if (!selection
|| selection
[i
]) {
313 struct hw_disk
*selected_disk
= disks
[i
];
314 selected_disk
->ref
++;
316 *selected_disks
++ = selected_disk
;
321 *selected_disks
= NULL
;
326 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
327 struct hw_disk
** ret
= hw_create_disks();
328 struct hw_disk
** selected_disks
= ret
;
330 unsigned int num_disks
= hw_count_disks(disks
);
331 assert(num_disks
> 0);
333 for (unsigned int i
= 0; i
< num_disks
; i
++) {
334 struct hw_disk
*disk
= disks
[i
];
337 *selected_disks
++ = disk
;
342 *selected_disks
= NULL
;
347 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
348 unsigned long long memory
= hw_memory();
350 unsigned long long swap_size
= memory
/ 4;
352 // Min. swap size is 128MB
353 if (swap_size
< MB2BYTES(128))
354 swap_size
= MB2BYTES(128);
356 // Cap swap size to 1GB
357 else if (swap_size
> MB2BYTES(1024))
358 swap_size
= MB2BYTES(1024);
363 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
364 unsigned long long root_size
;
366 if (dest
->size
< MB2BYTES(2048))
367 root_size
= MB2BYTES(1024);
369 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
370 root_size
= MB2BYTES(1536);
373 root_size
= MB2BYTES(2048);
378 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
382 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
383 // All RAID devices have the p suffix.
387 // Devices with a number at the end have the p suffix, too.
388 // e.g. mmcblk0, cciss0
389 unsigned int last_char
= strlen(dest
->path
) - 1;
390 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
396 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
400 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
401 hw_device_has_p_suffix(dest
) ? "p" : "");
402 dest
->part_boot_idx
= 0;
404 // Determine the size of the target block device
406 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
407 dest
->disk2
->size
: dest
->disk1
->size
;
409 // The RAID will install some metadata at the end of the disk
410 // and we will save up some space for that.
411 dest
->size
-= MB2BYTES(2);
413 dest
->size
= dest
->disk1
->size
;
416 // As we add some extra space before the beginning of the first
417 // partition, we need to substract that here.
418 dest
->size
-= MB2BYTES(1);
420 // Add some more space for partition tables, etc.
421 dest
->size
-= MB2BYTES(1);
423 // Determine partition table
424 dest
->part_table
= HW_PART_TABLE_MSDOS
;
426 // Disks over 2TB need to use GPT
427 if (dest
->size
>= MB2BYTES(2047 * 1024))
428 dest
->part_table
= HW_PART_TABLE_GPT
;
430 // We also use GPT on raid disks by default
431 else if (dest
->is_raid
)
432 dest
->part_table
= HW_PART_TABLE_GPT
;
434 // When using GPT, GRUB2 needs a little bit of space to put
436 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
437 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
438 "%s%d", path
, part_idx
);
440 dest
->size_bootldr
= MB2BYTES(4);
442 dest
->part_boot_idx
= part_idx
++;
444 *dest
->part_bootldr
= '\0';
445 dest
->size_bootldr
= 0;
448 dest
->size_boot
= hw_boot_size(dest
);
449 dest
->size_swap
= hw_swap_size(dest
);
450 dest
->size_root
= hw_root_size(dest
);
452 // Determine the size of the data partition.
453 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
454 + dest
->size_swap
+ dest
->size_root
;
456 // Disk is way too small
457 if (used_space
>= dest
->size
)
460 dest
->size_data
= dest
->size
- used_space
;
462 // If it gets too small, we remove the swap space.
463 if (dest
->size_data
<= MB2BYTES(256)) {
464 dest
->size_data
+= dest
->size_swap
;
468 // Set partition names
469 if (dest
->size_boot
> 0) {
470 if (dest
->part_boot_idx
== 0)
471 dest
->part_boot_idx
= part_idx
;
473 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
475 *dest
->part_boot
= '\0';
477 if (dest
->size_swap
> 0)
478 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
480 *dest
->part_swap
= '\0';
482 // There is always a root partition
483 if (dest
->part_boot_idx
== 0)
484 dest
->part_boot_idx
= part_idx
;
486 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
488 if (dest
->size_data
> 0)
489 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
491 *dest
->part_data
= '\0';
496 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
497 struct hw_destination
* dest
= malloc(sizeof(*dest
));
499 if (part_type
== HW_PART_TYPE_NORMAL
) {
500 dest
->disk1
= *disks
;
503 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
505 } else if (part_type
== HW_PART_TYPE_RAID1
) {
506 dest
->disk1
= *disks
++;
507 dest
->disk2
= *disks
;
508 dest
->raid_level
= 1;
510 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
513 // Is this a RAID device?
514 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
516 int r
= hw_calculate_partition_table(dest
);
520 // Set default filesystem
521 dest
->filesystem
= HW_FS_DEFAULT
;
526 unsigned long long hw_memory() {
529 int r
= sysinfo(&si
);
536 static int hw_zero_out_device(const char* path
, int bytes
) {
538 memset(block
, 0, sizeof(block
));
540 int blocks
= bytes
/ sizeof(block
);
542 int fd
= open(path
, O_WRONLY
);
546 unsigned int bytes_written
= 0;
547 while (blocks
-- > 0) {
548 bytes_written
+= write(fd
, block
, sizeof(block
));
554 return bytes_written
;
557 static int try_open(const char* path
) {
558 FILE* f
= fopen(path
, "r");
567 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
568 // Before we write a new partition table to the disk, we will erase
569 // the first couple of megabytes at the beginning of the device to
570 // get rid of all left other things like bootloaders and partition tables.
571 // This solves some problems when changing from MBR to GPT partitions or
572 // the other way around.
573 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
578 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
580 // Set partition type
581 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
582 asprintf(&cmd
, "%s mklabel msdos", cmd
);
583 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
584 asprintf(&cmd
, "%s mklabel gpt", cmd
);
586 unsigned long long part_start
= MB2BYTES(1);
588 if (*dest
->part_bootldr
) {
589 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
590 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
591 part_start
, part_start
+ dest
->size_bootldr
- 1);
593 part_start
+= dest
->size_bootldr
;
596 if (*dest
->part_boot
) {
597 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
598 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
599 part_start
, part_start
+ dest
->size_boot
- 1);
601 part_start
+= dest
->size_boot
;
604 if (*dest
->part_swap
) {
605 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
606 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
607 part_start
, part_start
+ dest
->size_swap
- 1);
609 part_start
+= dest
->size_swap
;
612 if (*dest
->part_root
) {
613 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
614 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
615 part_start
, part_start
+ dest
->size_root
- 1);
617 part_start
+= dest
->size_root
;
620 if (*dest
->part_data
) {
621 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
622 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
623 part_start
, part_start
+ dest
->size_data
- 1);
625 part_start
+= dest
->size_data
;
628 if (dest
->part_boot_idx
> 0)
629 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
631 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
632 if (*dest
->part_bootldr
) {
633 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
635 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
638 r
= mysystem(output
, cmd
);
640 // Wait until the system re-read the partition table
642 unsigned int counter
= 10;
644 while (counter
-- > 0) {
647 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
650 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
653 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
656 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
659 if (*dest
->part_data
&& (try_open(dest
->part_data
) != 0))
662 // All partitions do exist, exiting the loop.
673 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
674 char cmd
[STRING_SIZE
] = "\0";
677 if (fs
== HW_FS_SWAP
) {
678 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
680 } else if (fs
== HW_FS_REISERFS
) {
681 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
684 } else if (fs
== HW_FS_EXT4
) {
685 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
688 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
689 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
692 } else if (fs
== HW_FS_XFS
) {
693 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
698 int r
= mysystem(output
, cmd
);
703 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
707 if (*dest
->part_boot
) {
708 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
714 if (*dest
->part_swap
) {
715 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
721 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
726 if (*dest
->part_data
) {
727 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
, output
);
735 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
736 char target
[STRING_SIZE
];
738 assert(*prefix
== '/');
740 const char* filesystem
;
741 switch (dest
->filesystem
) {
743 filesystem
= "reiserfs";
747 case HW_FS_EXT4_WO_JOURNAL
:
760 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
765 if (*dest
->part_boot
) {
766 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
767 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
769 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
771 hw_umount_filesystems(dest
, prefix
);
778 if (*dest
->part_data
) {
779 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
780 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
782 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
784 hw_umount_filesystems(dest
, prefix
);
791 if (*dest
->part_swap
) {
792 r
= swapon(dest
->part_swap
, 0);
794 hw_umount_filesystems(dest
, prefix
);
800 // bind-mount misc filesystems
801 char** otherfs
= other_filesystems
;
803 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
805 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
806 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
808 hw_umount_filesystems(dest
, prefix
);
819 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
820 // Write all buffers to disk before umounting
824 if (*dest
->part_boot
) {
825 hw_umount(dest
->part_boot
);
829 if (*dest
->part_data
) {
830 hw_umount(dest
->part_data
);
834 hw_umount(dest
->part_root
);
837 if (*dest
->part_swap
) {
838 swapoff(dest
->part_swap
);
842 char target
[STRING_SIZE
];
843 char** otherfs
= other_filesystems
;
846 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
853 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
854 char cmd
[STRING_SIZE
];
856 hw_stop_all_raid_arrays(output
);
857 hw_stop_all_raid_arrays(output
);
860 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
861 mysystem(output
, cmd
);
865 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
866 mysystem(output
, cmd
);
872 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
876 assert(dest
->is_raid
);
878 // Stop all RAID arrays that might be around (again).
879 // It seems that there is some sort of race-condition with udev re-enabling
880 // the raid arrays and therefore locking the disks.
881 r
= hw_destroy_raid_superblocks(dest
, output
);
883 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
884 RAID_METADATA
, dest
->path
);
886 switch (dest
->raid_level
) {
888 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
896 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
898 // Clear all data at the beginning
899 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
905 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
907 // Clear all data at the beginning
908 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
913 r
= mysystem(output
, cmd
);
916 // Wait a moment until the device has been properly brought up
918 unsigned int counter
= 10;
919 while (counter
-- > 0) {
922 // If the raid device has not yet been properly brought up,
923 // opening it will fail with the message: Device or resource busy
924 // Hence we will wait a bit until it becomes usable.
925 if (try_open(dest
->path
) == 0)
933 int hw_stop_all_raid_arrays(const char* output
) {
934 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
937 int hw_install_bootloader(struct hw_destination
* dest
, const char* output
) {
938 char cmd
[STRING_SIZE
];
941 // Generate configuration file
942 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
943 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
947 char cmd_grub
[STRING_SIZE
];
948 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
951 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
952 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
956 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
957 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
959 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
960 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
966 static char* hw_get_uuid(const char* dev
) {
967 blkid_probe p
= blkid_new_probe_from_filename(dev
);
968 const char* buffer
= NULL
;
975 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
978 uuid
= strdup(buffer
);
985 int hw_write_fstab(struct hw_destination
* dest
) {
986 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
990 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
994 if (*dest
->part_boot
) {
995 uuid
= hw_get_uuid(dest
->part_boot
);
998 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
1004 if (*dest
->part_swap
) {
1005 uuid
= hw_get_uuid(dest
->part_swap
);
1008 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1014 uuid
= hw_get_uuid(dest
->part_root
);
1016 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
1021 if (*dest
->part_data
) {
1022 uuid
= hw_get_uuid(dest
->part_data
);
1025 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);
1041 int hw_start_networking(const char* output
) {
1042 return mysystem(output
, "/usr/bin/start-networking.sh");
1045 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1046 char path
[STRING_SIZE
];
1048 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1049 int r
= access(path
, R_OK
);
1052 return strdup(path
);
1057 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1058 char command
[STRING_SIZE
];
1060 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1061 int rc
= mysystem(output
, command
);