]>
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 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"))
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 udev_device_unref(dev
);
181 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
182 const char* devtype
= udev_device_get_property_value(dev
, "DEVTYPE");
183 if (devtype
&& (strcmp(devtype
, "disk") != 0)) {
184 udev_device_unref(dev
);
188 // Skip all source mediums
189 if (hw_test_source_medium(dev_path
) == 0) {
190 udev_device_unref(dev
);
194 // Skip devices with a size of zero
195 unsigned long long size
= hw_block_device_get_size(dev_path
);
197 udev_device_unref(dev
);
201 struct hw_disk
* disk
= malloc(sizeof(*disk
));
207 strncpy(disk
->path
, dev_path
, sizeof(disk
->path
));
212 const char* vendor
= udev_device_get_property_value(dev
, "ID_VENDOR");
214 vendor
= udev_device_get_sysattr_value(dev
, "vendor");
216 vendor
= udev_device_get_sysattr_value(dev
, "manufacturer");
220 strncpy(disk
->vendor
, vendor
, sizeof(disk
->vendor
));
223 const char* model
= udev_device_get_property_value(dev
, "ID_MODEL");
225 model
= udev_device_get_sysattr_value(dev
, "model");
227 model
= udev_device_get_sysattr_value(dev
, "product");
231 strncpy(disk
->model
, model
, sizeof(disk
->model
));
233 snprintf(disk
->description
, sizeof(disk
->description
),
234 "%4.1fGB %s - %s", (double)disk
->size
/ pow(1024, 3),
235 disk
->vendor
, disk
->model
);
242 udev_device_unref(dev
);
245 udev_enumerate_unref(enumerate
);
252 void hw_free_disks(struct hw_disk
** disks
) {
253 struct hw_disk
** disk
= disks
;
255 while (*disk
!= NULL
) {
256 if (--(*disk
)->ref
== 0)
265 unsigned int hw_count_disks(struct hw_disk
** disks
) {
266 unsigned int ret
= 0;
274 struct hw_disk
** hw_select_disks(struct hw_disk
** disks
, int* selection
) {
275 struct hw_disk
** ret
= hw_create_disks();
276 struct hw_disk
** selected_disks
= ret
;
278 unsigned int num_disks
= hw_count_disks(disks
);
280 for (unsigned int i
= 0; i
< num_disks
; i
++) {
281 if (selection
&& selection
[i
]) {
282 struct hw_disk
*selected_disk
= disks
[i
];
283 selected_disk
->ref
++;
285 *selected_disks
++ = selected_disk
;
290 *selected_disks
= NULL
;
295 static unsigned long long hw_swap_size(struct hw_destination
* dest
) {
296 unsigned long long memory
= hw_memory();
298 unsigned long long swap_size
= memory
/ 4;
300 // Min. swap size is 128MB
301 if (swap_size
< MB2BYTES(128))
302 swap_size
= MB2BYTES(128);
304 // Cap swap size to 1GB
305 else if (swap_size
> MB2BYTES(1024))
306 swap_size
= MB2BYTES(1024);
311 static unsigned long long hw_root_size(struct hw_destination
* dest
) {
312 unsigned long long root_size
;
314 if (dest
->size
< MB2BYTES(2048))
315 root_size
= MB2BYTES(1024);
317 else if (dest
->size
>= MB2BYTES(2048) && dest
->size
<= MB2BYTES(3072))
318 root_size
= MB2BYTES(1536);
321 root_size
= MB2BYTES(2048);
326 static unsigned long long hw_boot_size(struct hw_destination
* dest
) {
330 static int hw_calculate_partition_table(struct hw_destination
* dest
) {
331 // Determine the size of the target block device
333 dest
->size
= (dest
->disk1
->size
>= dest
->disk2
->size
) ?
334 dest
->disk1
->size
: dest
->disk2
->size
;
336 dest
->size
= dest
->disk1
->size
;
339 dest
->size_boot
= hw_boot_size(dest
);
340 dest
->size_swap
= hw_swap_size(dest
);
341 dest
->size_root
= hw_root_size(dest
);
343 // Determine the size of the data partition.
344 unsigned long long used_space
= dest
->size_boot
+ dest
->size_swap
+ dest
->size_root
;
346 // Disk is way too small
347 if (used_space
>= dest
->size
)
350 dest
->size_data
= dest
->size
- used_space
;
352 // If it gets too small, we remove the swap space.
353 if (dest
->size_data
<= MB2BYTES(256)) {
354 dest
->size_data
+= dest
->size_swap
;
358 // Set partition names
362 snprintf(path
, sizeof(path
), "%s%s", dest
->path
, (dest
->is_raid
) ? "p" : "");
364 if (dest
->size_boot
> 0) {
365 dest
->part_boot_idx
= part_idx
;
367 snprintf(dest
->part_boot
, sizeof(dest
->part_boot
), "%s%d", path
, part_idx
++);
369 *dest
->part_boot
= '\0';
371 if (dest
->size_swap
> 0)
372 snprintf(dest
->part_swap
, sizeof(dest
->part_swap
), "%s%d", path
, part_idx
++);
374 *dest
->part_swap
= '\0';
376 // There is always a root partition
377 if (!*dest
->part_boot
)
378 dest
->part_boot_idx
= part_idx
;
380 snprintf(dest
->part_root
, sizeof(dest
->part_root
), "%s%d", path
, part_idx
++);
382 if (dest
->size_data
> 0)
383 snprintf(dest
->part_data
, sizeof(dest
->part_data
), "%s%d", path
, part_idx
++);
385 *dest
->part_data
= '\0';
387 // Determine partition table
388 dest
->part_table
= HW_PART_TABLE_MSDOS
;
390 // Disks over 2TB need to use GPT
391 if (dest
->size
>= MB2BYTES(2047 * 1024))
392 dest
->part_table
= HW_PART_TABLE_GPT
;
394 // We also use GPT on raid disks by default
395 else if (dest
->is_raid
)
396 dest
->part_table
= HW_PART_TABLE_GPT
;
401 struct hw_destination
* hw_make_destination(int part_type
, struct hw_disk
** disks
) {
402 struct hw_destination
* dest
= malloc(sizeof(*dest
));
404 if (part_type
== HW_PART_TYPE_NORMAL
) {
405 dest
->disk1
= *disks
;
408 strncpy(dest
->path
, dest
->disk1
->path
, sizeof(dest
->path
));
410 } else if (part_type
== HW_PART_TYPE_RAID1
) {
411 dest
->disk1
= *disks
++;
412 dest
->disk2
= *disks
;
414 snprintf(dest
->path
, sizeof(dest
->path
), "/dev/md0");
417 // Is this a RAID device?
418 dest
->is_raid
= (part_type
> HW_PART_TYPE_NORMAL
);
420 int r
= hw_calculate_partition_table(dest
);
424 // Set default filesystem
425 dest
->filesystem
= HW_FS_DEFAULT
;
430 unsigned long long hw_memory() {
432 char line
[STRING_SIZE
];
434 unsigned long long memory
= 0;
436 /* Calculate amount of memory in machine */
437 if ((handle
= fopen("/proc/meminfo", "r"))) {
438 while (fgets(line
, sizeof(line
), handle
)) {
439 if (!sscanf (line
, "MemTotal: %llu kB", &memory
)) {
447 return memory
* 1024;
450 int hw_create_partitions(struct hw_destination
* dest
) {
453 asprintf(&cmd
, "/usr/sbin/parted -s %s -a optimal", dest
->path
);
455 // Set partition type
456 if (dest
->part_table
== HW_PART_TABLE_MSDOS
)
457 asprintf(&cmd
, "%s mklabel msdos", cmd
);
458 else if (dest
->part_table
== HW_PART_TABLE_GPT
)
459 asprintf(&cmd
, "%s mklabel gpt", cmd
);
461 unsigned long long part_start
= 0 * 1024 * 1024; // 1MB
463 if (*dest
->part_boot
) {
464 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
465 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "BOOT" : "primary",
466 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_boot
));
468 part_start
+= dest
->size_boot
;
471 if (*dest
->part_swap
) {
472 asprintf(&cmd
, "%s mkpart %s linux-swap %lluMB %lluMB", cmd
,
473 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "SWAP" : "primary",
474 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_swap
));
476 part_start
+= dest
->size_swap
;
479 if (*dest
->part_root
) {
480 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
481 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "ROOT" : "primary",
482 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_root
));
484 part_start
+= dest
->size_root
;
487 if (*dest
->part_data
) {
488 asprintf(&cmd
, "%s mkpart %s ext2 %lluMB %lluMB", cmd
,
489 (dest
->part_table
== HW_PART_TABLE_GPT
) ? "DATA" : "primary",
490 BYTES2MB(part_start
), BYTES2MB(part_start
+ dest
->size_data
));
492 part_start
+= dest
->size_data
;
495 if (dest
->part_table
== HW_PART_TABLE_MSDOS
&& dest
->part_boot_idx
> 0) {
496 asprintf(&cmd
, "%s set %d boot on", cmd
, dest
->part_boot_idx
);
498 } else if (dest
->part_table
== HW_PART_TABLE_GPT
) {
499 asprintf(&cmd
, "%s disk_set pmbr_boot on", cmd
);
502 int r
= mysystem(cmd
);
510 static int hw_format_filesystem(const char* path
, int fs
) {
511 char cmd
[STRING_SIZE
] = "\0";
514 if (fs
== HW_FS_SWAP
) {
515 snprintf(cmd
, sizeof(cmd
), "/sbin/mkswap -v1 %s &>/dev/null", path
);
517 } else if (fs
== HW_FS_REISERFS
) {
518 snprintf(cmd
, sizeof(cmd
), "/sbin/mkreiserfs -f %s ", path
);
521 } else if (fs
== HW_FS_EXT4
) {
522 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 %s", path
);
525 } else if (fs
== HW_FS_EXT4_WO_JOURNAL
) {
526 snprintf(cmd
, sizeof(cmd
), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path
);
531 int r
= mysystem(cmd
);
536 int hw_create_filesystems(struct hw_destination
* dest
) {
540 if (*dest
->part_boot
) {
541 r
= hw_format_filesystem(dest
->part_boot
, dest
->filesystem
);
547 if (*dest
->part_swap
) {
548 r
= hw_format_filesystem(dest
->part_swap
, HW_FS_SWAP
);
554 r
= hw_format_filesystem(dest
->part_root
, dest
->filesystem
);
559 if (*dest
->part_data
) {
560 r
= hw_format_filesystem(dest
->part_data
, dest
->filesystem
);
568 int hw_mount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
569 char target
[STRING_SIZE
];
571 assert(*prefix
== '/');
573 const char* filesystem
;
574 switch (dest
->filesystem
) {
576 filesystem
= "reiserfs";
580 case HW_FS_EXT4_WO_JOURNAL
:
589 int r
= hw_mount(dest
->part_root
, prefix
, filesystem
, 0);
594 if (*dest
->part_boot
) {
595 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_BOOT
);
596 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
598 r
= hw_mount(dest
->part_boot
, target
, filesystem
, 0);
600 hw_umount_filesystems(dest
, prefix
);
607 if (*dest
->part_data
) {
608 snprintf(target
, sizeof(target
), "%s%s", prefix
, HW_PATH_DATA
);
609 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
611 r
= hw_mount(dest
->part_data
, target
, filesystem
, 0);
613 hw_umount_filesystems(dest
, prefix
);
620 if (*dest
->part_swap
) {
621 r
= swapon(dest
->part_swap
, 0);
623 hw_umount_filesystems(dest
, prefix
);
629 // bind-mount misc filesystems
630 char** otherfs
= other_filesystems
;
632 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
);
634 mkdir(target
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
635 r
= hw_mount(*otherfs
, target
, NULL
, MS_BIND
);
637 hw_umount_filesystems(dest
, prefix
);
648 int hw_umount_filesystems(struct hw_destination
* dest
, const char* prefix
) {
650 if (*dest
->part_boot
) {
651 hw_umount(dest
->part_boot
);
655 if (*dest
->part_data
) {
656 hw_umount(dest
->part_data
);
660 hw_umount(dest
->part_root
);
663 if (*dest
->part_swap
) {
664 swapoff(dest
->part_swap
);
668 char target
[STRING_SIZE
];
669 char** otherfs
= other_filesystems
;
672 snprintf(target
, sizeof(target
), "%s%s", prefix
, *otherfs
++);