]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/install+setup/install/hw.c
0e1f499000fa46a04c1546eb71108040008c5ab8
[people/pmueller/ipfire-2.x.git] / src / install+setup / install / hw.c
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
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE
23 #endif
24
25 #include <assert.h>
26 #include <blkid/blkid.h>
27 #include <fcntl.h>
28 #include <libudev.h>
29 #include <math.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <sys/mount.h>
35 #include <sys/swap.h>
36 #include <unistd.h>
37
38 #include <linux/fs.h>
39
40 #include "hw.h"
41 #include "../libsmooth/libsmooth.h"
42
43 const char* other_filesystems[] = {
44 "/dev",
45 "/proc",
46 "/sys",
47 NULL
48 };
49
50 struct 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
64 void hw_free(struct hw* hw) {
65 if (hw->udev)
66 udev_unref(hw->udev);
67
68 free(hw);
69 }
70
71 static int strstartswith(const char* a, const char* b) {
72 return (strncmp(a, b, strlen(b)) == 0);
73 }
74
75 int hw_mount(const char* source, const char* target, const char* fs, int flags) {
76 return mount(source, target, fs, flags, NULL);
77 }
78
79 int hw_umount(const char* target) {
80 return umount2(target, MNT_DETACH);
81 }
82
83 static int hw_test_source_medium(const char* path) {
84 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
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
100 char* 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") ||
119 strstartswith(dev_path, "/dev/ram"))
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 }
137
138 static struct hw_disk** hw_create_disks() {
139 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
140
141 return ret;
142 }
143
144 static 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
155 struct 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") ||
176 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr")) {
177 udev_device_unref(dev);
178 continue;
179 }
180
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);
185 continue;
186 }
187
188 // Skip all source mediums
189 if (hw_test_source_medium(dev_path) == 0) {
190 udev_device_unref(dev);
191 continue;
192 }
193
194 // Skip devices with a size of zero
195 unsigned long long size = hw_block_device_get_size(dev_path);
196 if (size == 0) {
197 udev_device_unref(dev);
198 continue;
199 }
200
201 struct hw_disk* disk = malloc(sizeof(*disk));
202 if (disk == NULL)
203 return NULL;
204
205 disk->ref = 1;
206
207 strncpy(disk->path, dev_path, sizeof(disk->path));
208
209 disk->size = size;
210
211 // Vendor
212 const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR");
213 if (!vendor)
214 vendor = udev_device_get_sysattr_value(dev, "vendor");
215 if (!vendor)
216 vendor = udev_device_get_sysattr_value(dev, "manufacturer");
217 if (!vendor)
218 vendor = "N/A";
219
220 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
221
222 // Model
223 const char* model = udev_device_get_property_value(dev, "ID_MODEL");
224 if (!model)
225 model = udev_device_get_sysattr_value(dev, "model");
226 if (!model)
227 model = udev_device_get_sysattr_value(dev, "product");
228 if (!model)
229 model = "N/A";
230
231 strncpy(disk->model, model, sizeof(disk->model));
232
233 snprintf(disk->description, sizeof(disk->description),
234 "%4.1fGB %s - %s", (double)disk->size / pow(1024, 3),
235 disk->vendor, disk->model);
236
237 *disks++ = disk;
238
239 if (--i == 0)
240 break;
241
242 udev_device_unref(dev);
243 }
244
245 udev_enumerate_unref(enumerate);
246
247 *disks = NULL;
248
249 return ret;
250 }
251
252 void hw_free_disks(struct hw_disk** disks) {
253 struct hw_disk** disk = disks;
254
255 while (*disk != NULL) {
256 if (--(*disk)->ref == 0)
257 free(*disk);
258
259 disk++;
260 }
261
262 free(disks);
263 }
264
265 unsigned int hw_count_disks(struct hw_disk** disks) {
266 unsigned int ret = 0;
267
268 while (*disks++)
269 ret++;
270
271 return ret;
272 }
273
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;
277
278 unsigned int num_disks = hw_count_disks(disks);
279
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++;
284
285 *selected_disks++ = selected_disk;
286 }
287 }
288
289 // Set sentinel
290 *selected_disks = NULL;
291
292 return ret;
293 }
294
295 static unsigned long long hw_swap_size(struct hw_destination* dest) {
296 unsigned long long memory = hw_memory();
297
298 unsigned long long swap_size = memory / 4;
299
300 // Min. swap size is 128MB
301 if (swap_size < MB2BYTES(128))
302 swap_size = MB2BYTES(128);
303
304 // Cap swap size to 1GB
305 else if (swap_size > MB2BYTES(1024))
306 swap_size = MB2BYTES(1024);
307
308 return swap_size;
309 }
310
311 static unsigned long long hw_root_size(struct hw_destination* dest) {
312 unsigned long long root_size;
313
314 if (dest->size < MB2BYTES(2048))
315 root_size = MB2BYTES(1024);
316
317 else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072))
318 root_size = MB2BYTES(1536);
319
320 else
321 root_size = MB2BYTES(2048);
322
323 return root_size;
324 }
325
326 static unsigned long long hw_boot_size(struct hw_destination* dest) {
327 return MB2BYTES(64);
328 }
329
330 static int hw_calculate_partition_table(struct hw_destination* dest) {
331 // Determine the size of the target block device
332 if (dest->is_raid) {
333 dest->size = (dest->disk1->size >= dest->disk2->size) ?
334 dest->disk1->size : dest->disk2->size;
335 } else {
336 dest->size = dest->disk1->size;
337 }
338
339 dest->size_boot = hw_boot_size(dest);
340 dest->size_swap = hw_swap_size(dest);
341 dest->size_root = hw_root_size(dest);
342
343 // Determine the size of the data partition.
344 unsigned long long used_space = dest->size_boot + dest->size_swap + dest->size_root;
345
346 // Disk is way too small
347 if (used_space >= dest->size)
348 return -1;
349
350 dest->size_data = dest->size - used_space;
351
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;
355 dest->size_swap = 0;
356 }
357
358 // Set partition names
359 char path[DEV_SIZE];
360 int part_idx = 1;
361
362 snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : "");
363
364 if (dest->size_boot > 0) {
365 dest->part_boot_idx = part_idx;
366
367 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
368 } else
369 *dest->part_boot = '\0';
370
371 if (dest->size_swap > 0)
372 snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++);
373 else
374 *dest->part_swap = '\0';
375
376 // There is always a root partition
377 if (!*dest->part_boot)
378 dest->part_boot_idx = part_idx;
379
380 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
381
382 if (dest->size_data > 0)
383 snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++);
384 else
385 *dest->part_data = '\0';
386
387 // Determine partition table
388 dest->part_table = HW_PART_TABLE_MSDOS;
389
390 // Disks over 2TB need to use GPT
391 if (dest->size >= MB2BYTES(2047 * 1024))
392 dest->part_table = HW_PART_TABLE_GPT;
393
394 // We also use GPT on raid disks by default
395 else if (dest->is_raid)
396 dest->part_table = HW_PART_TABLE_GPT;
397
398 return 0;
399 }
400
401 struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) {
402 struct hw_destination* dest = malloc(sizeof(*dest));
403
404 if (part_type == HW_PART_TYPE_NORMAL) {
405 dest->disk1 = *disks;
406 dest->disk2 = NULL;
407
408 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
409
410 } else if (part_type == HW_PART_TYPE_RAID1) {
411 dest->disk1 = *disks++;
412 dest->disk2 = *disks;
413
414 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
415 }
416
417 // Is this a RAID device?
418 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
419
420 int r = hw_calculate_partition_table(dest);
421 if (r)
422 return NULL;
423
424 // Set default filesystem
425 dest->filesystem = HW_FS_DEFAULT;
426
427 return dest;
428 }
429
430 unsigned long long hw_memory() {
431 FILE* handle = NULL;
432 char line[STRING_SIZE];
433
434 unsigned long long memory = 0;
435
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)) {
440 memory = 0;
441 }
442 }
443
444 fclose(handle);
445 }
446
447 return memory * 1024;
448 }
449
450 int hw_create_partitions(struct hw_destination* dest) {
451 char* cmd = NULL;
452
453 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
454
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);
460
461 unsigned long long part_start = 0 * 1024 * 1024; // 1MB
462
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));
467
468 part_start += dest->size_boot;
469 }
470
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));
475
476 part_start += dest->size_swap;
477 }
478
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));
483
484 part_start += dest->size_root;
485 }
486
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));
491
492 part_start += dest->size_data;
493 }
494
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);
497
498 } else if (dest->part_table == HW_PART_TABLE_GPT) {
499 asprintf(&cmd, "%s disk_set pmbr_boot on", cmd);
500 }
501
502 int r = mysystem(cmd);
503
504 if (cmd)
505 free(cmd);
506
507 return r;
508 }
509
510 static int hw_format_filesystem(const char* path, int fs) {
511 char cmd[STRING_SIZE] = "\0";
512
513 // Swap
514 if (fs == HW_FS_SWAP) {
515 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
516 // ReiserFS
517 } else if (fs == HW_FS_REISERFS) {
518 snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path);
519
520 // EXT4
521 } else if (fs == HW_FS_EXT4) {
522 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path);
523
524 // EXT4 w/o journal
525 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
526 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path);
527 }
528
529 assert(*cmd);
530
531 int r = mysystem(cmd);
532
533 return r;
534 }
535
536 int hw_create_filesystems(struct hw_destination* dest) {
537 int r;
538
539 // boot
540 if (*dest->part_boot) {
541 r = hw_format_filesystem(dest->part_boot, dest->filesystem);
542 if (r)
543 return r;
544 }
545
546 // swap
547 if (*dest->part_swap) {
548 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP);
549 if (r)
550 return r;
551 }
552
553 // root
554 r = hw_format_filesystem(dest->part_root, dest->filesystem);
555 if (r)
556 return r;
557
558 // data
559 if (*dest->part_data) {
560 r = hw_format_filesystem(dest->part_data, dest->filesystem);
561 if (r)
562 return r;
563 }
564
565 return 0;
566 }
567
568 int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
569 char target[STRING_SIZE];
570
571 assert(*prefix == '/');
572
573 const char* filesystem;
574 switch (dest->filesystem) {
575 case HW_FS_REISERFS:
576 filesystem = "reiserfs";
577 break;
578
579 case HW_FS_EXT4:
580 case HW_FS_EXT4_WO_JOURNAL:
581 filesystem = "ext4";
582 break;
583
584 default:
585 assert(0);
586 }
587
588 // root
589 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
590 if (r)
591 return r;
592
593 // boot
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);
597
598 r = hw_mount(dest->part_boot, target, filesystem, 0);
599 if (r) {
600 hw_umount_filesystems(dest, prefix);
601
602 return r;
603 }
604 }
605
606 // data
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);
610
611 r = hw_mount(dest->part_data, target, filesystem, 0);
612 if (r) {
613 hw_umount_filesystems(dest, prefix);
614
615 return r;
616 }
617 }
618
619 // swap
620 if (*dest->part_swap) {
621 r = swapon(dest->part_swap, 0);
622 if (r) {
623 hw_umount_filesystems(dest, prefix);
624
625 return r;
626 }
627 }
628
629 // bind-mount misc filesystems
630 char** otherfs = other_filesystems;
631 while (*otherfs) {
632 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs);
633
634 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
635 r = hw_mount(*otherfs, target, NULL, MS_BIND);
636 if (r) {
637 hw_umount_filesystems(dest, prefix);
638
639 return r;
640 }
641
642 otherfs++;
643 }
644
645 return 0;
646 }
647
648 int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
649 // boot
650 if (*dest->part_boot) {
651 hw_umount(dest->part_boot);
652 }
653
654 // data
655 if (*dest->part_data) {
656 hw_umount(dest->part_data);
657 }
658
659 // root
660 hw_umount(dest->part_root);
661
662 // swap
663 if (*dest->part_swap) {
664 swapoff(dest->part_swap);
665 }
666
667 // misc filesystems
668 char target[STRING_SIZE];
669 char** otherfs = other_filesystems;
670
671 while (*otherfs) {
672 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++);
673 hw_umount(target);
674 }
675
676 return 0;
677 }