]>
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) { |
06cb5c1b | 493 | return MB2BYTES(256); |
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 | ||
25fcce25 | 566 | dest->size_boot = hw_boot_size(dest); |
c577571e | 567 | |
92e78233 MT |
568 | // Create an EFI partition when running in EFI mode |
569 | if (hw->efi) | |
570 | dest->size_boot_efi = MB2BYTES(32); | |
571 | else | |
572 | dest->size_boot_efi = 0; | |
573 | ||
c577571e MT |
574 | // Determine the size of the data partition. |
575 | unsigned long long space_left = dest->size - \ | |
92e78233 | 576 | (dest->size_bootldr + dest->size_boot + dest->size_boot_efi); |
c577571e MT |
577 | |
578 | // If we have less than 2GB left, we disable swap | |
579 | if (space_left <= MB2BYTES(2048)) | |
580 | disable_swap = 1; | |
25fcce25 | 581 | |
a8fca245 MT |
582 | // Should we use swap? |
583 | if (disable_swap) | |
584 | dest->size_swap = 0; | |
585 | else | |
586 | dest->size_swap = hw_swap_size(dest); | |
587 | ||
c577571e MT |
588 | // Subtract swap |
589 | space_left -= dest->size_swap; | |
25fcce25 | 590 | |
c577571e MT |
591 | // Root is getting what ever is left |
592 | dest->size_root = space_left; | |
25fcce25 MT |
593 | |
594 | // Set partition names | |
25fcce25 | 595 | if (dest->size_boot > 0) { |
48d6a112 MT |
596 | if (dest->part_boot_idx == 0) |
597 | dest->part_boot_idx = part_idx; | |
25fcce25 MT |
598 | |
599 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
600 | } else | |
601 | *dest->part_boot = '\0'; | |
602 | ||
92e78233 MT |
603 | if (dest->size_boot_efi > 0) { |
604 | dest->part_boot_efi_idx = part_idx; | |
605 | ||
606 | snprintf(dest->part_boot_efi, sizeof(dest->part_boot_efi), | |
607 | "%s%d", path, part_idx++); | |
58e840bd | 608 | } else { |
92e78233 | 609 | *dest->part_boot_efi = '\0'; |
58e840bd MT |
610 | dest->part_boot_efi_idx = 0; |
611 | } | |
92e78233 | 612 | |
25fcce25 MT |
613 | if (dest->size_swap > 0) |
614 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
615 | else | |
616 | *dest->part_swap = '\0'; | |
617 | ||
618 | // There is always a root partition | |
48d6a112 | 619 | if (dest->part_boot_idx == 0) |
25fcce25 MT |
620 | dest->part_boot_idx = part_idx; |
621 | ||
622 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
623 | ||
25fcce25 MT |
624 | return 0; |
625 | } | |
626 | ||
92e78233 | 627 | struct hw_destination* hw_make_destination(struct hw* hw, int part_type, struct hw_disk** disks, int disable_swap) { |
d7dd283b MT |
628 | struct hw_destination* dest = malloc(sizeof(*dest)); |
629 | ||
630 | if (part_type == HW_PART_TYPE_NORMAL) { | |
631 | dest->disk1 = *disks; | |
632 | dest->disk2 = NULL; | |
633 | ||
634 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
635 | ||
636 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
637 | dest->disk1 = *disks++; | |
638 | dest->disk2 = *disks; | |
4a0d9bef | 639 | dest->raid_level = 1; |
d7dd283b MT |
640 | |
641 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
642 | } | |
643 | ||
644 | // Is this a RAID device? | |
645 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
646 | ||
92e78233 | 647 | int r = hw_calculate_partition_table(hw, dest, disable_swap); |
25fcce25 MT |
648 | if (r) |
649 | return NULL; | |
d7dd283b | 650 | |
25fcce25 MT |
651 | // Set default filesystem |
652 | dest->filesystem = HW_FS_DEFAULT; | |
d7dd283b MT |
653 | |
654 | return dest; | |
655 | } | |
c4e96674 MT |
656 | |
657 | unsigned long long hw_memory() { | |
5be66d81 | 658 | struct sysinfo si; |
c4e96674 | 659 | |
5be66d81 MT |
660 | int r = sysinfo(&si); |
661 | if (r < 0) | |
662 | return 0; | |
c4e96674 | 663 | |
5be66d81 | 664 | return si.totalram; |
c4e96674 | 665 | } |
25fcce25 | 666 | |
d2f993a7 MT |
667 | static int hw_zero_out_device(const char* path, int bytes) { |
668 | char block[512]; | |
669 | memset(block, 0, sizeof(block)); | |
670 | ||
671 | int blocks = bytes / sizeof(block); | |
672 | ||
673 | int fd = open(path, O_WRONLY); | |
674 | if (fd < 0) | |
675 | return -1; | |
676 | ||
677 | unsigned int bytes_written = 0; | |
678 | while (blocks-- > 0) { | |
679 | bytes_written += write(fd, block, sizeof(block)); | |
680 | } | |
681 | ||
682 | fsync(fd); | |
683 | close(fd); | |
684 | ||
685 | return bytes_written; | |
686 | } | |
687 | ||
0e491487 MT |
688 | static int try_open(const char* path) { |
689 | FILE* f = fopen(path, "r"); | |
690 | if (f) { | |
691 | fclose(f); | |
692 | return 0; | |
693 | } | |
694 | ||
695 | return -1; | |
696 | } | |
697 | ||
46b56e20 | 698 | int hw_create_partitions(struct hw_destination* dest, const char* output) { |
d2f993a7 MT |
699 | // Before we write a new partition table to the disk, we will erase |
700 | // the first couple of megabytes at the beginning of the device to | |
701 | // get rid of all left other things like bootloaders and partition tables. | |
702 | // This solves some problems when changing from MBR to GPT partitions or | |
703 | // the other way around. | |
704 | int r = hw_zero_out_device(dest->path, MB2BYTES(10)); | |
705 | if (r <= 0) | |
706 | return r; | |
25fcce25 | 707 | |
d2f993a7 | 708 | char* cmd = NULL; |
25fcce25 MT |
709 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); |
710 | ||
711 | // Set partition type | |
712 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
713 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
714 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
715 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
716 | ||
802a123b | 717 | unsigned long long part_start = MB2BYTES(1); |
25fcce25 | 718 | |
48d6a112 | 719 | if (*dest->part_bootldr) { |
802a123b | 720 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
48d6a112 | 721 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary", |
802a123b | 722 | part_start, part_start + dest->size_bootldr - 1); |
48d6a112 MT |
723 | |
724 | part_start += dest->size_bootldr; | |
725 | } | |
726 | ||
25fcce25 | 727 | if (*dest->part_boot) { |
802a123b | 728 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 729 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", |
802a123b | 730 | part_start, part_start + dest->size_boot - 1); |
25fcce25 MT |
731 | |
732 | part_start += dest->size_boot; | |
733 | } | |
734 | ||
92e78233 MT |
735 | if (*dest->part_boot_efi) { |
736 | asprintf(&cmd, "%s mkpart %s fat32 %lluB %lluB", cmd, | |
737 | (dest->part_table == HW_PART_TABLE_GPT) ? "ESP" : "primary", | |
738 | part_start, part_start + dest->size_boot_efi - 1); | |
739 | ||
740 | part_start += dest->size_boot_efi; | |
741 | } | |
742 | ||
25fcce25 | 743 | if (*dest->part_swap) { |
802a123b | 744 | asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd, |
25fcce25 | 745 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", |
802a123b | 746 | part_start, part_start + dest->size_swap - 1); |
25fcce25 MT |
747 | |
748 | part_start += dest->size_swap; | |
749 | } | |
750 | ||
751 | if (*dest->part_root) { | |
802a123b | 752 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 753 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", |
802a123b | 754 | part_start, part_start + dest->size_root - 1); |
25fcce25 MT |
755 | |
756 | part_start += dest->size_root; | |
757 | } | |
758 | ||
e9cf574d | 759 | if (dest->part_boot_idx > 0) |
25fcce25 MT |
760 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); |
761 | ||
92e78233 MT |
762 | if (dest->part_boot_efi_idx > 0) |
763 | asprintf(&cmd, "%s set %d esp on", cmd, dest->part_boot_efi_idx); | |
764 | ||
e9cf574d | 765 | if (dest->part_table == HW_PART_TABLE_GPT) { |
48d6a112 MT |
766 | if (*dest->part_bootldr) { |
767 | asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx); | |
768 | } | |
25fcce25 MT |
769 | } |
770 | ||
46b56e20 | 771 | r = mysystem(output, cmd); |
25fcce25 | 772 | |
268090a8 MT |
773 | // Wait until the system re-read the partition table |
774 | if (r == 0) { | |
775 | unsigned int counter = 10; | |
776 | ||
777 | while (counter-- > 0) { | |
778 | sleep(1); | |
779 | ||
0e491487 | 780 | if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0)) |
48d6a112 MT |
781 | continue; |
782 | ||
0e491487 | 783 | if (*dest->part_boot && (try_open(dest->part_boot) != 0)) |
268090a8 MT |
784 | continue; |
785 | ||
92e78233 MT |
786 | if (*dest->part_boot_efi && (try_open(dest->part_boot_efi) != 0)) |
787 | continue; | |
788 | ||
0e491487 | 789 | if (*dest->part_swap && (try_open(dest->part_swap) != 0)) |
268090a8 MT |
790 | continue; |
791 | ||
0e491487 | 792 | if (*dest->part_root && (try_open(dest->part_root) != 0)) |
268090a8 MT |
793 | continue; |
794 | ||
268090a8 MT |
795 | // All partitions do exist, exiting the loop. |
796 | break; | |
797 | } | |
798 | } | |
799 | ||
25fcce25 MT |
800 | if (cmd) |
801 | free(cmd); | |
802 | ||
803 | return r; | |
804 | } | |
805 | ||
46b56e20 | 806 | static int hw_format_filesystem(const char* path, int fs, const char* output) { |
25fcce25 MT |
807 | char cmd[STRING_SIZE] = "\0"; |
808 | ||
809 | // Swap | |
810 | if (fs == HW_FS_SWAP) { | |
811 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
25fcce25 MT |
812 | |
813 | // EXT4 | |
814 | } else if (fs == HW_FS_EXT4) { | |
5208ceed | 815 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 %s", path); |
25fcce25 MT |
816 | |
817 | // EXT4 w/o journal | |
818 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
5208ceed | 819 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path); |
70a44b52 MT |
820 | |
821 | // XFS | |
822 | } else if (fs == HW_FS_XFS) { | |
823 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path); | |
92e78233 MT |
824 | |
825 | // FAT32 | |
826 | } else if (fs == HW_FS_FAT32) { | |
827 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.vfat %s", path); | |
25fcce25 MT |
828 | } |
829 | ||
830 | assert(*cmd); | |
831 | ||
46b56e20 | 832 | int r = mysystem(output, cmd); |
25fcce25 MT |
833 | |
834 | return r; | |
835 | } | |
836 | ||
46b56e20 | 837 | int hw_create_filesystems(struct hw_destination* dest, const char* output) { |
25fcce25 MT |
838 | int r; |
839 | ||
840 | // boot | |
841 | if (*dest->part_boot) { | |
46b56e20 | 842 | r = hw_format_filesystem(dest->part_boot, dest->filesystem, output); |
25fcce25 MT |
843 | if (r) |
844 | return r; | |
845 | } | |
846 | ||
92e78233 MT |
847 | // ESP |
848 | if (*dest->part_boot_efi) { | |
849 | r = hw_format_filesystem(dest->part_boot_efi, HW_FS_FAT32, output); | |
850 | if (r) | |
851 | return r; | |
852 | } | |
853 | ||
25fcce25 MT |
854 | // swap |
855 | if (*dest->part_swap) { | |
46b56e20 | 856 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output); |
25fcce25 MT |
857 | if (r) |
858 | return r; | |
859 | } | |
860 | ||
861 | // root | |
46b56e20 | 862 | r = hw_format_filesystem(dest->part_root, dest->filesystem, output); |
25fcce25 MT |
863 | if (r) |
864 | return r; | |
865 | ||
25fcce25 MT |
866 | return 0; |
867 | } | |
868 | ||
869 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
870 | char target[STRING_SIZE]; | |
871 | ||
872 | assert(*prefix == '/'); | |
873 | ||
874 | const char* filesystem; | |
875 | switch (dest->filesystem) { | |
25fcce25 MT |
876 | case HW_FS_EXT4: |
877 | case HW_FS_EXT4_WO_JOURNAL: | |
878 | filesystem = "ext4"; | |
879 | break; | |
880 | ||
70a44b52 MT |
881 | case HW_FS_XFS: |
882 | filesystem = "xfs"; | |
883 | break; | |
884 | ||
92e78233 MT |
885 | case HW_FS_FAT32: |
886 | filesystem = "vfat"; | |
887 | break; | |
888 | ||
25fcce25 MT |
889 | default: |
890 | assert(0); | |
891 | } | |
892 | ||
893 | // root | |
894 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
895 | if (r) | |
896 | return r; | |
897 | ||
898 | // boot | |
899 | if (*dest->part_boot) { | |
900 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
901 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
902 | ||
903 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
904 | if (r) { | |
905 | hw_umount_filesystems(dest, prefix); | |
906 | ||
907 | return r; | |
908 | } | |
909 | } | |
910 | ||
92e78233 MT |
911 | // ESP |
912 | if (*dest->part_boot_efi) { | |
913 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT_EFI); | |
914 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
915 | ||
916 | r = hw_mount(dest->part_boot_efi, target, "vfat", 0); | |
917 | if (r) { | |
918 | hw_umount_filesystems(dest, prefix); | |
919 | ||
920 | return r; | |
921 | } | |
922 | } | |
923 | ||
25fcce25 MT |
924 | // swap |
925 | if (*dest->part_swap) { | |
926 | r = swapon(dest->part_swap, 0); | |
927 | if (r) { | |
928 | hw_umount_filesystems(dest, prefix); | |
929 | ||
930 | return r; | |
931 | } | |
932 | } | |
933 | ||
934 | // bind-mount misc filesystems | |
ced15fdf MT |
935 | r = hw_bind_mount("/dev", prefix); |
936 | if (r) | |
937 | return r; | |
25fcce25 | 938 | |
ced15fdf MT |
939 | r = hw_bind_mount("/proc", prefix); |
940 | if (r) | |
941 | return r; | |
25fcce25 | 942 | |
ced15fdf MT |
943 | r = hw_bind_mount("/sys", prefix); |
944 | if (r) | |
945 | return r; | |
25fcce25 | 946 | |
ced15fdf MT |
947 | r = hw_bind_mount("/sys/firmware/efi/efivars", prefix); |
948 | if (r && errno != ENOENT) | |
949 | return r; | |
25fcce25 MT |
950 | |
951 | return 0; | |
952 | } | |
953 | ||
954 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
ade96ba8 MT |
955 | int r; |
956 | char target[STRING_SIZE]; | |
957 | ||
ddd32a5c MT |
958 | // Write all buffers to disk before umounting |
959 | hw_sync(); | |
960 | ||
92e78233 MT |
961 | // ESP |
962 | if (*dest->part_boot_efi) { | |
ced15fdf | 963 | r = hw_umount(HW_PATH_BOOT_EFI, prefix); |
92e78233 MT |
964 | if (r) |
965 | return -1; | |
966 | } | |
967 | ||
25fcce25 MT |
968 | // boot |
969 | if (*dest->part_boot) { | |
ced15fdf | 970 | r = hw_umount(HW_PATH_BOOT, prefix); |
ade96ba8 MT |
971 | if (r) |
972 | return -1; | |
25fcce25 MT |
973 | } |
974 | ||
25fcce25 MT |
975 | // swap |
976 | if (*dest->part_swap) { | |
977 | swapoff(dest->part_swap); | |
978 | } | |
979 | ||
980 | // misc filesystems | |
ced15fdf MT |
981 | r = hw_umount("/sys/firmware/efi/efivars", prefix); |
982 | if (r) | |
983 | return -1; | |
984 | ||
985 | r = hw_umount("/sys", prefix); | |
986 | if (r) | |
987 | return -1; | |
988 | ||
989 | r = hw_umount("/proc", prefix); | |
990 | if (r) | |
991 | return -1; | |
992 | ||
993 | r = hw_umount("/dev", prefix); | |
994 | if (r) | |
995 | return -1; | |
25fcce25 | 996 | |
ade96ba8 | 997 | // root |
ced15fdf | 998 | r = hw_umount(prefix, NULL); |
ade96ba8 MT |
999 | if (r) |
1000 | return -1; | |
1001 | ||
25fcce25 MT |
1002 | return 0; |
1003 | } | |
4a0d9bef | 1004 | |
d78fffa0 | 1005 | int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) { |
d2fafe03 MT |
1006 | char cmd[STRING_SIZE]; |
1007 | ||
46b56e20 MT |
1008 | hw_stop_all_raid_arrays(output); |
1009 | hw_stop_all_raid_arrays(output); | |
d2fafe03 MT |
1010 | |
1011 | if (dest->disk1) { | |
46b56e20 MT |
1012 | snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path); |
1013 | mysystem(output, cmd); | |
d2fafe03 MT |
1014 | } |
1015 | ||
1016 | if (dest->disk2) { | |
46b56e20 MT |
1017 | snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path); |
1018 | mysystem(output, cmd); | |
d2fafe03 MT |
1019 | } |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
46b56e20 | 1024 | int hw_setup_raid(struct hw_destination* dest, const char* output) { |
4a0d9bef | 1025 | char* cmd = NULL; |
7b4790d3 | 1026 | int r; |
4a0d9bef MT |
1027 | |
1028 | assert(dest->is_raid); | |
1029 | ||
d2fafe03 MT |
1030 | // Stop all RAID arrays that might be around (again). |
1031 | // It seems that there is some sort of race-condition with udev re-enabling | |
1032 | // the raid arrays and therefore locking the disks. | |
46b56e20 | 1033 | r = hw_destroy_raid_superblocks(dest, output); |
d2fafe03 | 1034 | |
7b4790d3 MT |
1035 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s", |
1036 | RAID_METADATA, dest->path); | |
4a0d9bef MT |
1037 | |
1038 | switch (dest->raid_level) { | |
1039 | case 1: | |
1040 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
1041 | break; | |
1042 | ||
1043 | default: | |
1044 | assert(0); | |
1045 | } | |
1046 | ||
1047 | if (dest->disk1) { | |
1048 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
d2f993a7 MT |
1049 | |
1050 | // Clear all data at the beginning | |
1051 | r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10)); | |
1052 | if (r <= 0) | |
1053 | return r; | |
4a0d9bef MT |
1054 | } |
1055 | ||
1056 | if (dest->disk2) { | |
1057 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
d2f993a7 MT |
1058 | |
1059 | // Clear all data at the beginning | |
1060 | r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10)); | |
1061 | if (r <= 0) | |
1062 | return r; | |
4a0d9bef MT |
1063 | } |
1064 | ||
46b56e20 | 1065 | r = mysystem(output, cmd); |
4a0d9bef MT |
1066 | free(cmd); |
1067 | ||
1068 | // Wait a moment until the device has been properly brought up | |
1069 | if (r == 0) { | |
1070 | unsigned int counter = 10; | |
1071 | while (counter-- > 0) { | |
1072 | sleep(1); | |
1073 | ||
fde37387 MT |
1074 | // If the raid device has not yet been properly brought up, |
1075 | // opening it will fail with the message: Device or resource busy | |
1076 | // Hence we will wait a bit until it becomes usable. | |
0e491487 | 1077 | if (try_open(dest->path) == 0) |
4a0d9bef MT |
1078 | break; |
1079 | } | |
1080 | } | |
1081 | ||
1082 | return r; | |
1083 | } | |
1084 | ||
46b56e20 MT |
1085 | int hw_stop_all_raid_arrays(const char* output) { |
1086 | return mysystem(output, "/sbin/mdadm --stop --scan --verbose"); | |
4a0d9bef | 1087 | } |
f5007e9c | 1088 | |
92e78233 | 1089 | int hw_install_bootloader(struct hw* hw, struct hw_destination* dest, const char* output) { |
f5007e9c | 1090 | char cmd[STRING_SIZE]; |
f5007e9c | 1091 | |
12034118 MT |
1092 | snprintf(cmd, sizeof(cmd), "/usr/bin/install-bootloader %s", dest->path); |
1093 | int r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd); | |
92e78233 MT |
1094 | if (r) |
1095 | return r; | |
1096 | ||
ade96ba8 MT |
1097 | hw_sync(); |
1098 | ||
92e78233 | 1099 | return 0; |
f5007e9c | 1100 | } |
7f69d8a4 MT |
1101 | |
1102 | static char* hw_get_uuid(const char* dev) { | |
1103 | blkid_probe p = blkid_new_probe_from_filename(dev); | |
1104 | const char* buffer = NULL; | |
1105 | char* uuid = NULL; | |
1106 | ||
1107 | if (!p) | |
1108 | return NULL; | |
1109 | ||
1110 | blkid_do_probe(p); | |
1111 | blkid_probe_lookup_value(p, "UUID", &buffer, NULL); | |
1112 | ||
1113 | if (buffer) | |
1114 | uuid = strdup(buffer); | |
1115 | ||
1116 | blkid_free_probe(p); | |
1117 | ||
1118 | return uuid; | |
1119 | } | |
1120 | ||
335c5bd1 MT |
1121 | #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n" |
1122 | ||
7f69d8a4 MT |
1123 | int hw_write_fstab(struct hw_destination* dest) { |
1124 | FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w"); | |
1125 | if (!f) | |
1126 | return -1; | |
1127 | ||
7f69d8a4 MT |
1128 | char* uuid = NULL; |
1129 | ||
1130 | // boot | |
1131 | if (*dest->part_boot) { | |
1132 | uuid = hw_get_uuid(dest->part_boot); | |
1133 | ||
1134 | if (uuid) { | |
e404dab5 | 1135 | fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2); |
7f69d8a4 MT |
1136 | free(uuid); |
1137 | } | |
1138 | } | |
1139 | ||
92e78233 MT |
1140 | // ESP |
1141 | if (*dest->part_boot_efi) { | |
1142 | uuid = hw_get_uuid(dest->part_boot_efi); | |
1143 | ||
1144 | if (uuid) { | |
1145 | fprintf(f, FSTAB_FMT, uuid, "/boot/efi", "auto", "defaults", 1, 2); | |
1146 | free(uuid); | |
1147 | } | |
1148 | } | |
1149 | ||
1150 | ||
7f69d8a4 MT |
1151 | // swap |
1152 | if (*dest->part_swap) { | |
1153 | uuid = hw_get_uuid(dest->part_swap); | |
1154 | ||
1155 | if (uuid) { | |
335c5bd1 | 1156 | fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0); |
7f69d8a4 MT |
1157 | free(uuid); |
1158 | } | |
1159 | } | |
1160 | ||
1161 | // root | |
1162 | uuid = hw_get_uuid(dest->part_root); | |
1163 | if (uuid) { | |
335c5bd1 | 1164 | fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1); |
7f69d8a4 MT |
1165 | free(uuid); |
1166 | } | |
1167 | ||
7f69d8a4 MT |
1168 | fclose(f); |
1169 | ||
1170 | return 0; | |
1171 | } | |
282ec35e MT |
1172 | |
1173 | void hw_sync() { | |
1174 | sync(); | |
1175 | sync(); | |
1176 | sync(); | |
1177 | } | |
7d114284 MT |
1178 | |
1179 | int hw_start_networking(const char* output) { | |
1180 | return mysystem(output, "/usr/bin/start-networking.sh"); | |
1181 | } | |
38c6822d MT |
1182 | |
1183 | char* hw_find_backup_file(const char* output, const char* search_path) { | |
1184 | char path[STRING_SIZE]; | |
1185 | ||
1186 | snprintf(path, sizeof(path), "%s/backup.ipf", search_path); | |
1187 | int r = access(path, R_OK); | |
1188 | ||
1189 | if (r == 0) | |
1190 | return strdup(path); | |
1191 | ||
1192 | return NULL; | |
1193 | } | |
1194 | ||
1195 | int hw_restore_backup(const char* output, const char* backup_path, const char* destination) { | |
1196 | char command[STRING_SIZE]; | |
1197 | ||
3f8e70f6 MT |
1198 | snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s " |
1199 | "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user", | |
1200 | backup_path, destination, destination, destination); | |
38c6822d MT |
1201 | int rc = mysystem(output, command); |
1202 | ||
1203 | if (rc) | |
1204 | return -1; | |
1205 | ||
1206 | return 0; | |
1207 | } |