]>
git.ipfire.org Git - ipfire-2.x.git/blob - src/install+setup/install/hw.c
1863dfc90fac5d944c1d7fc8788b5b1a0e270d3b
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 struct hw
* hw_init() {
51 struct hw
* hw
= malloc(sizeof(*hw
));
55 hw
->udev
= udev_new();
57 fprintf(stderr
, "Could not create udev instance\n");
64 void hw_free(struct hw
* hw
) {
71 static int strstartswith(const char* a
, const char* b
) {
72 return (strncmp(a
, b
, strlen(b
)) == 0);
75 int hw_mount(const char* source
, const char* target
, const char* fs
, int flags
) {
76 return mount(source
, target
, fs
, flags
, NULL
);
79 int hw_umount(const char* target
) {
80 return umount2(target
, MNT_DETACH
);
83 static int hw_test_source_medium(const char* path
) {
84 int ret
= hw_mount(path
, SOURCE_MOUNT_PATH
, "iso9660", MS_RDONLY
);
86 // If the source could not be mounted we
91 // Check if the test file exists.
92 ret
= access(SOURCE_TEST_FILE
, F_OK
);
94 // Umount the test device.
95 hw_umount(SOURCE_MOUNT_PATH
);
100 char* hw_find_source_medium(struct hw
* hw
) {
103 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
105 udev_enumerate_add_match_subsystem(enumerate
, "block");
106 udev_enumerate_scan_devices(enumerate
);
108 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
110 struct udev_list_entry
* dev_list_entry
;
111 udev_list_entry_foreach(dev_list_entry
, devices
) {
112 const char* path
= udev_list_entry_get_name(dev_list_entry
);
113 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
115 const char* dev_path
= udev_device_get_devnode(dev
);
117 // Skip everything what we cannot work with
118 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
119 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/md"))
122 if (hw_test_source_medium(dev_path
)) {
123 ret
= strdup(dev_path
);
126 udev_device_unref(dev
);
128 // If a suitable device was found the search will end.
133 udev_enumerate_unref(enumerate
);
138 static struct hw_disk
** hw_create_disks() {
139 struct hw_disk
** ret
= malloc(sizeof(*ret
) * (HW_MAX_DISKS
+ 1));
144 static unsigned long long hw_block_device_get_size(const char* dev
) {
145 int fd
= open(dev
, O_RDONLY
);
149 unsigned long long size
= blkid_get_dev_size(fd
);
155 struct hw_disk
** hw_find_disks(struct hw
* hw
) {
156 struct hw_disk
** ret
= hw_create_disks();
157 struct hw_disk
** disks
= ret
;
159 struct udev_enumerate
* enumerate
= udev_enumerate_new(hw
->udev
);
161 udev_enumerate_add_match_subsystem(enumerate
, "block");
162 udev_enumerate_scan_devices(enumerate
);
164 struct udev_list_entry
* devices
= udev_enumerate_get_list_entry(enumerate
);
166 struct udev_list_entry
* dev_list_entry
;
167 unsigned int i
= HW_MAX_DISKS
;
168 udev_list_entry_foreach(dev_list_entry
, devices
) {
169 const char* path
= udev_list_entry_get_name(dev_list_entry
);
170 struct udev_device
* dev
= udev_device_new_from_syspath(hw
->udev
, path
);
172 const char* dev_path
= udev_device_get_devnode(dev
);
174 // Skip everything what we cannot work with
175 if (strstartswith(dev_path
, "/dev/loop") || strstartswith(dev_path
, "/dev/fd") ||
176 strstartswith(dev_path
, "/dev/ram") || strstartswith(dev_path
, "/dev/sr") ||
177 strstartswith(dev_path
, "/dev/md")) {
178 udev_device_unref(dev
);
182 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
183 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
184 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
185 udev_device_unref(dev
);
189 // Skip all source mediums
190 if (hw_test_source_medium(dev_path
) == 0) {
191 udev_device_unref(dev
);
195 // Skip devices with a size of zero
196 unsigned long long size
= hw_block_device_get_size(dev_path
);
198 udev_device_unref(dev
);
202 struct hw_disk
* disk
= malloc(sizeof(*disk
));
208 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
213 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
215 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
217 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
221 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
224 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
226 model
= udev_device_get_sysattr_value(dev
, "model");
228 model
= udev_device_get_sysattr_value(dev
, "product");
232 strncpy(disk
->model
, model
, sizeof(disk
->model
));
234 snprintf(disk
->description
, sizeof(disk
->description
),
235 "%4.1fGB %s - %s", (double)disk
->size
/ pow(1024, 3),
236 disk
->vendor
, disk
->model
);
243 udev_device_unref(dev
);
246 udev_enumerate_unref(enumerate
);
253 void hw_free_disks(struct hw_disk
** disks
) {
254 struct hw_disk
** disk
= disks
;
256 while (*disk
!= NULL
) {
257 if (--(*disk
)->ref
== 0)
266 unsigned int hw_count_disks(struct hw_disk
** disks
) {
267 unsigned int ret
= 0;
275 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
276 struct hw_disk
** ret
= hw_create_disks();
277 struct hw_disk
** selected_disks
= ret
;
279 unsigned int num_disks
= hw_count_disks(disks
);
281 for (unsigned int i
= 0; i
< num_disks
; i
++) {
282 if (selection
&& selection
[i
]) {
283 struct hw_disk
*selected_disk
= disks
[i
];
284 selected_disk
->ref
++;
286 *selected_disks
++ = selected_disk
;
291 *selected_disks
= NULL
;
296 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
297 unsigned long long memory
= hw_memory();
299 unsigned long long swap_size
= memory
/ 4;
301 // Min. swap size is 128MB
302 if (swap_size
< MB2BYTES(128))
303 swap_size
= MB2BYTES(128);
305 // Cap swap size to 1GB
306 else if (swap_size
> MB2BYTES(1024))
307 swap_size
= MB2BYTES(1024);
312 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
313 unsigned long long root_size
;
315 if (dest
->size
< MB2BYTES(2048))
316 root_size
= MB2BYTES(1024);
318 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
319 root_size
= MB2BYTES(1536);
322 root_size
= MB2BYTES(2048);
327 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
331 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
332 // Determine the size of the target block device
334 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
335 dest
->disk1
->size
: dest
->disk2
->size
;
337 dest
->size
= dest
->disk1
->size
;
340 dest
->size_boot
= hw_boot_size(dest
);
341 dest
->size_swap
= hw_swap_size(dest
);
342 dest
->size_root
= hw_root_size(dest
);
344 // Determine the size of the data partition.
345 unsigned long long used_space
= dest
->size_boot
+ dest
->size_swap
+ dest
->size_root
;
347 // Disk is way too small
348 if (used_space
>= dest
->size
)
351 dest
->size_data
= dest
->size
- used_space
;
353 // If it gets too small, we remove the swap space.
354 if (dest
->size_data
<= MB2BYTES(256)) {
355 dest
->size_data
+= dest
->size_swap
;
359 // Set partition names
363 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
365 if (dest
->size_boot
> 0) {
366 dest
->part_boot_idx
= part_idx
;
368 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
370 *dest
->part_boot
= '\0';
372 if (dest
->size_swap
> 0)
373 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
375 *dest
->part_swap
= '\0';
377 // There is always a root partition
378 if (!*dest
->part_boot
)
379 dest
->part_boot_idx
= part_idx
;
381 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
383 if (dest
->size_data
> 0)
384 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
386 *dest
->part_data
= '\0';
388 // Determine partition table
389 dest
->part_table
= HW_PART_TABLE_MSDOS
;
391 // Disks over 2TB need to use GPT
392 if (dest
->size
>= MB2BYTES(2047 * 1024))
393 dest
->part_table
= HW_PART_TABLE_GPT
;
395 // We also use GPT on raid disks by default
396 else if (dest
->is_raid
)
397 dest
->part_table
= HW_PART_TABLE_GPT
;
402 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
403 struct hw_destination
* dest
= malloc(sizeof(*dest
));
405 if (part_type
== HW_PART_TYPE_NORMAL
) {
406 dest
->disk1
= *disks
;
409 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
411 } else if (part_type
== HW_PART_TYPE_RAID1
) {
412 dest
->disk1
= *disks
++;
413 dest
->disk2
= *disks
;
414 dest
->raid_level
= 1;
416 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
419 // Is this a RAID device?
420 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
422 int r
= hw_calculate_partition_table(dest
);
426 // Set default filesystem
427 dest
->filesystem
= HW_FS_DEFAULT
;
432 unsigned long long hw_memory() {
434 char line
[STRING_SIZE
];
436 unsigned long long memory
= 0;
438 /* Calculate amount of memory in machine */
439 if ((handle
= fopen("/proc/meminfo", "r"))) {
440 while (fgets(line
, sizeof(line
), handle
)) {
441 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
449 return memory
* 1024;
452 int hw_create_partitions(struct hw_destination
* dest
) {
455 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
457 // Set partition type
458 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
459 asprintf(&cmd
, "%s mklabel msdos", cmd
);
460 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
461 asprintf(&cmd
, "%s mklabel gpt", cmd
);
463 unsigned long long part_start
= 0 * 1024 * 1024; // 1MB
465 if (*dest
->part_boot
) {
466 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
467 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
468 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_boot
));
470 part_start
+= dest
->size_boot
;
473 if (*dest
->part_swap
) {
474 asprintf(&cmd
, "%s mkpart %s linux-swap %lluMB %lluMB", cmd
,
475 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
476 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_swap
));
478 part_start
+= dest
->size_swap
;
481 if (*dest
->part_root
) {
482 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
483 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
484 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_root
));
486 part_start
+= dest
->size_root
;
489 if (*dest
->part_data
) {
490 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
491 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
492 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_data
));
494 part_start
+= dest
->size_data
;
497 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
498 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
500 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
501 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
504 int r
= mysystem(cmd
);
506 // Wait until the system re-read the partition table
508 unsigned int counter
= 10;
510 while (counter
-- > 0) {
513 if (*dest
->part_boot
&& (access(dest
->part_boot
, R_OK
) != 0))
516 if (*dest
->part_swap
&& (access(dest
->part_swap
, R_OK
) != 0))
519 if (*dest
->part_root
&& (access(dest
->part_root
, R_OK
) != 0))
522 if (*dest
->part_data
&& (access(dest
->part_data
, R_OK
) != 0))
525 // All partitions do exist, exiting the loop.
536 static int hw_format_filesystem(const char* path
, int fs
) {
537 char cmd
[STRING_SIZE
] = "\0";
540 if (fs
== HW_FS_SWAP
) {
541 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
543 } else if (fs
== HW_FS_REISERFS
) {
544 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
547 } else if (fs
== HW_FS_EXT4
) {
548 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
551 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
552 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
557 int r
= mysystem(cmd
);
562 int hw_create_filesystems(struct hw_destination
* dest
) {
566 if (*dest
->part_boot
) {
567 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
573 if (*dest
->part_swap
) {
574 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
580 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
585 if (*dest
->part_data
) {
586 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
594 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
595 char target
[STRING_SIZE
];
597 assert(*prefix
== '/');
599 const char* filesystem
;
600 switch (dest
->filesystem
) {
602 filesystem
= "reiserfs";
606 case HW_FS_EXT4_WO_JOURNAL
:
615 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
620 if (*dest
->part_boot
) {
621 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
622 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
624 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
626 hw_umount_filesystems(dest
, prefix
);
633 if (*dest
->part_data
) {
634 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
635 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
637 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
639 hw_umount_filesystems(dest
, prefix
);
646 if (*dest
->part_swap
) {
647 r
= swapon(dest
->part_swap
, 0);
649 hw_umount_filesystems(dest
, prefix
);
655 // bind-mount misc filesystems
656 char** otherfs
= other_filesystems
;
658 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
660 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
661 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
663 hw_umount_filesystems(dest
, prefix
);
674 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
676 if (*dest
->part_boot
) {
677 hw_umount(dest
->part_boot
);
681 if (*dest
->part_data
) {
682 hw_umount(dest
->part_data
);
686 hw_umount(dest
->part_root
);
689 if (*dest
->part_swap
) {
690 swapoff(dest
->part_swap
);
694 char target
[STRING_SIZE
];
695 char** otherfs
= other_filesystems
;
698 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);
705 int hw_setup_raid(struct hw_destination
* dest
) {
708 assert(dest
->is_raid
);
710 asprintf(&cmd
, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=0.9 %s", dest
->path
);
712 switch (dest
->raid_level
) {
714 asprintf(&cmd
, "%s --level=1 --raid-devices=2", cmd
);
722 asprintf(&cmd
, "%s %s", cmd
, dest
->disk1
->path
);
726 asprintf(&cmd
, "%s %s", cmd
, dest
->disk2
->path
);
729 int r
= mysystem(cmd
);
732 // Wait a moment until the device has been properly brought up
734 unsigned int counter
= 10;
735 while (counter
-- > 0) {
738 // If the raid device has not yet been properly brought up,
739 // opening it will fail with the message: Device or resource busy
740 // Hence we will wait a bit until it becomes usable.
741 FILE* f
= fopen(dest
->path
, "r");
752 int hw_stop_all_raid_arrays() {
753 return mysystem("/sbin/mdadm --stop --scan");