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