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