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