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