]>
git.ipfire.org Git - people/pmueller/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>
40 #include <libsmooth.h>
44 const char* other_filesystems
[] = {
51 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
52 char chroot_cmd
[STRING_SIZE
];
54 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
56 return mysystem(output
, chroot_cmd
);
59 struct hw
* hw_init() {
60 struct hw
* hw
= malloc(sizeof(*hw
));
64 hw
->udev
= udev_new();
66 fprintf(stderr
, "Could not create udev instance\n");
73 void hw_free(struct hw
* hw
) {
80 static int strstartswith(const char* a
, const char* b
) {
81 return (strncmp(a
, b
, strlen(b
)) == 0);
84 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
85 // Create target if it does not exist
86 if (access(target
, X_OK
) != 0)
87 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
89 return mount(source
, target
, fs
, flags
, NULL
);
92 int hw_umount(const char* target
) {
93 return umount2(target
, 0);
96 static int hw_test_source_medium(const char* path
) {
97 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
99 // If the source could not be mounted we
104 // Check if the test file exists.
105 ret
= access(SOURCE_TEST_FILE
, R_OK
);
107 // Umount the test device.
108 hw_umount(SOURCE_MOUNT_PATH
);
113 char* hw_find_source_medium(struct hw
* hw
) {
116 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
118 udev_enumerate_add_match_subsystem(enumerate
, "block");
119 udev_enumerate_scan_devices(enumerate
);
121 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
123 struct udev_list_entry
* dev_list_entry
;
124 udev_list_entry_foreach(dev_list_entry
, devices
) {
125 const char* path
= udev_list_entry_get_name(dev_list_entry
);
126 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
128 const char* dev_path
= udev_device_get_devnode(dev
);
130 // Skip everything what we cannot work with
131 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
132 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
135 if (hw_test_source_medium(dev_path
) == 0) {
136 ret
= strdup(dev_path
);
139 udev_device_unref(dev
);
141 // If a suitable device was found the search will end.
146 udev_enumerate_unref(enumerate
);
151 static struct hw_disk
** hw_create_disks() {
152 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
157 static unsigned long long hw_block_device_get_size(const char* dev
) {
158 int fd
= open(dev
, O_RDONLY
);
162 unsigned long long size
= blkid_get_dev_size(fd
);
168 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
169 struct hw_disk
** ret
= hw_create_disks();
170 struct hw_disk
** disks
= ret
;
172 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
174 udev_enumerate_add_match_subsystem(enumerate
, "block");
175 udev_enumerate_scan_devices(enumerate
);
177 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
179 struct udev_list_entry
* dev_list_entry
;
180 unsigned int i
= HW_MAX_DISKS
;
181 udev_list_entry_foreach(dev_list_entry
, devices
) {
182 const char* path
= udev_list_entry_get_name(dev_list_entry
);
183 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
185 const char* dev_path
= udev_device_get_devnode(dev
);
187 // Skip everything what we cannot work with
188 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
189 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
190 strstartswith(dev_path
, "/dev/md")) {
191 udev_device_unref(dev
);
195 // Skip sourcedrive if we need to
196 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
197 udev_device_unref(dev
);
201 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
202 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
203 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
204 udev_device_unref(dev
);
208 // Skip devices with a size of zero
209 unsigned long long size
= hw_block_device_get_size(dev_path
);
211 udev_device_unref(dev
);
215 struct hw_disk
* disk
= malloc(sizeof(*disk
));
221 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
222 const char* p
= disk
->path
+ 5;
227 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
229 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
231 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
234 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
236 *disk
->vendor
= '\0';
239 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
241 model
= udev_device_get_sysattr_value(dev
, "model");
243 model
= udev_device_get_sysattr_value(dev
, "product");
246 strncpy(disk
->model
, model
, sizeof(disk
->model
));
250 // Format description
251 char size_str
[STRING_SIZE
];
252 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
254 if (*disk
->vendor
&& *disk
->model
) {
255 snprintf(disk
->description
, sizeof(disk
->description
),
256 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
258 } else if (*disk
->vendor
|| *disk
->model
) {
259 snprintf(disk
->description
, sizeof(disk
->description
),
260 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
263 snprintf(disk
->description
, sizeof(disk
->description
),
264 "%s - %s", size_str
, p
);
272 udev_device_unref(dev
);
275 udev_enumerate_unref(enumerate
);
282 void hw_free_disks(struct hw_disk
** disks
) {
283 struct hw_disk
** disk
= disks
;
285 while (*disk
!= NULL
) {
286 if (--(*disk
)->ref
== 0)
295 unsigned int hw_count_disks(struct hw_disk
** disks
) {
296 unsigned int ret
= 0;
304 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
305 struct hw_disk
** ret
= hw_create_disks();
306 struct hw_disk
** selected_disks
= ret
;
308 unsigned int num_disks
= hw_count_disks(disks
);
310 for (unsigned int i
= 0; i
< num_disks
; i
++) {
311 if (!selection
|| selection
[i
]) {
312 struct hw_disk
*selected_disk
= disks
[i
];
313 selected_disk
->ref
++;
315 *selected_disks
++ = selected_disk
;
320 *selected_disks
= NULL
;
325 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
326 unsigned long long memory
= hw_memory();
328 unsigned long long swap_size
= memory
/ 4;
330 // Min. swap size is 128MB
331 if (swap_size
< MB2BYTES(128))
332 swap_size
= MB2BYTES(128);
334 // Cap swap size to 1GB
335 else if (swap_size
> MB2BYTES(1024))
336 swap_size
= MB2BYTES(1024);
341 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
342 unsigned long long root_size
;
344 if (dest
->size
< MB2BYTES(2048))
345 root_size
= MB2BYTES(1024);
347 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
348 root_size
= MB2BYTES(1536);
351 root_size
= MB2BYTES(2048);
356 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
360 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
361 // All RAID devices have the p suffix.
365 // Devices with a number at the end have the p suffix, too.
366 // e.g. mmcblk0, cciss0
367 unsigned int last_char
= strlen(dest
->path
);
368 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
374 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
378 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
379 hw_device_has_p_suffix(dest
) ? "p" : "");
380 dest
->part_boot_idx
= 0;
382 // Determine the size of the target block device
384 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
385 dest
->disk2
->size
: dest
->disk1
->size
;
387 // The RAID will install some metadata at the end of the disk
388 // and we will save up some space for that.
389 dest
->size
-= MB2BYTES(2);
391 dest
->size
= dest
->disk1
->size
;
394 // As we add some extra space before the beginning of the first
395 // partition, we need to substract that here.
396 dest
->size
-= MB2BYTES(1);
398 // Add some more space for partition tables, etc.
399 dest
->size
-= MB2BYTES(1);
401 // Determine partition table
402 dest
->part_table
= HW_PART_TABLE_MSDOS
;
404 // Disks over 2TB need to use GPT
405 if (dest
->size
>= MB2BYTES(2047 * 1024))
406 dest
->part_table
= HW_PART_TABLE_GPT
;
408 // We also use GPT on raid disks by default
409 else if (dest
->is_raid
)
410 dest
->part_table
= HW_PART_TABLE_GPT
;
412 // When using GPT, GRUB2 needs a little bit of space to put
414 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
415 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
416 "%s%d", path
, part_idx
);
418 dest
->size_bootldr
= MB2BYTES(4);
420 dest
->part_boot_idx
= part_idx
++;
422 *dest
->part_bootldr
= '\0';
423 dest
->size_bootldr
= 0;
426 dest
->size_boot
= hw_boot_size(dest
);
427 dest
->size_swap
= hw_swap_size(dest
);
428 dest
->size_root
= hw_root_size(dest
);
430 // Determine the size of the data partition.
431 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
432 + dest
->size_swap
+ dest
->size_root
;
434 // Disk is way too small
435 if (used_space
>= dest
->size
)
438 dest
->size_data
= dest
->size
- used_space
;
440 // If it gets too small, we remove the swap space.
441 if (dest
->size_data
<= MB2BYTES(256)) {
442 dest
->size_data
+= dest
->size_swap
;
446 // Set partition names
447 if (dest
->size_boot
> 0) {
448 if (dest
->part_boot_idx
== 0)
449 dest
->part_boot_idx
= part_idx
;
451 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
453 *dest
->part_boot
= '\0';
455 if (dest
->size_swap
> 0)
456 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
458 *dest
->part_swap
= '\0';
460 // There is always a root partition
461 if (dest
->part_boot_idx
== 0)
462 dest
->part_boot_idx
= part_idx
;
464 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
466 if (dest
->size_data
> 0)
467 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
469 *dest
->part_data
= '\0';
474 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
475 struct hw_destination
* dest
= malloc(sizeof(*dest
));
477 if (part_type
== HW_PART_TYPE_NORMAL
) {
478 dest
->disk1
= *disks
;
481 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
483 } else if (part_type
== HW_PART_TYPE_RAID1
) {
484 dest
->disk1
= *disks
++;
485 dest
->disk2
= *disks
;
486 dest
->raid_level
= 1;
488 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
491 // Is this a RAID device?
492 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
494 int r
= hw_calculate_partition_table(dest
);
498 // Set default filesystem
499 dest
->filesystem
= HW_FS_DEFAULT
;
504 unsigned long long hw_memory() {
506 char line
[STRING_SIZE
];
508 unsigned long long memory
= 0;
510 /* Calculate amount of memory in machine */
511 if ((handle
= fopen("/proc/meminfo", "r"))) {
512 while (fgets(line
, sizeof(line
), handle
)) {
513 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
521 return memory
* 1024;
524 static int hw_zero_out_device(const char* path
, int bytes
) {
526 memset(block
, 0, sizeof(block
));
528 int blocks
= bytes
/ sizeof(block
);
530 int fd
= open(path
, O_WRONLY
);
534 unsigned int bytes_written
= 0;
535 while (blocks
-- > 0) {
536 bytes_written
+= write(fd
, block
, sizeof(block
));
542 return bytes_written
;
545 static int try_open(const char* path
) {
546 FILE* f
= fopen(path
, "r");
555 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
556 // Before we write a new partition table to the disk, we will erase
557 // the first couple of megabytes at the beginning of the device to
558 // get rid of all left other things like bootloaders and partition tables.
559 // This solves some problems when changing from MBR to GPT partitions or
560 // the other way around.
561 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
566 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
568 // Set partition type
569 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
570 asprintf(&cmd
, "%s mklabel msdos", cmd
);
571 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
572 asprintf(&cmd
, "%s mklabel gpt", cmd
);
574 unsigned long long part_start
= MB2BYTES(1);
576 if (*dest
->part_bootldr
) {
577 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
578 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
579 part_start
, part_start
+ dest
->size_bootldr
- 1);
581 part_start
+= dest
->size_bootldr
;
584 if (*dest
->part_boot
) {
585 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
586 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
587 part_start
, part_start
+ dest
->size_boot
- 1);
589 part_start
+= dest
->size_boot
;
592 if (*dest
->part_swap
) {
593 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
594 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
595 part_start
, part_start
+ dest
->size_swap
- 1);
597 part_start
+= dest
->size_swap
;
600 if (*dest
->part_root
) {
601 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
602 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
603 part_start
, part_start
+ dest
->size_root
- 1);
605 part_start
+= dest
->size_root
;
608 if (*dest
->part_data
) {
609 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
610 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
611 part_start
, part_start
+ dest
->size_data
- 1);
613 part_start
+= dest
->size_data
;
616 if (dest
->part_boot_idx
> 0)
617 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
619 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
620 if (*dest
->part_bootldr
) {
621 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
623 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
626 r
= mysystem(output
, cmd
);
628 // Wait until the system re-read the partition table
630 unsigned int counter
= 10;
632 while (counter
-- > 0) {
635 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
638 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
641 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
644 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
647 if (*dest
->part_data
&& (try_open(dest
->part_data
) != 0))
650 // All partitions do exist, exiting the loop.
661 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
662 char cmd
[STRING_SIZE
] = "\0";
665 if (fs
== HW_FS_SWAP
) {
666 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
668 } else if (fs
== HW_FS_REISERFS
) {
669 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
672 } else if (fs
== HW_FS_EXT4
) {
673 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
676 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
677 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
680 } else if (fs
== HW_FS_XFS
) {
681 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
686 int r
= mysystem(output
, cmd
);
691 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
695 if (*dest
->part_boot
) {
696 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
702 if (*dest
->part_swap
) {
703 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
709 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
714 if (*dest
->part_data
) {
715 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
, output
);
723 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
724 char target
[STRING_SIZE
];
726 assert(*prefix
== '/');
728 const char* filesystem
;
729 switch (dest
->filesystem
) {
731 filesystem
= "reiserfs";
735 case HW_FS_EXT4_WO_JOURNAL
:
748 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
753 if (*dest
->part_boot
) {
754 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
755 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
757 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
759 hw_umount_filesystems(dest
, prefix
);
766 if (*dest
->part_data
) {
767 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
768 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
770 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
772 hw_umount_filesystems(dest
, prefix
);
779 if (*dest
->part_swap
) {
780 r
= swapon(dest
->part_swap
, 0);
782 hw_umount_filesystems(dest
, prefix
);
788 // bind-mount misc filesystems
789 char** otherfs
= other_filesystems
;
791 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
793 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
794 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
796 hw_umount_filesystems(dest
, prefix
);
807 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
808 // Write all buffers to disk before umounting
812 if (*dest
->part_boot
) {
813 hw_umount(dest
->part_boot
);
817 if (*dest
->part_data
) {
818 hw_umount(dest
->part_data
);
822 hw_umount(dest
->part_root
);
825 if (*dest
->part_swap
) {
826 swapoff(dest
->part_swap
);
830 char target
[STRING_SIZE
];
831 char** otherfs
= other_filesystems
;
834 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
841 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
842 char cmd
[STRING_SIZE
];
844 hw_stop_all_raid_arrays(output
);
845 hw_stop_all_raid_arrays(output
);
848 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
849 mysystem(output
, cmd
);
853 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
854 mysystem(output
, cmd
);
860 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
864 assert(dest
->is_raid
);
866 // Stop all RAID arrays that might be around (again).
867 // It seems that there is some sort of race-condition with udev re-enabling
868 // the raid arrays and therefore locking the disks.
869 r
= hw_destroy_raid_superblocks(dest
, output
);
871 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
872 RAID_METADATA
, dest
->path
);
874 switch (dest
->raid_level
) {
876 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
884 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
886 // Clear all data at the beginning
887 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
893 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
895 // Clear all data at the beginning
896 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
901 r
= mysystem(output
, cmd
);
904 // Wait a moment until the device has been properly brought up
906 unsigned int counter
= 10;
907 while (counter
-- > 0) {
910 // If the raid device has not yet been properly brought up,
911 // opening it will fail with the message: Device or resource busy
912 // Hence we will wait a bit until it becomes usable.
913 if (try_open(dest
->path
) == 0)
921 int hw_stop_all_raid_arrays(const char* output
) {
922 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
925 int hw_install_bootloader(struct hw_destination
* dest
, const char* output
) {
926 char cmd
[STRING_SIZE
];
929 // Generate configuration file
930 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
931 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
935 char cmd_grub
[STRING_SIZE
];
936 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
939 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
940 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
944 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
945 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
947 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
948 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
954 static char* hw_get_uuid(const char* dev
) {
955 blkid_probe p
= blkid_new_probe_from_filename(dev
);
956 const char* buffer
= NULL
;
963 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
966 uuid
= strdup(buffer
);
973 int hw_write_fstab(struct hw_destination
* dest
) {
974 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
978 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
982 if (*dest
->part_boot
) {
983 uuid
= hw_get_uuid(dest
->part_boot
);
986 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
992 if (*dest
->part_swap
) {
993 uuid
= hw_get_uuid(dest
->part_swap
);
996 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1002 uuid
= hw_get_uuid(dest
->part_root
);
1004 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
1009 if (*dest
->part_data
) {
1010 uuid
= hw_get_uuid(dest
->part_data
);
1013 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);