]>
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 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
327 unsigned long long memory
= hw_memory();
329 unsigned long long swap_size
= memory
/ 4;
331 // Min. swap size is 128MB
332 if (swap_size
< MB2BYTES(128))
333 swap_size
= MB2BYTES(128);
335 // Cap swap size to 1GB
336 else if (swap_size
> MB2BYTES(1024))
337 swap_size
= MB2BYTES(1024);
342 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
343 unsigned long long root_size
;
345 if (dest
->size
< MB2BYTES(2048))
346 root_size
= MB2BYTES(1024);
348 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
349 root_size
= MB2BYTES(1536);
352 root_size
= MB2BYTES(2048);
357 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
361 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
362 // All RAID devices have the p suffix.
366 // Devices with a number at the end have the p suffix, too.
367 // e.g. mmcblk0, cciss0
368 unsigned int last_char
= strlen(dest
->path
) - 1;
369 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
375 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
379 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
380 hw_device_has_p_suffix(dest
) ? "p" : "");
381 dest
->part_boot_idx
= 0;
383 // Determine the size of the target block device
385 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
386 dest
->disk2
->size
: dest
->disk1
->size
;
388 // The RAID will install some metadata at the end of the disk
389 // and we will save up some space for that.
390 dest
->size
-= MB2BYTES(2);
392 dest
->size
= dest
->disk1
->size
;
395 // As we add some extra space before the beginning of the first
396 // partition, we need to substract that here.
397 dest
->size
-= MB2BYTES(1);
399 // Add some more space for partition tables, etc.
400 dest
->size
-= MB2BYTES(1);
402 // Determine partition table
403 dest
->part_table
= HW_PART_TABLE_MSDOS
;
405 // Disks over 2TB need to use GPT
406 if (dest
->size
>= MB2BYTES(2047 * 1024))
407 dest
->part_table
= HW_PART_TABLE_GPT
;
409 // We also use GPT on raid disks by default
410 else if (dest
->is_raid
)
411 dest
->part_table
= HW_PART_TABLE_GPT
;
413 // When using GPT, GRUB2 needs a little bit of space to put
415 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
416 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
417 "%s%d", path
, part_idx
);
419 dest
->size_bootldr
= MB2BYTES(4);
421 dest
->part_boot_idx
= part_idx
++;
423 *dest
->part_bootldr
= '\0';
424 dest
->size_bootldr
= 0;
427 dest
->size_boot
= hw_boot_size(dest
);
428 dest
->size_swap
= hw_swap_size(dest
);
429 dest
->size_root
= hw_root_size(dest
);
431 // Determine the size of the data partition.
432 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
433 + dest
->size_swap
+ dest
->size_root
;
435 // Disk is way too small
436 if (used_space
>= dest
->size
)
439 dest
->size_data
= dest
->size
- used_space
;
441 // If it gets too small, we remove the swap space.
442 if (dest
->size_data
<= MB2BYTES(256)) {
443 dest
->size_data
+= dest
->size_swap
;
447 // Set partition names
448 if (dest
->size_boot
> 0) {
449 if (dest
->part_boot_idx
== 0)
450 dest
->part_boot_idx
= part_idx
;
452 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
454 *dest
->part_boot
= '\0';
456 if (dest
->size_swap
> 0)
457 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
459 *dest
->part_swap
= '\0';
461 // There is always a root partition
462 if (dest
->part_boot_idx
== 0)
463 dest
->part_boot_idx
= part_idx
;
465 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
467 if (dest
->size_data
> 0)
468 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
470 *dest
->part_data
= '\0';
475 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
476 struct hw_destination
* dest
= malloc(sizeof(*dest
));
478 if (part_type
== HW_PART_TYPE_NORMAL
) {
479 dest
->disk1
= *disks
;
482 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
484 } else if (part_type
== HW_PART_TYPE_RAID1
) {
485 dest
->disk1
= *disks
++;
486 dest
->disk2
= *disks
;
487 dest
->raid_level
= 1;
489 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
492 // Is this a RAID device?
493 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
495 int r
= hw_calculate_partition_table(dest
);
499 // Set default filesystem
500 dest
->filesystem
= HW_FS_DEFAULT
;
505 unsigned long long hw_memory() {
508 int r
= sysinfo(&si
);
515 static int hw_zero_out_device(const char* path
, int bytes
) {
517 memset(block
, 0, sizeof(block
));
519 int blocks
= bytes
/ sizeof(block
);
521 int fd
= open(path
, O_WRONLY
);
525 unsigned int bytes_written
= 0;
526 while (blocks
-- > 0) {
527 bytes_written
+= write(fd
, block
, sizeof(block
));
533 return bytes_written
;
536 static int try_open(const char* path
) {
537 FILE* f
= fopen(path
, "r");
546 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
547 // Before we write a new partition table to the disk, we will erase
548 // the first couple of megabytes at the beginning of the device to
549 // get rid of all left other things like bootloaders and partition tables.
550 // This solves some problems when changing from MBR to GPT partitions or
551 // the other way around.
552 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
557 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
559 // Set partition type
560 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
561 asprintf(&cmd
, "%s mklabel msdos", cmd
);
562 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
563 asprintf(&cmd
, "%s mklabel gpt", cmd
);
565 unsigned long long part_start
= MB2BYTES(1);
567 if (*dest
->part_bootldr
) {
568 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
569 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
570 part_start
, part_start
+ dest
->size_bootldr
- 1);
572 part_start
+= dest
->size_bootldr
;
575 if (*dest
->part_boot
) {
576 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
577 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
578 part_start
, part_start
+ dest
->size_boot
- 1);
580 part_start
+= dest
->size_boot
;
583 if (*dest
->part_swap
) {
584 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
585 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
586 part_start
, part_start
+ dest
->size_swap
- 1);
588 part_start
+= dest
->size_swap
;
591 if (*dest
->part_root
) {
592 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
593 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
594 part_start
, part_start
+ dest
->size_root
- 1);
596 part_start
+= dest
->size_root
;
599 if (*dest
->part_data
) {
600 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
601 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
602 part_start
, part_start
+ dest
->size_data
- 1);
604 part_start
+= dest
->size_data
;
607 if (dest
->part_boot_idx
> 0)
608 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
610 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
611 if (*dest
->part_bootldr
) {
612 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
614 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
617 r
= mysystem(output
, cmd
);
619 // Wait until the system re-read the partition table
621 unsigned int counter
= 10;
623 while (counter
-- > 0) {
626 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
629 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
632 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
635 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
638 if (*dest
->part_data
&& (try_open(dest
->part_data
) != 0))
641 // All partitions do exist, exiting the loop.
652 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
653 char cmd
[STRING_SIZE
] = "\0";
656 if (fs
== HW_FS_SWAP
) {
657 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
659 } else if (fs
== HW_FS_REISERFS
) {
660 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
663 } else if (fs
== HW_FS_EXT4
) {
664 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
667 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
668 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
671 } else if (fs
== HW_FS_XFS
) {
672 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
677 int r
= mysystem(output
, cmd
);
682 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
686 if (*dest
->part_boot
) {
687 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
693 if (*dest
->part_swap
) {
694 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
700 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
705 if (*dest
->part_data
) {
706 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
, output
);
714 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
715 char target
[STRING_SIZE
];
717 assert(*prefix
== '/');
719 const char* filesystem
;
720 switch (dest
->filesystem
) {
722 filesystem
= "reiserfs";
726 case HW_FS_EXT4_WO_JOURNAL
:
739 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
744 if (*dest
->part_boot
) {
745 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
746 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
748 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
750 hw_umount_filesystems(dest
, prefix
);
757 if (*dest
->part_data
) {
758 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
759 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
761 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
763 hw_umount_filesystems(dest
, prefix
);
770 if (*dest
->part_swap
) {
771 r
= swapon(dest
->part_swap
, 0);
773 hw_umount_filesystems(dest
, prefix
);
779 // bind-mount misc filesystems
780 char** otherfs
= other_filesystems
;
782 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
784 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
785 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
787 hw_umount_filesystems(dest
, prefix
);
798 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
799 // Write all buffers to disk before umounting
803 if (*dest
->part_boot
) {
804 hw_umount(dest
->part_boot
);
808 if (*dest
->part_data
) {
809 hw_umount(dest
->part_data
);
813 hw_umount(dest
->part_root
);
816 if (*dest
->part_swap
) {
817 swapoff(dest
->part_swap
);
821 char target
[STRING_SIZE
];
822 char** otherfs
= other_filesystems
;
825 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
832 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
833 char cmd
[STRING_SIZE
];
835 hw_stop_all_raid_arrays(output
);
836 hw_stop_all_raid_arrays(output
);
839 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
840 mysystem(output
, cmd
);
844 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
845 mysystem(output
, cmd
);
851 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
855 assert(dest
->is_raid
);
857 // Stop all RAID arrays that might be around (again).
858 // It seems that there is some sort of race-condition with udev re-enabling
859 // the raid arrays and therefore locking the disks.
860 r
= hw_destroy_raid_superblocks(dest
, output
);
862 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
863 RAID_METADATA
, dest
->path
);
865 switch (dest
->raid_level
) {
867 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
875 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
877 // Clear all data at the beginning
878 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
884 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
886 // Clear all data at the beginning
887 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
892 r
= mysystem(output
, cmd
);
895 // Wait a moment until the device has been properly brought up
897 unsigned int counter
= 10;
898 while (counter
-- > 0) {
901 // If the raid device has not yet been properly brought up,
902 // opening it will fail with the message: Device or resource busy
903 // Hence we will wait a bit until it becomes usable.
904 if (try_open(dest
->path
) == 0)
912 int hw_stop_all_raid_arrays(const char* output
) {
913 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
916 int hw_install_bootloader(struct hw_destination
* dest
, const char* output
) {
917 char cmd
[STRING_SIZE
];
920 // Generate configuration file
921 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
922 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
926 char cmd_grub
[STRING_SIZE
];
927 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
930 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
931 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
935 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
936 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
938 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
939 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
945 static char* hw_get_uuid(const char* dev
) {
946 blkid_probe p
= blkid_new_probe_from_filename(dev
);
947 const char* buffer
= NULL
;
954 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
957 uuid
= strdup(buffer
);
964 int hw_write_fstab(struct hw_destination
* dest
) {
965 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
969 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
973 if (*dest
->part_boot
) {
974 uuid
= hw_get_uuid(dest
->part_boot
);
977 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
983 if (*dest
->part_swap
) {
984 uuid
= hw_get_uuid(dest
->part_swap
);
987 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
993 uuid
= hw_get_uuid(dest
->part_root
);
995 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
1000 if (*dest
->part_data
) {
1001 uuid
= hw_get_uuid(dest
->part_data
);
1004 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);