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