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