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