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