]>
git.ipfire.org Git - 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>
33 #include <sys/ioctl.h>
34 #include <sys/mount.h>
40 #include <libsmooth.h>
44 const char* other_filesystems
[] = {
51 static int system_chroot(const char* path
, const char* cmd
) {
52 char chroot_cmd
[STRING_SIZE
];
54 snprintf(chroot_cmd
, sizeof(chroot_cmd
), "/usr/sbin/chroot %s %s", path
, cmd
);
56 return mysystem(chroot_cmd
);
59 struct hw
* hw_init() {
60 struct hw
* hw
= malloc(sizeof(*hw
));
64 hw
->udev
= udev_new();
66 fprintf(stderr
, "Could not create udev instance\n");
73 void hw_free(struct hw
* hw
) {
80 static int strstartswith(const char* a
, const char* b
) {
81 return (strncmp(a
, b
, strlen(b
)) == 0);
84 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
85 // Create target if it does not exist
86 if (access(target
, X_OK
) != 0)
87 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
89 return mount(source
, target
, fs
, flags
, NULL
);
92 int hw_umount(const char* target
) {
93 return umount2(target
, MNT_DETACH
);
96 static int hw_test_source_medium(const char* path
) {
97 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
99 // If the source could not be mounted we
104 // Check if the test file exists.
105 ret
= access(SOURCE_TEST_FILE
, R_OK
);
107 // Umount the test device.
108 hw_umount(SOURCE_MOUNT_PATH
);
113 char* hw_find_source_medium(struct hw
* hw
) {
116 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
118 udev_enumerate_add_match_subsystem(enumerate
, "block");
119 udev_enumerate_scan_devices(enumerate
);
121 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
123 struct udev_list_entry
* dev_list_entry
;
124 udev_list_entry_foreach(dev_list_entry
, devices
) {
125 const char* path
= udev_list_entry_get_name(dev_list_entry
);
126 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
128 const char* dev_path
= udev_device_get_devnode(dev
);
130 // Skip everything what we cannot work with
131 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
132 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
135 if (hw_test_source_medium(dev_path
) == 0) {
136 ret
= strdup(dev_path
);
139 udev_device_unref(dev
);
141 // If a suitable device was found the search will end.
146 udev_enumerate_unref(enumerate
);
151 static struct hw_disk
** hw_create_disks() {
152 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
157 static unsigned long long hw_block_device_get_size(const char* dev
) {
158 int fd
= open(dev
, O_RDONLY
);
162 unsigned long long size
= blkid_get_dev_size(fd
);
168 struct hw_disk
** hw_find_disks(struct hw
* hw
) {
169 struct hw_disk
** ret
= hw_create_disks();
170 struct hw_disk
** disks
= ret
;
172 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
174 udev_enumerate_add_match_subsystem(enumerate
, "block");
175 udev_enumerate_scan_devices(enumerate
);
177 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
179 struct udev_list_entry
* dev_list_entry
;
180 unsigned int i
= HW_MAX_DISKS
;
181 udev_list_entry_foreach(dev_list_entry
, devices
) {
182 const char* path
= udev_list_entry_get_name(dev_list_entry
);
183 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
185 const char* dev_path
= udev_device_get_devnode(dev
);
187 // Skip everything what we cannot work with
188 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
189 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
190 strstartswith(dev_path
, "/dev/md")) {
191 udev_device_unref(dev
);
195 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
196 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
197 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
198 udev_device_unref(dev
);
202 // Skip all source mediums
203 if (hw_test_source_medium(dev_path
) == 0) {
204 udev_device_unref(dev
);
208 // Skip devices with a size of zero
209 unsigned long long size
= hw_block_device_get_size(dev_path
);
211 udev_device_unref(dev
);
215 struct hw_disk
* disk
= malloc(sizeof(*disk
));
221 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
226 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
228 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
230 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
234 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
237 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
239 model
= udev_device_get_sysattr_value(dev
, "model");
241 model
= udev_device_get_sysattr_value(dev
, "product");
245 strncpy(disk
->model
, model
, sizeof(disk
->model
));
247 snprintf(disk
->description
, sizeof(disk
->description
),
248 "%4.1fGB %s - %s", (double)disk
->size
/ pow(1024, 3),
249 disk
->vendor
, disk
->model
);
256 udev_device_unref(dev
);
259 udev_enumerate_unref(enumerate
);
266 void hw_free_disks(struct hw_disk
** disks
) {
267 struct hw_disk
** disk
= disks
;
269 while (*disk
!= NULL
) {
270 if (--(*disk
)->ref
== 0)
279 unsigned int hw_count_disks(struct hw_disk
** disks
) {
280 unsigned int ret
= 0;
288 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
289 struct hw_disk
** ret
= hw_create_disks();
290 struct hw_disk
** selected_disks
= ret
;
292 unsigned int num_disks
= hw_count_disks(disks
);
294 for (unsigned int i
= 0; i
< num_disks
; i
++) {
295 if (selection
&& selection
[i
]) {
296 struct hw_disk
*selected_disk
= disks
[i
];
297 selected_disk
->ref
++;
299 *selected_disks
++ = selected_disk
;
304 *selected_disks
= NULL
;
309 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
310 unsigned long long memory
= hw_memory();
312 unsigned long long swap_size
= memory
/ 4;
314 // Min. swap size is 128MB
315 if (swap_size
< MB2BYTES(128))
316 swap_size
= MB2BYTES(128);
318 // Cap swap size to 1GB
319 else if (swap_size
> MB2BYTES(1024))
320 swap_size
= MB2BYTES(1024);
325 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
326 unsigned long long root_size
;
328 if (dest
->size
< MB2BYTES(2048))
329 root_size
= MB2BYTES(1024);
331 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
332 root_size
= MB2BYTES(1536);
335 root_size
= MB2BYTES(2048);
340 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
344 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
348 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
349 dest
->part_boot_idx
= 0;
351 // Determine the size of the target block device
353 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
354 dest
->disk2
->size
: dest
->disk1
->size
;
356 // The RAID will install some metadata at the end of the disk
357 // and we will save up some space for that.
358 dest
->size
-= MB2BYTES(2);
360 dest
->size
= dest
->disk1
->size
;
363 // As we add some extra space before the beginning of the first
364 // partition, we need to substract that here.
365 dest
->size
-= MB2BYTES(1);
367 // Add some more space for partition tables, etc.
368 dest
->size
-= MB2BYTES(1);
370 // Determine partition table
371 dest
->part_table
= HW_PART_TABLE_MSDOS
;
373 // Disks over 2TB need to use GPT
374 if (dest
->size
>= MB2BYTES(2047 * 1024))
375 dest
->part_table
= HW_PART_TABLE_GPT
;
377 // We also use GPT on raid disks by default
378 else if (dest
->is_raid
)
379 dest
->part_table
= HW_PART_TABLE_GPT
;
381 // When using GPT, GRUB2 needs a little bit of space to put
383 if (dest
->part_table
== HW_PART_TABLE_GPT
) {
384 snprintf(dest
->part_bootldr
, sizeof(dest
->part_bootldr
),
385 "%s%d", path
, part_idx
);
387 dest
->size_bootldr
= MB2BYTES(4);
389 dest
->part_boot_idx
= part_idx
++;
391 *dest
->part_bootldr
= '\0';
392 dest
->size_bootldr
= 0;
395 dest
->size_boot
= hw_boot_size(dest
);
396 dest
->size_swap
= hw_swap_size(dest
);
397 dest
->size_root
= hw_root_size(dest
);
399 // Determine the size of the data partition.
400 unsigned long long used_space
= dest
->size_bootldr
+ dest
->size_boot
401 + dest
->size_swap
+ dest
->size_root
;
403 // Disk is way too small
404 if (used_space
>= dest
->size
)
407 dest
->size_data
= dest
->size
- used_space
;
409 // If it gets too small, we remove the swap space.
410 if (dest
->size_data
<= MB2BYTES(256)) {
411 dest
->size_data
+= dest
->size_swap
;
415 // Set partition names
416 if (dest
->size_boot
> 0) {
417 if (dest
->part_boot_idx
== 0)
418 dest
->part_boot_idx
= part_idx
;
420 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
422 *dest
->part_boot
= '\0';
424 if (dest
->size_swap
> 0)
425 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
427 *dest
->part_swap
= '\0';
429 // There is always a root partition
430 if (dest
->part_boot_idx
== 0)
431 dest
->part_boot_idx
= part_idx
;
433 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
435 if (dest
->size_data
> 0)
436 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
438 *dest
->part_data
= '\0';
443 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
444 struct hw_destination
* dest
= malloc(sizeof(*dest
));
446 if (part_type
== HW_PART_TYPE_NORMAL
) {
447 dest
->disk1
= *disks
;
450 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
452 } else if (part_type
== HW_PART_TYPE_RAID1
) {
453 dest
->disk1
= *disks
++;
454 dest
->disk2
= *disks
;
455 dest
->raid_level
= 1;
457 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
460 // Is this a RAID device?
461 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
463 int r
= hw_calculate_partition_table(dest
);
467 // Set default filesystem
468 dest
->filesystem
= HW_FS_DEFAULT
;
473 unsigned long long hw_memory() {
475 char line
[STRING_SIZE
];
477 unsigned long long memory
= 0;
479 /* Calculate amount of memory in machine */
480 if ((handle
= fopen("/proc/meminfo", "r"))) {
481 while (fgets(line
, sizeof(line
), handle
)) {
482 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
490 return memory
* 1024;
493 int hw_create_partitions(struct hw_destination
* dest
) {
496 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
498 // Set partition type
499 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
500 asprintf(&cmd
, "%s mklabel msdos", cmd
);
501 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
502 asprintf(&cmd
, "%s mklabel gpt", cmd
);
504 unsigned long long part_start
= MB2BYTES(1);
506 if (*dest
->part_bootldr
) {
507 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
508 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOTLDR" : "primary",
509 part_start
, part_start
+ dest
->size_bootldr
- 1);
511 part_start
+= dest
->size_bootldr
;
514 if (*dest
->part_boot
) {
515 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
516 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
517 part_start
, part_start
+ dest
->size_boot
- 1);
519 part_start
+= dest
->size_boot
;
522 if (*dest
->part_swap
) {
523 asprintf(&cmd
, "%s mkpart %s linux-swap %lluB %lluB", cmd
,
524 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
525 part_start
, part_start
+ dest
->size_swap
- 1);
527 part_start
+= dest
->size_swap
;
530 if (*dest
->part_root
) {
531 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
532 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
533 part_start
, part_start
+ dest
->size_root
- 1);
535 part_start
+= dest
->size_root
;
538 if (*dest
->part_data
) {
539 asprintf(&cmd
, "%s mkpart %s ext2 %lluB %lluB", cmd
,
540 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
541 part_start
, part_start
+ dest
->size_data
- 1);
543 part_start
+= dest
->size_data
;
546 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
547 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
549 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
550 if (*dest
->part_bootldr
) {
551 asprintf(&cmd
, "%s set %d bios_grub on", cmd
, dest
->part_boot_idx
);
553 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
556 int r
= mysystem(cmd
);
558 // Wait until the system re-read the partition table
560 unsigned int counter
= 10;
562 while (counter
-- > 0) {
565 if (*dest
->part_bootldr
&& (access(dest
->part_bootldr
, R_OK
) != 0))
568 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
571 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
574 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
577 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
580 // All partitions do exist, exiting the loop.
591 static int hw_format_filesystem(const char* path
, int fs
) {
592 char cmd
[STRING_SIZE
] = "\0";
595 if (fs
== HW_FS_SWAP
) {
596 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
598 } else if (fs
== HW_FS_REISERFS
) {
599 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
602 } else if (fs
== HW_FS_EXT4
) {
603 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
606 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
607 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
610 } else if (fs
== HW_FS_XFS
) {
611 snprintf(cmd
, sizeof(cmd
), "/sbin/mkfs.xfs -f %s", path
);
616 int r
= mysystem(cmd
);
621 int hw_create_filesystems(struct hw_destination
* dest
) {
625 if (*dest
->part_boot
) {
626 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
632 if (*dest
->part_swap
) {
633 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
639 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
644 if (*dest
->part_data
) {
645 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
653 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
654 char target
[STRING_SIZE
];
656 assert(*prefix
== '/');
658 const char* filesystem
;
659 switch (dest
->filesystem
) {
661 filesystem
= "reiserfs";
665 case HW_FS_EXT4_WO_JOURNAL
:
678 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
683 if (*dest
->part_boot
) {
684 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
685 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
687 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
689 hw_umount_filesystems(dest
, prefix
);
696 if (*dest
->part_data
) {
697 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
698 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
700 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
702 hw_umount_filesystems(dest
, prefix
);
709 if (*dest
->part_swap
) {
710 r
= swapon(dest
->part_swap
, 0);
712 hw_umount_filesystems(dest
, prefix
);
718 // bind-mount misc filesystems
719 char** otherfs
= other_filesystems
;
721 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
723 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
724 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
726 hw_umount_filesystems(dest
, prefix
);
737 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
739 if (*dest
->part_boot
) {
740 hw_umount(dest
->part_boot
);
744 if (*dest
->part_data
) {
745 hw_umount(dest
->part_data
);
749 hw_umount(dest
->part_root
);
752 if (*dest
->part_swap
) {
753 swapoff(dest
->part_swap
);
757 char target
[STRING_SIZE
];
758 char** otherfs
= other_filesystems
;
761 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
768 int hw_setup_raid(struct hw_destination
* dest
) {
771 assert(dest
->is_raid
);
773 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest
->path
);
775 switch (dest
->raid_level
) {
777 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
785 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
789 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
792 int r
= mysystem(cmd
);
795 // Wait a moment until the device has been properly brought up
797 unsigned int counter
= 10;
798 while (counter
-- > 0) {
801 // If the raid device has not yet been properly brought up,
802 // opening it will fail with the message: Device or resource busy
803 // Hence we will wait a bit until it becomes usable.
804 FILE* f
= fopen(dest
->path
, "r");
815 int hw_stop_all_raid_arrays() {
816 return mysystem("/sbin/mdadm --stop --scan");
819 int hw_install_bootloader(struct hw_destination
* dest
) {
820 char cmd
[STRING_SIZE
];
823 // Generate configuration file
824 snprintf(cmd
, sizeof(cmd
), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
825 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
829 char cmd_grub
[STRING_SIZE
];
830 snprintf(cmd_grub
, sizeof(cmd_grub
), "/usr/sbin/grub-install --no-floppy --recheck");
832 if (dest
->is_raid
&& (dest
->part_table
== HW_PART_TABLE_MSDOS
)) {
833 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk1
->path
);
834 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
838 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->disk2
->path
);
839 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
841 snprintf(cmd
, sizeof(cmd
), "%s %s", cmd_grub
, dest
->path
);
842 r
= system_chroot(DESTINATION_MOUNT_PATH
, cmd
);
848 static char* hw_get_uuid(const char* dev
) {
849 blkid_probe p
= blkid_new_probe_from_filename(dev
);
850 const char* buffer
= NULL
;
857 blkid_probe_lookup_value(p
, "UUID", &buffer
, NULL
);
860 uuid
= strdup(buffer
);
867 int hw_write_fstab(struct hw_destination
* dest
) {
868 FILE* f
= fopen(DESTINATION_MOUNT_PATH
"/etc/fstab", "w");
872 const char* fmt
= "UUID=%s %-8s %-4s %-10s %d %d\n";
876 if (*dest
->part_boot
) {
877 uuid
= hw_get_uuid(dest
->part_boot
);
880 fprintf(f
, fmt
, uuid
, "/boot", "auto", "defaults", 1, 2);
886 if (*dest
->part_swap
) {
887 uuid
= hw_get_uuid(dest
->part_swap
);
890 fprintf(f
, fmt
, uuid
, "swap", "swap", "defaults,pri=1", 0, 0);
896 uuid
= hw_get_uuid(dest
->part_root
);
898 fprintf(f
, fmt
, uuid
, "/", "auto", "defaults", 1, 1);
903 if (*dest
->part_data
) {
904 uuid
= hw_get_uuid(dest
->part_data
);
907 fprintf(f
, fmt
, uuid
, "/var", "auto", "defaults", 1, 1);