]>
git.ipfire.org Git - 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 // 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 // As we add some extra space before the beginning of the first
363 // partition, we need to substract that here.
364 dest
->size
-= MB2BYTES(1);
366 // Add some more space for partition tables, etc.
367 dest
->size
-= MB2BYTES(1);
369 // Determine partition table
370 dest
->part_table
= HW_PART_TABLE_MSDOS
;
372 // Disks over 2TB need to use GPT
373 if (dest
->size
>= MB2BYTES(2047 * 1024))
374 dest
->part_table
= HW_PART_TABLE_GPT
;
376 // We also use GPT on raid disks by default
377 else if (dest
->is_raid
)
378 dest
->part_table
= HW_PART_TABLE_GPT
;
380 // When using GPT, GRUB2 needs a little bit of space to put
382 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
383 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
384 "%s%d", path
, part_idx
);
386 dest
->size_bootldr
= MB2BYTES(4);
388 dest
->part_boot_idx
= part_idx
++;
390 *dest
->part_bootldr
= '\0';
391 dest
->size_bootldr
= 0;
394 dest
->size_boot
= hw_boot_size(dest
);
395 dest
->size_swap
= hw_swap_size(dest
);
396 dest
->size_root
= hw_root_size(dest
);
398 // Determine the size of the data partition.
399 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
400 + dest
->size_swap
+ dest
->size_root
;
402 // Disk is way too small
403 if (used_space
>= dest
->size
)
406 dest
->size_data
= dest
->size
- used_space
;
408 // If it gets too small, we remove the swap space.
409 if (dest
->size_data
<= MB2BYTES(256)) {
410 dest
->size_data
+= dest
->size_swap
;
414 // Set partition names
415 if (dest
->size_boot
> 0) {
416 if (dest
->part_boot_idx
== 0)
417 dest
->part_boot_idx
= part_idx
;
419 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
421 *dest
->part_boot
= '\0';
423 if (dest
->size_swap
> 0)
424 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
426 *dest
->part_swap
= '\0';
428 // There is always a root partition
429 if (dest
->part_boot_idx
== 0)
430 dest
->part_boot_idx
= part_idx
;
432 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
434 if (dest
->size_data
> 0)
435 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
437 *dest
->part_data
= '\0';
442 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
443 struct hw_destination
* dest
= malloc(sizeof(*dest
));
445 if (part_type
== HW_PART_TYPE_NORMAL
) {
446 dest
->disk1
= *disks
;
449 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
451 } else if (part_type
== HW_PART_TYPE_RAID1
) {
452 dest
->disk1
= *disks
++;
453 dest
->disk2
= *disks
;
454 dest
->raid_level
= 1;
456 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
459 // Is this a RAID device?
460 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
462 int r
= hw_calculate_partition_table(dest
);
466 // Set default filesystem
467 dest
->filesystem
= HW_FS_DEFAULT
;
472 unsigned long long hw_memory() {
474 char line
[STRING_SIZE
];
476 unsigned long long memory
= 0;
478 /* Calculate amount of memory in machine */
479 if ((handle
= fopen("/proc/meminfo", "r"))) {
480 while (fgets(line
, sizeof(line
), handle
)) {
481 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
489 return memory
* 1024;
492 int hw_create_partitions(struct hw_destination
* dest
) {
495 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
497 // Set partition type
498 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
499 asprintf(&cmd
, "%s mklabel msdos", cmd
);
500 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
501 asprintf(&cmd
, "%s mklabel gpt", cmd
);
503 unsigned long long part_start
= MB2BYTES(1);
505 if (*dest
->part_bootldr
) {
506 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
507 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
508 part_start
, part_start
+ dest
->size_bootldr
- 1);
510 part_start
+= dest
->size_bootldr
;
513 if (*dest
->part_boot
) {
514 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
515 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
516 part_start
, part_start
+ dest
->size_boot
- 1);
518 part_start
+= dest
->size_boot
;
521 if (*dest
->part_swap
) {
522 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
523 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
524 part_start
, part_start
+ dest
->size_swap
- 1);
526 part_start
+= dest
->size_swap
;
529 if (*dest
->part_root
) {
530 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
531 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
532 part_start
, part_start
+ dest
->size_root
- 1);
534 part_start
+= dest
->size_root
;
537 if (*dest
->part_data
) {
538 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
539 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
540 part_start
, part_start
+ dest
->size_data
- 1);
542 part_start
+= dest
->size_data
;
545 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
546 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
548 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
549 if (*dest
->part_bootldr
) {
550 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
552 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
555 int r
= mysystem(cmd
);
557 // Wait until the system re-read the partition table
559 unsigned int counter
= 10;
561 while (counter
-- > 0) {
564 if (*dest
->part_bootldr
&& (access(dest
->part_bootldr
, R_OK
) != 0))
567 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
570 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
573 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
576 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
579 // All partitions do exist, exiting the loop.
590 static int hw_format_filesystem(const char* path
, int fs
) {
591 char cmd
[STRING_SIZE
] = "\0";
594 if (fs
== HW_FS_SWAP
) {
595 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
597 } else if (fs
== HW_FS_REISERFS
) {
598 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
601 } else if (fs
== HW_FS_EXT4
) {
602 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
605 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
606 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
609 } else if (fs
== HW_FS_XFS
) {
610 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
615 int r
= mysystem(cmd
);
620 int hw_create_filesystems(struct hw_destination
* dest
) {
624 if (*dest
->part_boot
) {
625 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
631 if (*dest
->part_swap
) {
632 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
638 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
643 if (*dest
->part_data
) {
644 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
652 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
653 char target
[STRING_SIZE
];
655 assert(*prefix
== '/');
657 const char* filesystem
;
658 switch (dest
->filesystem
) {
660 filesystem
= "reiserfs";
664 case HW_FS_EXT4_WO_JOURNAL
:
677 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
682 if (*dest
->part_boot
) {
683 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
684 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
686 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
688 hw_umount_filesystems(dest
, prefix
);
695 if (*dest
->part_data
) {
696 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
697 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
699 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
701 hw_umount_filesystems(dest
, prefix
);
708 if (*dest
->part_swap
) {
709 r
= swapon(dest
->part_swap
, 0);
711 hw_umount_filesystems(dest
, prefix
);
717 // bind-mount misc filesystems
718 char** otherfs
= other_filesystems
;
720 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
722 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
723 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
725 hw_umount_filesystems(dest
, prefix
);
736 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
738 if (*dest
->part_boot
) {
739 hw_umount(dest
->part_boot
);
743 if (*dest
->part_data
) {
744 hw_umount(dest
->part_data
);
748 hw_umount(dest
->part_root
);
751 if (*dest
->part_swap
) {
752 swapoff(dest
->part_swap
);
756 char target
[STRING_SIZE
];
757 char** otherfs
= other_filesystems
;
760 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
767 int hw_setup_raid(struct hw_destination
* dest
) {
770 assert(dest
->is_raid
);
772 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest
->path
);
774 switch (dest
->raid_level
) {
776 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
784 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
788 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
791 int r
= mysystem(cmd
);
794 // Wait a moment until the device has been properly brought up
796 unsigned int counter
= 10;
797 while (counter
-- > 0) {
800 // If the raid device has not yet been properly brought up,
801 // opening it will fail with the message: Device or resource busy
802 // Hence we will wait a bit until it becomes usable.
803 FILE* f
= fopen(dest
->path
, "r");
814 int hw_stop_all_raid_arrays() {
815 return mysystem("/sbin/mdadm --stop --scan");
818 int hw_install_bootloader(struct hw_destination
* dest
) {
819 char cmd
[STRING_SIZE
];
822 // Generate configuration file
823 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
824 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
828 char cmd_grub
[STRING_SIZE
];
829 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
831 if (dest
->is_raid
&& (dest
->part_table
== HW_PART_TABLE_MSDOS
)) {
832 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
833 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
837 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
838 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
840 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
841 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
847 static char* hw_get_uuid(const char* dev
) {
848 blkid_probe p
= blkid_new_probe_from_filename(dev
);
849 const char* buffer
= NULL
;
856 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
859 uuid
= strdup(buffer
);
866 int hw_write_fstab(struct hw_destination
* dest
) {
867 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
871 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
875 if (*dest
->part_boot
) {
876 uuid
= hw_get_uuid(dest
->part_boot
);
879 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
885 if (*dest
->part_swap
) {
886 uuid
= hw_get_uuid(dest
->part_swap
);
889 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
895 uuid
= hw_get_uuid(dest
->part_root
);
897 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
902 if (*dest
->part_data
) {
903 uuid
= hw_get_uuid(dest
->part_data
);
906 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);