]> git.ipfire.org Git - ipfire-2.x.git/blame - src/install+setup/install/hw.c
installer: Wait until the kernel re-read the partition table.
[ipfire-2.x.git] / src / install+setup / install / hw.c
CommitLineData
f0fa1795
MT
1/*#############################################################################
2# #
3# IPFire - An Open Source Firewall Distribution #
4# Copyright (C) 2014 IPFire development team #
5# #
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. #
10# #
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. #
15# #
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/>. #
18# #
19#############################################################################*/
20
25fcce25
MT
21#ifndef _GNU_SOURCE
22#define _GNU_SOURCE
23#endif
24
f0fa1795 25#include <assert.h>
d7dd283b
MT
26#include <blkid/blkid.h>
27#include <fcntl.h>
f0fa1795 28#include <libudev.h>
d7dd283b 29#include <math.h>
f0fa1795
MT
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
d7dd283b 33#include <sys/ioctl.h>
f0fa1795 34#include <sys/mount.h>
25fcce25 35#include <sys/swap.h>
f0fa1795
MT
36#include <unistd.h>
37
d7dd283b
MT
38#include <linux/fs.h>
39
f0fa1795 40#include "hw.h"
25fcce25
MT
41#include "../libsmooth/libsmooth.h"
42
43const char* other_filesystems[] = {
44 "/dev",
45 "/proc",
46 "/sys",
47 NULL
48};
f0fa1795
MT
49
50struct hw* hw_init() {
51 struct hw* hw = malloc(sizeof(*hw));
52 assert(hw);
53
54 // Initialize libudev
55 hw->udev = udev_new();
56 if (!hw->udev) {
57 fprintf(stderr, "Could not create udev instance\n");
58 exit(1);
59 }
60
61 return hw;
62}
63
64void hw_free(struct hw* hw) {
65 if (hw->udev)
66 udev_unref(hw->udev);
67
68 free(hw);
69}
70
71static int strstartswith(const char* a, const char* b) {
72 return (strncmp(a, b, strlen(b)) == 0);
73}
74
25fcce25
MT
75int hw_mount(const char* source, const char* target, const char* fs, int flags) {
76 return mount(source, target, fs, flags, NULL);
f0fa1795
MT
77}
78
79int hw_umount(const char* target) {
80 return umount2(target, MNT_DETACH);
81}
82
83static int hw_test_source_medium(const char* path) {
25fcce25 84 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
f0fa1795
MT
85
86 // If the source could not be mounted we
87 // cannot proceed.
88 if (ret)
89 return ret;
90
91 // Check if the test file exists.
92 ret = access(SOURCE_TEST_FILE, F_OK);
93
94 // Umount the test device.
95 hw_umount(SOURCE_MOUNT_PATH);
96
97 return ret;
98}
99
100char* hw_find_source_medium(struct hw* hw) {
101 char* ret = NULL;
102
103 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
104
105 udev_enumerate_add_match_subsystem(enumerate, "block");
106 udev_enumerate_scan_devices(enumerate);
107
108 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
109
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);
114
115 const char* dev_path = udev_device_get_devnode(dev);
116
117 // Skip everything what we cannot work with
118 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
4a0d9bef 119 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md"))
f0fa1795
MT
120 continue;
121
122 if (hw_test_source_medium(dev_path)) {
123 ret = strdup(dev_path);
124 }
125
126 udev_device_unref(dev);
127
128 // If a suitable device was found the search will end.
129 if (ret)
130 break;
131 }
132
133 udev_enumerate_unref(enumerate);
134
135 return ret;
136}
d7dd283b
MT
137
138static struct hw_disk** hw_create_disks() {
139 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
140
141 return ret;
142}
143
144static unsigned long long hw_block_device_get_size(const char* dev) {
145 int fd = open(dev, O_RDONLY);
146 if (fd < 0)
147 return 0;
148
149 unsigned long long size = blkid_get_dev_size(fd);
150 close(fd);
151
152 return size;
153}
154
155struct hw_disk** hw_find_disks(struct hw* hw) {
156 struct hw_disk** ret = hw_create_disks();
157 struct hw_disk** disks = ret;
158
159 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
160
161 udev_enumerate_add_match_subsystem(enumerate, "block");
162 udev_enumerate_scan_devices(enumerate);
163
164 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
165
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);
171
172 const char* dev_path = udev_device_get_devnode(dev);
173
174 // Skip everything what we cannot work with
175 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
4a0d9bef
MT
176 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") ||
177 strstartswith(dev_path, "/dev/md")) {
d7dd283b
MT
178 udev_device_unref(dev);
179 continue;
180 }
181
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);
186 continue;
187 }
188
189 // Skip all source mediums
190 if (hw_test_source_medium(dev_path) == 0) {
191 udev_device_unref(dev);
192 continue;
193 }
194
195 // Skip devices with a size of zero
196 unsigned long long size = hw_block_device_get_size(dev_path);
197 if (size == 0) {
198 udev_device_unref(dev);
199 continue;
200 }
201
202 struct hw_disk* disk = malloc(sizeof(*disk));
203 if (disk == NULL)
204 return NULL;
205
206 disk->ref = 1;
207
208 strncpy(disk->path, dev_path, sizeof(disk->path));
209
210 disk->size = size;
211
212 // Vendor
213 const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR");
214 if (!vendor)
215 vendor = udev_device_get_sysattr_value(dev, "vendor");
216 if (!vendor)
217 vendor = udev_device_get_sysattr_value(dev, "manufacturer");
218 if (!vendor)
219 vendor = "N/A";
220
221 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
222
223 // Model
224 const char* model = udev_device_get_property_value(dev, "ID_MODEL");
225 if (!model)
226 model = udev_device_get_sysattr_value(dev, "model");
227 if (!model)
228 model = udev_device_get_sysattr_value(dev, "product");
229 if (!model)
230 model = "N/A";
231
232 strncpy(disk->model, model, sizeof(disk->model));
233
234 snprintf(disk->description, sizeof(disk->description),
235 "%4.1fGB %s - %s", (double)disk->size / pow(1024, 3),
236 disk->vendor, disk->model);
237
238 *disks++ = disk;
239
240 if (--i == 0)
241 break;
242
243 udev_device_unref(dev);
244 }
245
246 udev_enumerate_unref(enumerate);
247
248 *disks = NULL;
249
250 return ret;
251}
252
253void hw_free_disks(struct hw_disk** disks) {
254 struct hw_disk** disk = disks;
255
256 while (*disk != NULL) {
257 if (--(*disk)->ref == 0)
258 free(*disk);
259
260 disk++;
261 }
262
263 free(disks);
264}
265
266unsigned int hw_count_disks(struct hw_disk** disks) {
267 unsigned int ret = 0;
268
269 while (*disks++)
270 ret++;
271
272 return ret;
273}
274
275struct 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;
278
279 unsigned int num_disks = hw_count_disks(disks);
280
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++;
285
286 *selected_disks++ = selected_disk;
287 }
288 }
289
290 // Set sentinel
291 *selected_disks = NULL;
292
293 return ret;
294}
295
25fcce25
MT
296static unsigned long long hw_swap_size(struct hw_destination* dest) {
297 unsigned long long memory = hw_memory();
298
299 unsigned long long swap_size = memory / 4;
300
301 // Min. swap size is 128MB
302 if (swap_size < MB2BYTES(128))
303 swap_size = MB2BYTES(128);
304
305 // Cap swap size to 1GB
306 else if (swap_size > MB2BYTES(1024))
307 swap_size = MB2BYTES(1024);
308
309 return swap_size;
310}
311
312static unsigned long long hw_root_size(struct hw_destination* dest) {
313 unsigned long long root_size;
314
315 if (dest->size < MB2BYTES(2048))
316 root_size = MB2BYTES(1024);
317
318 else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072))
319 root_size = MB2BYTES(1536);
320
321 else
322 root_size = MB2BYTES(2048);
323
324 return root_size;
325}
326
327static unsigned long long hw_boot_size(struct hw_destination* dest) {
328 return MB2BYTES(64);
329}
330
331static int hw_calculate_partition_table(struct hw_destination* dest) {
332 // Determine the size of the target block device
333 if (dest->is_raid) {
334 dest->size = (dest->disk1->size >= dest->disk2->size) ?
335 dest->disk1->size : dest->disk2->size;
336 } else {
337 dest->size = dest->disk1->size;
338 }
339
340 dest->size_boot = hw_boot_size(dest);
341 dest->size_swap = hw_swap_size(dest);
342 dest->size_root = hw_root_size(dest);
343
344 // Determine the size of the data partition.
345 unsigned long long used_space = dest->size_boot + dest->size_swap + dest->size_root;
346
347 // Disk is way too small
348 if (used_space >= dest->size)
349 return -1;
350
351 dest->size_data = dest->size - used_space;
352
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;
356 dest->size_swap = 0;
357 }
358
359 // Set partition names
360 char path[DEV_SIZE];
361 int part_idx = 1;
362
363 snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : "");
364
365 if (dest->size_boot > 0) {
366 dest->part_boot_idx = part_idx;
367
368 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
369 } else
370 *dest->part_boot = '\0';
371
372 if (dest->size_swap > 0)
373 snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++);
374 else
375 *dest->part_swap = '\0';
376
377 // There is always a root partition
378 if (!*dest->part_boot)
379 dest->part_boot_idx = part_idx;
380
381 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
382
383 if (dest->size_data > 0)
384 snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++);
385 else
386 *dest->part_data = '\0';
387
388 // Determine partition table
389 dest->part_table = HW_PART_TABLE_MSDOS;
390
391 // Disks over 2TB need to use GPT
392 if (dest->size >= MB2BYTES(2047 * 1024))
393 dest->part_table = HW_PART_TABLE_GPT;
394
395 // We also use GPT on raid disks by default
396 else if (dest->is_raid)
397 dest->part_table = HW_PART_TABLE_GPT;
398
399 return 0;
400}
401
d7dd283b
MT
402struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) {
403 struct hw_destination* dest = malloc(sizeof(*dest));
404
405 if (part_type == HW_PART_TYPE_NORMAL) {
406 dest->disk1 = *disks;
407 dest->disk2 = NULL;
408
409 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
410
411 } else if (part_type == HW_PART_TYPE_RAID1) {
412 dest->disk1 = *disks++;
413 dest->disk2 = *disks;
4a0d9bef 414 dest->raid_level = 1;
d7dd283b
MT
415
416 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
417 }
418
419 // Is this a RAID device?
420 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
421
25fcce25
MT
422 int r = hw_calculate_partition_table(dest);
423 if (r)
424 return NULL;
d7dd283b 425
25fcce25
MT
426 // Set default filesystem
427 dest->filesystem = HW_FS_DEFAULT;
d7dd283b
MT
428
429 return dest;
430}
c4e96674
MT
431
432unsigned long long hw_memory() {
433 FILE* handle = NULL;
434 char line[STRING_SIZE];
435
436 unsigned long long memory = 0;
437
438 /* Calculate amount of memory in machine */
439 if ((handle = fopen("/proc/meminfo", "r"))) {
440 while (fgets(line, sizeof(line), handle)) {
25fcce25 441 if (!sscanf (line, "MemTotal: %llu kB", &memory)) {
c4e96674
MT
442 memory = 0;
443 }
444 }
445
446 fclose(handle);
447 }
448
449 return memory * 1024;
450}
25fcce25
MT
451
452int hw_create_partitions(struct hw_destination* dest) {
453 char* cmd = NULL;
454
455 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
456
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);
462
463 unsigned long long part_start = 0 * 1024 * 1024; // 1MB
464
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));
469
470 part_start += dest->size_boot;
471 }
472
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));
477
478 part_start += dest->size_swap;
479 }
480
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));
485
486 part_start += dest->size_root;
487 }
488
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));
493
494 part_start += dest->size_data;
495 }
496
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);
499
500 } else if (dest->part_table == HW_PART_TABLE_GPT) {
501 asprintf(&cmd, "%s disk_set pmbr_boot on", cmd);
502 }
503
504 int r = mysystem(cmd);
505
268090a8
MT
506 // Wait until the system re-read the partition table
507 if (r == 0) {
508 unsigned int counter = 10;
509
510 while (counter-- > 0) {
511 sleep(1);
512
513 if (*dest->part_boot && (access(dest->part_boot, R_OK) != 0))
514 continue;
515
516 if (*dest->part_swap && (access(dest->part_swap, R_OK) != 0))
517 continue;
518
519 if (*dest->part_root && (access(dest->part_root, R_OK) != 0))
520 continue;
521
522 if (*dest->part_data && (access(dest->part_data, R_OK) != 0))
523 continue;
524
525 // All partitions do exist, exiting the loop.
526 break;
527 }
528 }
529
25fcce25
MT
530 if (cmd)
531 free(cmd);
532
533 return r;
534}
535
536static int hw_format_filesystem(const char* path, int fs) {
537 char cmd[STRING_SIZE] = "\0";
538
539 // Swap
540 if (fs == HW_FS_SWAP) {
541 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
542 // ReiserFS
543 } else if (fs == HW_FS_REISERFS) {
544 snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path);
545
546 // EXT4
547 } else if (fs == HW_FS_EXT4) {
548 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path);
549
550 // EXT4 w/o journal
551 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
552 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path);
553 }
554
555 assert(*cmd);
556
557 int r = mysystem(cmd);
558
559 return r;
560}
561
562int hw_create_filesystems(struct hw_destination* dest) {
563 int r;
564
565 // boot
566 if (*dest->part_boot) {
567 r = hw_format_filesystem(dest->part_boot, dest->filesystem);
568 if (r)
569 return r;
570 }
571
572 // swap
573 if (*dest->part_swap) {
574 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP);
575 if (r)
576 return r;
577 }
578
579 // root
580 r = hw_format_filesystem(dest->part_root, dest->filesystem);
581 if (r)
582 return r;
583
584 // data
585 if (*dest->part_data) {
586 r = hw_format_filesystem(dest->part_data, dest->filesystem);
587 if (r)
588 return r;
589 }
590
591 return 0;
592}
593
594int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
595 char target[STRING_SIZE];
596
597 assert(*prefix == '/');
598
599 const char* filesystem;
600 switch (dest->filesystem) {
601 case HW_FS_REISERFS:
602 filesystem = "reiserfs";
603 break;
604
605 case HW_FS_EXT4:
606 case HW_FS_EXT4_WO_JOURNAL:
607 filesystem = "ext4";
608 break;
609
610 default:
611 assert(0);
612 }
613
614 // root
615 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
616 if (r)
617 return r;
618
619 // boot
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);
623
624 r = hw_mount(dest->part_boot, target, filesystem, 0);
625 if (r) {
626 hw_umount_filesystems(dest, prefix);
627
628 return r;
629 }
630 }
631
632 // data
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);
636
637 r = hw_mount(dest->part_data, target, filesystem, 0);
638 if (r) {
639 hw_umount_filesystems(dest, prefix);
640
641 return r;
642 }
643 }
644
645 // swap
646 if (*dest->part_swap) {
647 r = swapon(dest->part_swap, 0);
648 if (r) {
649 hw_umount_filesystems(dest, prefix);
650
651 return r;
652 }
653 }
654
655 // bind-mount misc filesystems
656 char** otherfs = other_filesystems;
657 while (*otherfs) {
658 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs);
659
660 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
661 r = hw_mount(*otherfs, target, NULL, MS_BIND);
662 if (r) {
663 hw_umount_filesystems(dest, prefix);
664
665 return r;
666 }
667
668 otherfs++;
669 }
670
671 return 0;
672}
673
674int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
675 // boot
676 if (*dest->part_boot) {
677 hw_umount(dest->part_boot);
678 }
679
680 // data
681 if (*dest->part_data) {
682 hw_umount(dest->part_data);
683 }
684
685 // root
686 hw_umount(dest->part_root);
687
688 // swap
689 if (*dest->part_swap) {
690 swapoff(dest->part_swap);
691 }
692
693 // misc filesystems
694 char target[STRING_SIZE];
695 char** otherfs = other_filesystems;
696
697 while (*otherfs) {
698 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++);
699 hw_umount(target);
700 }
701
702 return 0;
703}
4a0d9bef
MT
704
705int hw_setup_raid(struct hw_destination* dest) {
706 char* cmd = NULL;
707
708 assert(dest->is_raid);
709
710 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=0.9 %s", dest->path);
711
712 switch (dest->raid_level) {
713 case 1:
714 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
715 break;
716
717 default:
718 assert(0);
719 }
720
721 if (dest->disk1) {
722 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
723 }
724
725 if (dest->disk2) {
726 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
727 }
728
729 int r = mysystem(cmd);
730 free(cmd);
731
732 // Wait a moment until the device has been properly brought up
733 if (r == 0) {
734 unsigned int counter = 10;
735 while (counter-- > 0) {
736 sleep(1);
737
fde37387
MT
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");
742 if (f) {
743 fclose(f);
4a0d9bef 744 break;
fde37387 745 }
4a0d9bef
MT
746 }
747 }
748
749 return r;
750}
751
752int hw_stop_all_raid_arrays() {
753 return mysystem("/sbin/mdadm --stop --scan");
754}