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