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