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