]>
git.ipfire.org Git - people/mfischer/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>
29 #include <linux/loop.h>
34 #include <sys/ioctl.h>
35 #include <sys/mount.h>
38 #include <sys/sysinfo.h>
43 #include <libsmooth.h>
47 const char* other_filesystems
[] = {
54 static int system_chroot(const char* output
, const char* path
, const char* cmd
) {
55 char chroot_cmd
[STRING_SIZE
];
57 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
59 return mysystem(output
, chroot_cmd
);
62 struct hw
* hw_init() {
63 struct hw
* hw
= malloc(sizeof(*hw
));
67 hw
->udev
= udev_new();
69 fprintf(stderr
, "Could not create udev instance\n");
76 void hw_free(struct hw
* hw
) {
83 static int strstartswith(const char* a
, const char* b
) {
84 return (strncmp(a
, b
, strlen(b
)) == 0);
87 static char loop_device
[STRING_SIZE
];
89 static int setup_loop_device(const char* source
, const char* device
) {
90 int file_fd
= open(source
, O_RDWR
);
95 if ((device_fd
= open(device
, O_RDWR
)) < 0)
98 if (ioctl(device_fd
, LOOP_SET_FD
, file_fd
) < 0)
110 if (device_fd
>= 0) {
111 ioctl(device_fd
, LOOP_CLR_FD
, 0);
118 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
119 const char* loop_device
= "/dev/loop0";
121 // Create target if it does not exist
122 if (access(target
, X_OK
) != 0)
123 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
128 if (S_ISREG(st
.st_mode
)) {
129 int r
= setup_loop_device(source
, loop_device
);
131 source
= loop_device
;
137 return mount(source
, target
, fs
, flags
, NULL
);
140 int hw_umount(const char* target
) {
141 return umount2(target
, 0);
144 static int hw_test_source_medium(const char* path
) {
145 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
147 // If the source could not be mounted we
152 // Check if the test file exists.
153 ret
= access(SOURCE_TEST_FILE
, R_OK
);
155 // Umount the test device.
156 hw_umount(SOURCE_MOUNT_PATH
);
161 char* hw_find_source_medium(struct hw
* hw
) {
164 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
166 udev_enumerate_add_match_subsystem(enumerate
, "block");
167 udev_enumerate_scan_devices(enumerate
);
169 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
171 struct udev_list_entry
* dev_list_entry
;
172 udev_list_entry_foreach(dev_list_entry
, devices
) {
173 const char* path
= udev_list_entry_get_name(dev_list_entry
);
174 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
176 const char* dev_path
= udev_device_get_devnode(dev
);
178 // Skip everything what we cannot work with
179 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
180 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
183 if (hw_test_source_medium(dev_path
) == 0) {
184 ret
= strdup(dev_path
);
187 udev_device_unref(dev
);
189 // If a suitable device was found the search will end.
194 udev_enumerate_unref(enumerate
);
199 static struct hw_disk
** hw_create_disks() {
200 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
205 static unsigned long long hw_block_device_get_size(const char* dev
) {
206 int fd
= open(dev
, O_RDONLY
);
210 unsigned long long size
= blkid_get_dev_size(fd
);
216 struct hw_disk
** hw_find_disks(struct hw
* hw
, const char* sourcedrive
) {
217 struct hw_disk
** ret
= hw_create_disks();
218 struct hw_disk
** disks
= ret
;
220 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
222 udev_enumerate_add_match_subsystem(enumerate
, "block");
223 udev_enumerate_scan_devices(enumerate
);
225 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
227 struct udev_list_entry
* dev_list_entry
;
228 unsigned int i
= HW_MAX_DISKS
;
229 udev_list_entry_foreach(dev_list_entry
, devices
) {
230 const char* path
= udev_list_entry_get_name(dev_list_entry
);
231 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
233 const char* dev_path
= udev_device_get_devnode(dev
);
235 // Skip everything what we cannot work with
236 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
237 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
238 strstartswith(dev_path
, "/dev/md")) {
239 udev_device_unref(dev
);
243 // Skip sourcedrive if we need to
244 if (sourcedrive
&& (strcmp(dev_path
, sourcedrive
) == 0)) {
245 udev_device_unref(dev
);
249 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
250 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
251 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
252 udev_device_unref(dev
);
256 // Skip devices with a size of zero
257 unsigned long long size
= hw_block_device_get_size(dev_path
);
259 udev_device_unref(dev
);
263 struct hw_disk
* disk
= malloc(sizeof(*disk
));
269 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
270 const char* p
= disk
->path
+ 5;
275 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
277 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
279 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
282 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
284 *disk
->vendor
= '\0';
287 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
289 model
= udev_device_get_sysattr_value(dev
, "model");
291 model
= udev_device_get_sysattr_value(dev
, "product");
294 strncpy(disk
->model
, model
, sizeof(disk
->model
));
298 // Format description
299 char size_str
[STRING_SIZE
];
300 snprintf(size_str
, sizeof(size_str
), "%4.1fGB", (double)disk
->size
/ pow(1024, 3));
302 if (*disk
->vendor
&& *disk
->model
) {
303 snprintf(disk
->description
, sizeof(disk
->description
),
304 "%s - %s - %s - %s", size_str
, p
, disk
->vendor
, disk
->model
);
306 } else if (*disk
->vendor
|| *disk
->model
) {
307 snprintf(disk
->description
, sizeof(disk
->description
),
308 "%s - %s - %s", size_str
, p
, (*disk
->vendor
) ? disk
->vendor
: disk
->model
);
311 snprintf(disk
->description
, sizeof(disk
->description
),
312 "%s - %s", size_str
, p
);
320 udev_device_unref(dev
);
323 udev_enumerate_unref(enumerate
);
330 void hw_free_disks(struct hw_disk
** disks
) {
331 struct hw_disk
** disk
= disks
;
333 while (*disk
!= NULL
) {
334 if (--(*disk
)->ref
== 0)
343 unsigned int hw_count_disks(const struct hw_disk
** disks
) {
344 unsigned int ret
= 0;
352 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
353 struct hw_disk
** ret
= hw_create_disks();
354 struct hw_disk
** selected_disks
= ret
;
356 unsigned int num_disks
= hw_count_disks((const struct hw_disk
**)disks
);
358 for (unsigned int i
= 0; i
< num_disks
; i
++) {
359 if (!selection
|| selection
[i
]) {
360 struct hw_disk
*selected_disk
= disks
[i
];
361 selected_disk
->ref
++;
363 *selected_disks
++ = selected_disk
;
368 *selected_disks
= NULL
;
373 struct hw_disk
** hw_select_first_disk(const struct hw_disk
** disks
) {
374 struct hw_disk
** ret
= hw_create_disks();
375 struct hw_disk
** selected_disks
= ret
;
377 unsigned int num_disks
= hw_count_disks(disks
);
378 assert(num_disks
> 0);
380 for (unsigned int i
= 0; i
< num_disks
; i
++) {
381 struct hw_disk
*disk
= disks
[i
];
384 *selected_disks
++ = disk
;
389 *selected_disks
= NULL
;
394 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
395 unsigned long long memory
= hw_memory();
397 unsigned long long swap_size
= memory
/ 4;
399 // Min. swap size is 128MB
400 if (swap_size
< MB2BYTES(128))
401 swap_size
= MB2BYTES(128);
403 // Cap swap size to 1GB
404 else if (swap_size
> MB2BYTES(1024))
405 swap_size
= MB2BYTES(1024);
410 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
411 unsigned long long root_size
;
413 if (dest
->size
< MB2BYTES(2048))
414 root_size
= MB2BYTES(1024);
416 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
417 root_size
= MB2BYTES(1536);
420 root_size
= MB2BYTES(2048);
425 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
429 static int hw_device_has_p_suffix(const struct hw_destination
* dest
) {
430 // All RAID devices have the p suffix.
434 // Devices with a number at the end have the p suffix, too.
435 // e.g. mmcblk0, cciss0
436 unsigned int last_char
= strlen(dest
->path
) - 1;
437 if ((dest
->path
[last_char
] >= '0') && (dest
->path
[last_char
] <= '9'))
443 static int hw_calculate_partition_table(struct hw_destination
* dest
, int disable_swap
) {
447 snprintf(path
, sizeof(path
), "%s%s", dest
->path
,
448 hw_device_has_p_suffix(dest
) ? "p" : "");
449 dest
->part_boot_idx
= 0;
451 // Determine the size of the target block device
453 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
454 dest
->disk2
->size
: dest
->disk1
->size
;
456 // The RAID will install some metadata at the end of the disk
457 // and we will save up some space for that.
458 dest
->size
-= MB2BYTES(2);
460 dest
->size
= dest
->disk1
->size
;
463 // As we add some extra space before the beginning of the first
464 // partition, we need to substract that here.
465 dest
->size
-= MB2BYTES(1);
467 // Add some more space for partition tables, etc.
468 dest
->size
-= MB2BYTES(1);
470 // Determine partition table
471 dest
->part_table
= HW_PART_TABLE_MSDOS
;
473 // Disks over 2TB need to use GPT
474 if (dest
->size
>= MB2BYTES(2047 * 1024))
475 dest
->part_table
= HW_PART_TABLE_GPT
;
477 // We also use GPT on raid disks by default
478 else if (dest
->is_raid
)
479 dest
->part_table
= HW_PART_TABLE_GPT
;
481 // When using GPT, GRUB2 needs a little bit of space to put
483 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
484 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
485 "%s%d", path
, part_idx
);
487 dest
->size_bootldr
= MB2BYTES(4);
489 dest
->part_boot_idx
= part_idx
++;
491 *dest
->part_bootldr
= '\0';
492 dest
->size_bootldr
= 0;
495 dest
->size_boot
= hw_boot_size(dest
);
496 dest
->size_root
= hw_root_size(dest
);
498 // Should we use swap?
502 dest
->size_swap
= hw_swap_size(dest
);
504 // Determine the size of the data partition.
505 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
506 + dest
->size_swap
+ dest
->size_root
;
508 // Disk is way too small
509 if (used_space
>= dest
->size
)
512 dest
->size_data
= dest
->size
- used_space
;
514 // If it gets too small, we remove the swap space.
515 if (dest
->size_data
<= MB2BYTES(256)) {
516 dest
->size_data
+= dest
->size_swap
;
520 // Set partition names
521 if (dest
->size_boot
> 0) {
522 if (dest
->part_boot_idx
== 0)
523 dest
->part_boot_idx
= part_idx
;
525 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
527 *dest
->part_boot
= '\0';
529 if (dest
->size_swap
> 0)
530 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
532 *dest
->part_swap
= '\0';
534 // There is always a root partition
535 if (dest
->part_boot_idx
== 0)
536 dest
->part_boot_idx
= part_idx
;
538 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
540 if (dest
->size_data
> 0)
541 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
543 *dest
->part_data
= '\0';
548 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
, int disable_swap
) {
549 struct hw_destination
* dest
= malloc(sizeof(*dest
));
551 if (part_type
== HW_PART_TYPE_NORMAL
) {
552 dest
->disk1
= *disks
;
555 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
557 } else if (part_type
== HW_PART_TYPE_RAID1
) {
558 dest
->disk1
= *disks
++;
559 dest
->disk2
= *disks
;
560 dest
->raid_level
= 1;
562 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
565 // Is this a RAID device?
566 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
568 int r
= hw_calculate_partition_table(dest
, disable_swap
);
572 // Set default filesystem
573 dest
->filesystem
= HW_FS_DEFAULT
;
578 unsigned long long hw_memory() {
581 int r
= sysinfo(&si
);
588 static int hw_zero_out_device(const char* path
, int bytes
) {
590 memset(block
, 0, sizeof(block
));
592 int blocks
= bytes
/ sizeof(block
);
594 int fd
= open(path
, O_WRONLY
);
598 unsigned int bytes_written
= 0;
599 while (blocks
-- > 0) {
600 bytes_written
+= write(fd
, block
, sizeof(block
));
606 return bytes_written
;
609 static int try_open(const char* path
) {
610 FILE* f
= fopen(path
, "r");
619 int hw_create_partitions(struct hw_destination
* dest
, const char* output
) {
620 // Before we write a new partition table to the disk, we will erase
621 // the first couple of megabytes at the beginning of the device to
622 // get rid of all left other things like bootloaders and partition tables.
623 // This solves some problems when changing from MBR to GPT partitions or
624 // the other way around.
625 int r
= hw_zero_out_device(dest
->path
, MB2BYTES(10));
630 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
632 // Set partition type
633 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
634 asprintf(&cmd
, "%s mklabel msdos", cmd
);
635 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
636 asprintf(&cmd
, "%s mklabel gpt", cmd
);
638 unsigned long long part_start
= MB2BYTES(1);
640 if (*dest
->part_bootldr
) {
641 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
642 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
643 part_start
, part_start
+ dest
->size_bootldr
- 1);
645 part_start
+= dest
->size_bootldr
;
648 if (*dest
->part_boot
) {
649 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
650 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
651 part_start
, part_start
+ dest
->size_boot
- 1);
653 part_start
+= dest
->size_boot
;
656 if (*dest
->part_swap
) {
657 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
658 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
659 part_start
, part_start
+ dest
->size_swap
- 1);
661 part_start
+= dest
->size_swap
;
664 if (*dest
->part_root
) {
665 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
666 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
667 part_start
, part_start
+ dest
->size_root
- 1);
669 part_start
+= dest
->size_root
;
672 if (*dest
->part_data
) {
673 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
674 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
675 part_start
, part_start
+ dest
->size_data
- 1);
677 part_start
+= dest
->size_data
;
680 if (dest
->part_boot_idx
> 0)
681 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
683 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
684 if (*dest
->part_bootldr
) {
685 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
687 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
690 r
= mysystem(output
, cmd
);
692 // Wait until the system re-read the partition table
694 unsigned int counter
= 10;
696 while (counter
-- > 0) {
699 if (*dest
->part_bootldr
&& (try_open(dest
->part_bootldr
) != 0))
702 if (*dest
->part_boot
&& (try_open(dest
->part_boot
) != 0))
705 if (*dest
->part_swap
&& (try_open(dest
->part_swap
) != 0))
708 if (*dest
->part_root
&& (try_open(dest
->part_root
) != 0))
711 if (*dest
->part_data
&& (try_open(dest
->part_data
) != 0))
714 // All partitions do exist, exiting the loop.
725 static int hw_format_filesystem(const char* path
, int fs
, const char* output
) {
726 char cmd
[STRING_SIZE
] = "\0";
729 if (fs
== HW_FS_SWAP
) {
730 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
732 } else if (fs
== HW_FS_REISERFS
) {
733 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
736 } else if (fs
== HW_FS_EXT4
) {
737 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
740 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
741 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
744 } else if (fs
== HW_FS_XFS
) {
745 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
750 int r
= mysystem(output
, cmd
);
755 int hw_create_filesystems(struct hw_destination
* dest
, const char* output
) {
759 if (*dest
->part_boot
) {
760 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
, output
);
766 if (*dest
->part_swap
) {
767 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
, output
);
773 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
, output
);
778 if (*dest
->part_data
) {
779 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
, output
);
787 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
788 char target
[STRING_SIZE
];
790 assert(*prefix
== '/');
792 const char* filesystem
;
793 switch (dest
->filesystem
) {
795 filesystem
= "reiserfs";
799 case HW_FS_EXT4_WO_JOURNAL
:
812 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
817 if (*dest
->part_boot
) {
818 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
819 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
821 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
823 hw_umount_filesystems(dest
, prefix
);
830 if (*dest
->part_data
) {
831 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
832 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
834 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
836 hw_umount_filesystems(dest
, prefix
);
843 if (*dest
->part_swap
) {
844 r
= swapon(dest
->part_swap
, 0);
846 hw_umount_filesystems(dest
, prefix
);
852 // bind-mount misc filesystems
853 char** otherfs
= other_filesystems
;
855 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
857 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
858 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
860 hw_umount_filesystems(dest
, prefix
);
871 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
872 // Write all buffers to disk before umounting
876 if (*dest
->part_boot
) {
877 hw_umount(dest
->part_boot
);
881 if (*dest
->part_data
) {
882 hw_umount(dest
->part_data
);
886 hw_umount(dest
->part_root
);
889 if (*dest
->part_swap
) {
890 swapoff(dest
->part_swap
);
894 char target
[STRING_SIZE
];
895 char** otherfs
= other_filesystems
;
898 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
905 int hw_destroy_raid_superblocks(const struct hw_destination
* dest
, const char* output
) {
906 char cmd
[STRING_SIZE
];
908 hw_stop_all_raid_arrays(output
);
909 hw_stop_all_raid_arrays(output
);
912 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk1
->path
);
913 mysystem(output
, cmd
);
917 snprintf(cmd
, sizeof(cmd
), "/sbin/mdadm --zero-superblock %s", dest
->disk2
->path
);
918 mysystem(output
, cmd
);
924 int hw_setup_raid(struct hw_destination
* dest
, const char* output
) {
928 assert(dest
->is_raid
);
930 // Stop all RAID arrays that might be around (again).
931 // It seems that there is some sort of race-condition with udev re-enabling
932 // the raid arrays and therefore locking the disks.
933 r
= hw_destroy_raid_superblocks(dest
, output
);
935 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
936 RAID_METADATA
, dest
->path
);
938 switch (dest
->raid_level
) {
940 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
948 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
950 // Clear all data at the beginning
951 r
= hw_zero_out_device(dest
->disk1
->path
, MB2BYTES(10));
957 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
959 // Clear all data at the beginning
960 r
= hw_zero_out_device(dest
->disk2
->path
, MB2BYTES(10));
965 r
= mysystem(output
, cmd
);
968 // Wait a moment until the device has been properly brought up
970 unsigned int counter
= 10;
971 while (counter
-- > 0) {
974 // If the raid device has not yet been properly brought up,
975 // opening it will fail with the message: Device or resource busy
976 // Hence we will wait a bit until it becomes usable.
977 if (try_open(dest
->path
) == 0)
985 int hw_stop_all_raid_arrays(const char* output
) {
986 return mysystem(output
, "/sbin/mdadm --stop --scan --verbose");
989 int hw_install_bootloader(struct hw_destination
* dest
, const char* output
) {
990 char cmd
[STRING_SIZE
];
993 // Generate configuration file
994 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
995 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
999 char cmd_grub
[STRING_SIZE
];
1000 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
1002 if (dest
->is_raid
) {
1003 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
1004 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1008 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
1009 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1011 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
1012 r
= system_chroot(output
, DESTINATION_MOUNT_PATH
, cmd
);
1018 static char* hw_get_uuid(const char* dev
) {
1019 blkid_probe p
= blkid_new_probe_from_filename(dev
);
1020 const char* buffer
= NULL
;
1027 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
1030 uuid
= strdup(buffer
);
1032 blkid_free_probe(p
);
1037 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1039 int hw_write_fstab(struct hw_destination
* dest
) {
1040 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
1047 if (*dest
->part_boot
) {
1048 uuid
= hw_get_uuid(dest
->part_boot
);
1051 fprintf(f
, FSTAB_FMT
, uuid
, "/boot", "auto", "defaults", 1, 2);
1057 if (*dest
->part_swap
) {
1058 uuid
= hw_get_uuid(dest
->part_swap
);
1061 fprintf(f
, FSTAB_FMT
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
1067 uuid
= hw_get_uuid(dest
->part_root
);
1069 fprintf(f
, FSTAB_FMT
, uuid
, "/", "auto", "defaults", 1, 1);
1074 if (*dest
->part_data
) {
1075 uuid
= hw_get_uuid(dest
->part_data
);
1078 fprintf(f
, FSTAB_FMT
, uuid
, "/var", "auto", "defaults", 1, 1);
1094 int hw_start_networking(const char* output
) {
1095 return mysystem(output
, "/usr/bin/start-networking.sh");
1098 char* hw_find_backup_file(const char* output
, const char* search_path
) {
1099 char path
[STRING_SIZE
];
1101 snprintf(path
, sizeof(path
), "%s/backup.ipf", search_path
);
1102 int r
= access(path
, R_OK
);
1105 return strdup(path
);
1110 int hw_restore_backup(const char* output
, const char* backup_path
, const char* destination
) {
1111 char command
[STRING_SIZE
];
1113 snprintf(command
, sizeof(command
), "/bin/tar xzpf %s -C %s", backup_path
, destination
);
1114 int rc
= mysystem(output
, command
);