]>
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>
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_calculate_partition_table(struct hw_destination
* dest
) {
364 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
365 dest
->part_boot_idx
= 0;
367 // Determine the size of the target block device
369 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
370 dest
->disk2
->size
: dest
->disk1
->size
;
372 // The RAID will install some metadata at the end of the disk
373 // and we will save up some space for that.
374 dest
->size
-= MB2BYTES(2);
376 dest
->size
= dest
->disk1
->size
;
379 // As we add some extra space before the beginning of the first
380 // partition, we need to substract that here.
381 dest
->size
-= MB2BYTES(1);
383 // Add some more space for partition tables, etc.
384 dest
->size
-= MB2BYTES(1);
386 // Determine partition table
387 dest
->part_table
= HW_PART_TABLE_MSDOS
;
389 // Disks over 2TB need to use GPT
390 if (dest
->size
>= MB2BYTES(2047 * 1024))
391 dest
->part_table
= HW_PART_TABLE_GPT
;
393 // We also use GPT on raid disks by default
394 else if (dest
->is_raid
)
395 dest
->part_table
= HW_PART_TABLE_GPT
;
397 // When using GPT, GRUB2 needs a little bit of space to put
399 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
400 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
401 "%s%d", path
, part_idx
);
403 dest
->size_bootldr
= MB2BYTES(4);
405 dest
->part_boot_idx
= part_idx
++;
407 *dest
->part_bootldr
= '\0';
408 dest
->size_bootldr
= 0;
411 dest
->size_boot
= hw_boot_size(dest
);
412 dest
->size_swap
= hw_swap_size(dest
);
413 dest
->size_root
= hw_root_size(dest
);
415 // Determine the size of the data partition.
416 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
417 + dest
->size_swap
+ dest
->size_root
;
419 // Disk is way too small
420 if (used_space
>= dest
->size
)
423 dest
->size_data
= dest
->size
- used_space
;
425 // If it gets too small, we remove the swap space.
426 if (dest
->size_data
<= MB2BYTES(256)) {
427 dest
->size_data
+= dest
->size_swap
;
431 // Set partition names
432 if (dest
->size_boot
> 0) {
433 if (dest
->part_boot_idx
== 0)
434 dest
->part_boot_idx
= part_idx
;
436 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
438 *dest
->part_boot
= '\0';
440 if (dest
->size_swap
> 0)
441 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
443 *dest
->part_swap
= '\0';
445 // There is always a root partition
446 if (dest
->part_boot_idx
== 0)
447 dest
->part_boot_idx
= part_idx
;
449 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
451 if (dest
->size_data
> 0)
452 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
454 *dest
->part_data
= '\0';
459 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
460 struct hw_destination
* dest
= malloc(sizeof(*dest
));
462 if (part_type
== HW_PART_TYPE_NORMAL
) {
463 dest
->disk1
= *disks
;
466 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
468 } else if (part_type
== HW_PART_TYPE_RAID1
) {
469 dest
->disk1
= *disks
++;
470 dest
->disk2
= *disks
;
471 dest
->raid_level
= 1;
473 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
476 // Is this a RAID device?
477 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
479 int r
= hw_calculate_partition_table(dest
);
483 // Set default filesystem
484 dest
->filesystem
= HW_FS_DEFAULT
;
489 unsigned long long hw_memory() {
491 char line
[STRING_SIZE
];
493 unsigned long long memory
= 0;
495 /* Calculate amount of memory in machine */
496 if ((handle
= fopen("/proc/meminfo", "r"))) {
497 while (fgets(line
, sizeof(line
), handle
)) {
498 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
506 return memory
* 1024;
509 static int hw_zero_out_device(const char* path
, int bytes
) {
511 memset(block
, 0, sizeof(block
));
513 int blocks
= bytes
/ sizeof(block
);
515 int fd
= open(path
, O_WRONLY
);
519 unsigned int bytes_written
= 0;
520 while (blocks
-- > 0) {
521 bytes_written
+= write(fd
, block
, sizeof(block
));
527 return bytes_written
;
530 static int try_open(const char* path
) {
531 FILE* f
= fopen(path
, "r");
540 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
541 // Before we write a new partition table to the disk, we will erase
542 // the first couple of megabytes at the beginning of the device to
543 // get rid of all left other things like bootloaders and partition tables.
544 // This solves some problems when changing from MBR to GPT partitions or
545 // the other way around.
546 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
551 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
553 // Set partition type
554 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
555 asprintf(&cmd
, "%s mklabel msdos", cmd
);
556 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
557 asprintf(&cmd
, "%s mklabel gpt", cmd
);
559 unsigned long long part_start
= MB2BYTES(1);
561 if (*dest
->part_bootldr
) {
562 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
563 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
564 part_start
, part_start
+ dest
->size_bootldr
- 1);
566 part_start
+= dest
->size_bootldr
;
569 if (*dest
->part_boot
) {
570 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
571 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
572 part_start
, part_start
+ dest
->size_boot
- 1);
574 part_start
+= dest
->size_boot
;
577 if (*dest
->part_swap
) {
578 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
579 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
580 part_start
, part_start
+ dest
->size_swap
- 1);
582 part_start
+= dest
->size_swap
;
585 if (*dest
->part_root
) {
586 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
587 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
588 part_start
, part_start
+ dest
->size_root
- 1);
590 part_start
+= dest
->size_root
;
593 if (*dest
->part_data
) {
594 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
595 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
596 part_start
, part_start
+ dest
->size_data
- 1);
598 part_start
+= dest
->size_data
;
601 if (dest
->part_boot_idx
> 0)
602 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
604 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
605 if (*dest
->part_bootldr
) {
606 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
608 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
611 r
= mysystem(output
, cmd
);
613 // Wait until the system re-read the partition table
615 unsigned int counter
= 10;
617 while (counter
-- > 0) {
620 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
623 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
626 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
629 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
632 if (*dest
->part_data
&& (try_open(dest
->part_data
) != 0))
635 // All partitions do exist, exiting the loop.
646 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
647 char cmd
[STRING_SIZE
] = "\0";
650 if (fs
== HW_FS_SWAP
) {
651 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
653 } else if (fs
== HW_FS_REISERFS
) {
654 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
657 } else if (fs
== HW_FS_EXT4
) {
658 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
661 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
662 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
665 } else if (fs
== HW_FS_XFS
) {
666 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
671 int r
= mysystem(output
, cmd
);
676 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
680 if (*dest
->part_boot
) {
681 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
687 if (*dest
->part_swap
) {
688 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
694 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
699 if (*dest
->part_data
) {
700 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
, output
);
708 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
709 char target
[STRING_SIZE
];
711 assert(*prefix
== '/');
713 const char* filesystem
;
714 switch (dest
->filesystem
) {
716 filesystem
= "reiserfs";
720 case HW_FS_EXT4_WO_JOURNAL
:
733 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
738 if (*dest
->part_boot
) {
739 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
740 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
742 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
744 hw_umount_filesystems(dest
, prefix
);
751 if (*dest
->part_data
) {
752 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
753 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
755 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
757 hw_umount_filesystems(dest
, prefix
);
764 if (*dest
->part_swap
) {
765 r
= swapon(dest
->part_swap
, 0);
767 hw_umount_filesystems(dest
, prefix
);
773 // bind-mount misc filesystems
774 char** otherfs
= other_filesystems
;
776 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
778 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
779 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
781 hw_umount_filesystems(dest
, prefix
);
792 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
793 // Write all buffers to disk before umounting
797 if (*dest
->part_boot
) {
798 hw_umount(dest
->part_boot
);
802 if (*dest
->part_data
) {
803 hw_umount(dest
->part_data
);
807 hw_umount(dest
->part_root
);
810 if (*dest
->part_swap
) {
811 swapoff(dest
->part_swap
);
815 char target
[STRING_SIZE
];
816 char** otherfs
= other_filesystems
;
819 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
826 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
827 char cmd
[STRING_SIZE
];
829 hw_stop_all_raid_arrays(output
);
830 hw_stop_all_raid_arrays(output
);
833 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
834 mysystem(output
, cmd
);
838 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
839 mysystem(output
, cmd
);
845 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
849 assert(dest
->is_raid
);
851 // Stop all RAID arrays that might be around (again).
852 // It seems that there is some sort of race-condition with udev re-enabling
853 // the raid arrays and therefore locking the disks.
854 r
= hw_destroy_raid_superblocks(dest
, output
);
856 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
857 RAID_METADATA
, dest
->path
);
859 switch (dest
->raid_level
) {
861 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
869 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
871 // Clear all data at the beginning
872 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
878 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
880 // Clear all data at the beginning
881 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
886 r
= mysystem(output
, cmd
);
889 // Wait a moment until the device has been properly brought up
891 unsigned int counter
= 10;
892 while (counter
-- > 0) {
895 // If the raid device has not yet been properly brought up,
896 // opening it will fail with the message: Device or resource busy
897 // Hence we will wait a bit until it becomes usable.
898 if (try_open(dest
->path
) == 0)
906 int hw_stop_all_raid_arrays(const char* output
) {
907 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
910 int hw_install_bootloader(struct hw_destination
* dest
, const char* output
) {
911 char cmd
[STRING_SIZE
];
914 // Generate configuration file
915 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
916 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
920 char cmd_grub
[STRING_SIZE
];
921 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
924 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
925 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
929 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
930 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
932 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
933 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
939 static char* hw_get_uuid(const char* dev
) {
940 blkid_probe p
= blkid_new_probe_from_filename(dev
);
941 const char* buffer
= NULL
;
948 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
951 uuid
= strdup(buffer
);
958 int hw_write_fstab(struct hw_destination
* dest
) {
959 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
963 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
967 if (*dest
->part_boot
) {
968 uuid
= hw_get_uuid(dest
->part_boot
);
971 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
977 if (*dest
->part_swap
) {
978 uuid
= hw_get_uuid(dest
->part_swap
);
981 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
987 uuid
= hw_get_uuid(dest
->part_root
);
989 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
994 if (*dest
->part_data
) {
995 uuid
= hw_get_uuid(dest
->part_data
);
998 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);