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