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