]> git.ipfire.org Git - ipfire-2.x.git/blame - src/installer/hw.c
installer: Don't try to install /etc/hosts which does not exist
[ipfire-2.x.git] / src / installer / hw.c
CommitLineData
f0fa1795
MT
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
25fcce25
MT
21#ifndef _GNU_SOURCE
22#define _GNU_SOURCE
23#endif
24
f0fa1795 25#include <assert.h>
d7dd283b
MT
26#include <blkid/blkid.h>
27#include <fcntl.h>
f0fa1795 28#include <libudev.h>
c0511f3a 29#include <linux/loop.h>
d7dd283b 30#include <math.h>
f0fa1795
MT
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
d7dd283b 34#include <sys/ioctl.h>
f0fa1795 35#include <sys/mount.h>
c0511f3a 36#include <sys/stat.h>
25fcce25 37#include <sys/swap.h>
5be66d81 38#include <sys/sysinfo.h>
f0fa1795
MT
39#include <unistd.h>
40
d7dd283b
MT
41#include <linux/fs.h>
42
5315fae6
MT
43#include <libsmooth.h>
44
f0fa1795 45#include "hw.h"
25fcce25
MT
46
47const char* other_filesystems[] = {
48 "/dev",
49 "/proc",
50 "/sys",
51 NULL
52};
f0fa1795 53
46b56e20 54static int system_chroot(const char* output, const char* path, const char* cmd) {
f5007e9c
MT
55 char chroot_cmd[STRING_SIZE];
56
57 snprintf(chroot_cmd, sizeof(chroot_cmd), "/usr/sbin/chroot %s %s", path, cmd);
58
46b56e20 59 return mysystem(output, chroot_cmd);
f5007e9c
MT
60}
61
f0fa1795
MT
62struct 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
76void hw_free(struct hw* hw) {
77 if (hw->udev)
78 udev_unref(hw->udev);
79
80 free(hw);
81}
82
83static int strstartswith(const char* a, const char* b) {
84 return (strncmp(a, b, strlen(b)) == 0);
85}
86
c0511f3a
MT
87static char loop_device[STRING_SIZE];
88
89static 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
106ERROR:
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
25fcce25 118int hw_mount(const char* source, const char* target, const char* fs, int flags) {
c0511f3a
MT
119 const char* loop_device = "/dev/loop0";
120
85f2892b
MT
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
c0511f3a
MT
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
25fcce25 137 return mount(source, target, fs, flags, NULL);
f0fa1795
MT
138}
139
140int hw_umount(const char* target) {
a8d8b557 141 return umount2(target, 0);
f0fa1795
MT
142}
143
144static int hw_test_source_medium(const char* path) {
25fcce25 145 int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY);
f0fa1795
MT
146
147 // If the source could not be mounted we
148 // cannot proceed.
5315fae6 149 if (ret != 0)
f0fa1795
MT
150 return ret;
151
152 // Check if the test file exists.
5315fae6 153 ret = access(SOURCE_TEST_FILE, R_OK);
f0fa1795
MT
154
155 // Umount the test device.
156 hw_umount(SOURCE_MOUNT_PATH);
157
5315fae6 158 return (ret == 0);
f0fa1795
MT
159}
160
161char* 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") ||
4a0d9bef 180 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md"))
f0fa1795
MT
181 continue;
182
5315fae6 183 if (hw_test_source_medium(dev_path) == 0) {
f0fa1795
MT
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}
d7dd283b
MT
198
199static struct hw_disk** hw_create_disks() {
200 struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1));
201
202 return ret;
203}
204
205static 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
ee00d203 216struct hw_disk** hw_find_disks(struct hw* hw, const char* sourcedrive) {
d7dd283b
MT
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") ||
4a0d9bef
MT
237 strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") ||
238 strstartswith(dev_path, "/dev/md")) {
d7dd283b
MT
239 udev_device_unref(dev);
240 continue;
241 }
242
ee00d203
MT
243 // Skip sourcedrive if we need to
244 if (sourcedrive && (strcmp(dev_path, sourcedrive) == 0)) {
d7dd283b
MT
245 udev_device_unref(dev);
246 continue;
247 }
248
ee00d203
MT
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)) {
d7dd283b
MT
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));
0c8b2d99 270 const char* p = disk->path + 5;
d7dd283b
MT
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");
d7dd283b 280
4c812463
MT
281 if (vendor)
282 strncpy(disk->vendor, vendor, sizeof(disk->vendor));
283 else
284 *disk->vendor = '\0';
d7dd283b
MT
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");
d7dd283b 292
4c812463
MT
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));
d7dd283b 301
4c812463
MT
302 if (*disk->vendor && *disk->model) {
303 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 304 "%s - %s - %s - %s", size_str, p, disk->vendor, disk->model);
4c812463
MT
305
306 } else if (*disk->vendor || *disk->model) {
307 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 308 "%s - %s - %s", size_str, p, (*disk->vendor) ? disk->vendor : disk->model);
4c812463
MT
309
310 } else {
311 snprintf(disk->description, sizeof(disk->description),
0c8b2d99 312 "%s - %s", size_str, p);
4c812463 313 }
d7dd283b
MT
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
330void 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
335c5bd1 343unsigned int hw_count_disks(const struct hw_disk** disks) {
d7dd283b
MT
344 unsigned int ret = 0;
345
346 while (*disks++)
347 ret++;
348
349 return ret;
350}
351
352struct 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
335c5bd1 356 unsigned int num_disks = hw_count_disks((const struct hw_disk**)disks);
d7dd283b
MT
357
358 for (unsigned int i = 0; i < num_disks; i++) {
ee43f517 359 if (!selection || selection[i]) {
d7dd283b
MT
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
a3e135c8
MT
373struct 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
25fcce25
MT
394static 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
410static 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
425static unsigned long long hw_boot_size(struct hw_destination* dest) {
426 return MB2BYTES(64);
427}
428
a691d4b3
MT
429static 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
126d3570 436 unsigned int last_char = strlen(dest->path) - 1;
a691d4b3
MT
437 if ((dest->path[last_char] >= '0') && (dest->path[last_char] <= '9'))
438 return 1;
439
440 return 0;
441}
442
a8fca245 443static int hw_calculate_partition_table(struct hw_destination* dest, int disable_swap) {
48d6a112
MT
444 char path[DEV_SIZE];
445 int part_idx = 1;
446
a691d4b3
MT
447 snprintf(path, sizeof(path), "%s%s", dest->path,
448 hw_device_has_p_suffix(dest) ? "p" : "");
48d6a112
MT
449 dest->part_boot_idx = 0;
450
25fcce25
MT
451 // Determine the size of the target block device
452 if (dest->is_raid) {
453 dest->size = (dest->disk1->size >= dest->disk2->size) ?
5315fae6 454 dest->disk2->size : dest->disk1->size;
22581f51
MT
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);
25fcce25
MT
459 } else {
460 dest->size = dest->disk1->size;
461 }
462
802a123b
MT
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
48d6a112
MT
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.
5689fc72 483 if (dest->part_table == HW_PART_TABLE_GPT) {
48d6a112
MT
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
25fcce25 495 dest->size_boot = hw_boot_size(dest);
25fcce25
MT
496 dest->size_root = hw_root_size(dest);
497
a8fca245
MT
498 // Should we use swap?
499 if (disable_swap)
500 dest->size_swap = 0;
501 else
502 dest->size_swap = hw_swap_size(dest);
503
25fcce25 504 // Determine the size of the data partition.
48d6a112
MT
505 unsigned long long used_space = dest->size_bootldr + dest->size_boot
506 + dest->size_swap + dest->size_root;
25fcce25
MT
507
508 // Disk is way too small
509 if (used_space >= dest->size)
510 return -1;
511
512 dest->size_data = dest->size - used_space;
513
514 // If it gets too small, we remove the swap space.
515 if (dest->size_data <= MB2BYTES(256)) {
516 dest->size_data += dest->size_swap;
517 dest->size_swap = 0;
518 }
519
520 // Set partition names
25fcce25 521 if (dest->size_boot > 0) {
48d6a112
MT
522 if (dest->part_boot_idx == 0)
523 dest->part_boot_idx = part_idx;
25fcce25
MT
524
525 snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++);
526 } else
527 *dest->part_boot = '\0';
528
529 if (dest->size_swap > 0)
530 snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++);
531 else
532 *dest->part_swap = '\0';
533
534 // There is always a root partition
48d6a112 535 if (dest->part_boot_idx == 0)
25fcce25
MT
536 dest->part_boot_idx = part_idx;
537
538 snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++);
539
540 if (dest->size_data > 0)
541 snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++);
542 else
543 *dest->part_data = '\0';
544
25fcce25
MT
545 return 0;
546}
547
a8fca245 548struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks, int disable_swap) {
d7dd283b
MT
549 struct hw_destination* dest = malloc(sizeof(*dest));
550
551 if (part_type == HW_PART_TYPE_NORMAL) {
552 dest->disk1 = *disks;
553 dest->disk2 = NULL;
554
555 strncpy(dest->path, dest->disk1->path, sizeof(dest->path));
556
557 } else if (part_type == HW_PART_TYPE_RAID1) {
558 dest->disk1 = *disks++;
559 dest->disk2 = *disks;
4a0d9bef 560 dest->raid_level = 1;
d7dd283b
MT
561
562 snprintf(dest->path, sizeof(dest->path), "/dev/md0");
563 }
564
565 // Is this a RAID device?
566 dest->is_raid = (part_type > HW_PART_TYPE_NORMAL);
567
a8fca245 568 int r = hw_calculate_partition_table(dest, disable_swap);
25fcce25
MT
569 if (r)
570 return NULL;
d7dd283b 571
25fcce25
MT
572 // Set default filesystem
573 dest->filesystem = HW_FS_DEFAULT;
d7dd283b
MT
574
575 return dest;
576}
c4e96674
MT
577
578unsigned long long hw_memory() {
5be66d81 579 struct sysinfo si;
c4e96674 580
5be66d81
MT
581 int r = sysinfo(&si);
582 if (r < 0)
583 return 0;
c4e96674 584
5be66d81 585 return si.totalram;
c4e96674 586}
25fcce25 587
d2f993a7
MT
588static int hw_zero_out_device(const char* path, int bytes) {
589 char block[512];
590 memset(block, 0, sizeof(block));
591
592 int blocks = bytes / sizeof(block);
593
594 int fd = open(path, O_WRONLY);
595 if (fd < 0)
596 return -1;
597
598 unsigned int bytes_written = 0;
599 while (blocks-- > 0) {
600 bytes_written += write(fd, block, sizeof(block));
601 }
602
603 fsync(fd);
604 close(fd);
605
606 return bytes_written;
607}
608
0e491487
MT
609static int try_open(const char* path) {
610 FILE* f = fopen(path, "r");
611 if (f) {
612 fclose(f);
613 return 0;
614 }
615
616 return -1;
617}
618
46b56e20 619int hw_create_partitions(struct hw_destination* dest, const char* output) {
d2f993a7
MT
620 // Before we write a new partition table to the disk, we will erase
621 // the first couple of megabytes at the beginning of the device to
622 // get rid of all left other things like bootloaders and partition tables.
623 // This solves some problems when changing from MBR to GPT partitions or
624 // the other way around.
625 int r = hw_zero_out_device(dest->path, MB2BYTES(10));
626 if (r <= 0)
627 return r;
25fcce25 628
d2f993a7 629 char* cmd = NULL;
25fcce25
MT
630 asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path);
631
632 // Set partition type
633 if (dest->part_table == HW_PART_TABLE_MSDOS)
634 asprintf(&cmd, "%s mklabel msdos", cmd);
635 else if (dest->part_table == HW_PART_TABLE_GPT)
636 asprintf(&cmd, "%s mklabel gpt", cmd);
637
802a123b 638 unsigned long long part_start = MB2BYTES(1);
25fcce25 639
48d6a112 640 if (*dest->part_bootldr) {
802a123b 641 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
48d6a112 642 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary",
802a123b 643 part_start, part_start + dest->size_bootldr - 1);
48d6a112
MT
644
645 part_start += dest->size_bootldr;
646 }
647
25fcce25 648 if (*dest->part_boot) {
802a123b 649 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
25fcce25 650 (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary",
802a123b 651 part_start, part_start + dest->size_boot - 1);
25fcce25
MT
652
653 part_start += dest->size_boot;
654 }
655
656 if (*dest->part_swap) {
802a123b 657 asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd,
25fcce25 658 (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary",
802a123b 659 part_start, part_start + dest->size_swap - 1);
25fcce25
MT
660
661 part_start += dest->size_swap;
662 }
663
664 if (*dest->part_root) {
802a123b 665 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
25fcce25 666 (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary",
802a123b 667 part_start, part_start + dest->size_root - 1);
25fcce25
MT
668
669 part_start += dest->size_root;
670 }
671
672 if (*dest->part_data) {
802a123b 673 asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd,
25fcce25 674 (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary",
802a123b 675 part_start, part_start + dest->size_data - 1);
25fcce25
MT
676
677 part_start += dest->size_data;
678 }
679
e9cf574d 680 if (dest->part_boot_idx > 0)
25fcce25
MT
681 asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx);
682
e9cf574d 683 if (dest->part_table == HW_PART_TABLE_GPT) {
48d6a112
MT
684 if (*dest->part_bootldr) {
685 asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx);
686 }
25fcce25
MT
687 asprintf(&cmd, "%s disk_set pmbr_boot on", cmd);
688 }
689
46b56e20 690 r = mysystem(output, cmd);
25fcce25 691
268090a8
MT
692 // Wait until the system re-read the partition table
693 if (r == 0) {
694 unsigned int counter = 10;
695
696 while (counter-- > 0) {
697 sleep(1);
698
0e491487 699 if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0))
48d6a112
MT
700 continue;
701
0e491487 702 if (*dest->part_boot && (try_open(dest->part_boot) != 0))
268090a8
MT
703 continue;
704
0e491487 705 if (*dest->part_swap && (try_open(dest->part_swap) != 0))
268090a8
MT
706 continue;
707
0e491487 708 if (*dest->part_root && (try_open(dest->part_root) != 0))
268090a8
MT
709 continue;
710
0e491487 711 if (*dest->part_data && (try_open(dest->part_data) != 0))
268090a8
MT
712 continue;
713
714 // All partitions do exist, exiting the loop.
715 break;
716 }
717 }
718
25fcce25
MT
719 if (cmd)
720 free(cmd);
721
722 return r;
723}
724
46b56e20 725static int hw_format_filesystem(const char* path, int fs, const char* output) {
25fcce25
MT
726 char cmd[STRING_SIZE] = "\0";
727
728 // Swap
729 if (fs == HW_FS_SWAP) {
730 snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path);
731 // ReiserFS
732 } else if (fs == HW_FS_REISERFS) {
733 snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path);
734
735 // EXT4
736 } else if (fs == HW_FS_EXT4) {
737 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path);
738
739 // EXT4 w/o journal
740 } else if (fs == HW_FS_EXT4_WO_JOURNAL) {
741 snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path);
70a44b52
MT
742
743 // XFS
744 } else if (fs == HW_FS_XFS) {
745 snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path);
25fcce25
MT
746 }
747
748 assert(*cmd);
749
46b56e20 750 int r = mysystem(output, cmd);
25fcce25
MT
751
752 return r;
753}
754
46b56e20 755int hw_create_filesystems(struct hw_destination* dest, const char* output) {
25fcce25
MT
756 int r;
757
758 // boot
759 if (*dest->part_boot) {
46b56e20 760 r = hw_format_filesystem(dest->part_boot, dest->filesystem, output);
25fcce25
MT
761 if (r)
762 return r;
763 }
764
765 // swap
766 if (*dest->part_swap) {
46b56e20 767 r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output);
25fcce25
MT
768 if (r)
769 return r;
770 }
771
772 // root
46b56e20 773 r = hw_format_filesystem(dest->part_root, dest->filesystem, output);
25fcce25
MT
774 if (r)
775 return r;
776
777 // data
778 if (*dest->part_data) {
46b56e20 779 r = hw_format_filesystem(dest->part_data, dest->filesystem, output);
25fcce25
MT
780 if (r)
781 return r;
782 }
783
784 return 0;
785}
786
787int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) {
788 char target[STRING_SIZE];
789
790 assert(*prefix == '/');
791
792 const char* filesystem;
793 switch (dest->filesystem) {
794 case HW_FS_REISERFS:
795 filesystem = "reiserfs";
796 break;
797
798 case HW_FS_EXT4:
799 case HW_FS_EXT4_WO_JOURNAL:
800 filesystem = "ext4";
801 break;
802
70a44b52
MT
803 case HW_FS_XFS:
804 filesystem = "xfs";
805 break;
806
25fcce25
MT
807 default:
808 assert(0);
809 }
810
811 // root
812 int r = hw_mount(dest->part_root, prefix, filesystem, 0);
813 if (r)
814 return r;
815
816 // boot
817 if (*dest->part_boot) {
818 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT);
819 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
820
821 r = hw_mount(dest->part_boot, target, filesystem, 0);
822 if (r) {
823 hw_umount_filesystems(dest, prefix);
824
825 return r;
826 }
827 }
828
829 // data
830 if (*dest->part_data) {
831 snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA);
832 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
833
834 r = hw_mount(dest->part_data, target, filesystem, 0);
835 if (r) {
836 hw_umount_filesystems(dest, prefix);
837
838 return r;
839 }
840 }
841
842 // swap
843 if (*dest->part_swap) {
844 r = swapon(dest->part_swap, 0);
845 if (r) {
846 hw_umount_filesystems(dest, prefix);
847
848 return r;
849 }
850 }
851
852 // bind-mount misc filesystems
853 char** otherfs = other_filesystems;
854 while (*otherfs) {
855 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs);
856
857 mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO);
858 r = hw_mount(*otherfs, target, NULL, MS_BIND);
859 if (r) {
860 hw_umount_filesystems(dest, prefix);
861
862 return r;
863 }
864
865 otherfs++;
866 }
867
868 return 0;
869}
870
871int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) {
ddd32a5c
MT
872 // Write all buffers to disk before umounting
873 hw_sync();
874
25fcce25
MT
875 // boot
876 if (*dest->part_boot) {
877 hw_umount(dest->part_boot);
878 }
879
880 // data
881 if (*dest->part_data) {
882 hw_umount(dest->part_data);
883 }
884
885 // root
886 hw_umount(dest->part_root);
887
888 // swap
889 if (*dest->part_swap) {
890 swapoff(dest->part_swap);
891 }
892
893 // misc filesystems
894 char target[STRING_SIZE];
895 char** otherfs = other_filesystems;
896
897 while (*otherfs) {
898 snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++);
899 hw_umount(target);
900 }
901
902 return 0;
903}
4a0d9bef 904
d78fffa0 905int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) {
d2fafe03
MT
906 char cmd[STRING_SIZE];
907
46b56e20
MT
908 hw_stop_all_raid_arrays(output);
909 hw_stop_all_raid_arrays(output);
d2fafe03
MT
910
911 if (dest->disk1) {
46b56e20
MT
912 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path);
913 mysystem(output, cmd);
d2fafe03
MT
914 }
915
916 if (dest->disk2) {
46b56e20
MT
917 snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path);
918 mysystem(output, cmd);
d2fafe03
MT
919 }
920
921 return 0;
922}
923
46b56e20 924int hw_setup_raid(struct hw_destination* dest, const char* output) {
4a0d9bef 925 char* cmd = NULL;
7b4790d3 926 int r;
4a0d9bef
MT
927
928 assert(dest->is_raid);
929
d2fafe03
MT
930 // Stop all RAID arrays that might be around (again).
931 // It seems that there is some sort of race-condition with udev re-enabling
932 // the raid arrays and therefore locking the disks.
46b56e20 933 r = hw_destroy_raid_superblocks(dest, output);
d2fafe03 934
7b4790d3
MT
935 asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s",
936 RAID_METADATA, dest->path);
4a0d9bef
MT
937
938 switch (dest->raid_level) {
939 case 1:
940 asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd);
941 break;
942
943 default:
944 assert(0);
945 }
946
947 if (dest->disk1) {
948 asprintf(&cmd, "%s %s", cmd, dest->disk1->path);
d2f993a7
MT
949
950 // Clear all data at the beginning
951 r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10));
952 if (r <= 0)
953 return r;
4a0d9bef
MT
954 }
955
956 if (dest->disk2) {
957 asprintf(&cmd, "%s %s", cmd, dest->disk2->path);
d2f993a7
MT
958
959 // Clear all data at the beginning
960 r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10));
961 if (r <= 0)
962 return r;
4a0d9bef
MT
963 }
964
46b56e20 965 r = mysystem(output, cmd);
4a0d9bef
MT
966 free(cmd);
967
968 // Wait a moment until the device has been properly brought up
969 if (r == 0) {
970 unsigned int counter = 10;
971 while (counter-- > 0) {
972 sleep(1);
973
fde37387
MT
974 // If the raid device has not yet been properly brought up,
975 // opening it will fail with the message: Device or resource busy
976 // Hence we will wait a bit until it becomes usable.
0e491487 977 if (try_open(dest->path) == 0)
4a0d9bef
MT
978 break;
979 }
980 }
981
982 return r;
983}
984
46b56e20
MT
985int hw_stop_all_raid_arrays(const char* output) {
986 return mysystem(output, "/sbin/mdadm --stop --scan --verbose");
4a0d9bef 987}
f5007e9c 988
46b56e20 989int hw_install_bootloader(struct hw_destination* dest, const char* output) {
f5007e9c
MT
990 char cmd[STRING_SIZE];
991 int r;
992
993 // Generate configuration file
994 snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg");
46b56e20 995 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
f5007e9c
MT
996 if (r)
997 return r;
998
999 char cmd_grub[STRING_SIZE];
1000 snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck");
1001
e9cf574d 1002 if (dest->is_raid) {
f5007e9c 1003 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path);
46b56e20 1004 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
f5007e9c
MT
1005 if (r)
1006 return r;
1007
1008 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path);
46b56e20 1009 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
f5007e9c
MT
1010 } else {
1011 snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path);
46b56e20 1012 r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd);
f5007e9c
MT
1013 }
1014
1015 return r;
1016}
7f69d8a4
MT
1017
1018static char* hw_get_uuid(const char* dev) {
1019 blkid_probe p = blkid_new_probe_from_filename(dev);
1020 const char* buffer = NULL;
1021 char* uuid = NULL;
1022
1023 if (!p)
1024 return NULL;
1025
1026 blkid_do_probe(p);
1027 blkid_probe_lookup_value(p, "UUID", &buffer, NULL);
1028
1029 if (buffer)
1030 uuid = strdup(buffer);
1031
1032 blkid_free_probe(p);
1033
1034 return uuid;
1035}
1036
335c5bd1
MT
1037#define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n"
1038
7f69d8a4
MT
1039int hw_write_fstab(struct hw_destination* dest) {
1040 FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w");
1041 if (!f)
1042 return -1;
1043
7f69d8a4
MT
1044 char* uuid = NULL;
1045
1046 // boot
1047 if (*dest->part_boot) {
1048 uuid = hw_get_uuid(dest->part_boot);
1049
1050 if (uuid) {
335c5bd1 1051 fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults", 1, 2);
7f69d8a4
MT
1052 free(uuid);
1053 }
1054 }
1055
1056 // swap
1057 if (*dest->part_swap) {
1058 uuid = hw_get_uuid(dest->part_swap);
1059
1060 if (uuid) {
335c5bd1 1061 fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0);
7f69d8a4
MT
1062 free(uuid);
1063 }
1064 }
1065
1066 // root
1067 uuid = hw_get_uuid(dest->part_root);
1068 if (uuid) {
335c5bd1 1069 fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1);
7f69d8a4
MT
1070 free(uuid);
1071 }
1072
1073 // data
1074 if (*dest->part_data) {
1075 uuid = hw_get_uuid(dest->part_data);
1076
1077 if (uuid) {
335c5bd1 1078 fprintf(f, FSTAB_FMT, uuid, "/var", "auto", "defaults", 1, 1);
7f69d8a4
MT
1079 free(uuid);
1080 }
1081 }
1082
1083 fclose(f);
1084
1085 return 0;
1086}
282ec35e
MT
1087
1088void hw_sync() {
1089 sync();
1090 sync();
1091 sync();
1092}
7d114284
MT
1093
1094int hw_start_networking(const char* output) {
1095 return mysystem(output, "/usr/bin/start-networking.sh");
1096}
38c6822d
MT
1097
1098char* hw_find_backup_file(const char* output, const char* search_path) {
1099 char path[STRING_SIZE];
1100
1101 snprintf(path, sizeof(path), "%s/backup.ipf", search_path);
1102 int r = access(path, R_OK);
1103
1104 if (r == 0)
1105 return strdup(path);
1106
1107 return NULL;
1108}
1109
1110int hw_restore_backup(const char* output, const char* backup_path, const char* destination) {
1111 char command[STRING_SIZE];
1112
1113 snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s", backup_path, destination);
1114 int rc = mysystem(output, command);
1115
1116 if (rc)
1117 return -1;
1118
1119 return 0;
1120}