]>
git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/install/hw.c
36d881b0c8901a70b3ec08b6396aa6942d2ac93d
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 // Create target if it does not exist
85 if (access(target
, X_OK
) != 0)
86 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
88 return mount(source
, target
, fs
, flags
, NULL
);
91 int hw_umount(const char* target
) {
92 return umount2(target
, MNT_DETACH
);
95 static int hw_test_source_medium(const char* path
) {
96 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
98 // If the source could not be mounted we
103 // Check if the test file exists.
104 ret
= access(SOURCE_TEST_FILE
, F_OK
);
106 // Umount the test device.
107 hw_umount(SOURCE_MOUNT_PATH
);
112 char* hw_find_source_medium(struct hw
* hw
) {
115 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
117 udev_enumerate_add_match_subsystem(enumerate
, "block");
118 udev_enumerate_scan_devices(enumerate
);
120 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
122 struct udev_list_entry
* dev_list_entry
;
123 udev_list_entry_foreach(dev_list_entry
, devices
) {
124 const char* path
= udev_list_entry_get_name(dev_list_entry
);
125 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
127 const char* dev_path
= udev_device_get_devnode(dev
);
129 // Skip everything what we cannot work with
130 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
131 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
134 if (hw_test_source_medium(dev_path
)) {
135 ret
= strdup(dev_path
);
138 udev_device_unref(dev
);
140 // If a suitable device was found the search will end.
145 udev_enumerate_unref(enumerate
);
150 static struct hw_disk
** hw_create_disks() {
151 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
156 static unsigned long long hw_block_device_get_size(const char* dev
) {
157 int fd
= open(dev
, O_RDONLY
);
161 unsigned long long size
= blkid_get_dev_size(fd
);
167 struct hw_disk
** hw_find_disks(struct hw
* hw
) {
168 struct hw_disk
** ret
= hw_create_disks();
169 struct hw_disk
** disks
= ret
;
171 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
173 udev_enumerate_add_match_subsystem(enumerate
, "block");
174 udev_enumerate_scan_devices(enumerate
);
176 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
178 struct udev_list_entry
* dev_list_entry
;
179 unsigned int i
= HW_MAX_DISKS
;
180 udev_list_entry_foreach(dev_list_entry
, devices
) {
181 const char* path
= udev_list_entry_get_name(dev_list_entry
);
182 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
184 const char* dev_path
= udev_device_get_devnode(dev
);
186 // Skip everything what we cannot work with
187 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
188 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
189 strstartswith(dev_path
, "/dev/md")) {
190 udev_device_unref(dev
);
194 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
195 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
196 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
197 udev_device_unref(dev
);
201 // Skip all source mediums
202 if (hw_test_source_medium(dev_path
) == 0) {
203 udev_device_unref(dev
);
207 // Skip devices with a size of zero
208 unsigned long long size
= hw_block_device_get_size(dev_path
);
210 udev_device_unref(dev
);
214 struct hw_disk
* disk
= malloc(sizeof(*disk
));
220 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
225 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
227 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
229 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
233 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
236 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
238 model
= udev_device_get_sysattr_value(dev
, "model");
240 model
= udev_device_get_sysattr_value(dev
, "product");
244 strncpy(disk
->model
, model
, sizeof(disk
->model
));
246 snprintf(disk
->description
, sizeof(disk
->description
),
247 "%4.1fGB %s - %s", (double)disk
->size
/ pow(1024, 3),
248 disk
->vendor
, disk
->model
);
255 udev_device_unref(dev
);
258 udev_enumerate_unref(enumerate
);
265 void hw_free_disks(struct hw_disk
** disks
) {
266 struct hw_disk
** disk
= disks
;
268 while (*disk
!= NULL
) {
269 if (--(*disk
)->ref
== 0)
278 unsigned int hw_count_disks(struct hw_disk
** disks
) {
279 unsigned int ret
= 0;
287 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
288 struct hw_disk
** ret
= hw_create_disks();
289 struct hw_disk
** selected_disks
= ret
;
291 unsigned int num_disks
= hw_count_disks(disks
);
293 for (unsigned int i
= 0; i
< num_disks
; i
++) {
294 if (selection
&& selection
[i
]) {
295 struct hw_disk
*selected_disk
= disks
[i
];
296 selected_disk
->ref
++;
298 *selected_disks
++ = selected_disk
;
303 *selected_disks
= NULL
;
308 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
309 unsigned long long memory
= hw_memory();
311 unsigned long long swap_size
= memory
/ 4;
313 // Min. swap size is 128MB
314 if (swap_size
< MB2BYTES(128))
315 swap_size
= MB2BYTES(128);
317 // Cap swap size to 1GB
318 else if (swap_size
> MB2BYTES(1024))
319 swap_size
= MB2BYTES(1024);
324 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
325 unsigned long long root_size
;
327 if (dest
->size
< MB2BYTES(2048))
328 root_size
= MB2BYTES(1024);
330 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
331 root_size
= MB2BYTES(1536);
334 root_size
= MB2BYTES(2048);
339 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
343 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
347 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
348 dest
->part_boot_idx
= 0;
350 // Determine the size of the target block device
352 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
353 dest
->disk1
->size
: dest
->disk2
->size
;
355 // The RAID will install some metadata at the end of the disk
356 // and we will save up some space for that.
357 dest
->size
-= MB2BYTES(2);
359 dest
->size
= dest
->disk1
->size
;
362 // Determine partition table
363 dest
->part_table
= HW_PART_TABLE_MSDOS
;
365 // Disks over 2TB need to use GPT
366 if (dest
->size
>= MB2BYTES(2047 * 1024))
367 dest
->part_table
= HW_PART_TABLE_GPT
;
369 // We also use GPT on raid disks by default
370 else if (dest
->is_raid
)
371 dest
->part_table
= HW_PART_TABLE_GPT
;
373 // When using GPT, GRUB2 needs a little bit of space to put
375 if (dest
->part_table
= HW_PART_TABLE_GPT
) {
376 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
377 "%s%d", path
, part_idx
);
379 dest
->size_bootldr
= MB2BYTES(4);
381 dest
->part_boot_idx
= part_idx
++;
383 *dest
->part_bootldr
= '\0';
384 dest
->size_bootldr
= 0;
387 dest
->size_boot
= hw_boot_size(dest
);
388 dest
->size_swap
= hw_swap_size(dest
);
389 dest
->size_root
= hw_root_size(dest
);
391 // Determine the size of the data partition.
392 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
393 + dest
->size_swap
+ dest
->size_root
;
395 // Disk is way too small
396 if (used_space
>= dest
->size
)
399 dest
->size_data
= dest
->size
- used_space
;
401 // If it gets too small, we remove the swap space.
402 if (dest
->size_data
<= MB2BYTES(256)) {
403 dest
->size_data
+= dest
->size_swap
;
407 // Set partition names
408 if (dest
->size_boot
> 0) {
409 if (dest
->part_boot_idx
== 0)
410 dest
->part_boot_idx
= part_idx
;
412 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
414 *dest
->part_boot
= '\0';
416 if (dest
->size_swap
> 0)
417 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
419 *dest
->part_swap
= '\0';
421 // There is always a root partition
422 if (dest
->part_boot_idx
== 0)
423 dest
->part_boot_idx
= part_idx
;
425 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
427 if (dest
->size_data
> 0)
428 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
430 *dest
->part_data
= '\0';
435 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
436 struct hw_destination
* dest
= malloc(sizeof(*dest
));
438 if (part_type
== HW_PART_TYPE_NORMAL
) {
439 dest
->disk1
= *disks
;
442 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
444 } else if (part_type
== HW_PART_TYPE_RAID1
) {
445 dest
->disk1
= *disks
++;
446 dest
->disk2
= *disks
;
447 dest
->raid_level
= 1;
449 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
452 // Is this a RAID device?
453 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
455 int r
= hw_calculate_partition_table(dest
);
459 // Set default filesystem
460 dest
->filesystem
= HW_FS_DEFAULT
;
465 unsigned long long hw_memory() {
467 char line
[STRING_SIZE
];
469 unsigned long long memory
= 0;
471 /* Calculate amount of memory in machine */
472 if ((handle
= fopen("/proc/meminfo", "r"))) {
473 while (fgets(line
, sizeof(line
), handle
)) {
474 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
482 return memory
* 1024;
485 int hw_create_partitions(struct hw_destination
* dest
) {
488 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
490 // Set partition type
491 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
492 asprintf(&cmd
, "%s mklabel msdos", cmd
);
493 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
494 asprintf(&cmd
, "%s mklabel gpt", cmd
);
496 unsigned long long part_start
= 1 * 1024 * 1024; // 1MB
498 if (*dest
->part_bootldr
) {
499 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
500 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
501 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_bootldr
));
503 part_start
+= dest
->size_bootldr
;
506 if (*dest
->part_boot
) {
507 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
508 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
509 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_boot
));
511 part_start
+= dest
->size_boot
;
514 if (*dest
->part_swap
) {
515 asprintf(&cmd
, "%s mkpart %s linux-swap %lluMB %lluMB", cmd
,
516 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
517 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_swap
));
519 part_start
+= dest
->size_swap
;
522 if (*dest
->part_root
) {
523 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
524 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
525 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_root
));
527 part_start
+= dest
->size_root
;
530 if (*dest
->part_data
) {
531 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
532 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
533 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_data
));
535 part_start
+= dest
->size_data
;
538 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
539 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
541 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
542 if (*dest
->part_bootldr
) {
543 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
545 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
548 int r
= mysystem(cmd
);
550 // Wait until the system re-read the partition table
552 unsigned int counter
= 10;
554 while (counter
-- > 0) {
557 if (*dest
->part_bootldr
&& (access(dest
->part_bootldr
, R_OK
) != 0))
560 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
563 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
566 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
569 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
572 // All partitions do exist, exiting the loop.
583 static int hw_format_filesystem(const char* path
, int fs
) {
584 char cmd
[STRING_SIZE
] = "\0";
587 if (fs
== HW_FS_SWAP
) {
588 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
590 } else if (fs
== HW_FS_REISERFS
) {
591 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
594 } else if (fs
== HW_FS_EXT4
) {
595 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
598 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
599 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
604 int r
= mysystem(cmd
);
609 int hw_create_filesystems(struct hw_destination
* dest
) {
613 if (*dest
->part_boot
) {
614 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
620 if (*dest
->part_swap
) {
621 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
627 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
632 if (*dest
->part_data
) {
633 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
641 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
642 char target
[STRING_SIZE
];
644 assert(*prefix
== '/');
646 const char* filesystem
;
647 switch (dest
->filesystem
) {
649 filesystem
= "reiserfs";
653 case HW_FS_EXT4_WO_JOURNAL
:
662 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
667 if (*dest
->part_boot
) {
668 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
669 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
671 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
673 hw_umount_filesystems(dest
, prefix
);
680 if (*dest
->part_data
) {
681 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
682 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
684 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
686 hw_umount_filesystems(dest
, prefix
);
693 if (*dest
->part_swap
) {
694 r
= swapon(dest
->part_swap
, 0);
696 hw_umount_filesystems(dest
, prefix
);
702 // bind-mount misc filesystems
703 char** otherfs
= other_filesystems
;
705 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
707 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
708 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
710 hw_umount_filesystems(dest
, prefix
);
721 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
723 if (*dest
->part_boot
) {
724 hw_umount(dest
->part_boot
);
728 if (*dest
->part_data
) {
729 hw_umount(dest
->part_data
);
733 hw_umount(dest
->part_root
);
736 if (*dest
->part_swap
) {
737 swapoff(dest
->part_swap
);
741 char target
[STRING_SIZE
];
742 char** otherfs
= other_filesystems
;
745 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
752 int hw_setup_raid(struct hw_destination
* dest
) {
755 assert(dest
->is_raid
);
757 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest
->path
);
759 switch (dest
->raid_level
) {
761 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
769 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
773 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
776 int r
= mysystem(cmd
);
779 // Wait a moment until the device has been properly brought up
781 unsigned int counter
= 10;
782 while (counter
-- > 0) {
785 // If the raid device has not yet been properly brought up,
786 // opening it will fail with the message: Device or resource busy
787 // Hence we will wait a bit until it becomes usable.
788 FILE* f
= fopen(dest
->path
, "r");
799 int hw_stop_all_raid_arrays() {
800 return mysystem("/sbin/mdadm --stop --scan");
803 int hw_install_bootloader(struct hw_destination
* dest
) {
804 char cmd
[STRING_SIZE
];
807 // Generate configuration file
808 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
809 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
813 char cmd_grub
[STRING_SIZE
];
814 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
816 if (dest
->is_raid
&& (dest
->part_table
== HW_PART_TABLE_MSDOS
)) {
817 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
818 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
822 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
823 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
825 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
826 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);