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