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