]> git.ipfire.org Git - people/pmueller/ipfire-2.x.git/blob - src/installer/hw.c
installer: Bind-mount /sys/firmware/efi/efivars into chroot
[people/pmueller/ipfire-2.x.git] / src / installer / hw.c
1 /*#############################################################################
2 # #
3 # IPFire - An Open Source Firewall Distribution #
4 # Copyright (C) 2014 IPFire development team #
5 # #
6 # This program is free software: you can redistribute it and/or modify #
7 # it under the terms of the GNU General Public License as published by #
8 # the Free Software Foundation, either version 3 of the License, or #
9 # (at your option) any later version. #
10 # #
11 # This program is distributed in the hope that it will be useful, #
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
14 # GNU General Public License for more details. #
15 # #
16 # You should have received a copy of the GNU General Public License #
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
18 # #
19 #############################################################################*/
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE
23 #endif
24
25 #include <assert.h>
26 #include <blkid/blkid.h>
27 #include <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 <linux/fs.h>
44
45 #include <libsmooth.h>
46
47 #include "hw.h"
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 }
193
194 return r;
195 }
196
197 static int hw_test_source_medium(const char* path) {
198 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
199
200 // If the source could not be mounted we
201 // cannot proceed.
202 if (ret != 0)
203 return ret;
204
205 // Check if the test file exists.
206 ret = access(SOURCE_TEST_FILE, R_OK);
207
208 // Umount the test device.
209 hw_umount(SOURCE_MOUNT_PATH, NULL);
210
211 return ret;
212 }
213
214 char* hw_find_source_medium(struct hw* hw) {
215 char* ret = NULL;
216
217 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
218
219 udev_enumerate_add_match_subsystem(enumerate, "block");
220 udev_enumerate_scan_devices(enumerate);
221
222 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
223
224 struct udev_list_entry* dev_list_entry;
225 udev_list_entry_foreach(dev_list_entry, devices) {
226 const char* path = udev_list_entry_get_name(dev_list_entry);
227 struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path);
228
229 const char* dev_path = udev_device_get_devnode(dev);
230
231 // Skip everything what we cannot work with
232 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
233 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md"))
234 continue;
235
236 if (hw_test_source_medium(dev_path) == 0) {
237 ret = strdup(dev_path);
238 }
239
240 udev_device_unref(dev);
241
242 // If a suitable device was found the search will end.
243 if (ret)
244 break;
245 }
246
247 udev_enumerate_unref(enumerate);
248
249 return ret;
250 }
251
252 static struct hw_disk** hw_create_disks() {
253 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
254
255 return ret;
256 }
257
258 static unsigned long long hw_block_device_get_size(const char* dev) {
259 int fd = open(dev, O_RDONLY);
260 if (fd < 0)
261 return 0;
262
263 unsigned long long size = blkid_get_dev_size(fd);
264 close(fd);
265
266 return size;
267 }
268
269 struct hw_disk** hw_find_disks(struct hw* hw, const char* sourcedrive) {
270 struct hw_disk** ret = hw_create_disks();
271 struct hw_disk** disks = ret;
272
273 struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev);
274
275 udev_enumerate_add_match_subsystem(enumerate, "block");
276 udev_enumerate_scan_devices(enumerate);
277
278 struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate);
279
280 struct udev_list_entry* dev_list_entry;
281 unsigned int i = HW_MAX_DISKS;
282 udev_list_entry_foreach(dev_list_entry, devices) {
283 const char* path = udev_list_entry_get_name(dev_list_entry);
284 struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path);
285
286 const char* dev_path = udev_device_get_devnode(dev);
287
288 // Skip everything what we cannot work with
289 if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") ||
290 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") ||
291 strstartswith(dev_path, "/dev/md")) {
292 udev_device_unref(dev);
293 continue;
294 }
295
296 // Skip sourcedrive if we need to
297 if (sourcedrive && (strcmp(dev_path, sourcedrive) == 0)) {
298 udev_device_unref(dev);
299 continue;
300 }
301
302 // DEVTYPE must be disk (otherwise we will see all sorts of partitions here)
303 const char* devtype = udev_device_get_property_value(dev, "DEVTYPE");
304 if (devtype && (strcmp(devtype, "disk") != 0)) {
305 udev_device_unref(dev);
306 continue;
307 }
308
309 // Skip devices with a size of zero
310 unsigned long long size = hw_block_device_get_size(dev_path);
311 if (size == 0) {
312 udev_device_unref(dev);
313 continue;
314 }
315
316 struct hw_disk* disk = malloc(sizeof(*disk));
317 if (disk == NULL)
318 return NULL;
319
320 disk->ref = 1;
321
322 strncpy(disk->path, dev_path, sizeof(disk->path));
323 const char* p = disk->path + 5;
324
325 disk->size = size;
326
327 // Vendor
328 const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR");
329 if (!vendor)
330 vendor = udev_device_get_sysattr_value(dev, "vendor");
331 if (!vendor)
332 vendor = udev_device_get_sysattr_value(dev, "manufacturer");
333
334 if (vendor)
335 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
336 else
337 *disk->vendor = '\0';
338
339 // Model
340 const char* model = udev_device_get_property_value(dev, "ID_MODEL");
341 if (!model)
342 model = udev_device_get_sysattr_value(dev, "model");
343 if (!model)
344 model = udev_device_get_sysattr_value(dev, "product");
345
346 if (model)
347 strncpy(disk->model, model, sizeof(disk->model));
348 else
349 *disk->model = '\0';
350
351 // Format description
352 char size_str[STRING_SIZE];
353 snprintf(size_str, sizeof(size_str), "%4.1fGB", (double)disk->size / pow(1024, 3));
354
355 if (*disk->vendor && *disk->model) {
356 snprintf(disk->description, sizeof(disk->description),
357 "%s - %s - %s - %s", size_str, p, disk->vendor, disk->model);
358
359 } else if (*disk->vendor || *disk->model) {
360 snprintf(disk->description, sizeof(disk->description),
361 "%s - %s - %s", size_str, p, (*disk->vendor) ? disk->vendor : disk->model);
362
363 } else {
364 snprintf(disk->description, sizeof(disk->description),
365 "%s - %s", size_str, p);
366 }
367
368 // Cut off the description string after 40 characters
369 disk->description[41] = '\0';
370
371 *disks++ = disk;
372
373 if (--i == 0)
374 break;
375
376 udev_device_unref(dev);
377 }
378
379 udev_enumerate_unref(enumerate);
380
381 *disks = NULL;
382
383 return ret;
384 }
385
386 void hw_free_disks(struct hw_disk** disks) {
387 struct hw_disk** disk = disks;
388
389 while (*disk != NULL) {
390 if (--(*disk)->ref == 0)
391 free(*disk);
392
393 disk++;
394 }
395
396 free(disks);
397 }
398
399 unsigned int hw_count_disks(const struct hw_disk** disks) {
400 unsigned int ret = 0;
401
402 while (*disks++)
403 ret++;
404
405 return ret;
406 }
407
408 struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) {
409 struct hw_disk** ret = hw_create_disks();
410 struct hw_disk** selected_disks = ret;
411
412 unsigned int num_disks = hw_count_disks((const struct hw_disk**)disks);
413
414 for (unsigned int i = 0; i < num_disks; i++) {
415 if (!selection || selection[i]) {
416 struct hw_disk *selected_disk = disks[i];
417 selected_disk->ref++;
418
419 *selected_disks++ = selected_disk;
420 }
421 }
422
423 // Set sentinel
424 *selected_disks = NULL;
425
426 return ret;
427 }
428
429 struct hw_disk** hw_select_first_disk(const struct hw_disk** disks) {
430 struct hw_disk** ret = hw_create_disks();
431 struct hw_disk** selected_disks = ret;
432
433 unsigned int num_disks = hw_count_disks(disks);
434 assert(num_disks > 0);
435
436 for (unsigned int i = 0; i < num_disks; i++) {
437 struct hw_disk *disk = disks[i];
438 disk->ref++;
439
440 *selected_disks++ = disk;
441 break;
442 }
443
444 // Set sentinel
445 *selected_disks = NULL;
446
447 return ret;
448 }
449
450 static unsigned long long hw_swap_size(struct hw_destination* dest) {
451 unsigned long long memory = hw_memory();
452
453 unsigned long long swap_size = memory / 4;
454
455 // Min. swap size is 128MB
456 if (swap_size < MB2BYTES(128))
457 swap_size = MB2BYTES(128);
458
459 // Cap swap size to 1GB
460 else if (swap_size > MB2BYTES(1024))
461 swap_size = MB2BYTES(1024);
462
463 return swap_size;
464 }
465
466 static unsigned long long hw_boot_size(struct hw_destination* dest) {
467 return MB2BYTES(128);
468 }
469
470 static int hw_device_has_p_suffix(const struct hw_destination* dest) {
471 // All RAID devices have the p suffix.
472 if (dest->is_raid)
473 return 1;
474
475 // Devices with a number at the end have the p suffix, too.
476 // e.g. mmcblk0, cciss0
477 unsigned int last_char = strlen(dest->path) - 1;
478 if ((dest->path[last_char] >= '0') && (dest->path[last_char] <= '9'))
479 return 1;
480
481 return 0;
482 }
483
484 static int hw_calculate_partition_table(struct hw* hw, struct hw_destination* dest, int disable_swap) {
485 char path[DEV_SIZE];
486 int part_idx = 1;
487
488 snprintf(path, sizeof(path), "%s%s", dest->path,
489 hw_device_has_p_suffix(dest) ? "p" : "");
490 dest->part_boot_idx = 0;
491
492 // Determine the size of the target block device
493 if (dest->is_raid) {
494 dest->size = (dest->disk1->size >= dest->disk2->size) ?
495 dest->disk2->size : dest->disk1->size;
496
497 // The RAID will install some metadata at the end of the disk
498 // and we will save up some space for that.
499 dest->size -= MB2BYTES(2);
500 } else {
501 dest->size = dest->disk1->size;
502 }
503
504 // As we add some extra space before the beginning of the first
505 // partition, we need to substract that here.
506 dest->size -= MB2BYTES(1);
507
508 // Add some more space for partition tables, etc.
509 dest->size -= MB2BYTES(1);
510
511 // The disk has to have at least 2GB
512 if (dest->size <= MB2BYTES(2048))
513 return -1;
514
515 // Determine partition table
516 dest->part_table = HW_PART_TABLE_MSDOS;
517
518 // Disks over 2TB need to use GPT
519 if (dest->size >= MB2BYTES(2047 * 1024))
520 dest->part_table = HW_PART_TABLE_GPT;
521
522 // We also use GPT on raid disks by default
523 else if (dest->is_raid)
524 dest->part_table = HW_PART_TABLE_GPT;
525
526 // When using GPT, GRUB2 needs a little bit of space to put
527 // itself in.
528 if (dest->part_table == HW_PART_TABLE_GPT) {
529 snprintf(dest->part_bootldr, sizeof(dest->part_bootldr),
530 "%s%d", path, part_idx);
531
532 dest->size_bootldr = MB2BYTES(4);
533
534 dest->part_boot_idx = part_idx++;
535 } else {
536 *dest->part_bootldr = '\0';
537 dest->size_bootldr = 0;
538 }
539
540 dest->size_boot = hw_boot_size(dest);
541
542 // Create an EFI partition when running in EFI mode
543 if (hw->efi)
544 dest->size_boot_efi = MB2BYTES(32);
545 else
546 dest->size_boot_efi = 0;
547
548 // Determine the size of the data partition.
549 unsigned long long space_left = dest->size - \
550 (dest->size_bootldr + dest->size_boot + dest->size_boot_efi);
551
552 // If we have less than 2GB left, we disable swap
553 if (space_left <= MB2BYTES(2048))
554 disable_swap = 1;
555
556 // Should we use swap?
557 if (disable_swap)
558 dest->size_swap = 0;
559 else
560 dest->size_swap = hw_swap_size(dest);
561
562 // Subtract swap
563 space_left -= dest->size_swap;
564
565 // Root is getting what ever is left
566 dest->size_root = space_left;
567
568 // Set partition names
569 if (dest->size_boot > 0) {
570 if (dest->part_boot_idx == 0)
571 dest->part_boot_idx = part_idx;
572
573 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
574 } else
575 *dest->part_boot = '\0';
576
577 if (dest->size_boot_efi > 0) {
578 dest->part_boot_efi_idx = part_idx;
579
580 snprintf(dest->part_boot_efi, sizeof(dest->part_boot_efi),
581 "%s%d", path, part_idx++);
582 } else {
583 *dest->part_boot_efi = '\0';
584 dest->part_boot_efi_idx = 0;
585 }
586
587 if (dest->size_swap > 0)
588 snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++);
589 else
590 *dest->part_swap = '\0';
591
592 // There is always a root partition
593 if (dest->part_boot_idx == 0)
594 dest->part_boot_idx = part_idx;
595
596 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
597
598 return 0;
599 }
600
601 struct hw_destination* hw_make_destination(struct hw* hw, int part_type, struct hw_disk** disks, int disable_swap) {
602 struct hw_destination* dest = malloc(sizeof(*dest));
603
604 if (part_type == HW_PART_TYPE_NORMAL) {
605 dest->disk1 = *disks;
606 dest->disk2 = NULL;
607
608 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
609
610 } else if (part_type == HW_PART_TYPE_RAID1) {
611 dest->disk1 = *disks++;
612 dest->disk2 = *disks;
613 dest->raid_level = 1;
614
615 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
616 }
617
618 // Is this a RAID device?
619 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
620
621 int r = hw_calculate_partition_table(hw, dest, disable_swap);
622 if (r)
623 return NULL;
624
625 // Set default filesystem
626 dest->filesystem = HW_FS_DEFAULT;
627
628 return dest;
629 }
630
631 unsigned long long hw_memory() {
632 struct sysinfo si;
633
634 int r = sysinfo(&si);
635 if (r < 0)
636 return 0;
637
638 return si.totalram;
639 }
640
641 static int hw_zero_out_device(const char* path, int bytes) {
642 char block[512];
643 memset(block, 0, sizeof(block));
644
645 int blocks = bytes / sizeof(block);
646
647 int fd = open(path, O_WRONLY);
648 if (fd < 0)
649 return -1;
650
651 unsigned int bytes_written = 0;
652 while (blocks-- > 0) {
653 bytes_written += write(fd, block, sizeof(block));
654 }
655
656 fsync(fd);
657 close(fd);
658
659 return bytes_written;
660 }
661
662 static int try_open(const char* path) {
663 FILE* f = fopen(path, "r");
664 if (f) {
665 fclose(f);
666 return 0;
667 }
668
669 return -1;
670 }
671
672 int hw_create_partitions(struct hw_destination* dest, const char* output) {
673 // Before we write a new partition table to the disk, we will erase
674 // the first couple of megabytes at the beginning of the device to
675 // get rid of all left other things like bootloaders and partition tables.
676 // This solves some problems when changing from MBR to GPT partitions or
677 // the other way around.
678 int r = hw_zero_out_device(dest->path, MB2BYTES(10));
679 if (r <= 0)
680 return r;
681
682 char* cmd = NULL;
683 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
684
685 // Set partition type
686 if (dest->part_table == HW_PART_TABLE_MSDOS)
687 asprintf(&cmd, "%s mklabel msdos", cmd);
688 else if (dest->part_table == HW_PART_TABLE_GPT)
689 asprintf(&cmd, "%s mklabel gpt", cmd);
690
691 unsigned long long part_start = MB2BYTES(1);
692
693 if (*dest->part_bootldr) {
694 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
695 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary",
696 part_start, part_start + dest->size_bootldr - 1);
697
698 part_start += dest->size_bootldr;
699 }
700
701 if (*dest->part_boot) {
702 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
703 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary",
704 part_start, part_start + dest->size_boot - 1);
705
706 part_start += dest->size_boot;
707 }
708
709 if (*dest->part_boot_efi) {
710 asprintf(&cmd, "%s mkpart %s fat32 %lluB %lluB", cmd,
711 (dest->part_table == HW_PART_TABLE_GPT) ? "ESP" : "primary",
712 part_start, part_start + dest->size_boot_efi - 1);
713
714 part_start += dest->size_boot_efi;
715 }
716
717 if (*dest->part_swap) {
718 asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd,
719 (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary",
720 part_start, part_start + dest->size_swap - 1);
721
722 part_start += dest->size_swap;
723 }
724
725 if (*dest->part_root) {
726 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
727 (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary",
728 part_start, part_start + dest->size_root - 1);
729
730 part_start += dest->size_root;
731 }
732
733 if (dest->part_boot_idx > 0)
734 asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
735
736 if (dest->part_boot_efi_idx > 0)
737 asprintf(&cmd, "%s set %d esp on", cmd, dest->part_boot_efi_idx);
738
739 if (dest->part_table == HW_PART_TABLE_GPT) {
740 if (*dest->part_bootldr) {
741 asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx);
742 }
743 }
744
745 r = mysystem(output, cmd);
746
747 // Wait until the system re-read the partition table
748 if (r == 0) {
749 unsigned int counter = 10;
750
751 while (counter-- > 0) {
752 sleep(1);
753
754 if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0))
755 continue;
756
757 if (*dest->part_boot && (try_open(dest->part_boot) != 0))
758 continue;
759
760 if (*dest->part_boot_efi && (try_open(dest->part_boot_efi) != 0))
761 continue;
762
763 if (*dest->part_swap && (try_open(dest->part_swap) != 0))
764 continue;
765
766 if (*dest->part_root && (try_open(dest->part_root) != 0))
767 continue;
768
769 // All partitions do exist, exiting the loop.
770 break;
771 }
772 }
773
774 if (cmd)
775 free(cmd);
776
777 return r;
778 }
779
780 static int hw_format_filesystem(const char* path, int fs, const char* output) {
781 char cmd[STRING_SIZE] = "\0";
782
783 // Swap
784 if (fs == HW_FS_SWAP) {
785 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
786 // ReiserFS
787 } else if (fs == HW_FS_REISERFS) {
788 snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path);
789
790 // EXT4
791 } else if (fs == HW_FS_EXT4) {
792 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 %s", path);
793
794 // EXT4 w/o journal
795 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
796 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path);
797
798 // XFS
799 } else if (fs == HW_FS_XFS) {
800 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path);
801
802 // FAT32
803 } else if (fs == HW_FS_FAT32) {
804 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.vfat %s", path);
805 }
806
807 assert(*cmd);
808
809 int r = mysystem(output, cmd);
810
811 return r;
812 }
813
814 int hw_create_filesystems(struct hw_destination* dest, const char* output) {
815 int r;
816
817 // boot
818 if (*dest->part_boot) {
819 r = hw_format_filesystem(dest->part_boot, dest->filesystem, output);
820 if (r)
821 return r;
822 }
823
824 // ESP
825 if (*dest->part_boot_efi) {
826 r = hw_format_filesystem(dest->part_boot_efi, HW_FS_FAT32, output);
827 if (r)
828 return r;
829 }
830
831 // swap
832 if (*dest->part_swap) {
833 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output);
834 if (r)
835 return r;
836 }
837
838 // root
839 r = hw_format_filesystem(dest->part_root, dest->filesystem, output);
840 if (r)
841 return r;
842
843 return 0;
844 }
845
846 int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
847 char target[STRING_SIZE];
848
849 assert(*prefix == '/');
850
851 const char* filesystem;
852 switch (dest->filesystem) {
853 case HW_FS_REISERFS:
854 filesystem = "reiserfs";
855 break;
856
857 case HW_FS_EXT4:
858 case HW_FS_EXT4_WO_JOURNAL:
859 filesystem = "ext4";
860 break;
861
862 case HW_FS_XFS:
863 filesystem = "xfs";
864 break;
865
866 case HW_FS_FAT32:
867 filesystem = "vfat";
868 break;
869
870 default:
871 assert(0);
872 }
873
874 // root
875 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
876 if (r)
877 return r;
878
879 // boot
880 if (*dest->part_boot) {
881 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT);
882 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
883
884 r = hw_mount(dest->part_boot, target, filesystem, 0);
885 if (r) {
886 hw_umount_filesystems(dest, prefix);
887
888 return r;
889 }
890 }
891
892 // ESP
893 if (*dest->part_boot_efi) {
894 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT_EFI);
895 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
896
897 r = hw_mount(dest->part_boot_efi, target, "vfat", 0);
898 if (r) {
899 hw_umount_filesystems(dest, prefix);
900
901 return r;
902 }
903 }
904
905 // swap
906 if (*dest->part_swap) {
907 r = swapon(dest->part_swap, 0);
908 if (r) {
909 hw_umount_filesystems(dest, prefix);
910
911 return r;
912 }
913 }
914
915 // bind-mount misc filesystems
916 r = hw_bind_mount("/dev", prefix);
917 if (r)
918 return r;
919
920 r = hw_bind_mount("/proc", prefix);
921 if (r)
922 return r;
923
924 r = hw_bind_mount("/sys", prefix);
925 if (r)
926 return r;
927
928 r = hw_bind_mount("/sys/firmware/efi/efivars", prefix);
929 if (r && errno != ENOENT)
930 return r;
931
932 return 0;
933 }
934
935 int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
936 int r;
937 char target[STRING_SIZE];
938
939 // Write all buffers to disk before umounting
940 hw_sync();
941
942 // ESP
943 if (*dest->part_boot_efi) {
944 r = hw_umount(HW_PATH_BOOT_EFI, prefix);
945 if (r)
946 return -1;
947 }
948
949 // boot
950 if (*dest->part_boot) {
951 r = hw_umount(HW_PATH_BOOT, prefix);
952 if (r)
953 return -1;
954 }
955
956 // swap
957 if (*dest->part_swap) {
958 swapoff(dest->part_swap);
959 }
960
961 // misc filesystems
962 r = hw_umount("/sys/firmware/efi/efivars", prefix);
963 if (r)
964 return -1;
965
966 r = hw_umount("/sys", prefix);
967 if (r)
968 return -1;
969
970 r = hw_umount("/proc", prefix);
971 if (r)
972 return -1;
973
974 r = hw_umount("/dev", prefix);
975 if (r)
976 return -1;
977
978 // root
979 r = hw_umount(prefix, NULL);
980 if (r)
981 return -1;
982
983 return 0;
984 }
985
986 int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) {
987 char cmd[STRING_SIZE];
988
989 hw_stop_all_raid_arrays(output);
990 hw_stop_all_raid_arrays(output);
991
992 if (dest->disk1) {
993 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path);
994 mysystem(output, cmd);
995 }
996
997 if (dest->disk2) {
998 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path);
999 mysystem(output, cmd);
1000 }
1001
1002 return 0;
1003 }
1004
1005 int hw_setup_raid(struct hw_destination* dest, const char* output) {
1006 char* cmd = NULL;
1007 int r;
1008
1009 assert(dest->is_raid);
1010
1011 // Stop all RAID arrays that might be around (again).
1012 // It seems that there is some sort of race-condition with udev re-enabling
1013 // the raid arrays and therefore locking the disks.
1014 r = hw_destroy_raid_superblocks(dest, output);
1015
1016 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
1017 RAID_METADATA, dest->path);
1018
1019 switch (dest->raid_level) {
1020 case 1:
1021 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
1022 break;
1023
1024 default:
1025 assert(0);
1026 }
1027
1028 if (dest->disk1) {
1029 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
1030
1031 // Clear all data at the beginning
1032 r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10));
1033 if (r <= 0)
1034 return r;
1035 }
1036
1037 if (dest->disk2) {
1038 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
1039
1040 // Clear all data at the beginning
1041 r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10));
1042 if (r <= 0)
1043 return r;
1044 }
1045
1046 r = mysystem(output, cmd);
1047 free(cmd);
1048
1049 // Wait a moment until the device has been properly brought up
1050 if (r == 0) {
1051 unsigned int counter = 10;
1052 while (counter-- > 0) {
1053 sleep(1);
1054
1055 // If the raid device has not yet been properly brought up,
1056 // opening it will fail with the message: Device or resource busy
1057 // Hence we will wait a bit until it becomes usable.
1058 if (try_open(dest->path) == 0)
1059 break;
1060 }
1061 }
1062
1063 return r;
1064 }
1065
1066 int hw_stop_all_raid_arrays(const char* output) {
1067 return mysystem(output, "/sbin/mdadm --stop --scan --verbose");
1068 }
1069
1070 int hw_install_bootloader(struct hw* hw, struct hw_destination* dest, const char* output) {
1071 char cmd[STRING_SIZE];
1072
1073 snprintf(cmd, sizeof(cmd), "/usr/bin/install-bootloader %s", dest->path);
1074 int r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
1075 if (r)
1076 return r;
1077
1078 hw_sync();
1079
1080 return 0;
1081 }
1082
1083 static char* hw_get_uuid(const char* dev) {
1084 blkid_probe p = blkid_new_probe_from_filename(dev);
1085 const char* buffer = NULL;
1086 char* uuid = NULL;
1087
1088 if (!p)
1089 return NULL;
1090
1091 blkid_do_probe(p);
1092 blkid_probe_lookup_value(p, "UUID", &buffer, NULL);
1093
1094 if (buffer)
1095 uuid = strdup(buffer);
1096
1097 blkid_free_probe(p);
1098
1099 return uuid;
1100 }
1101
1102 #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1103
1104 int hw_write_fstab(struct hw_destination* dest) {
1105 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w");
1106 if (!f)
1107 return -1;
1108
1109 char* uuid = NULL;
1110
1111 // boot
1112 if (*dest->part_boot) {
1113 uuid = hw_get_uuid(dest->part_boot);
1114
1115 if (uuid) {
1116 fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults", 1, 2);
1117 free(uuid);
1118 }
1119 }
1120
1121 // ESP
1122 if (*dest->part_boot_efi) {
1123 uuid = hw_get_uuid(dest->part_boot_efi);
1124
1125 if (uuid) {
1126 fprintf(f, FSTAB_FMT, uuid, "/boot/efi", "auto", "defaults", 1, 2);
1127 free(uuid);
1128 }
1129 }
1130
1131
1132 // swap
1133 if (*dest->part_swap) {
1134 uuid = hw_get_uuid(dest->part_swap);
1135
1136 if (uuid) {
1137 fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0);
1138 free(uuid);
1139 }
1140 }
1141
1142 // root
1143 uuid = hw_get_uuid(dest->part_root);
1144 if (uuid) {
1145 fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1);
1146 free(uuid);
1147 }
1148
1149 fclose(f);
1150
1151 return 0;
1152 }
1153
1154 void hw_sync() {
1155 sync();
1156 sync();
1157 sync();
1158 }
1159
1160 int hw_start_networking(const char* output) {
1161 return mysystem(output, "/usr/bin/start-networking.sh");
1162 }
1163
1164 char* hw_find_backup_file(const char* output, const char* search_path) {
1165 char path[STRING_SIZE];
1166
1167 snprintf(path, sizeof(path), "%s/backup.ipf", search_path);
1168 int r = access(path, R_OK);
1169
1170 if (r == 0)
1171 return strdup(path);
1172
1173 return NULL;
1174 }
1175
1176 int hw_restore_backup(const char* output, const char* backup_path, const char* destination) {
1177 char command[STRING_SIZE];
1178
1179 snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s", backup_path, destination);
1180 int rc = mysystem(output, command);
1181
1182 if (rc)
1183 return -1;
1184
1185 return 0;
1186 }