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