]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/install/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>
41 #include "../libsmooth/libsmooth.h"
43 const char* other_filesystems
[] = {
50 static int system_chroot(const char* path
, const char* cmd
) {
51 char chroot_cmd
[STRING_SIZE
];
53 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
55 return mysystem(chroot_cmd
);
58 struct hw
* hw_init() {
59 struct hw
* hw
= malloc(sizeof(*hw
));
63 hw
->udev
= udev_new();
65 fprintf(stderr
, "Could not create udev instance\n");
72 void hw_free(struct hw
* hw
) {
79 static int strstartswith(const char* a
, const char* b
) {
80 return (strncmp(a
, b
, strlen(b
)) == 0);
83 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
84 return mount(source
, target
, fs
, flags
, NULL
);
87 int hw_umount(const char* target
) {
88 return umount2(target
, MNT_DETACH
);
91 static int hw_test_source_medium(const char* path
) {
92 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
94 // If the source could not be mounted we
99 // Check if the test file exists.
100 ret
= access(SOURCE_TEST_FILE
, F_OK
);
102 // Umount the test device.
103 hw_umount(SOURCE_MOUNT_PATH
);
108 char* hw_find_source_medium(struct hw
* hw
) {
111 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
113 udev_enumerate_add_match_subsystem(enumerate
, "block");
114 udev_enumerate_scan_devices(enumerate
);
116 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
118 struct udev_list_entry
* dev_list_entry
;
119 udev_list_entry_foreach(dev_list_entry
, devices
) {
120 const char* path
= udev_list_entry_get_name(dev_list_entry
);
121 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
123 const char* dev_path
= udev_device_get_devnode(dev
);
125 // Skip everything what we cannot work with
126 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
127 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
130 if (hw_test_source_medium(dev_path
)) {
131 ret
= strdup(dev_path
);
134 udev_device_unref(dev
);
136 // If a suitable device was found the search will end.
141 udev_enumerate_unref(enumerate
);
146 static struct hw_disk
** hw_create_disks() {
147 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
152 static unsigned long long hw_block_device_get_size(const char* dev
) {
153 int fd
= open(dev
, O_RDONLY
);
157 unsigned long long size
= blkid_get_dev_size(fd
);
163 struct hw_disk
** hw_find_disks(struct hw
* hw
) {
164 struct hw_disk
** ret
= hw_create_disks();
165 struct hw_disk
** disks
= ret
;
167 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
169 udev_enumerate_add_match_subsystem(enumerate
, "block");
170 udev_enumerate_scan_devices(enumerate
);
172 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
174 struct udev_list_entry
* dev_list_entry
;
175 unsigned int i
= HW_MAX_DISKS
;
176 udev_list_entry_foreach(dev_list_entry
, devices
) {
177 const char* path
= udev_list_entry_get_name(dev_list_entry
);
178 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
180 const char* dev_path
= udev_device_get_devnode(dev
);
182 // Skip everything what we cannot work with
183 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
184 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
185 strstartswith(dev_path
, "/dev/md")) {
186 udev_device_unref(dev
);
190 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
191 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
192 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
193 udev_device_unref(dev
);
197 // Skip all source mediums
198 if (hw_test_source_medium(dev_path
) == 0) {
199 udev_device_unref(dev
);
203 // Skip devices with a size of zero
204 unsigned long long size
= hw_block_device_get_size(dev_path
);
206 udev_device_unref(dev
);
210 struct hw_disk
* disk
= malloc(sizeof(*disk
));
216 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
221 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
223 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
225 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
229 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
232 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
234 model
= udev_device_get_sysattr_value(dev
, "model");
236 model
= udev_device_get_sysattr_value(dev
, "product");
240 strncpy(disk
->model
, model
, sizeof(disk
->model
));
242 snprintf(disk
->description
, sizeof(disk
->description
),
243 "%4.1fGB %s - %s", (double)disk
->size
/ pow(1024, 3),
244 disk
->vendor
, disk
->model
);
251 udev_device_unref(dev
);
254 udev_enumerate_unref(enumerate
);
261 void hw_free_disks(struct hw_disk
** disks
) {
262 struct hw_disk
** disk
= disks
;
264 while (*disk
!= NULL
) {
265 if (--(*disk
)->ref
== 0)
274 unsigned int hw_count_disks(struct hw_disk
** disks
) {
275 unsigned int ret
= 0;
283 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
284 struct hw_disk
** ret
= hw_create_disks();
285 struct hw_disk
** selected_disks
= ret
;
287 unsigned int num_disks
= hw_count_disks(disks
);
289 for (unsigned int i
= 0; i
< num_disks
; i
++) {
290 if (selection
&& selection
[i
]) {
291 struct hw_disk
*selected_disk
= disks
[i
];
292 selected_disk
->ref
++;
294 *selected_disks
++ = selected_disk
;
299 *selected_disks
= NULL
;
304 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
305 unsigned long long memory
= hw_memory();
307 unsigned long long swap_size
= memory
/ 4;
309 // Min. swap size is 128MB
310 if (swap_size
< MB2BYTES(128))
311 swap_size
= MB2BYTES(128);
313 // Cap swap size to 1GB
314 else if (swap_size
> MB2BYTES(1024))
315 swap_size
= MB2BYTES(1024);
320 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
321 unsigned long long root_size
;
323 if (dest
->size
< MB2BYTES(2048))
324 root_size
= MB2BYTES(1024);
326 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
327 root_size
= MB2BYTES(1536);
330 root_size
= MB2BYTES(2048);
335 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
339 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
343 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
344 dest
->part_boot_idx
= 0;
346 // Determine the size of the target block device
348 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
349 dest
->disk1
->size
: dest
->disk2
->size
;
351 // The RAID will install some metadata at the end of the disk
352 // and we will save up some space for that.
353 dest
->size
-= MB2BYTES(2);
355 dest
->size
= dest
->disk1
->size
;
358 // Determine partition table
359 dest
->part_table
= HW_PART_TABLE_MSDOS
;
361 // Disks over 2TB need to use GPT
362 if (dest
->size
>= MB2BYTES(2047 * 1024))
363 dest
->part_table
= HW_PART_TABLE_GPT
;
365 // We also use GPT on raid disks by default
366 else if (dest
->is_raid
)
367 dest
->part_table
= HW_PART_TABLE_GPT
;
369 // When using GPT, GRUB2 needs a little bit of space to put
371 if (dest
->part_table
= HW_PART_TABLE_GPT
) {
372 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
373 "%s%d", path
, part_idx
);
375 dest
->size_bootldr
= MB2BYTES(4);
377 dest
->part_boot_idx
= part_idx
++;
379 *dest
->part_bootldr
= '\0';
380 dest
->size_bootldr
= 0;
383 dest
->size_boot
= hw_boot_size(dest
);
384 dest
->size_swap
= hw_swap_size(dest
);
385 dest
->size_root
= hw_root_size(dest
);
387 // Determine the size of the data partition.
388 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
389 + dest
->size_swap
+ dest
->size_root
;
391 // Disk is way too small
392 if (used_space
>= dest
->size
)
395 dest
->size_data
= dest
->size
- used_space
;
397 // If it gets too small, we remove the swap space.
398 if (dest
->size_data
<= MB2BYTES(256)) {
399 dest
->size_data
+= dest
->size_swap
;
403 // Set partition names
404 if (dest
->size_boot
> 0) {
405 if (dest
->part_boot_idx
== 0)
406 dest
->part_boot_idx
= part_idx
;
408 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
410 *dest
->part_boot
= '\0';
412 if (dest
->size_swap
> 0)
413 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
415 *dest
->part_swap
= '\0';
417 // There is always a root partition
418 if (dest
->part_boot_idx
== 0)
419 dest
->part_boot_idx
= part_idx
;
421 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
423 if (dest
->size_data
> 0)
424 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
426 *dest
->part_data
= '\0';
431 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
432 struct hw_destination
* dest
= malloc(sizeof(*dest
));
434 if (part_type
== HW_PART_TYPE_NORMAL
) {
435 dest
->disk1
= *disks
;
438 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
440 } else if (part_type
== HW_PART_TYPE_RAID1
) {
441 dest
->disk1
= *disks
++;
442 dest
->disk2
= *disks
;
443 dest
->raid_level
= 1;
445 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
448 // Is this a RAID device?
449 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
451 int r
= hw_calculate_partition_table(dest
);
455 // Set default filesystem
456 dest
->filesystem
= HW_FS_DEFAULT
;
461 unsigned long long hw_memory() {
463 char line
[STRING_SIZE
];
465 unsigned long long memory
= 0;
467 /* Calculate amount of memory in machine */
468 if ((handle
= fopen("/proc/meminfo", "r"))) {
469 while (fgets(line
, sizeof(line
), handle
)) {
470 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
478 return memory
* 1024;
481 int hw_create_partitions(struct hw_destination
* dest
) {
484 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
486 // Set partition type
487 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
488 asprintf(&cmd
, "%s mklabel msdos", cmd
);
489 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
490 asprintf(&cmd
, "%s mklabel gpt", cmd
);
492 unsigned long long part_start
= 1 * 1024 * 1024; // 1MB
494 if (*dest
->part_bootldr
) {
495 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
496 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
497 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_bootldr
));
499 part_start
+= dest
->size_bootldr
;
502 if (*dest
->part_boot
) {
503 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
504 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
505 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_boot
));
507 part_start
+= dest
->size_boot
;
510 if (*dest
->part_swap
) {
511 asprintf(&cmd
, "%s mkpart %s linux-swap %lluMB %lluMB", cmd
,
512 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
513 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_swap
));
515 part_start
+= dest
->size_swap
;
518 if (*dest
->part_root
) {
519 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
520 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
521 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_root
));
523 part_start
+= dest
->size_root
;
526 if (*dest
->part_data
) {
527 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
528 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
529 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_data
));
531 part_start
+= dest
->size_data
;
534 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
535 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
537 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
538 if (*dest
->part_bootldr
) {
539 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
541 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
544 int r
= mysystem(cmd
);
546 // Wait until the system re-read the partition table
548 unsigned int counter
= 10;
550 while (counter
-- > 0) {
553 if (*dest
->part_bootldr
&& (access(dest
->part_bootldr
, R_OK
) != 0))
556 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
559 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
562 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
565 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
568 // All partitions do exist, exiting the loop.
579 static int hw_format_filesystem(const char* path
, int fs
) {
580 char cmd
[STRING_SIZE
] = "\0";
583 if (fs
== HW_FS_SWAP
) {
584 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
586 } else if (fs
== HW_FS_REISERFS
) {
587 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
590 } else if (fs
== HW_FS_EXT4
) {
591 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
594 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
595 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
600 int r
= mysystem(cmd
);
605 int hw_create_filesystems(struct hw_destination
* dest
) {
609 if (*dest
->part_boot
) {
610 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
616 if (*dest
->part_swap
) {
617 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
623 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
628 if (*dest
->part_data
) {
629 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
637 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
638 char target
[STRING_SIZE
];
640 assert(*prefix
== '/');
642 const char* filesystem
;
643 switch (dest
->filesystem
) {
645 filesystem
= "reiserfs";
649 case HW_FS_EXT4_WO_JOURNAL
:
658 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
663 if (*dest
->part_boot
) {
664 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
665 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
667 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
669 hw_umount_filesystems(dest
, prefix
);
676 if (*dest
->part_data
) {
677 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
678 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
680 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
682 hw_umount_filesystems(dest
, prefix
);
689 if (*dest
->part_swap
) {
690 r
= swapon(dest
->part_swap
, 0);
692 hw_umount_filesystems(dest
, prefix
);
698 // bind-mount misc filesystems
699 char** otherfs
= other_filesystems
;
701 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
703 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
704 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
706 hw_umount_filesystems(dest
, prefix
);
717 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
719 if (*dest
->part_boot
) {
720 hw_umount(dest
->part_boot
);
724 if (*dest
->part_data
) {
725 hw_umount(dest
->part_data
);
729 hw_umount(dest
->part_root
);
732 if (*dest
->part_swap
) {
733 swapoff(dest
->part_swap
);
737 char target
[STRING_SIZE
];
738 char** otherfs
= other_filesystems
;
741 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
748 int hw_setup_raid(struct hw_destination
* dest
) {
751 assert(dest
->is_raid
);
753 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest
->path
);
755 switch (dest
->raid_level
) {
757 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
765 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
769 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
772 int r
= mysystem(cmd
);
775 // Wait a moment until the device has been properly brought up
777 unsigned int counter
= 10;
778 while (counter
-- > 0) {
781 // If the raid device has not yet been properly brought up,
782 // opening it will fail with the message: Device or resource busy
783 // Hence we will wait a bit until it becomes usable.
784 FILE* f
= fopen(dest
->path
, "r");
795 int hw_stop_all_raid_arrays() {
796 return mysystem("/sbin/mdadm --stop --scan");
799 int hw_install_bootloader(struct hw_destination
* dest
) {
800 char cmd
[STRING_SIZE
];
803 // Generate configuration file
804 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
805 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
809 char cmd_grub
[STRING_SIZE
];
810 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
812 if (dest
->is_raid
&& (dest
->part_table
== HW_PART_TABLE_MSDOS
)) {
813 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
814 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
818 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
819 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
821 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
822 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);