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