]> git.ipfire.org Git - ipfire-2.x.git/blame - src/installer/hw.c
installer: Make btrfs functions static
[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
f0fa1795 57struct hw* hw_init() {
5fb499f1 58 struct hw* hw = calloc(1, sizeof(*hw));
f0fa1795
MT
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
92e78233
MT
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
8fbadfc7 74 // Should we install in EFI mode?
7dc7880d
MT
75 if ((strcmp(hw->arch, "x86_64") == 0) || (strcmp(hw->arch, "aarch64") == 0))
76 hw->efi = 1;
5fb499f1 77
f0fa1795
MT
78 return hw;
79}
80
81void hw_free(struct hw* hw) {
82 if (hw->udev)
83 udev_unref(hw->udev);
84
85 free(hw);
86}
87
88static int strstartswith(const char* a, const char* b) {
89 return (strncmp(a, b, strlen(b)) == 0);
90}
91
c0511f3a
MT
92static char loop_device[STRING_SIZE];
93
94static 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
111ERROR:
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
25fcce25 123int hw_mount(const char* source, const char* target, const char* fs, int flags) {
c0511f3a
MT
124 const char* loop_device = "/dev/loop0";
125
85f2892b
MT
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
c0511f3a
MT
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
366b40c7 142 return mount(source, target, fs, flags, NULL);
f0fa1795
MT
143}
144
ced15fdf
MT
145static 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);
ade96ba8 161
ced15fdf
MT
162 return hw_mount(source, target, NULL, MS_BIND);
163}
164
165int hw_umount(const char* source, const char* prefix) {
166 char target[PATH_MAX];
167 int r;
ade96ba8 168
ced15fdf
MT
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;
91588cb4
MT
191
192 // target doesn't exist
193 case ENOENT:
194 r = 0;
195 break;
ced15fdf 196 }
ade96ba8
MT
197 }
198
199 return r;
f0fa1795
MT
200}
201
202static int hw_test_source_medium(const char* path) {
25fcce25 203 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
f0fa1795 204
68a50dd1
AF
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
f0fa1795
MT
214 // If the source could not be mounted we
215 // cannot proceed.
5315fae6 216 if (ret != 0)
f0fa1795
MT
217 return ret;
218
219 // Check if the test file exists.
5315fae6 220 ret = access(SOURCE_TEST_FILE, R_OK);
f0fa1795
MT
221
222 // Umount the test device.
ced15fdf 223 hw_umount(SOURCE_MOUNT_PATH, NULL);
f0fa1795 224
fb76fc51 225 return ret;
f0fa1795
MT
226}
227
228char* 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") ||
4a0d9bef 247 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md"))
f0fa1795
MT
248 continue;
249
5315fae6 250 if (hw_test_source_medium(dev_path) == 0) {
f0fa1795
MT
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}
d7dd283b
MT
265
266static struct hw_disk** hw_create_disks() {
267 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
268
269 return ret;
270}
271
272static 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
ee00d203 283struct hw_disk** hw_find_disks(struct hw* hw, const char* sourcedrive) {
d7dd283b
MT
284 struct hw_disk** ret = hw_create_disks();
285 struct hw_disk** disks = ret;
286
68a50dd1 287 // Determine the disk device of source if it is a partition
16640487 288 const char* sourcedisk = NULL;
68a50dd1
AF
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
d7dd283b
MT
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") ||
4a0d9bef
MT
318 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") ||
319 strstartswith(dev_path, "/dev/md")) {
d7dd283b
MT
320 udev_device_unref(dev);
321 continue;
322 }
323
68a50dd1
AF
324 // Skip sourcedisk if we need to
325 if (sourcedisk && (strcmp(dev_path, sourcedisk) == 0)) {
d7dd283b
MT
326 udev_device_unref(dev);
327 continue;
328 }
329
ee00d203
MT
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)) {
d7dd283b
MT
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));
0c8b2d99 351 const char* p = disk->path + 5;
d7dd283b
MT
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");
d7dd283b 361
4c812463
MT
362 if (vendor)
363 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
364 else
365 *disk->vendor = '\0';
d7dd283b
MT
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");
d7dd283b 373
4c812463
MT
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));
d7dd283b 382
4c812463
MT
383 if (*disk->vendor && *disk->model) {
384 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 385 "%s - %s - %s - %s", size_str, p, disk->vendor, disk->model);
4c812463
MT
386
387 } else if (*disk->vendor || *disk->model) {
388 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 389 "%s - %s - %s", size_str, p, (*disk->vendor) ? disk->vendor : disk->model);
4c812463
MT
390
391 } else {
392 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 393 "%s - %s", size_str, p);
4c812463 394 }
d7dd283b 395
29afd408
MT
396 // Cut off the description string after 40 characters
397 disk->description[41] = '\0';
398
d7dd283b
MT
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
414void 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
16640487 427unsigned int hw_count_disks(struct hw_disk** disks) {
d7dd283b
MT
428 unsigned int ret = 0;
429
430 while (*disks++)
431 ret++;
432
433 return ret;
434}
435
436struct 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
16640487 440 unsigned int num_disks = hw_count_disks(disks);
d7dd283b
MT
441
442 for (unsigned int i = 0; i < num_disks; i++) {
ee43f517 443 if (!selection || selection[i]) {
d7dd283b
MT
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
16640487 457struct hw_disk** hw_select_first_disk(struct hw_disk** disks) {
a3e135c8
MT
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
25fcce25
MT
478static 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
25fcce25 494static unsigned long long hw_boot_size(struct hw_destination* dest) {
ffdc6fbb 495 return MB2BYTES(512);
25fcce25
MT
496}
497
a691d4b3
MT
498static 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
126d3570 505 unsigned int last_char = strlen(dest->path) - 1;
a691d4b3
MT
506 if ((dest->path[last_char] >= '0') && (dest->path[last_char] <= '9'))
507 return 1;
508
509 return 0;
510}
511
92e78233 512static int hw_calculate_partition_table(struct hw* hw, struct hw_destination* dest, int disable_swap) {
48d6a112
MT
513 char path[DEV_SIZE];
514 int part_idx = 1;
515
a691d4b3
MT
516 snprintf(path, sizeof(path), "%s%s", dest->path,
517 hw_device_has_p_suffix(dest) ? "p" : "");
48d6a112
MT
518 dest->part_boot_idx = 0;
519
25fcce25
MT
520 // Determine the size of the target block device
521 if (dest->is_raid) {
522 dest->size = (dest->disk1->size >= dest->disk2->size) ?
5315fae6 523 dest->disk2->size : dest->disk1->size;
22581f51
MT
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);
25fcce25
MT
528 } else {
529 dest->size = dest->disk1->size;
530 }
531
802a123b
MT
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
c577571e
MT
539 // The disk has to have at least 2GB
540 if (dest->size <= MB2BYTES(2048))
541 return -1;
542
48d6a112
MT
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.
5689fc72 556 if (dest->part_table == HW_PART_TABLE_GPT) {
48d6a112
MT
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
58a46f0b
SS
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 }
c577571e 574
92e78233
MT
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
c577571e
MT
581 // Determine the size of the data partition.
582 unsigned long long space_left = dest->size - \
92e78233 583 (dest->size_bootldr + dest->size_boot + dest->size_boot_efi);
c577571e
MT
584
585 // If we have less than 2GB left, we disable swap
586 if (space_left <= MB2BYTES(2048))
587 disable_swap = 1;
25fcce25 588
a8fca245
MT
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
c577571e
MT
595 // Subtract swap
596 space_left -= dest->size_swap;
25fcce25 597
c577571e
MT
598 // Root is getting what ever is left
599 dest->size_root = space_left;
25fcce25
MT
600
601 // Set partition names
25fcce25 602 if (dest->size_boot > 0) {
48d6a112
MT
603 if (dest->part_boot_idx == 0)
604 dest->part_boot_idx = part_idx;
25fcce25
MT
605
606 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
607 } else
608 *dest->part_boot = '\0';
609
92e78233
MT
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++);
58e840bd 615 } else {
92e78233 616 *dest->part_boot_efi = '\0';
58e840bd
MT
617 dest->part_boot_efi_idx = 0;
618 }
92e78233 619
25fcce25
MT
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
48d6a112 626 if (dest->part_boot_idx == 0)
25fcce25
MT
627 dest->part_boot_idx = part_idx;
628
629 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
630
25fcce25
MT
631 return 0;
632}
633
ad73749d
SS
634struct hw_destination* hw_make_destination(struct hw* hw, int part_type, struct hw_disk** disks,
635 int disable_swap, int filesystem) {
d7dd283b
MT
636 struct hw_destination* dest = malloc(sizeof(*dest));
637
ad73749d
SS
638 // Assign filesystem
639 dest->filesystem = filesystem;
640
d7dd283b
MT
641 if (part_type == HW_PART_TYPE_NORMAL) {
642 dest->disk1 = *disks;
643 dest->disk2 = NULL;
644
645 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
646
647 } else if (part_type == HW_PART_TYPE_RAID1) {
648 dest->disk1 = *disks++;
649 dest->disk2 = *disks;
4a0d9bef 650 dest->raid_level = 1;
d7dd283b
MT
651
652 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
653 }
654
655 // Is this a RAID device?
656 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
657
92e78233 658 int r = hw_calculate_partition_table(hw, dest, disable_swap);
25fcce25
MT
659 if (r)
660 return NULL;
d7dd283b 661
d7dd283b
MT
662 return dest;
663}
c4e96674
MT
664
665unsigned long long hw_memory() {
5be66d81 666 struct sysinfo si;
c4e96674 667
5be66d81
MT
668 int r = sysinfo(&si);
669 if (r < 0)
670 return 0;
c4e96674 671
5be66d81 672 return si.totalram;
c4e96674 673}
25fcce25 674
d2f993a7
MT
675static int hw_zero_out_device(const char* path, int bytes) {
676 char block[512];
677 memset(block, 0, sizeof(block));
678
679 int blocks = bytes / sizeof(block);
680
681 int fd = open(path, O_WRONLY);
682 if (fd < 0)
683 return -1;
684
685 unsigned int bytes_written = 0;
686 while (blocks-- > 0) {
687 bytes_written += write(fd, block, sizeof(block));
688 }
689
690 fsync(fd);
691 close(fd);
692
693 return bytes_written;
694}
695
0e491487
MT
696static int try_open(const char* path) {
697 FILE* f = fopen(path, "r");
698 if (f) {
699 fclose(f);
700 return 0;
701 }
702
703 return -1;
704}
705
46b56e20 706int hw_create_partitions(struct hw_destination* dest, const char* output) {
d2f993a7
MT
707 // Before we write a new partition table to the disk, we will erase
708 // the first couple of megabytes at the beginning of the device to
709 // get rid of all left other things like bootloaders and partition tables.
710 // This solves some problems when changing from MBR to GPT partitions or
711 // the other way around.
712 int r = hw_zero_out_device(dest->path, MB2BYTES(10));
713 if (r <= 0)
714 return r;
25fcce25 715
d2f993a7 716 char* cmd = NULL;
25fcce25
MT
717 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
718
719 // Set partition type
720 if (dest->part_table == HW_PART_TABLE_MSDOS)
721 asprintf(&cmd, "%s mklabel msdos", cmd);
722 else if (dest->part_table == HW_PART_TABLE_GPT)
723 asprintf(&cmd, "%s mklabel gpt", cmd);
724
802a123b 725 unsigned long long part_start = MB2BYTES(1);
25fcce25 726
48d6a112 727 if (*dest->part_bootldr) {
802a123b 728 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
48d6a112 729 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary",
802a123b 730 part_start, part_start + dest->size_bootldr - 1);
48d6a112
MT
731
732 part_start += dest->size_bootldr;
733 }
734
25fcce25 735 if (*dest->part_boot) {
802a123b 736 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
25fcce25 737 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary",
802a123b 738 part_start, part_start + dest->size_boot - 1);
25fcce25
MT
739
740 part_start += dest->size_boot;
741 }
742
92e78233
MT
743 if (*dest->part_boot_efi) {
744 asprintf(&cmd, "%s mkpart %s fat32 %lluB %lluB", cmd,
745 (dest->part_table == HW_PART_TABLE_GPT) ? "ESP" : "primary",
746 part_start, part_start + dest->size_boot_efi - 1);
747
748 part_start += dest->size_boot_efi;
749 }
750
25fcce25 751 if (*dest->part_swap) {
802a123b 752 asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd,
25fcce25 753 (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary",
802a123b 754 part_start, part_start + dest->size_swap - 1);
25fcce25
MT
755
756 part_start += dest->size_swap;
757 }
758
759 if (*dest->part_root) {
802a123b 760 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
25fcce25 761 (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary",
802a123b 762 part_start, part_start + dest->size_root - 1);
25fcce25
MT
763
764 part_start += dest->size_root;
765 }
766
e9cf574d 767 if (dest->part_boot_idx > 0)
25fcce25
MT
768 asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
769
92e78233
MT
770 if (dest->part_boot_efi_idx > 0)
771 asprintf(&cmd, "%s set %d esp on", cmd, dest->part_boot_efi_idx);
772
e9cf574d 773 if (dest->part_table == HW_PART_TABLE_GPT) {
48d6a112
MT
774 if (*dest->part_bootldr) {
775 asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx);
776 }
25fcce25
MT
777 }
778
46b56e20 779 r = mysystem(output, cmd);
25fcce25 780
268090a8
MT
781 // Wait until the system re-read the partition table
782 if (r == 0) {
783 unsigned int counter = 10;
784
785 while (counter-- > 0) {
786 sleep(1);
787
0e491487 788 if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0))
48d6a112
MT
789 continue;
790
0e491487 791 if (*dest->part_boot && (try_open(dest->part_boot) != 0))
268090a8
MT
792 continue;
793
92e78233
MT
794 if (*dest->part_boot_efi && (try_open(dest->part_boot_efi) != 0))
795 continue;
796
0e491487 797 if (*dest->part_swap && (try_open(dest->part_swap) != 0))
268090a8
MT
798 continue;
799
0e491487 800 if (*dest->part_root && (try_open(dest->part_root) != 0))
268090a8
MT
801 continue;
802
268090a8
MT
803 // All partitions do exist, exiting the loop.
804 break;
805 }
806 }
807
25fcce25
MT
808 if (cmd)
809 free(cmd);
810
811 return r;
812}
813
42849040
MT
814static int hw_create_btrfs_subvolume(const char* output, const char* subvolume) {
815 char command [STRING_SIZE];
816 int r;
817
818 // Abort if the command could not be assigned.
819 r = snprintf(command, sizeof(command), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH, subvolume);
820 if (r < 0)
821 return r;
822
823 // Create the subvolume
824 r = mysystem(output, command);
825 if (r)
826 return r;
827
828 return 0;
829}
830
831static int hw_create_btrfs_layout(const char* path, const char* output) {
832 const struct btrfs_subvolumes* subvolume = NULL;
833 char cmd[STRING_SIZE];
834 char volume[STRING_SIZE];
835 int r;
836
837 r = snprintf(cmd, sizeof(cmd), "/usr/bin/mkfs.btrfs -f %s", path);
838 if (r < 0)
839 return r;
840
841 // Create the main BTRFS file system.
842 r = mysystem(output, cmd);
843 if (r)
844 return r;
845
846 // We need to mount the FS in order to create any subvolumes.
847 r = hw_mount(path, DESTINATION_MOUNT_PATH, "btrfs", 0);
848 if (r)
849 return r;
850
851 // Loop through the list of subvolumes to create.
852 for ( subvolume = btrfs_subvolumes; subvolume->name; subvolume++ ) {
853 r = snprintf(volume, sizeof(volume), "%s", subvolume->name);
854
855 // Abort if snprintf fails.
856 if (r < 0)
857 return r;
858
859 // Call function to create the subvolume
860 r = hw_create_btrfs_subvolume(output, volume);
861 if (r)
862 return r;
863 }
864
865 // Umount the main BTRFS after subvolume creation.
866 r = hw_umount(DESTINATION_MOUNT_PATH, 0);
867 if (r)
868 return r;
869
870 return 0;
871}
872
46b56e20 873static int hw_format_filesystem(const char* path, int fs, const char* output) {
25fcce25 874 char cmd[STRING_SIZE] = "\0";
0465449e 875 int r;
25fcce25
MT
876
877 // Swap
878 if (fs == HW_FS_SWAP) {
879 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
25fcce25
MT
880
881 // EXT4
882 } else if (fs == HW_FS_EXT4) {
5208ceed 883 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 %s", path);
25fcce25
MT
884
885 // EXT4 w/o journal
886 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
5208ceed 887 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path);
70a44b52
MT
888
889 // XFS
890 } else if (fs == HW_FS_XFS) {
891 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path);
92e78233 892
130815d3
SS
893 // BTRFS
894 } else if (fs == HW_FS_BTRFS) {
0465449e
SS
895 r = hw_create_btrfs_layout(path, output);
896
897 return r;
130815d3 898
92e78233
MT
899 // FAT32
900 } else if (fs == HW_FS_FAT32) {
901 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.vfat %s", path);
25fcce25
MT
902 }
903
904 assert(*cmd);
905
0465449e 906 r = mysystem(output, cmd);
25fcce25
MT
907
908 return r;
909}
910
46b56e20 911int hw_create_filesystems(struct hw_destination* dest, const char* output) {
25fcce25
MT
912 int r;
913
914 // boot
915 if (*dest->part_boot) {
46b56e20 916 r = hw_format_filesystem(dest->part_boot, dest->filesystem, output);
25fcce25
MT
917 if (r)
918 return r;
919 }
920
92e78233
MT
921 // ESP
922 if (*dest->part_boot_efi) {
923 r = hw_format_filesystem(dest->part_boot_efi, HW_FS_FAT32, output);
924 if (r)
925 return r;
926 }
927
25fcce25
MT
928 // swap
929 if (*dest->part_swap) {
46b56e20 930 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output);
25fcce25
MT
931 if (r)
932 return r;
933 }
934
935 // root
46b56e20 936 r = hw_format_filesystem(dest->part_root, dest->filesystem, output);
25fcce25
MT
937 if (r)
938 return r;
939
25fcce25
MT
940 return 0;
941}
942
42849040 943static int hw_mount_btrfs_subvolumes(const char* source) {
0465449e 944 const struct btrfs_subvolumes* subvolume = NULL;
42849040
MT
945 char path[STRING_SIZE];
946 char options[STRING_SIZE];
0465449e
SS
947 int r;
948
42849040
MT
949 // Loop through the list of known subvolumes.
950 for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) {
951 // Assign subvolume path.
952 r = snprintf(path, sizeof(path), "%s%s", DESTINATION_MOUNT_PATH, subvolume->mount_path);
953 if (r < 0)
954 return r;
0465449e 955
42849040
MT
956 // Assign subvolume name.
957 r = snprintf(options, sizeof(options), "subvol=%s,%s", subvolume->name, BTRFS_MOUNT_OPTIONS);
958 if (r < 0)
0465449e 959 return r;
0465449e 960
42849040
MT
961 // Create the directory.
962 r = hw_mkdir(path, S_IRWXU|S_IRWXG|S_IRWXO);
0465449e 963
42849040
MT
964 // Abort if the directory could not be created.
965 if(r != 0 && errno != EEXIST)
0465449e 966 return r;
0465449e 967
42849040
MT
968 // Print log message
969 fprintf(flog, "Mounting subvolume %s to %s\n", subvolume->name, subvolume->mount_path);
0465449e 970
42849040
MT
971 // Try to mount the subvolume.
972 r = mount(source, path, "btrfs", NULL, options);
973 if (r)
974 return r;
0465449e
SS
975 }
976
977 return 0;
978}
979
25fcce25
MT
980int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
981 char target[STRING_SIZE];
e7740eaf 982 int r;
25fcce25
MT
983
984 assert(*prefix == '/');
985
986 const char* filesystem;
987 switch (dest->filesystem) {
25fcce25
MT
988 case HW_FS_EXT4:
989 case HW_FS_EXT4_WO_JOURNAL:
990 filesystem = "ext4";
991 break;
992
70a44b52
MT
993 case HW_FS_XFS:
994 filesystem = "xfs";
995 break;
996
130815d3
SS
997 case HW_FS_BTRFS:
998 filesystem = "btrfs";
999 break;
1000
92e78233
MT
1001 case HW_FS_FAT32:
1002 filesystem = "vfat";
1003 break;
1004
25fcce25
MT
1005 default:
1006 assert(0);
1007 }
1008
1009 // root
e7740eaf
SS
1010 if (dest->filesystem == HW_FS_BTRFS) {
1011 r = hw_mount_btrfs_subvolumes(dest->part_root);
e7740eaf
SS
1012 if (r)
1013 return r;
1014 } else {
1015 r = hw_mount(dest->part_root, prefix, filesystem, 0);
e7740eaf
SS
1016 if (r)
1017 return r;
1018 }
25fcce25
MT
1019
1020 // boot
fbeac096
SS
1021 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT);
1022 r = mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
1023
1024 if (r) {
1025 hw_umount_filesystems(dest, prefix);
25fcce25 1026
fbeac096
SS
1027 return r;
1028 }
1029
1030 if (*dest->part_boot) {
25fcce25
MT
1031 r = hw_mount(dest->part_boot, target, filesystem, 0);
1032 if (r) {
1033 hw_umount_filesystems(dest, prefix);
1034
1035 return r;
1036 }
1037 }
1038
92e78233
MT
1039 // ESP
1040 if (*dest->part_boot_efi) {
1041 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT_EFI);
1042 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
1043
1044 r = hw_mount(dest->part_boot_efi, target, "vfat", 0);
1045 if (r) {
1046 hw_umount_filesystems(dest, prefix);
1047
1048 return r;
1049 }
1050 }
1051
25fcce25
MT
1052 // swap
1053 if (*dest->part_swap) {
1054 r = swapon(dest->part_swap, 0);
1055 if (r) {
1056 hw_umount_filesystems(dest, prefix);
1057
1058 return r;
1059 }
1060 }
1061
1062 // bind-mount misc filesystems
ced15fdf
MT
1063 r = hw_bind_mount("/dev", prefix);
1064 if (r)
1065 return r;
25fcce25 1066
ced15fdf
MT
1067 r = hw_bind_mount("/proc", prefix);
1068 if (r)
1069 return r;
25fcce25 1070
ced15fdf
MT
1071 r = hw_bind_mount("/sys", prefix);
1072 if (r)
1073 return r;
25fcce25 1074
ced15fdf
MT
1075 r = hw_bind_mount("/sys/firmware/efi/efivars", prefix);
1076 if (r && errno != ENOENT)
1077 return r;
25fcce25
MT
1078
1079 return 0;
1080}
1081
42849040 1082static int hw_umount_btrfs_layout() {
e7740eaf
SS
1083 const struct btrfs_subvolumes* subvolume = NULL;
1084 char path[STRING_SIZE];
42849040
MT
1085 int counter = 0;
1086 int retry = 1;
e7740eaf
SS
1087 int r;
1088
42849040
MT
1089 do {
1090 // Reset the retry marker
1091 retry = 0;
e7740eaf 1092
42849040
MT
1093 // Loop through the list of subvolumes
1094 for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) {
1095 // Abort if the subvolume path could not be assigned.
1096 r = snprintf(path, sizeof(path), "%s%s", DESTINATION_MOUNT_PATH, subvolume->mount_path);
1097 if (r < 0)
1098 return r;
e7740eaf 1099
42849040
MT
1100 // Try to umount the subvolume.
1101 r = umount2(path, 0);
e7740eaf 1102
42849040
MT
1103 // Handle return codes.
1104 if (r) {
1105 switch(errno) {
1106 case EBUSY:
1107 // Set marker to retry the umount.
1108 retry = 1;
e7740eaf 1109
42849040
MT
1110 // Ignore if the subvolume could not be unmounted yet,
1111 // because it is still used.
1112 continue;
e7740eaf 1113
42849040
MT
1114 case EINVAL:
1115 // Ignore if the subvolume already has been unmounted
1116 continue;
1117 case ENOENT:
1118 // Ignore if the directory does not longer exist.
1119 continue;
1120 default:
1121 fprintf(flog, "Could not umount %s from %s - Error: %d\n", subvolume->name, path, r);
1122 return r;
1123 }
1124 }
e7740eaf 1125
42849040
MT
1126 // Print log message
1127 fprintf(flog, "Umounted %s from %s\n", subvolume->name, path);
e7740eaf 1128 }
42849040
MT
1129
1130 // Abort loop if all mountpoins got umounted
1131 if (retry == 0)
1132 return 0;
1133
1134 // Abort after five failed umount attempts
1135 if (counter == 5)
1136 return -1;
1137
1138 // Increment counter.
1139 counter++;
1140 } while (1);
e7740eaf
SS
1141}
1142
25fcce25 1143int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
ade96ba8
MT
1144 int r;
1145 char target[STRING_SIZE];
1146
ddd32a5c
MT
1147 // Write all buffers to disk before umounting
1148 hw_sync();
1149
92e78233
MT
1150 // ESP
1151 if (*dest->part_boot_efi) {
ced15fdf 1152 r = hw_umount(HW_PATH_BOOT_EFI, prefix);
92e78233
MT
1153 if (r)
1154 return -1;
1155 }
1156
25fcce25
MT
1157 // boot
1158 if (*dest->part_boot) {
ced15fdf 1159 r = hw_umount(HW_PATH_BOOT, prefix);
ade96ba8
MT
1160 if (r)
1161 return -1;
25fcce25
MT
1162 }
1163
25fcce25
MT
1164 // swap
1165 if (*dest->part_swap) {
1166 swapoff(dest->part_swap);
1167 }
1168
1169 // misc filesystems
ced15fdf
MT
1170 r = hw_umount("/sys/firmware/efi/efivars", prefix);
1171 if (r)
1172 return -1;
1173
1174 r = hw_umount("/sys", prefix);
1175 if (r)
1176 return -1;
1177
1178 r = hw_umount("/proc", prefix);
1179 if (r)
1180 return -1;
1181
1182 r = hw_umount("/dev", prefix);
1183 if (r)
1184 return -1;
25fcce25 1185
ade96ba8 1186 // root
2a981396
SS
1187 if(dest->filesystem == HW_FS_BTRFS) {
1188 r = hw_umount_btrfs_layout();
1189 } else {
1190 r = hw_umount(prefix, NULL);
1191 }
1192
ade96ba8
MT
1193 if (r)
1194 return -1;
1195
25fcce25
MT
1196 return 0;
1197}
4a0d9bef 1198
d78fffa0 1199int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) {
d2fafe03
MT
1200 char cmd[STRING_SIZE];
1201
46b56e20
MT
1202 hw_stop_all_raid_arrays(output);
1203 hw_stop_all_raid_arrays(output);
d2fafe03
MT
1204
1205 if (dest->disk1) {
46b56e20
MT
1206 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path);
1207 mysystem(output, cmd);
d2fafe03
MT
1208 }
1209
1210 if (dest->disk2) {
46b56e20
MT
1211 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path);
1212 mysystem(output, cmd);
d2fafe03
MT
1213 }
1214
1215 return 0;
1216}
1217
46b56e20 1218int hw_setup_raid(struct hw_destination* dest, const char* output) {
4a0d9bef 1219 char* cmd = NULL;
7b4790d3 1220 int r;
4a0d9bef
MT
1221
1222 assert(dest->is_raid);
1223
d2fafe03
MT
1224 // Stop all RAID arrays that might be around (again).
1225 // It seems that there is some sort of race-condition with udev re-enabling
1226 // the raid arrays and therefore locking the disks.
46b56e20 1227 r = hw_destroy_raid_superblocks(dest, output);
d2fafe03 1228
7b4790d3
MT
1229 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1230 RAID_METADATA, dest->path);
4a0d9bef
MT
1231
1232 switch (dest->raid_level) {
1233 case 1:
1234 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
1235 break;
1236
1237 default:
1238 assert(0);
1239 }
1240
1241 if (dest->disk1) {
1242 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
d2f993a7
MT
1243
1244 // Clear all data at the beginning
1245 r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10));
1246 if (r <= 0)
1247 return r;
4a0d9bef
MT
1248 }
1249
1250 if (dest->disk2) {
1251 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
d2f993a7
MT
1252
1253 // Clear all data at the beginning
1254 r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10));
1255 if (r <= 0)
1256 return r;
4a0d9bef
MT
1257 }
1258
46b56e20 1259 r = mysystem(output, cmd);
4a0d9bef
MT
1260 free(cmd);
1261
1262 // Wait a moment until the device has been properly brought up
1263 if (r == 0) {
1264 unsigned int counter = 10;
1265 while (counter-- > 0) {
1266 sleep(1);
1267
fde37387
MT
1268 // If the raid device has not yet been properly brought up,
1269 // opening it will fail with the message: Device or resource busy
1270 // Hence we will wait a bit until it becomes usable.
0e491487 1271 if (try_open(dest->path) == 0)
4a0d9bef
MT
1272 break;
1273 }
1274 }
1275
1276 return r;
1277}
1278
46b56e20
MT
1279int hw_stop_all_raid_arrays(const char* output) {
1280 return mysystem(output, "/sbin/mdadm --stop --scan --verbose");
4a0d9bef 1281}
f5007e9c 1282
92e78233 1283int hw_install_bootloader(struct hw* hw, struct hw_destination* dest, const char* output) {
f5007e9c 1284 char cmd[STRING_SIZE];
f5007e9c 1285
12034118
MT
1286 snprintf(cmd, sizeof(cmd), "/usr/bin/install-bootloader %s", dest->path);
1287 int r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
92e78233
MT
1288 if (r)
1289 return r;
1290
ade96ba8
MT
1291 hw_sync();
1292
92e78233 1293 return 0;
f5007e9c 1294}
7f69d8a4
MT
1295
1296static char* hw_get_uuid(const char* dev) {
1297 blkid_probe p = blkid_new_probe_from_filename(dev);
1298 const char* buffer = NULL;
1299 char* uuid = NULL;
1300
1301 if (!p)
1302 return NULL;
1303
1304 blkid_do_probe(p);
1305 blkid_probe_lookup_value(p, "UUID", &buffer, NULL);
1306
1307 if (buffer)
1308 uuid = strdup(buffer);
1309
1310 blkid_free_probe(p);
1311
1312 return uuid;
1313}
1314
335c5bd1
MT
1315#define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1316
7f69d8a4 1317int hw_write_fstab(struct hw_destination* dest) {
15be1e1b 1318 const struct btrfs_subvolumes* subvolume = NULL;
7f69d8a4
MT
1319 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w");
1320 if (!f)
1321 return -1;
1322
7f69d8a4 1323 char* uuid = NULL;
15be1e1b 1324 char mount_options[STRING_SIZE];
7f69d8a4
MT
1325
1326 // boot
1327 if (*dest->part_boot) {
1328 uuid = hw_get_uuid(dest->part_boot);
1329
1330 if (uuid) {
e404dab5 1331 fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2);
7f69d8a4
MT
1332 free(uuid);
1333 }
1334 }
1335
92e78233
MT
1336 // ESP
1337 if (*dest->part_boot_efi) {
1338 uuid = hw_get_uuid(dest->part_boot_efi);
1339
1340 if (uuid) {
1341 fprintf(f, FSTAB_FMT, uuid, "/boot/efi", "auto", "defaults", 1, 2);
1342 free(uuid);
1343 }
1344 }
1345
1346
7f69d8a4
MT
1347 // swap
1348 if (*dest->part_swap) {
1349 uuid = hw_get_uuid(dest->part_swap);
1350
1351 if (uuid) {
335c5bd1 1352 fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0);
7f69d8a4
MT
1353 free(uuid);
1354 }
1355 }
1356
1357 // root
1358 uuid = hw_get_uuid(dest->part_root);
1359 if (uuid) {
15be1e1b
SS
1360 if(dest->filesystem == HW_FS_BTRFS) {
1361 // Loop through the list of subvolumes
1362 for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) {
1363 // Abort if the mount options could not be assigned
1364 int r = snprintf(mount_options, sizeof(mount_options), "defaults,%s,subvol=%s", BTRFS_MOUNT_OPTIONS, subvolume->name);
1365 if (r < 0) {
1366 return r;
1367 }
1368
1369 // Write the entry to the file
1370 fprintf(f, FSTAB_FMT, uuid, subvolume->mount_path, "btrfs", mount_options, 1, 1);
1371 }
1372 } else {
1373 fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1);
1374 }
1375
7f69d8a4
MT
1376 free(uuid);
1377 }
1378
7f69d8a4
MT
1379 fclose(f);
1380
1381 return 0;
1382}
282ec35e
MT
1383
1384void hw_sync() {
1385 sync();
1386 sync();
1387 sync();
1388}
7d114284
MT
1389
1390int hw_start_networking(const char* output) {
1391 return mysystem(output, "/usr/bin/start-networking.sh");
1392}
38c6822d
MT
1393
1394char* hw_find_backup_file(const char* output, const char* search_path) {
1395 char path[STRING_SIZE];
1396
1397 snprintf(path, sizeof(path), "%s/backup.ipf", search_path);
1398 int r = access(path, R_OK);
1399
1400 if (r == 0)
1401 return strdup(path);
1402
1403 return NULL;
1404}
1405
1406int hw_restore_backup(const char* output, const char* backup_path, const char* destination) {
1407 char command[STRING_SIZE];
1408
3f8e70f6
MT
1409 snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s "
1410 "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user",
1411 backup_path, destination, destination, destination);
38c6822d
MT
1412 int rc = mysystem(output, command);
1413
1414 if (rc)
1415 return -1;
1416
1417 return 0;
1418}
0465449e 1419
e0a7cdd8
SS
1420int hw_mkdir(const char *dir) {
1421 char tmp[STRING_SIZE];
1422 char *p = NULL;
1423 size_t len;
1424 int r;
1425
1426 snprintf(tmp, sizeof(tmp),"%s",dir);
1427 len = strlen(tmp);
1428
1429 if (tmp[len - 1] == '/') {
1430 tmp[len - 1] = 0;
1431 }
1432
1433 for (p = tmp + 1; *p; p++) {
1434 if (*p == '/') {
1435 *p = 0;
1436
1437 // Create target if it does not exist
1438 if (access(tmp, X_OK) != 0) {
1439 r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO);
1440
1441 if (r) {
1442 return r;
1443 }
1444 }
1445
1446 *p = '/';
1447 }
1448 }
1449
1450 // Create target if it does not exist
1451 if (access(tmp, X_OK) != 0) {
1452 r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO);
1453
1454 if (r) {
1455 return r;
1456 }
1457 }
1458
1459 return 0;
1460}