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