]> git.ipfire.org Git - ipfire-2.x.git/blob - src/installer/hw.c
installer: Destroy RAID superblocks on single disk installations
[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 static int try_open(const char* path) {
530 FILE* f = fopen(path, "r");
531 if (f) {
532 fclose(f);
533 return 0;
534 }
535
536 return -1;
537 }
538
539 int hw_create_partitions(struct hw_destination* dest, const char* output) {
540 // Before we write a new partition table to the disk, we will erase
541 // the first couple of megabytes at the beginning of the device to
542 // get rid of all left other things like bootloaders and partition tables.
543 // This solves some problems when changing from MBR to GPT partitions or
544 // the other way around.
545 int r = hw_zero_out_device(dest->path, MB2BYTES(10));
546 if (r <= 0)
547 return r;
548
549 char* cmd = NULL;
550 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
551
552 // Set partition type
553 if (dest->part_table == HW_PART_TABLE_MSDOS)
554 asprintf(&cmd, "%s mklabel msdos", cmd);
555 else if (dest->part_table == HW_PART_TABLE_GPT)
556 asprintf(&cmd, "%s mklabel gpt", cmd);
557
558 unsigned long long part_start = MB2BYTES(1);
559
560 if (*dest->part_bootldr) {
561 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
562 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary",
563 part_start, part_start + dest->size_bootldr - 1);
564
565 part_start += dest->size_bootldr;
566 }
567
568 if (*dest->part_boot) {
569 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
570 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary",
571 part_start, part_start + dest->size_boot - 1);
572
573 part_start += dest->size_boot;
574 }
575
576 if (*dest->part_swap) {
577 asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd,
578 (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary",
579 part_start, part_start + dest->size_swap - 1);
580
581 part_start += dest->size_swap;
582 }
583
584 if (*dest->part_root) {
585 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
586 (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary",
587 part_start, part_start + dest->size_root - 1);
588
589 part_start += dest->size_root;
590 }
591
592 if (*dest->part_data) {
593 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
594 (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary",
595 part_start, part_start + dest->size_data - 1);
596
597 part_start += dest->size_data;
598 }
599
600 if (dest->part_boot_idx > 0)
601 asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
602
603 if (dest->part_table == HW_PART_TABLE_GPT) {
604 if (*dest->part_bootldr) {
605 asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx);
606 }
607 asprintf(&cmd, "%s disk_set pmbr_boot on", cmd);
608 }
609
610 r = mysystem(output, cmd);
611
612 // Wait until the system re-read the partition table
613 if (r == 0) {
614 unsigned int counter = 10;
615
616 while (counter-- > 0) {
617 sleep(1);
618
619 if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0))
620 continue;
621
622 if (*dest->part_boot && (try_open(dest->part_boot) != 0))
623 continue;
624
625 if (*dest->part_swap && (try_open(dest->part_swap) != 0))
626 continue;
627
628 if (*dest->part_root && (try_open(dest->part_root) != 0))
629 continue;
630
631 if (*dest->part_data && (try_open(dest->part_data) != 0))
632 continue;
633
634 // All partitions do exist, exiting the loop.
635 break;
636 }
637 }
638
639 if (cmd)
640 free(cmd);
641
642 return r;
643 }
644
645 static int hw_format_filesystem(const char* path, int fs, const char* output) {
646 char cmd[STRING_SIZE] = "\0";
647
648 // Swap
649 if (fs == HW_FS_SWAP) {
650 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
651 // ReiserFS
652 } else if (fs == HW_FS_REISERFS) {
653 snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path);
654
655 // EXT4
656 } else if (fs == HW_FS_EXT4) {
657 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path);
658
659 // EXT4 w/o journal
660 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
661 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path);
662
663 // XFS
664 } else if (fs == HW_FS_XFS) {
665 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path);
666 }
667
668 assert(*cmd);
669
670 int r = mysystem(output, cmd);
671
672 return r;
673 }
674
675 int hw_create_filesystems(struct hw_destination* dest, const char* output) {
676 int r;
677
678 // boot
679 if (*dest->part_boot) {
680 r = hw_format_filesystem(dest->part_boot, dest->filesystem, output);
681 if (r)
682 return r;
683 }
684
685 // swap
686 if (*dest->part_swap) {
687 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output);
688 if (r)
689 return r;
690 }
691
692 // root
693 r = hw_format_filesystem(dest->part_root, dest->filesystem, output);
694 if (r)
695 return r;
696
697 // data
698 if (*dest->part_data) {
699 r = hw_format_filesystem(dest->part_data, dest->filesystem, output);
700 if (r)
701 return r;
702 }
703
704 return 0;
705 }
706
707 int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
708 char target[STRING_SIZE];
709
710 assert(*prefix == '/');
711
712 const char* filesystem;
713 switch (dest->filesystem) {
714 case HW_FS_REISERFS:
715 filesystem = "reiserfs";
716 break;
717
718 case HW_FS_EXT4:
719 case HW_FS_EXT4_WO_JOURNAL:
720 filesystem = "ext4";
721 break;
722
723 case HW_FS_XFS:
724 filesystem = "xfs";
725 break;
726
727 default:
728 assert(0);
729 }
730
731 // root
732 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
733 if (r)
734 return r;
735
736 // boot
737 if (*dest->part_boot) {
738 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT);
739 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
740
741 r = hw_mount(dest->part_boot, target, filesystem, 0);
742 if (r) {
743 hw_umount_filesystems(dest, prefix);
744
745 return r;
746 }
747 }
748
749 // data
750 if (*dest->part_data) {
751 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA);
752 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
753
754 r = hw_mount(dest->part_data, target, filesystem, 0);
755 if (r) {
756 hw_umount_filesystems(dest, prefix);
757
758 return r;
759 }
760 }
761
762 // swap
763 if (*dest->part_swap) {
764 r = swapon(dest->part_swap, 0);
765 if (r) {
766 hw_umount_filesystems(dest, prefix);
767
768 return r;
769 }
770 }
771
772 // bind-mount misc filesystems
773 char** otherfs = other_filesystems;
774 while (*otherfs) {
775 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs);
776
777 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
778 r = hw_mount(*otherfs, target, NULL, MS_BIND);
779 if (r) {
780 hw_umount_filesystems(dest, prefix);
781
782 return r;
783 }
784
785 otherfs++;
786 }
787
788 return 0;
789 }
790
791 int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
792 // Write all buffers to disk before umounting
793 hw_sync();
794
795 // boot
796 if (*dest->part_boot) {
797 hw_umount(dest->part_boot);
798 }
799
800 // data
801 if (*dest->part_data) {
802 hw_umount(dest->part_data);
803 }
804
805 // root
806 hw_umount(dest->part_root);
807
808 // swap
809 if (*dest->part_swap) {
810 swapoff(dest->part_swap);
811 }
812
813 // misc filesystems
814 char target[STRING_SIZE];
815 char** otherfs = other_filesystems;
816
817 while (*otherfs) {
818 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++);
819 hw_umount(target);
820 }
821
822 return 0;
823 }
824
825 int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) {
826 char cmd[STRING_SIZE];
827
828 hw_stop_all_raid_arrays(output);
829 hw_stop_all_raid_arrays(output);
830
831 if (dest->disk1) {
832 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path);
833 mysystem(output, cmd);
834 }
835
836 if (dest->disk2) {
837 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path);
838 mysystem(output, cmd);
839 }
840
841 return 0;
842 }
843
844 int hw_setup_raid(struct hw_destination* dest, const char* output) {
845 char* cmd = NULL;
846 int r;
847
848 assert(dest->is_raid);
849
850 // Stop all RAID arrays that might be around (again).
851 // It seems that there is some sort of race-condition with udev re-enabling
852 // the raid arrays and therefore locking the disks.
853 r = hw_destroy_raid_superblocks(dest, output);
854
855 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
856 RAID_METADATA, dest->path);
857
858 switch (dest->raid_level) {
859 case 1:
860 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
861 break;
862
863 default:
864 assert(0);
865 }
866
867 if (dest->disk1) {
868 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
869
870 // Clear all data at the beginning
871 r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10));
872 if (r <= 0)
873 return r;
874 }
875
876 if (dest->disk2) {
877 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
878
879 // Clear all data at the beginning
880 r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10));
881 if (r <= 0)
882 return r;
883 }
884
885 r = mysystem(output, cmd);
886 free(cmd);
887
888 // Wait a moment until the device has been properly brought up
889 if (r == 0) {
890 unsigned int counter = 10;
891 while (counter-- > 0) {
892 sleep(1);
893
894 // If the raid device has not yet been properly brought up,
895 // opening it will fail with the message: Device or resource busy
896 // Hence we will wait a bit until it becomes usable.
897 if (try_open(dest->path) == 0)
898 break;
899 }
900 }
901
902 return r;
903 }
904
905 int hw_stop_all_raid_arrays(const char* output) {
906 return mysystem(output, "/sbin/mdadm --stop --scan --verbose");
907 }
908
909 int hw_install_bootloader(struct hw_destination* dest, const char* output) {
910 char cmd[STRING_SIZE];
911 int r;
912
913 // Generate configuration file
914 snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
915 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
916 if (r)
917 return r;
918
919 char cmd_grub[STRING_SIZE];
920 snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck");
921
922 if (dest->is_raid) {
923 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path);
924 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
925 if (r)
926 return r;
927
928 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path);
929 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
930 } else {
931 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path);
932 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
933 }
934
935 return r;
936 }
937
938 static char* hw_get_uuid(const char* dev) {
939 blkid_probe p = blkid_new_probe_from_filename(dev);
940 const char* buffer = NULL;
941 char* uuid = NULL;
942
943 if (!p)
944 return NULL;
945
946 blkid_do_probe(p);
947 blkid_probe_lookup_value(p, "UUID", &buffer, NULL);
948
949 if (buffer)
950 uuid = strdup(buffer);
951
952 blkid_free_probe(p);
953
954 return uuid;
955 }
956
957 int hw_write_fstab(struct hw_destination* dest) {
958 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w");
959 if (!f)
960 return -1;
961
962 const char* fmt = "UUID=%s %-8s %-4s %-10s %d %d\n";
963 char* uuid = NULL;
964
965 // boot
966 if (*dest->part_boot) {
967 uuid = hw_get_uuid(dest->part_boot);
968
969 if (uuid) {
970 fprintf(f, fmt, uuid, "/boot", "auto", "defaults", 1, 2);
971 free(uuid);
972 }
973 }
974
975 // swap
976 if (*dest->part_swap) {
977 uuid = hw_get_uuid(dest->part_swap);
978
979 if (uuid) {
980 fprintf(f, fmt, uuid, "swap", "swap", "defaults,pri=1", 0, 0);
981 free(uuid);
982 }
983 }
984
985 // root
986 uuid = hw_get_uuid(dest->part_root);
987 if (uuid) {
988 fprintf(f, fmt, uuid, "/", "auto", "defaults", 1, 1);
989 free(uuid);
990 }
991
992 // data
993 if (*dest->part_data) {
994 uuid = hw_get_uuid(dest->part_data);
995
996 if (uuid) {
997 fprintf(f, fmt, uuid, "/var", "auto", "defaults", 1, 1);
998 free(uuid);
999 }
1000 }
1001
1002 fclose(f);
1003
1004 return 0;
1005 }
1006
1007 void hw_sync() {
1008 sync();
1009 sync();
1010 sync();
1011 }