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