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