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