]> git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
installer: Add recurisve mkdir function
[ipfire-2.x.git] / src / installer / hw.c
1 /*#############################################################################
2 # #
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> #
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 <errno.h>
28 #include <fcntl.h>
29 #include <libudev.h>
30 #include <linux/loop.h>
31 #include <math.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <sys/mount.h>
37 #include <sys/stat.h>
38 #include <sys/swap.h>
39 #include <sys/sysinfo.h>
40 #include <sys/utsname.h>
41 #include <unistd.h>
42
43 #include <libsmooth.h>
44
45 #include "hw.h"
46
47 static int system_chroot(const char* output, const char* path, const char* cmd) {
48 char chroot_cmd[STRING_SIZE];
49
50 snprintf(chroot_cmd, sizeof(chroot_cmd), "/usr/sbin/chroot %s %s", path, cmd);
51
52 return mysystem(output, chroot_cmd);
53 }
54
55 struct hw* hw_init() {
56 struct hw* hw = calloc(1, sizeof(*hw));
57 assert(hw);
58
59 // Initialize libudev
60 hw->udev = udev_new();
61 if (!hw->udev) {
62 fprintf(stderr, "Could not create udev instance\n");
63 exit(1);
64 }
65
66 // What architecture are we running on?
67 struct utsname uname_data;
68 int ret = uname(&uname_data);
69 if (ret == 0)
70 snprintf(hw->arch, sizeof(hw->arch), "%s", uname_data.machine);
71
72 // Should we install in EFI mode?
73 if ((strcmp(hw->arch, "x86_64") == 0) || (strcmp(hw->arch, "aarch64") == 0))
74 hw->efi = 1;
75
76 return hw;
77 }
78
79 void hw_free(struct hw* hw) {
80 if (hw->udev)
81 udev_unref(hw->udev);
82
83 free(hw);
84 }
85
86 static int strstartswith(const char* a, const char* b) {
87 return (strncmp(a, b, strlen(b)) == 0);
88 }
89
90 static char loop_device[STRING_SIZE];
91
92 static int setup_loop_device(const char* source, const char* device) {
93 int file_fd = open(source, O_RDWR);
94 if (file_fd < 0)
95 goto ERROR;
96
97 int device_fd = -1;
98 if ((device_fd = open(device, O_RDWR)) < 0)
99 goto ERROR;
100
101 if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0)
102 goto ERROR;
103
104 close(file_fd);
105 close(device_fd);
106
107 return 0;
108
109 ERROR:
110 if (file_fd >= 0)
111 close(file_fd);
112
113 if (device_fd >= 0) {
114 ioctl(device_fd, LOOP_CLR_FD, 0);
115 close(device_fd);
116 }
117
118 return -1;
119 }
120
121 int hw_mount(const char* source, const char* target, const char* fs, int flags) {
122 const char* loop_device = "/dev/loop0";
123
124 // Create target if it does not exist
125 if (access(target, X_OK) != 0)
126 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
127
128 struct stat st;
129 stat(source, &st);
130
131 if (S_ISREG(st.st_mode)) {
132 int r = setup_loop_device(source, loop_device);
133 if (r == 0) {
134 source = loop_device;
135 } else {
136 return -1;
137 }
138 }
139
140 return mount(source, target, fs, flags, NULL);
141 }
142
143 static int hw_bind_mount(const char* source, const char* prefix) {
144 if (!source || !prefix) {
145 errno = EINVAL;
146 return 1;
147 }
148
149 char target[PATH_MAX];
150 int r;
151
152 // Format target
153 r = snprintf(target, sizeof(target) - 1, "%s/%s", prefix, source);
154 if (r < 0)
155 return 1;
156
157 // Ensure target exists
158 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
159
160 return hw_mount(source, target, NULL, MS_BIND);
161 }
162
163 int hw_umount(const char* source, const char* prefix) {
164 char target[PATH_MAX];
165 int r;
166
167 if (prefix)
168 r = snprintf(target, sizeof(target) - 1, "%s/%s", prefix, source);
169 else
170 r = snprintf(target, sizeof(target) - 1, "%s", source);
171 if (r < 0)
172 return r;
173
174 // Perform umount
175 r = umount2(target, 0);
176 if (r) {
177 switch (errno) {
178 // Try again with force if umount wasn't successful
179 case EBUSY:
180 sleep(1);
181
182 r = umount2(target, MNT_FORCE);
183 break;
184
185 // target wasn't a mountpoint. Ignore.
186 case EINVAL:
187 r = 0;
188 break;
189
190 // target doesn't exist
191 case ENOENT:
192 r = 0;
193 break;
194 }
195 }
196
197 return r;
198 }
199
200 static int hw_test_source_medium(const char* path) {
201 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
202
203 if (ret != 0) {
204 // 2nd try, ntfs for a rufus converted usb key
205 ret = hw_mount(path, SOURCE_MOUNT_PATH, "ntfs3", MS_RDONLY);
206 }
207 if (ret != 0) {
208 // 3rd try, vfat for a rufus converted usb key
209 ret = hw_mount(path, SOURCE_MOUNT_PATH, "vfat", MS_RDONLY);
210 }
211
212 // If the source could not be mounted we
213 // cannot proceed.
214 if (ret != 0)
215 return ret;
216
217 // Check if the test file exists.
218 ret = access(SOURCE_TEST_FILE, R_OK);
219
220 // Umount the test device.
221 hw_umount(SOURCE_MOUNT_PATH, NULL);
222
223 return ret;
224 }
225
226 char* hw_find_source_medium(struct hw* hw) {
227 char* ret = NULL;
228
229 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
230
231 udev_enumerate_add_match_subsystem(enumerate, "block");
232 udev_enumerate_scan_devices(enumerate);
233
234 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
235
236 struct udev_list_entry* dev_list_entry;
237 udev_list_entry_foreach(dev_list_entry, devices) {
238 const char* path = udev_list_entry_get_name(dev_list_entry);
239 struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path);
240
241 const char* dev_path = udev_device_get_devnode(dev);
242
243 // Skip everything what we cannot work with
244 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
245 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md"))
246 continue;
247
248 if (hw_test_source_medium(dev_path) == 0) {
249 ret = strdup(dev_path);
250 }
251
252 udev_device_unref(dev);
253
254 // If a suitable device was found the search will end.
255 if (ret)
256 break;
257 }
258
259 udev_enumerate_unref(enumerate);
260
261 return ret;
262 }
263
264 static struct hw_disk** hw_create_disks() {
265 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
266
267 return ret;
268 }
269
270 static unsigned long long hw_block_device_get_size(const char* dev) {
271 int fd = open(dev, O_RDONLY);
272 if (fd < 0)
273 return 0;
274
275 unsigned long long size = blkid_get_dev_size(fd);
276 close(fd);
277
278 return size;
279 }
280
281 struct hw_disk** hw_find_disks(struct hw* hw, const char* sourcedrive) {
282 struct hw_disk** ret = hw_create_disks();
283 struct hw_disk** disks = ret;
284
285 // Determine the disk device of source if it is a partition
286 char* sourcedisk = NULL;
287 char syssource[PATH_MAX];
288 (void)snprintf(syssource, sizeof(syssource) - 1, "/sys/class/block/%s", sourcedrive + 5);
289 struct udev_device* s_dev = udev_device_new_from_syspath(hw->udev, syssource);
290 const char* s_devtype = udev_device_get_property_value(s_dev, "DEVTYPE");
291 if (s_devtype && (strcmp(s_devtype, "partition") == 0)) {
292 struct udev_device* p_dev = udev_device_get_parent_with_subsystem_devtype(s_dev,"block","disk");
293 if (p_dev) {
294 sourcedisk = udev_device_get_devnode(p_dev);
295 }
296 }
297 if (!sourcedisk) sourcedisk = sourcedrive;
298
299 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
300
301 udev_enumerate_add_match_subsystem(enumerate, "block");
302 udev_enumerate_scan_devices(enumerate);
303
304 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
305
306 struct udev_list_entry* dev_list_entry;
307 unsigned int i = HW_MAX_DISKS;
308 udev_list_entry_foreach(dev_list_entry, devices) {
309 const char* path = udev_list_entry_get_name(dev_list_entry);
310 struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path);
311
312 const char* dev_path = udev_device_get_devnode(dev);
313
314 // Skip everything what we cannot work with
315 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
316 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") ||
317 strstartswith(dev_path, "/dev/md")) {
318 udev_device_unref(dev);
319 continue;
320 }
321
322 // Skip sourcedisk if we need to
323 if (sourcedisk && (strcmp(dev_path, sourcedisk) == 0)) {
324 udev_device_unref(dev);
325 continue;
326 }
327
328 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
329 const char* devtype = udev_device_get_property_value(dev, "DEVTYPE");
330 if (devtype && (strcmp(devtype, "disk") != 0)) {
331 udev_device_unref(dev);
332 continue;
333 }
334
335 // Skip devices with a size of zero
336 unsigned long long size = hw_block_device_get_size(dev_path);
337 if (size == 0) {
338 udev_device_unref(dev);
339 continue;
340 }
341
342 struct hw_disk* disk = malloc(sizeof(*disk));
343 if (disk == NULL)
344 return NULL;
345
346 disk->ref = 1;
347
348 strncpy(disk->path, dev_path, sizeof(disk->path));
349 const char* p = disk->path + 5;
350
351 disk->size = size;
352
353 // Vendor
354 const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR");
355 if (!vendor)
356 vendor = udev_device_get_sysattr_value(dev, "vendor");
357 if (!vendor)
358 vendor = udev_device_get_sysattr_value(dev, "manufacturer");
359
360 if (vendor)
361 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
362 else
363 *disk->vendor = '\0';
364
365 // Model
366 const char* model = udev_device_get_property_value(dev, "ID_MODEL");
367 if (!model)
368 model = udev_device_get_sysattr_value(dev, "model");
369 if (!model)
370 model = udev_device_get_sysattr_value(dev, "product");
371
372 if (model)
373 strncpy(disk->model, model, sizeof(disk->model));
374 else
375 *disk->model = '\0';
376
377 // Format description
378 char size_str[STRING_SIZE];
379 snprintf(size_str, sizeof(size_str), "%4.1fGB", (double)disk->size / pow(1024, 3));
380
381 if (*disk->vendor && *disk->model) {
382 snprintf(disk->description, sizeof(disk->description),
383 "%s - %s - %s - %s", size_str, p, disk->vendor, disk->model);
384
385 } else if (*disk->vendor || *disk->model) {
386 snprintf(disk->description, sizeof(disk->description),
387 "%s - %s - %s", size_str, p, (*disk->vendor) ? disk->vendor : disk->model);
388
389 } else {
390 snprintf(disk->description, sizeof(disk->description),
391 "%s - %s", size_str, p);
392 }
393
394 // Cut off the description string after 40 characters
395 disk->description[41] = '\0';
396
397 *disks++ = disk;
398
399 if (--i == 0)
400 break;
401
402 udev_device_unref(dev);
403 }
404
405 udev_enumerate_unref(enumerate);
406
407 *disks = NULL;
408
409 return ret;
410 }
411
412 void hw_free_disks(struct hw_disk** disks) {
413 struct hw_disk** disk = disks;
414
415 while (*disk != NULL) {
416 if (--(*disk)->ref == 0)
417 free(*disk);
418
419 disk++;
420 }
421
422 free(disks);
423 }
424
425 unsigned int hw_count_disks(const struct hw_disk** disks) {
426 unsigned int ret = 0;
427
428 while (*disks++)
429 ret++;
430
431 return ret;
432 }
433
434 struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) {
435 struct hw_disk** ret = hw_create_disks();
436 struct hw_disk** selected_disks = ret;
437
438 unsigned int num_disks = hw_count_disks((const struct hw_disk**)disks);
439
440 for (unsigned int i = 0; i < num_disks; i++) {
441 if (!selection || selection[i]) {
442 struct hw_disk *selected_disk = disks[i];
443 selected_disk->ref++;
444
445 *selected_disks++ = selected_disk;
446 }
447 }
448
449 // Set sentinel
450 *selected_disks = NULL;
451
452 return ret;
453 }
454
455 struct hw_disk** hw_select_first_disk(const struct hw_disk** disks) {
456 struct hw_disk** ret = hw_create_disks();
457 struct hw_disk** selected_disks = ret;
458
459 unsigned int num_disks = hw_count_disks(disks);
460 assert(num_disks > 0);
461
462 for (unsigned int i = 0; i < num_disks; i++) {
463 struct hw_disk *disk = disks[i];
464 disk->ref++;
465
466 *selected_disks++ = disk;
467 break;
468 }
469
470 // Set sentinel
471 *selected_disks = NULL;
472
473 return ret;
474 }
475
476 static unsigned long long hw_swap_size(struct hw_destination* dest) {
477 unsigned long long memory = hw_memory();
478
479 unsigned long long swap_size = memory / 4;
480
481 // Min. swap size is 128MB
482 if (swap_size < MB2BYTES(128))
483 swap_size = MB2BYTES(128);
484
485 // Cap swap size to 1GB
486 else if (swap_size > MB2BYTES(1024))
487 swap_size = MB2BYTES(1024);
488
489 return swap_size;
490 }
491
492 static unsigned long long hw_boot_size(struct hw_destination* dest) {
493 return MB2BYTES(512);
494 }
495
496 static int hw_device_has_p_suffix(const struct hw_destination* dest) {
497 // All RAID devices have the p suffix.
498 if (dest->is_raid)
499 return 1;
500
501 // Devices with a number at the end have the p suffix, too.
502 // e.g. mmcblk0, cciss0
503 unsigned int last_char = strlen(dest->path) - 1;
504 if ((dest->path[last_char] >= '0') && (dest->path[last_char] <= '9'))
505 return 1;
506
507 return 0;
508 }
509
510 static int hw_calculate_partition_table(struct hw* hw, struct hw_destination* dest, int disable_swap) {
511 char path[DEV_SIZE];
512 int part_idx = 1;
513
514 snprintf(path, sizeof(path), "%s%s", dest->path,
515 hw_device_has_p_suffix(dest) ? "p" : "");
516 dest->part_boot_idx = 0;
517
518 // Determine the size of the target block device
519 if (dest->is_raid) {
520 dest->size = (dest->disk1->size >= dest->disk2->size) ?
521 dest->disk2->size : dest->disk1->size;
522
523 // The RAID will install some metadata at the end of the disk
524 // and we will save up some space for that.
525 dest->size -= MB2BYTES(2);
526 } else {
527 dest->size = dest->disk1->size;
528 }
529
530 // As we add some extra space before the beginning of the first
531 // partition, we need to substract that here.
532 dest->size -= MB2BYTES(1);
533
534 // Add some more space for partition tables, etc.
535 dest->size -= MB2BYTES(1);
536
537 // The disk has to have at least 2GB
538 if (dest->size <= MB2BYTES(2048))
539 return -1;
540
541 // Determine partition table
542 dest->part_table = HW_PART_TABLE_MSDOS;
543
544 // Disks over 2TB need to use GPT
545 if (dest->size >= MB2BYTES(2047 * 1024))
546 dest->part_table = HW_PART_TABLE_GPT;
547
548 // We also use GPT on raid disks by default
549 else if (dest->is_raid)
550 dest->part_table = HW_PART_TABLE_GPT;
551
552 // When using GPT, GRUB2 needs a little bit of space to put
553 // itself in.
554 if (dest->part_table == HW_PART_TABLE_GPT) {
555 snprintf(dest->part_bootldr, sizeof(dest->part_bootldr),
556 "%s%d", path, part_idx);
557
558 dest->size_bootldr = MB2BYTES(4);
559
560 dest->part_boot_idx = part_idx++;
561 } else {
562 *dest->part_bootldr = '\0';
563 dest->size_bootldr = 0;
564 }
565
566 // Disable seperate boot partition for BTRFS installations.
567 if(dest->filesystem == HW_FS_BTRFS) {
568 dest->size_boot = 0;
569 } else {
570 dest->size_boot = hw_boot_size(dest);
571 }
572
573 // Create an EFI partition when running in EFI mode
574 if (hw->efi)
575 dest->size_boot_efi = MB2BYTES(32);
576 else
577 dest->size_boot_efi = 0;
578
579 // Determine the size of the data partition.
580 unsigned long long space_left = dest->size - \
581 (dest->size_bootldr + dest->size_boot + dest->size_boot_efi);
582
583 // If we have less than 2GB left, we disable swap
584 if (space_left <= MB2BYTES(2048))
585 disable_swap = 1;
586
587 // Should we use swap?
588 if (disable_swap)
589 dest->size_swap = 0;
590 else
591 dest->size_swap = hw_swap_size(dest);
592
593 // Subtract swap
594 space_left -= dest->size_swap;
595
596 // Root is getting what ever is left
597 dest->size_root = space_left;
598
599 // Set partition names
600 if (dest->size_boot > 0) {
601 if (dest->part_boot_idx == 0)
602 dest->part_boot_idx = part_idx;
603
604 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
605 } else
606 *dest->part_boot = '\0';
607
608 if (dest->size_boot_efi > 0) {
609 dest->part_boot_efi_idx = part_idx;
610
611 snprintf(dest->part_boot_efi, sizeof(dest->part_boot_efi),
612 "%s%d", path, part_idx++);
613 } else {
614 *dest->part_boot_efi = '\0';
615 dest->part_boot_efi_idx = 0;
616 }
617
618 if (dest->size_swap > 0)
619 snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++);
620 else
621 *dest->part_swap = '\0';
622
623 // There is always a root partition
624 if (dest->part_boot_idx == 0)
625 dest->part_boot_idx = part_idx;
626
627 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
628
629 return 0;
630 }
631
632 struct hw_destination* hw_make_destination(struct hw* hw, int part_type, struct hw_disk** disks, int disable_swap) {
633 struct hw_destination* dest = malloc(sizeof(*dest));
634
635 if (part_type == HW_PART_TYPE_NORMAL) {
636 dest->disk1 = *disks;
637 dest->disk2 = NULL;
638
639 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
640
641 } else if (part_type == HW_PART_TYPE_RAID1) {
642 dest->disk1 = *disks++;
643 dest->disk2 = *disks;
644 dest->raid_level = 1;
645
646 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
647 }
648
649 // Is this a RAID device?
650 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
651
652 int r = hw_calculate_partition_table(hw, dest, disable_swap);
653 if (r)
654 return NULL;
655
656 // Set default filesystem
657 dest->filesystem = HW_FS_DEFAULT;
658
659 return dest;
660 }
661
662 unsigned long long hw_memory() {
663 struct sysinfo si;
664
665 int r = sysinfo(&si);
666 if (r < 0)
667 return 0;
668
669 return si.totalram;
670 }
671
672 static int hw_zero_out_device(const char* path, int bytes) {
673 char block[512];
674 memset(block, 0, sizeof(block));
675
676 int blocks = bytes / sizeof(block);
677
678 int fd = open(path, O_WRONLY);
679 if (fd < 0)
680 return -1;
681
682 unsigned int bytes_written = 0;
683 while (blocks-- > 0) {
684 bytes_written += write(fd, block, sizeof(block));
685 }
686
687 fsync(fd);
688 close(fd);
689
690 return bytes_written;
691 }
692
693 static int try_open(const char* path) {
694 FILE* f = fopen(path, "r");
695 if (f) {
696 fclose(f);
697 return 0;
698 }
699
700 return -1;
701 }
702
703 int hw_create_partitions(struct hw_destination* dest, const char* output) {
704 // Before we write a new partition table to the disk, we will erase
705 // the first couple of megabytes at the beginning of the device to
706 // get rid of all left other things like bootloaders and partition tables.
707 // This solves some problems when changing from MBR to GPT partitions or
708 // the other way around.
709 int r = hw_zero_out_device(dest->path, MB2BYTES(10));
710 if (r <= 0)
711 return r;
712
713 char* cmd = NULL;
714 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
715
716 // Set partition type
717 if (dest->part_table == HW_PART_TABLE_MSDOS)
718 asprintf(&cmd, "%s mklabel msdos", cmd);
719 else if (dest->part_table == HW_PART_TABLE_GPT)
720 asprintf(&cmd, "%s mklabel gpt", cmd);
721
722 unsigned long long part_start = MB2BYTES(1);
723
724 if (*dest->part_bootldr) {
725 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
726 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary",
727 part_start, part_start + dest->size_bootldr - 1);
728
729 part_start += dest->size_bootldr;
730 }
731
732 if (*dest->part_boot) {
733 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
734 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary",
735 part_start, part_start + dest->size_boot - 1);
736
737 part_start += dest->size_boot;
738 }
739
740 if (*dest->part_boot_efi) {
741 asprintf(&cmd, "%s mkpart %s fat32 %lluB %lluB", cmd,
742 (dest->part_table == HW_PART_TABLE_GPT) ? "ESP" : "primary",
743 part_start, part_start + dest->size_boot_efi - 1);
744
745 part_start += dest->size_boot_efi;
746 }
747
748 if (*dest->part_swap) {
749 asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd,
750 (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary",
751 part_start, part_start + dest->size_swap - 1);
752
753 part_start += dest->size_swap;
754 }
755
756 if (*dest->part_root) {
757 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
758 (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary",
759 part_start, part_start + dest->size_root - 1);
760
761 part_start += dest->size_root;
762 }
763
764 if (dest->part_boot_idx > 0)
765 asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
766
767 if (dest->part_boot_efi_idx > 0)
768 asprintf(&cmd, "%s set %d esp on", cmd, dest->part_boot_efi_idx);
769
770 if (dest->part_table == HW_PART_TABLE_GPT) {
771 if (*dest->part_bootldr) {
772 asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx);
773 }
774 }
775
776 r = mysystem(output, cmd);
777
778 // Wait until the system re-read the partition table
779 if (r == 0) {
780 unsigned int counter = 10;
781
782 while (counter-- > 0) {
783 sleep(1);
784
785 if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0))
786 continue;
787
788 if (*dest->part_boot && (try_open(dest->part_boot) != 0))
789 continue;
790
791 if (*dest->part_boot_efi && (try_open(dest->part_boot_efi) != 0))
792 continue;
793
794 if (*dest->part_swap && (try_open(dest->part_swap) != 0))
795 continue;
796
797 if (*dest->part_root && (try_open(dest->part_root) != 0))
798 continue;
799
800 // All partitions do exist, exiting the loop.
801 break;
802 }
803 }
804
805 if (cmd)
806 free(cmd);
807
808 return r;
809 }
810
811 static int hw_format_filesystem(const char* path, int fs, const char* output) {
812 char cmd[STRING_SIZE] = "\0";
813 int r;
814
815 // Swap
816 if (fs == HW_FS_SWAP) {
817 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
818
819 // EXT4
820 } else if (fs == HW_FS_EXT4) {
821 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 %s", path);
822
823 // EXT4 w/o journal
824 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
825 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path);
826
827 // XFS
828 } else if (fs == HW_FS_XFS) {
829 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path);
830
831 // BTRFS
832 } else if (fs == HW_FS_BTRFS) {
833 r = hw_create_btrfs_layout(path, output);
834
835 return r;
836
837 // FAT32
838 } else if (fs == HW_FS_FAT32) {
839 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.vfat %s", path);
840 }
841
842 assert(*cmd);
843
844 r = mysystem(output, cmd);
845
846 return r;
847 }
848
849 int hw_create_filesystems(struct hw_destination* dest, const char* output) {
850 int r;
851
852 // boot
853 if (*dest->part_boot) {
854 r = hw_format_filesystem(dest->part_boot, dest->filesystem, output);
855 if (r)
856 return r;
857 }
858
859 // ESP
860 if (*dest->part_boot_efi) {
861 r = hw_format_filesystem(dest->part_boot_efi, HW_FS_FAT32, output);
862 if (r)
863 return r;
864 }
865
866 // swap
867 if (*dest->part_swap) {
868 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output);
869 if (r)
870 return r;
871 }
872
873 // root
874 r = hw_format_filesystem(dest->part_root, dest->filesystem, output);
875 if (r)
876 return r;
877
878 return 0;
879 }
880
881 int hw_create_btrfs_layout(const char* path, const char* output) {
882 const struct btrfs_subvolumes* subvolume = NULL;
883 char cmd[STRING_SIZE];
884 char volume[STRING_SIZE];
885 int r;
886
887 r = snprintf(cmd, sizeof(cmd), "/usr/bin/mkfs.btrfs -f %s", path);
888 if (r < 0) {
889 return r;
890 }
891
892 // Create the main BTRFS file system.
893 r = mysystem(output, cmd);
894
895 if (r) {
896 return r;
897 }
898
899
900 // We need to mount the FS in order to create any subvolumes.
901 r = hw_mount(path, DESTINATION_MOUNT_PATH, "btrfs", 0);
902
903 if (r) {
904 return r;
905 }
906
907 // Loop through the list of subvolumes to create.
908 for ( subvolume = btrfs_subvolumes; subvolume->name; subvolume++ ) {
909 r = snprintf(volume, sizeof(volume), "%s", subvolume->name);
910
911 // Abort if snprintf fails.
912 if (r < 0) {
913 return r;
914 }
915
916 // Call function to create the subvolume
917 r = hw_create_btrfs_subvolume(output, volume);
918
919 if (r) {
920 return r;
921 }
922 }
923
924 // Umount the main BTRFS after subvolume creation.
925 r = hw_umount(DESTINATION_MOUNT_PATH, 0);
926
927 if (r) {
928 return r;
929 }
930
931 return 0;
932 }
933
934 int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
935 char target[STRING_SIZE];
936
937 assert(*prefix == '/');
938
939 const char* filesystem;
940 switch (dest->filesystem) {
941 case HW_FS_EXT4:
942 case HW_FS_EXT4_WO_JOURNAL:
943 filesystem = "ext4";
944 break;
945
946 case HW_FS_XFS:
947 filesystem = "xfs";
948 break;
949
950 case HW_FS_BTRFS:
951 filesystem = "btrfs";
952 break;
953
954 case HW_FS_FAT32:
955 filesystem = "vfat";
956 break;
957
958 default:
959 assert(0);
960 }
961
962 // root
963 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
964 if (r)
965 return r;
966
967 // boot
968 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT);
969 r = mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
970
971 if (r) {
972 hw_umount_filesystems(dest, prefix);
973
974 return r;
975 }
976
977 if (*dest->part_boot) {
978 r = hw_mount(dest->part_boot, target, filesystem, 0);
979 if (r) {
980 hw_umount_filesystems(dest, prefix);
981
982 return r;
983 }
984 }
985
986 // ESP
987 if (*dest->part_boot_efi) {
988 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT_EFI);
989 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
990
991 r = hw_mount(dest->part_boot_efi, target, "vfat", 0);
992 if (r) {
993 hw_umount_filesystems(dest, prefix);
994
995 return r;
996 }
997 }
998
999 // swap
1000 if (*dest->part_swap) {
1001 r = swapon(dest->part_swap, 0);
1002 if (r) {
1003 hw_umount_filesystems(dest, prefix);
1004
1005 return r;
1006 }
1007 }
1008
1009 // bind-mount misc filesystems
1010 r = hw_bind_mount("/dev", prefix);
1011 if (r)
1012 return r;
1013
1014 r = hw_bind_mount("/proc", prefix);
1015 if (r)
1016 return r;
1017
1018 r = hw_bind_mount("/sys", prefix);
1019 if (r)
1020 return r;
1021
1022 r = hw_bind_mount("/sys/firmware/efi/efivars", prefix);
1023 if (r && errno != ENOENT)
1024 return r;
1025
1026 return 0;
1027 }
1028
1029 int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
1030 int r;
1031 char target[STRING_SIZE];
1032
1033 // Write all buffers to disk before umounting
1034 hw_sync();
1035
1036 // ESP
1037 if (*dest->part_boot_efi) {
1038 r = hw_umount(HW_PATH_BOOT_EFI, prefix);
1039 if (r)
1040 return -1;
1041 }
1042
1043 // boot
1044 if (*dest->part_boot) {
1045 r = hw_umount(HW_PATH_BOOT, prefix);
1046 if (r)
1047 return -1;
1048 }
1049
1050 // swap
1051 if (*dest->part_swap) {
1052 swapoff(dest->part_swap);
1053 }
1054
1055 // misc filesystems
1056 r = hw_umount("/sys/firmware/efi/efivars", prefix);
1057 if (r)
1058 return -1;
1059
1060 r = hw_umount("/sys", prefix);
1061 if (r)
1062 return -1;
1063
1064 r = hw_umount("/proc", prefix);
1065 if (r)
1066 return -1;
1067
1068 r = hw_umount("/dev", prefix);
1069 if (r)
1070 return -1;
1071
1072 // root
1073 r = hw_umount(prefix, NULL);
1074 if (r)
1075 return -1;
1076
1077 return 0;
1078 }
1079
1080 int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) {
1081 char cmd[STRING_SIZE];
1082
1083 hw_stop_all_raid_arrays(output);
1084 hw_stop_all_raid_arrays(output);
1085
1086 if (dest->disk1) {
1087 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path);
1088 mysystem(output, cmd);
1089 }
1090
1091 if (dest->disk2) {
1092 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path);
1093 mysystem(output, cmd);
1094 }
1095
1096 return 0;
1097 }
1098
1099 int hw_setup_raid(struct hw_destination* dest, const char* output) {
1100 char* cmd = NULL;
1101 int r;
1102
1103 assert(dest->is_raid);
1104
1105 // Stop all RAID arrays that might be around (again).
1106 // It seems that there is some sort of race-condition with udev re-enabling
1107 // the raid arrays and therefore locking the disks.
1108 r = hw_destroy_raid_superblocks(dest, output);
1109
1110 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1111 RAID_METADATA, dest->path);
1112
1113 switch (dest->raid_level) {
1114 case 1:
1115 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
1116 break;
1117
1118 default:
1119 assert(0);
1120 }
1121
1122 if (dest->disk1) {
1123 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
1124
1125 // Clear all data at the beginning
1126 r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10));
1127 if (r <= 0)
1128 return r;
1129 }
1130
1131 if (dest->disk2) {
1132 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
1133
1134 // Clear all data at the beginning
1135 r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10));
1136 if (r <= 0)
1137 return r;
1138 }
1139
1140 r = mysystem(output, cmd);
1141 free(cmd);
1142
1143 // Wait a moment until the device has been properly brought up
1144 if (r == 0) {
1145 unsigned int counter = 10;
1146 while (counter-- > 0) {
1147 sleep(1);
1148
1149 // If the raid device has not yet been properly brought up,
1150 // opening it will fail with the message: Device or resource busy
1151 // Hence we will wait a bit until it becomes usable.
1152 if (try_open(dest->path) == 0)
1153 break;
1154 }
1155 }
1156
1157 return r;
1158 }
1159
1160 int hw_stop_all_raid_arrays(const char* output) {
1161 return mysystem(output, "/sbin/mdadm --stop --scan --verbose");
1162 }
1163
1164 int hw_install_bootloader(struct hw* hw, struct hw_destination* dest, const char* output) {
1165 char cmd[STRING_SIZE];
1166
1167 snprintf(cmd, sizeof(cmd), "/usr/bin/install-bootloader %s", dest->path);
1168 int r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
1169 if (r)
1170 return r;
1171
1172 hw_sync();
1173
1174 return 0;
1175 }
1176
1177 static char* hw_get_uuid(const char* dev) {
1178 blkid_probe p = blkid_new_probe_from_filename(dev);
1179 const char* buffer = NULL;
1180 char* uuid = NULL;
1181
1182 if (!p)
1183 return NULL;
1184
1185 blkid_do_probe(p);
1186 blkid_probe_lookup_value(p, "UUID", &buffer, NULL);
1187
1188 if (buffer)
1189 uuid = strdup(buffer);
1190
1191 blkid_free_probe(p);
1192
1193 return uuid;
1194 }
1195
1196 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1197
1198 int hw_write_fstab(struct hw_destination* dest) {
1199 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w");
1200 if (!f)
1201 return -1;
1202
1203 char* uuid = NULL;
1204
1205 // boot
1206 if (*dest->part_boot) {
1207 uuid = hw_get_uuid(dest->part_boot);
1208
1209 if (uuid) {
1210 fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
1211 free(uuid);
1212 }
1213 }
1214
1215 // ESP
1216 if (*dest->part_boot_efi) {
1217 uuid = hw_get_uuid(dest->part_boot_efi);
1218
1219 if (uuid) {
1220 fprintf(f, FSTAB_FMT, uuid, "/boot/efi", "auto", "defaults", 1, 2);
1221 free(uuid);
1222 }
1223 }
1224
1225
1226 // swap
1227 if (*dest->part_swap) {
1228 uuid = hw_get_uuid(dest->part_swap);
1229
1230 if (uuid) {
1231 fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0);
1232 free(uuid);
1233 }
1234 }
1235
1236 // root
1237 uuid = hw_get_uuid(dest->part_root);
1238 if (uuid) {
1239 fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1);
1240 free(uuid);
1241 }
1242
1243 fclose(f);
1244
1245 return 0;
1246 }
1247
1248 void hw_sync() {
1249 sync();
1250 sync();
1251 sync();
1252 }
1253
1254 int hw_start_networking(const char* output) {
1255 return mysystem(output, "/usr/bin/start-networking.sh");
1256 }
1257
1258 char* hw_find_backup_file(const char* output, const char* search_path) {
1259 char path[STRING_SIZE];
1260
1261 snprintf(path, sizeof(path), "%s/backup.ipf", search_path);
1262 int r = access(path, R_OK);
1263
1264 if (r == 0)
1265 return strdup(path);
1266
1267 return NULL;
1268 }
1269
1270 int hw_restore_backup(const char* output, const char* backup_path, const char* destination) {
1271 char command[STRING_SIZE];
1272
1273 snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s "
1274 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1275 backup_path, destination, destination, destination);
1276 int rc = mysystem(output, command);
1277
1278 if (rc)
1279 return -1;
1280
1281 return 0;
1282 }
1283
1284 int hw_mkdir(const char *dir) {
1285 char tmp[STRING_SIZE];
1286 char *p = NULL;
1287 size_t len;
1288 int r;
1289
1290 snprintf(tmp, sizeof(tmp),"%s",dir);
1291 len = strlen(tmp);
1292
1293 if (tmp[len - 1] == '/') {
1294 tmp[len - 1] = 0;
1295 }
1296
1297 for (p = tmp + 1; *p; p++) {
1298 if (*p == '/') {
1299 *p = 0;
1300
1301 // Create target if it does not exist
1302 if (access(tmp, X_OK) != 0) {
1303 r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO);
1304
1305 if (r) {
1306 return r;
1307 }
1308 }
1309
1310 *p = '/';
1311 }
1312 }
1313
1314 // Create target if it does not exist
1315 if (access(tmp, X_OK) != 0) {
1316 r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO);
1317
1318 if (r) {
1319 return r;
1320 }
1321 }
1322
1323 return 0;
1324 }
1325
1326
1327 int hw_create_btrfs_subvolume(const char* output, const char* subvolume) {
1328 char command [STRING_SIZE];
1329 int r;
1330
1331 // Abort if the command could not be assigned.
1332 r = snprintf(command, sizeof(command), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH, subvolume);
1333 if (r < 0) {
1334 return r;
1335 }
1336
1337 // Create the subvolume
1338 r = mysystem(output, command);
1339
1340 if (r) {
1341 return r;
1342 }
1343
1344 return 0;
1345 }