]>
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
) {
340 // Determine the size of the target block device
342 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
343 dest
->disk1
->size
: dest
->disk2
->size
;
345 // The RAID will install some metadata at the end of the disk
346 // and we will save up some space for that.
347 dest
->size
-= MB2BYTES(2);
349 dest
->size
= dest
->disk1
->size
;
352 dest
->size_boot
= hw_boot_size(dest
);
353 dest
->size_swap
= hw_swap_size(dest
);
354 dest
->size_root
= hw_root_size(dest
);
356 // Determine the size of the data partition.
357 unsigned long long used_space
= dest
->size_boot
+ dest
->size_swap
+ dest
->size_root
;
359 // Disk is way too small
360 if (used_space
>= dest
->size
)
363 dest
->size_data
= dest
->size
- used_space
;
365 // If it gets too small, we remove the swap space.
366 if (dest
->size_data
<= MB2BYTES(256)) {
367 dest
->size_data
+= dest
->size_swap
;
371 // Set partition names
375 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
377 if (dest
->size_boot
> 0) {
378 dest
->part_boot_idx
= part_idx
;
380 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
382 *dest
->part_boot
= '\0';
384 if (dest
->size_swap
> 0)
385 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
387 *dest
->part_swap
= '\0';
389 // There is always a root partition
390 if (!*dest
->part_boot
)
391 dest
->part_boot_idx
= part_idx
;
393 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
395 if (dest
->size_data
> 0)
396 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
398 *dest
->part_data
= '\0';
400 // Determine partition table
401 dest
->part_table
= HW_PART_TABLE_MSDOS
;
403 // Disks over 2TB need to use GPT
404 if (dest
->size
>= MB2BYTES(2047 * 1024))
405 dest
->part_table
= HW_PART_TABLE_GPT
;
407 // We also use GPT on raid disks by default
408 else if (dest
->is_raid
)
409 dest
->part_table
= HW_PART_TABLE_GPT
;
414 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
415 struct hw_destination
* dest
= malloc(sizeof(*dest
));
417 if (part_type
== HW_PART_TYPE_NORMAL
) {
418 dest
->disk1
= *disks
;
421 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
423 } else if (part_type
== HW_PART_TYPE_RAID1
) {
424 dest
->disk1
= *disks
++;
425 dest
->disk2
= *disks
;
426 dest
->raid_level
= 1;
428 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
431 // Is this a RAID device?
432 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
434 int r
= hw_calculate_partition_table(dest
);
438 // Set default filesystem
439 dest
->filesystem
= HW_FS_DEFAULT
;
444 unsigned long long hw_memory() {
446 char line
[STRING_SIZE
];
448 unsigned long long memory
= 0;
450 /* Calculate amount of memory in machine */
451 if ((handle
= fopen("/proc/meminfo", "r"))) {
452 while (fgets(line
, sizeof(line
), handle
)) {
453 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
461 return memory
* 1024;
464 int hw_create_partitions(struct hw_destination
* dest
) {
467 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
469 // Set partition type
470 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
471 asprintf(&cmd
, "%s mklabel msdos", cmd
);
472 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
473 asprintf(&cmd
, "%s mklabel gpt", cmd
);
475 unsigned long long part_start
= 1 * 1024 * 1024; // 1MB
477 if (*dest
->part_boot
) {
478 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
479 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
480 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_boot
));
482 part_start
+= dest
->size_boot
;
485 if (*dest
->part_swap
) {
486 asprintf(&cmd
, "%s mkpart %s linux-swap %lluMB %lluMB", cmd
,
487 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
488 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_swap
));
490 part_start
+= dest
->size_swap
;
493 if (*dest
->part_root
) {
494 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
495 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
496 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_root
));
498 part_start
+= dest
->size_root
;
501 if (*dest
->part_data
) {
502 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
503 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
504 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_data
));
506 part_start
+= dest
->size_data
;
509 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
510 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
512 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
513 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
516 int r
= mysystem(cmd
);
518 // Wait until the system re-read the partition table
520 unsigned int counter
= 10;
522 while (counter
-- > 0) {
525 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
528 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
531 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
534 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
537 // All partitions do exist, exiting the loop.
548 static int hw_format_filesystem(const char* path
, int fs
) {
549 char cmd
[STRING_SIZE
] = "\0";
552 if (fs
== HW_FS_SWAP
) {
553 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
555 } else if (fs
== HW_FS_REISERFS
) {
556 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
559 } else if (fs
== HW_FS_EXT4
) {
560 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
563 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
564 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
569 int r
= mysystem(cmd
);
574 int hw_create_filesystems(struct hw_destination
* dest
) {
578 if (*dest
->part_boot
) {
579 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
585 if (*dest
->part_swap
) {
586 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
592 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
597 if (*dest
->part_data
) {
598 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
606 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
607 char target
[STRING_SIZE
];
609 assert(*prefix
== '/');
611 const char* filesystem
;
612 switch (dest
->filesystem
) {
614 filesystem
= "reiserfs";
618 case HW_FS_EXT4_WO_JOURNAL
:
627 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
632 if (*dest
->part_boot
) {
633 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
634 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
636 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
638 hw_umount_filesystems(dest
, prefix
);
645 if (*dest
->part_data
) {
646 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
647 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
649 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
651 hw_umount_filesystems(dest
, prefix
);
658 if (*dest
->part_swap
) {
659 r
= swapon(dest
->part_swap
, 0);
661 hw_umount_filesystems(dest
, prefix
);
667 // bind-mount misc filesystems
668 char** otherfs
= other_filesystems
;
670 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
672 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
673 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
675 hw_umount_filesystems(dest
, prefix
);
686 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
688 if (*dest
->part_boot
) {
689 hw_umount(dest
->part_boot
);
693 if (*dest
->part_data
) {
694 hw_umount(dest
->part_data
);
698 hw_umount(dest
->part_root
);
701 if (*dest
->part_swap
) {
702 swapoff(dest
->part_swap
);
706 char target
[STRING_SIZE
];
707 char** otherfs
= other_filesystems
;
710 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
717 int hw_setup_raid(struct hw_destination
* dest
) {
720 assert(dest
->is_raid
);
722 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest
->path
);
724 switch (dest
->raid_level
) {
726 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
734 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
738 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
741 int r
= mysystem(cmd
);
744 // Wait a moment until the device has been properly brought up
746 unsigned int counter
= 10;
747 while (counter
-- > 0) {
750 // If the raid device has not yet been properly brought up,
751 // opening it will fail with the message: Device or resource busy
752 // Hence we will wait a bit until it becomes usable.
753 FILE* f
= fopen(dest
->path
, "r");
764 int hw_stop_all_raid_arrays() {
765 return mysystem("/sbin/mdadm --stop --scan");
768 int hw_install_bootloader(struct hw_destination
* dest
) {
769 char cmd
[STRING_SIZE
];
772 // Generate configuration file
773 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
774 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
778 char cmd_grub
[STRING_SIZE
];
779 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
782 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
783 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
787 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
788 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
790 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
791 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);