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