]>
Commit | Line | Data |
---|---|---|
f0fa1795 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # IPFire - An Open Source Firewall Distribution # | |
e404dab5 | 4 | # Copyright (C) 2007-2022 IPFire Team <info@ipfire.org> # |
f0fa1795 MT |
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 | ||
5315fae6 MT |
43 | #include <libsmooth.h> |
44 | ||
f0fa1795 | 45 | #include "hw.h" |
25fcce25 | 46 | |
8e3b022a SS |
47 | extern FILE* flog; |
48 | ||
46b56e20 | 49 | static int system_chroot(const char* output, const char* path, const char* cmd) { |
f5007e9c MT |
50 | char chroot_cmd[STRING_SIZE]; |
51 | ||
52 | snprintf(chroot_cmd, sizeof(chroot_cmd), "/usr/sbin/chroot %s %s", path, cmd); | |
53 | ||
46b56e20 | 54 | return mysystem(output, chroot_cmd); |
f5007e9c MT |
55 | } |
56 | ||
0e0346cc MT |
57 | static int hw_mkdir(const char *dir) { |
58 | char tmp[STRING_SIZE]; | |
59 | char *p = NULL; | |
60 | size_t len; | |
61 | int r; | |
62 | ||
63 | snprintf(tmp, sizeof(tmp),"%s",dir); | |
64 | len = strlen(tmp); | |
65 | ||
66 | if (tmp[len - 1] == '/') { | |
67 | tmp[len - 1] = 0; | |
68 | } | |
69 | ||
70 | for (p = tmp + 1; *p; p++) { | |
71 | if (*p == '/') { | |
72 | *p = 0; | |
73 | ||
74 | // Create target if it does not exist | |
75 | if (access(tmp, X_OK) != 0) { | |
76 | r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO); | |
77 | ||
78 | if (r) { | |
79 | return r; | |
80 | } | |
81 | } | |
82 | ||
83 | *p = '/'; | |
84 | } | |
85 | } | |
86 | ||
87 | // Create target if it does not exist | |
88 | if (access(tmp, X_OK) != 0) { | |
89 | r = mkdir(tmp, S_IRWXU|S_IRWXG|S_IRWXO); | |
90 | ||
91 | if (r) { | |
92 | return r; | |
93 | } | |
94 | } | |
95 | ||
96 | return 0; | |
97 | } | |
98 | ||
f0fa1795 | 99 | struct hw* hw_init() { |
5fb499f1 | 100 | struct hw* hw = calloc(1, sizeof(*hw)); |
f0fa1795 MT |
101 | assert(hw); |
102 | ||
103 | // Initialize libudev | |
104 | hw->udev = udev_new(); | |
105 | if (!hw->udev) { | |
106 | fprintf(stderr, "Could not create udev instance\n"); | |
107 | exit(1); | |
108 | } | |
109 | ||
92e78233 MT |
110 | // What architecture are we running on? |
111 | struct utsname uname_data; | |
112 | int ret = uname(&uname_data); | |
113 | if (ret == 0) | |
114 | snprintf(hw->arch, sizeof(hw->arch), "%s", uname_data.machine); | |
115 | ||
8fbadfc7 | 116 | // Should we install in EFI mode? |
7dc7880d MT |
117 | if ((strcmp(hw->arch, "x86_64") == 0) || (strcmp(hw->arch, "aarch64") == 0)) |
118 | hw->efi = 1; | |
5fb499f1 | 119 | |
f0fa1795 MT |
120 | return hw; |
121 | } | |
122 | ||
123 | void hw_free(struct hw* hw) { | |
124 | if (hw->udev) | |
125 | udev_unref(hw->udev); | |
126 | ||
127 | free(hw); | |
128 | } | |
129 | ||
130 | static int strstartswith(const char* a, const char* b) { | |
131 | return (strncmp(a, b, strlen(b)) == 0); | |
132 | } | |
133 | ||
c0511f3a | 134 | static int setup_loop_device(const char* source, const char* device) { |
eb64913b MT |
135 | int device_fd = -1; |
136 | ||
c0511f3a MT |
137 | int file_fd = open(source, O_RDWR); |
138 | if (file_fd < 0) | |
139 | goto ERROR; | |
140 | ||
c0511f3a MT |
141 | if ((device_fd = open(device, O_RDWR)) < 0) |
142 | goto ERROR; | |
143 | ||
144 | if (ioctl(device_fd, LOOP_SET_FD, file_fd) < 0) | |
145 | goto ERROR; | |
146 | ||
147 | close(file_fd); | |
148 | close(device_fd); | |
149 | ||
150 | return 0; | |
151 | ||
152 | ERROR: | |
153 | if (file_fd >= 0) | |
154 | close(file_fd); | |
155 | ||
156 | if (device_fd >= 0) { | |
157 | ioctl(device_fd, LOOP_CLR_FD, 0); | |
158 | close(device_fd); | |
159 | } | |
160 | ||
161 | return -1; | |
162 | } | |
163 | ||
25fcce25 | 164 | int hw_mount(const char* source, const char* target, const char* fs, int flags) { |
c0511f3a MT |
165 | const char* loop_device = "/dev/loop0"; |
166 | ||
85f2892b MT |
167 | // Create target if it does not exist |
168 | if (access(target, X_OK) != 0) | |
169 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
170 | ||
c0511f3a MT |
171 | struct stat st; |
172 | stat(source, &st); | |
173 | ||
174 | if (S_ISREG(st.st_mode)) { | |
175 | int r = setup_loop_device(source, loop_device); | |
176 | if (r == 0) { | |
177 | source = loop_device; | |
178 | } else { | |
179 | return -1; | |
180 | } | |
181 | } | |
182 | ||
366b40c7 | 183 | return mount(source, target, fs, flags, NULL); |
f0fa1795 MT |
184 | } |
185 | ||
ced15fdf MT |
186 | static int hw_bind_mount(const char* source, const char* prefix) { |
187 | if (!source || !prefix) { | |
188 | errno = EINVAL; | |
189 | return 1; | |
190 | } | |
191 | ||
192 | char target[PATH_MAX]; | |
193 | int r; | |
194 | ||
195 | // Format target | |
196 | r = snprintf(target, sizeof(target) - 1, "%s/%s", prefix, source); | |
197 | if (r < 0) | |
198 | return 1; | |
199 | ||
200 | // Ensure target exists | |
201 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
ade96ba8 | 202 | |
ced15fdf MT |
203 | return hw_mount(source, target, NULL, MS_BIND); |
204 | } | |
205 | ||
206 | int hw_umount(const char* source, const char* prefix) { | |
207 | char target[PATH_MAX]; | |
208 | int r; | |
ade96ba8 | 209 | |
ced15fdf MT |
210 | if (prefix) |
211 | r = snprintf(target, sizeof(target) - 1, "%s/%s", prefix, source); | |
212 | else | |
213 | r = snprintf(target, sizeof(target) - 1, "%s", source); | |
214 | if (r < 0) | |
215 | return r; | |
216 | ||
217 | // Perform umount | |
218 | r = umount2(target, 0); | |
219 | if (r) { | |
220 | switch (errno) { | |
221 | // Try again with force if umount wasn't successful | |
222 | case EBUSY: | |
223 | sleep(1); | |
224 | ||
225 | r = umount2(target, MNT_FORCE); | |
226 | break; | |
227 | ||
228 | // target wasn't a mountpoint. Ignore. | |
229 | case EINVAL: | |
230 | r = 0; | |
231 | break; | |
91588cb4 MT |
232 | |
233 | // target doesn't exist | |
234 | case ENOENT: | |
235 | r = 0; | |
236 | break; | |
ced15fdf | 237 | } |
ade96ba8 MT |
238 | } |
239 | ||
240 | return r; | |
f0fa1795 MT |
241 | } |
242 | ||
243 | static int hw_test_source_medium(const char* path) { | |
25fcce25 | 244 | int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY); |
f0fa1795 | 245 | |
68a50dd1 AF |
246 | if (ret != 0) { |
247 | // 2nd try, ntfs for a rufus converted usb key | |
248 | ret = hw_mount(path, SOURCE_MOUNT_PATH, "ntfs3", MS_RDONLY); | |
249 | } | |
250 | if (ret != 0) { | |
251 | // 3rd try, vfat for a rufus converted usb key | |
252 | ret = hw_mount(path, SOURCE_MOUNT_PATH, "vfat", MS_RDONLY); | |
253 | } | |
254 | ||
f0fa1795 MT |
255 | // If the source could not be mounted we |
256 | // cannot proceed. | |
5315fae6 | 257 | if (ret != 0) |
f0fa1795 MT |
258 | return ret; |
259 | ||
260 | // Check if the test file exists. | |
5315fae6 | 261 | ret = access(SOURCE_TEST_FILE, R_OK); |
f0fa1795 MT |
262 | |
263 | // Umount the test device. | |
ced15fdf | 264 | hw_umount(SOURCE_MOUNT_PATH, NULL); |
f0fa1795 | 265 | |
fb76fc51 | 266 | return ret; |
f0fa1795 MT |
267 | } |
268 | ||
269 | char* hw_find_source_medium(struct hw* hw) { | |
270 | char* ret = NULL; | |
271 | ||
272 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); | |
273 | ||
274 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
275 | udev_enumerate_scan_devices(enumerate); | |
276 | ||
277 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
278 | ||
279 | struct udev_list_entry* dev_list_entry; | |
280 | udev_list_entry_foreach(dev_list_entry, devices) { | |
281 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
282 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
283 | ||
284 | const char* dev_path = udev_device_get_devnode(dev); | |
285 | ||
286 | // Skip everything what we cannot work with | |
287 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef | 288 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md")) |
f0fa1795 MT |
289 | continue; |
290 | ||
5315fae6 | 291 | if (hw_test_source_medium(dev_path) == 0) { |
f0fa1795 MT |
292 | ret = strdup(dev_path); |
293 | } | |
294 | ||
295 | udev_device_unref(dev); | |
296 | ||
297 | // If a suitable device was found the search will end. | |
298 | if (ret) | |
299 | break; | |
300 | } | |
301 | ||
302 | udev_enumerate_unref(enumerate); | |
303 | ||
304 | return ret; | |
305 | } | |
d7dd283b MT |
306 | |
307 | static struct hw_disk** hw_create_disks() { | |
308 | struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1)); | |
309 | ||
310 | return ret; | |
311 | } | |
312 | ||
313 | static unsigned long long hw_block_device_get_size(const char* dev) { | |
314 | int fd = open(dev, O_RDONLY); | |
315 | if (fd < 0) | |
316 | return 0; | |
317 | ||
318 | unsigned long long size = blkid_get_dev_size(fd); | |
319 | close(fd); | |
320 | ||
321 | return size; | |
322 | } | |
323 | ||
ee00d203 | 324 | struct hw_disk** hw_find_disks(struct hw* hw, const char* sourcedrive) { |
d7dd283b MT |
325 | struct hw_disk** ret = hw_create_disks(); |
326 | struct hw_disk** disks = ret; | |
874ec4b0 | 327 | char size_str[32]; |
d7dd283b | 328 | |
68a50dd1 | 329 | // Determine the disk device of source if it is a partition |
16640487 | 330 | const char* sourcedisk = NULL; |
68a50dd1 AF |
331 | char syssource[PATH_MAX]; |
332 | (void)snprintf(syssource, sizeof(syssource) - 1, "/sys/class/block/%s", sourcedrive + 5); | |
333 | struct udev_device* s_dev = udev_device_new_from_syspath(hw->udev, syssource); | |
334 | const char* s_devtype = udev_device_get_property_value(s_dev, "DEVTYPE"); | |
335 | if (s_devtype && (strcmp(s_devtype, "partition") == 0)) { | |
336 | struct udev_device* p_dev = udev_device_get_parent_with_subsystem_devtype(s_dev,"block","disk"); | |
337 | if (p_dev) { | |
338 | sourcedisk = udev_device_get_devnode(p_dev); | |
339 | } | |
340 | } | |
341 | if (!sourcedisk) sourcedisk = sourcedrive; | |
342 | ||
d7dd283b MT |
343 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); |
344 | ||
345 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
346 | udev_enumerate_scan_devices(enumerate); | |
347 | ||
348 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
349 | ||
350 | struct udev_list_entry* dev_list_entry; | |
351 | unsigned int i = HW_MAX_DISKS; | |
352 | udev_list_entry_foreach(dev_list_entry, devices) { | |
353 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
354 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
355 | ||
356 | const char* dev_path = udev_device_get_devnode(dev); | |
357 | ||
358 | // Skip everything what we cannot work with | |
359 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef MT |
360 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") || |
361 | strstartswith(dev_path, "/dev/md")) { | |
d7dd283b MT |
362 | udev_device_unref(dev); |
363 | continue; | |
364 | } | |
365 | ||
68a50dd1 AF |
366 | // Skip sourcedisk if we need to |
367 | if (sourcedisk && (strcmp(dev_path, sourcedisk) == 0)) { | |
d7dd283b MT |
368 | udev_device_unref(dev); |
369 | continue; | |
370 | } | |
371 | ||
ee00d203 MT |
372 | // DEVTYPE must be disk (otherwise we will see all sorts of partitions here) |
373 | const char* devtype = udev_device_get_property_value(dev, "DEVTYPE"); | |
374 | if (devtype && (strcmp(devtype, "disk") != 0)) { | |
d7dd283b MT |
375 | udev_device_unref(dev); |
376 | continue; | |
377 | } | |
378 | ||
379 | // Skip devices with a size of zero | |
380 | unsigned long long size = hw_block_device_get_size(dev_path); | |
381 | if (size == 0) { | |
382 | udev_device_unref(dev); | |
383 | continue; | |
384 | } | |
385 | ||
386 | struct hw_disk* disk = malloc(sizeof(*disk)); | |
387 | if (disk == NULL) | |
388 | return NULL; | |
389 | ||
390 | disk->ref = 1; | |
391 | ||
874ec4b0 | 392 | snprintf(disk->path, sizeof(disk->path), "%s", dev_path); |
0c8b2d99 | 393 | const char* p = disk->path + 5; |
d7dd283b MT |
394 | |
395 | disk->size = size; | |
396 | ||
397 | // Vendor | |
398 | const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR"); | |
399 | if (!vendor) | |
400 | vendor = udev_device_get_sysattr_value(dev, "vendor"); | |
401 | if (!vendor) | |
402 | vendor = udev_device_get_sysattr_value(dev, "manufacturer"); | |
d7dd283b | 403 | |
4c812463 | 404 | if (vendor) |
874ec4b0 | 405 | snprintf(disk->vendor, sizeof(disk->vendor), "%s", vendor); |
4c812463 MT |
406 | else |
407 | *disk->vendor = '\0'; | |
d7dd283b MT |
408 | |
409 | // Model | |
410 | const char* model = udev_device_get_property_value(dev, "ID_MODEL"); | |
411 | if (!model) | |
412 | model = udev_device_get_sysattr_value(dev, "model"); | |
413 | if (!model) | |
414 | model = udev_device_get_sysattr_value(dev, "product"); | |
d7dd283b | 415 | |
4c812463 | 416 | if (model) |
874ec4b0 | 417 | snprintf(disk->model, sizeof(disk->model), "%s", model); |
4c812463 MT |
418 | else |
419 | *disk->model = '\0'; | |
420 | ||
421 | // Format description | |
4c812463 | 422 | snprintf(size_str, sizeof(size_str), "%4.1fGB", (double)disk->size / pow(1024, 3)); |
d7dd283b | 423 | |
4c812463 MT |
424 | if (*disk->vendor && *disk->model) { |
425 | snprintf(disk->description, sizeof(disk->description), | |
0c8b2d99 | 426 | "%s - %s - %s - %s", size_str, p, disk->vendor, disk->model); |
4c812463 MT |
427 | |
428 | } else if (*disk->vendor || *disk->model) { | |
429 | snprintf(disk->description, sizeof(disk->description), | |
0c8b2d99 | 430 | "%s - %s - %s", size_str, p, (*disk->vendor) ? disk->vendor : disk->model); |
4c812463 MT |
431 | |
432 | } else { | |
433 | snprintf(disk->description, sizeof(disk->description), | |
0c8b2d99 | 434 | "%s - %s", size_str, p); |
4c812463 | 435 | } |
d7dd283b | 436 | |
29afd408 MT |
437 | // Cut off the description string after 40 characters |
438 | disk->description[41] = '\0'; | |
439 | ||
d7dd283b MT |
440 | *disks++ = disk; |
441 | ||
442 | if (--i == 0) | |
443 | break; | |
444 | ||
445 | udev_device_unref(dev); | |
446 | } | |
447 | ||
448 | udev_enumerate_unref(enumerate); | |
449 | ||
450 | *disks = NULL; | |
451 | ||
452 | return ret; | |
453 | } | |
454 | ||
455 | void hw_free_disks(struct hw_disk** disks) { | |
456 | struct hw_disk** disk = disks; | |
457 | ||
458 | while (*disk != NULL) { | |
459 | if (--(*disk)->ref == 0) | |
460 | free(*disk); | |
461 | ||
462 | disk++; | |
463 | } | |
464 | ||
465 | free(disks); | |
466 | } | |
467 | ||
16640487 | 468 | unsigned int hw_count_disks(struct hw_disk** disks) { |
d7dd283b MT |
469 | unsigned int ret = 0; |
470 | ||
471 | while (*disks++) | |
472 | ret++; | |
473 | ||
474 | return ret; | |
475 | } | |
476 | ||
477 | struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) { | |
478 | struct hw_disk** ret = hw_create_disks(); | |
479 | struct hw_disk** selected_disks = ret; | |
480 | ||
16640487 | 481 | unsigned int num_disks = hw_count_disks(disks); |
d7dd283b MT |
482 | |
483 | for (unsigned int i = 0; i < num_disks; i++) { | |
ee43f517 | 484 | if (!selection || selection[i]) { |
d7dd283b MT |
485 | struct hw_disk *selected_disk = disks[i]; |
486 | selected_disk->ref++; | |
487 | ||
488 | *selected_disks++ = selected_disk; | |
489 | } | |
490 | } | |
491 | ||
492 | // Set sentinel | |
493 | *selected_disks = NULL; | |
494 | ||
495 | return ret; | |
496 | } | |
497 | ||
16640487 | 498 | struct hw_disk** hw_select_first_disk(struct hw_disk** disks) { |
a3e135c8 MT |
499 | struct hw_disk** ret = hw_create_disks(); |
500 | struct hw_disk** selected_disks = ret; | |
501 | ||
502 | unsigned int num_disks = hw_count_disks(disks); | |
503 | assert(num_disks > 0); | |
504 | ||
505 | for (unsigned int i = 0; i < num_disks; i++) { | |
506 | struct hw_disk *disk = disks[i]; | |
507 | disk->ref++; | |
508 | ||
509 | *selected_disks++ = disk; | |
510 | break; | |
511 | } | |
512 | ||
513 | // Set sentinel | |
514 | *selected_disks = NULL; | |
515 | ||
516 | return ret; | |
517 | } | |
518 | ||
25fcce25 MT |
519 | static unsigned long long hw_swap_size(struct hw_destination* dest) { |
520 | unsigned long long memory = hw_memory(); | |
521 | ||
522 | unsigned long long swap_size = memory / 4; | |
523 | ||
524 | // Min. swap size is 128MB | |
525 | if (swap_size < MB2BYTES(128)) | |
526 | swap_size = MB2BYTES(128); | |
527 | ||
528 | // Cap swap size to 1GB | |
529 | else if (swap_size > MB2BYTES(1024)) | |
530 | swap_size = MB2BYTES(1024); | |
531 | ||
532 | return swap_size; | |
533 | } | |
534 | ||
25fcce25 | 535 | static unsigned long long hw_boot_size(struct hw_destination* dest) { |
ffdc6fbb | 536 | return MB2BYTES(512); |
25fcce25 MT |
537 | } |
538 | ||
a691d4b3 MT |
539 | static int hw_device_has_p_suffix(const struct hw_destination* dest) { |
540 | // All RAID devices have the p suffix. | |
541 | if (dest->is_raid) | |
542 | return 1; | |
543 | ||
544 | // Devices with a number at the end have the p suffix, too. | |
545 | // e.g. mmcblk0, cciss0 | |
126d3570 | 546 | unsigned int last_char = strlen(dest->path) - 1; |
a691d4b3 MT |
547 | if ((dest->path[last_char] >= '0') && (dest->path[last_char] <= '9')) |
548 | return 1; | |
549 | ||
550 | return 0; | |
551 | } | |
552 | ||
92e78233 | 553 | static int hw_calculate_partition_table(struct hw* hw, struct hw_destination* dest, int disable_swap) { |
48d6a112 MT |
554 | char path[DEV_SIZE]; |
555 | int part_idx = 1; | |
556 | ||
a691d4b3 MT |
557 | snprintf(path, sizeof(path), "%s%s", dest->path, |
558 | hw_device_has_p_suffix(dest) ? "p" : ""); | |
48d6a112 MT |
559 | dest->part_boot_idx = 0; |
560 | ||
25fcce25 MT |
561 | // Determine the size of the target block device |
562 | if (dest->is_raid) { | |
563 | dest->size = (dest->disk1->size >= dest->disk2->size) ? | |
5315fae6 | 564 | dest->disk2->size : dest->disk1->size; |
22581f51 MT |
565 | |
566 | // The RAID will install some metadata at the end of the disk | |
567 | // and we will save up some space for that. | |
568 | dest->size -= MB2BYTES(2); | |
25fcce25 MT |
569 | } else { |
570 | dest->size = dest->disk1->size; | |
571 | } | |
572 | ||
802a123b MT |
573 | // As we add some extra space before the beginning of the first |
574 | // partition, we need to substract that here. | |
575 | dest->size -= MB2BYTES(1); | |
576 | ||
577 | // Add some more space for partition tables, etc. | |
578 | dest->size -= MB2BYTES(1); | |
579 | ||
c577571e MT |
580 | // The disk has to have at least 2GB |
581 | if (dest->size <= MB2BYTES(2048)) | |
582 | return -1; | |
583 | ||
48d6a112 MT |
584 | // Determine partition table |
585 | dest->part_table = HW_PART_TABLE_MSDOS; | |
586 | ||
587 | // Disks over 2TB need to use GPT | |
588 | if (dest->size >= MB2BYTES(2047 * 1024)) | |
589 | dest->part_table = HW_PART_TABLE_GPT; | |
590 | ||
591 | // We also use GPT on raid disks by default | |
592 | else if (dest->is_raid) | |
593 | dest->part_table = HW_PART_TABLE_GPT; | |
594 | ||
595 | // When using GPT, GRUB2 needs a little bit of space to put | |
596 | // itself in. | |
5689fc72 | 597 | if (dest->part_table == HW_PART_TABLE_GPT) { |
48d6a112 MT |
598 | snprintf(dest->part_bootldr, sizeof(dest->part_bootldr), |
599 | "%s%d", path, part_idx); | |
600 | ||
601 | dest->size_bootldr = MB2BYTES(4); | |
602 | ||
603 | dest->part_boot_idx = part_idx++; | |
604 | } else { | |
605 | *dest->part_bootldr = '\0'; | |
606 | dest->size_bootldr = 0; | |
607 | } | |
608 | ||
58a46f0b SS |
609 | // Disable seperate boot partition for BTRFS installations. |
610 | if(dest->filesystem == HW_FS_BTRFS) { | |
611 | dest->size_boot = 0; | |
612 | } else { | |
613 | dest->size_boot = hw_boot_size(dest); | |
614 | } | |
c577571e | 615 | |
92e78233 MT |
616 | // Create an EFI partition when running in EFI mode |
617 | if (hw->efi) | |
618 | dest->size_boot_efi = MB2BYTES(32); | |
619 | else | |
620 | dest->size_boot_efi = 0; | |
621 | ||
c577571e MT |
622 | // Determine the size of the data partition. |
623 | unsigned long long space_left = dest->size - \ | |
92e78233 | 624 | (dest->size_bootldr + dest->size_boot + dest->size_boot_efi); |
c577571e MT |
625 | |
626 | // If we have less than 2GB left, we disable swap | |
627 | if (space_left <= MB2BYTES(2048)) | |
628 | disable_swap = 1; | |
25fcce25 | 629 | |
a8fca245 MT |
630 | // Should we use swap? |
631 | if (disable_swap) | |
632 | dest->size_swap = 0; | |
633 | else | |
634 | dest->size_swap = hw_swap_size(dest); | |
635 | ||
c577571e MT |
636 | // Subtract swap |
637 | space_left -= dest->size_swap; | |
25fcce25 | 638 | |
c577571e MT |
639 | // Root is getting what ever is left |
640 | dest->size_root = space_left; | |
25fcce25 MT |
641 | |
642 | // Set partition names | |
25fcce25 | 643 | if (dest->size_boot > 0) { |
48d6a112 MT |
644 | if (dest->part_boot_idx == 0) |
645 | dest->part_boot_idx = part_idx; | |
25fcce25 MT |
646 | |
647 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
648 | } else | |
649 | *dest->part_boot = '\0'; | |
650 | ||
92e78233 MT |
651 | if (dest->size_boot_efi > 0) { |
652 | dest->part_boot_efi_idx = part_idx; | |
653 | ||
654 | snprintf(dest->part_boot_efi, sizeof(dest->part_boot_efi), | |
655 | "%s%d", path, part_idx++); | |
58e840bd | 656 | } else { |
92e78233 | 657 | *dest->part_boot_efi = '\0'; |
58e840bd MT |
658 | dest->part_boot_efi_idx = 0; |
659 | } | |
92e78233 | 660 | |
25fcce25 MT |
661 | if (dest->size_swap > 0) |
662 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
663 | else | |
664 | *dest->part_swap = '\0'; | |
665 | ||
666 | // There is always a root partition | |
48d6a112 | 667 | if (dest->part_boot_idx == 0) |
25fcce25 MT |
668 | dest->part_boot_idx = part_idx; |
669 | ||
670 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
671 | ||
25fcce25 MT |
672 | return 0; |
673 | } | |
674 | ||
ad73749d SS |
675 | struct hw_destination* hw_make_destination(struct hw* hw, int part_type, struct hw_disk** disks, |
676 | int disable_swap, int filesystem) { | |
d7dd283b MT |
677 | struct hw_destination* dest = malloc(sizeof(*dest)); |
678 | ||
ad73749d SS |
679 | // Assign filesystem |
680 | dest->filesystem = filesystem; | |
681 | ||
d7dd283b MT |
682 | if (part_type == HW_PART_TYPE_NORMAL) { |
683 | dest->disk1 = *disks; | |
684 | dest->disk2 = NULL; | |
685 | ||
686 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
687 | ||
688 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
689 | dest->disk1 = *disks++; | |
690 | dest->disk2 = *disks; | |
4a0d9bef | 691 | dest->raid_level = 1; |
d7dd283b MT |
692 | |
693 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
694 | } | |
695 | ||
696 | // Is this a RAID device? | |
697 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
698 | ||
92e78233 | 699 | int r = hw_calculate_partition_table(hw, dest, disable_swap); |
25fcce25 MT |
700 | if (r) |
701 | return NULL; | |
d7dd283b | 702 | |
d7dd283b MT |
703 | return dest; |
704 | } | |
c4e96674 MT |
705 | |
706 | unsigned long long hw_memory() { | |
5be66d81 | 707 | struct sysinfo si; |
c4e96674 | 708 | |
5be66d81 MT |
709 | int r = sysinfo(&si); |
710 | if (r < 0) | |
711 | return 0; | |
c4e96674 | 712 | |
5be66d81 | 713 | return si.totalram; |
c4e96674 | 714 | } |
25fcce25 | 715 | |
d2f993a7 MT |
716 | static int hw_zero_out_device(const char* path, int bytes) { |
717 | char block[512]; | |
718 | memset(block, 0, sizeof(block)); | |
719 | ||
720 | int blocks = bytes / sizeof(block); | |
721 | ||
722 | int fd = open(path, O_WRONLY); | |
723 | if (fd < 0) | |
724 | return -1; | |
725 | ||
726 | unsigned int bytes_written = 0; | |
727 | while (blocks-- > 0) { | |
728 | bytes_written += write(fd, block, sizeof(block)); | |
729 | } | |
730 | ||
731 | fsync(fd); | |
732 | close(fd); | |
733 | ||
734 | return bytes_written; | |
735 | } | |
736 | ||
0e491487 MT |
737 | static int try_open(const char* path) { |
738 | FILE* f = fopen(path, "r"); | |
739 | if (f) { | |
740 | fclose(f); | |
741 | return 0; | |
742 | } | |
743 | ||
744 | return -1; | |
745 | } | |
746 | ||
46b56e20 | 747 | int hw_create_partitions(struct hw_destination* dest, const char* output) { |
d2f993a7 MT |
748 | // Before we write a new partition table to the disk, we will erase |
749 | // the first couple of megabytes at the beginning of the device to | |
750 | // get rid of all left other things like bootloaders and partition tables. | |
751 | // This solves some problems when changing from MBR to GPT partitions or | |
752 | // the other way around. | |
753 | int r = hw_zero_out_device(dest->path, MB2BYTES(10)); | |
754 | if (r <= 0) | |
755 | return r; | |
25fcce25 | 756 | |
d2f993a7 | 757 | char* cmd = NULL; |
25fcce25 MT |
758 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); |
759 | ||
760 | // Set partition type | |
761 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
762 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
763 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
764 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
765 | ||
802a123b | 766 | unsigned long long part_start = MB2BYTES(1); |
25fcce25 | 767 | |
48d6a112 | 768 | if (*dest->part_bootldr) { |
802a123b | 769 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
48d6a112 | 770 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary", |
802a123b | 771 | part_start, part_start + dest->size_bootldr - 1); |
48d6a112 MT |
772 | |
773 | part_start += dest->size_bootldr; | |
774 | } | |
775 | ||
25fcce25 | 776 | if (*dest->part_boot) { |
802a123b | 777 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 778 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", |
802a123b | 779 | part_start, part_start + dest->size_boot - 1); |
25fcce25 MT |
780 | |
781 | part_start += dest->size_boot; | |
782 | } | |
783 | ||
92e78233 MT |
784 | if (*dest->part_boot_efi) { |
785 | asprintf(&cmd, "%s mkpart %s fat32 %lluB %lluB", cmd, | |
786 | (dest->part_table == HW_PART_TABLE_GPT) ? "ESP" : "primary", | |
787 | part_start, part_start + dest->size_boot_efi - 1); | |
788 | ||
789 | part_start += dest->size_boot_efi; | |
790 | } | |
791 | ||
25fcce25 | 792 | if (*dest->part_swap) { |
802a123b | 793 | asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd, |
25fcce25 | 794 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", |
802a123b | 795 | part_start, part_start + dest->size_swap - 1); |
25fcce25 MT |
796 | |
797 | part_start += dest->size_swap; | |
798 | } | |
799 | ||
800 | if (*dest->part_root) { | |
802a123b | 801 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 802 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", |
802a123b | 803 | part_start, part_start + dest->size_root - 1); |
25fcce25 MT |
804 | |
805 | part_start += dest->size_root; | |
806 | } | |
807 | ||
e9cf574d | 808 | if (dest->part_boot_idx > 0) |
25fcce25 MT |
809 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); |
810 | ||
92e78233 MT |
811 | if (dest->part_boot_efi_idx > 0) |
812 | asprintf(&cmd, "%s set %d esp on", cmd, dest->part_boot_efi_idx); | |
813 | ||
e9cf574d | 814 | if (dest->part_table == HW_PART_TABLE_GPT) { |
48d6a112 MT |
815 | if (*dest->part_bootldr) { |
816 | asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx); | |
817 | } | |
25fcce25 MT |
818 | } |
819 | ||
46b56e20 | 820 | r = mysystem(output, cmd); |
25fcce25 | 821 | |
268090a8 MT |
822 | // Wait until the system re-read the partition table |
823 | if (r == 0) { | |
824 | unsigned int counter = 10; | |
825 | ||
826 | while (counter-- > 0) { | |
827 | sleep(1); | |
828 | ||
0e491487 | 829 | if (*dest->part_bootldr && (try_open(dest->part_bootldr) != 0)) |
48d6a112 MT |
830 | continue; |
831 | ||
0e491487 | 832 | if (*dest->part_boot && (try_open(dest->part_boot) != 0)) |
268090a8 MT |
833 | continue; |
834 | ||
92e78233 MT |
835 | if (*dest->part_boot_efi && (try_open(dest->part_boot_efi) != 0)) |
836 | continue; | |
837 | ||
0e491487 | 838 | if (*dest->part_swap && (try_open(dest->part_swap) != 0)) |
268090a8 MT |
839 | continue; |
840 | ||
0e491487 | 841 | if (*dest->part_root && (try_open(dest->part_root) != 0)) |
268090a8 MT |
842 | continue; |
843 | ||
268090a8 MT |
844 | // All partitions do exist, exiting the loop. |
845 | break; | |
846 | } | |
847 | } | |
848 | ||
25fcce25 MT |
849 | if (cmd) |
850 | free(cmd); | |
851 | ||
852 | return r; | |
853 | } | |
854 | ||
42849040 MT |
855 | static int hw_create_btrfs_subvolume(const char* output, const char* subvolume) { |
856 | char command [STRING_SIZE]; | |
857 | int r; | |
858 | ||
859 | // Abort if the command could not be assigned. | |
860 | r = snprintf(command, sizeof(command), "/usr/bin/btrfs subvolume create %s/%s", DESTINATION_MOUNT_PATH, subvolume); | |
861 | if (r < 0) | |
862 | return r; | |
863 | ||
864 | // Create the subvolume | |
865 | r = mysystem(output, command); | |
866 | if (r) | |
867 | return r; | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
872 | static int hw_create_btrfs_layout(const char* path, const char* output) { | |
873 | const struct btrfs_subvolumes* subvolume = NULL; | |
874 | char cmd[STRING_SIZE]; | |
875 | char volume[STRING_SIZE]; | |
876 | int r; | |
877 | ||
878 | r = snprintf(cmd, sizeof(cmd), "/usr/bin/mkfs.btrfs -f %s", path); | |
879 | if (r < 0) | |
880 | return r; | |
881 | ||
882 | // Create the main BTRFS file system. | |
883 | r = mysystem(output, cmd); | |
884 | if (r) | |
885 | return r; | |
886 | ||
887 | // We need to mount the FS in order to create any subvolumes. | |
888 | r = hw_mount(path, DESTINATION_MOUNT_PATH, "btrfs", 0); | |
889 | if (r) | |
890 | return r; | |
891 | ||
892 | // Loop through the list of subvolumes to create. | |
893 | for ( subvolume = btrfs_subvolumes; subvolume->name; subvolume++ ) { | |
894 | r = snprintf(volume, sizeof(volume), "%s", subvolume->name); | |
895 | ||
896 | // Abort if snprintf fails. | |
897 | if (r < 0) | |
898 | return r; | |
899 | ||
900 | // Call function to create the subvolume | |
901 | r = hw_create_btrfs_subvolume(output, volume); | |
902 | if (r) | |
903 | return r; | |
904 | } | |
905 | ||
906 | // Umount the main BTRFS after subvolume creation. | |
907 | r = hw_umount(DESTINATION_MOUNT_PATH, 0); | |
908 | if (r) | |
909 | return r; | |
910 | ||
911 | return 0; | |
912 | } | |
913 | ||
46b56e20 | 914 | static int hw_format_filesystem(const char* path, int fs, const char* output) { |
25fcce25 | 915 | char cmd[STRING_SIZE] = "\0"; |
0465449e | 916 | int r; |
25fcce25 MT |
917 | |
918 | // Swap | |
919 | if (fs == HW_FS_SWAP) { | |
920 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
25fcce25 MT |
921 | |
922 | // EXT4 | |
923 | } else if (fs == HW_FS_EXT4) { | |
5208ceed | 924 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 %s", path); |
25fcce25 MT |
925 | |
926 | // EXT4 w/o journal | |
927 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
5208ceed | 928 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -FF -T ext4 -O ^has_journal %s", path); |
70a44b52 MT |
929 | |
930 | // XFS | |
931 | } else if (fs == HW_FS_XFS) { | |
932 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path); | |
92e78233 | 933 | |
130815d3 SS |
934 | // BTRFS |
935 | } else if (fs == HW_FS_BTRFS) { | |
0465449e SS |
936 | r = hw_create_btrfs_layout(path, output); |
937 | ||
938 | return r; | |
130815d3 | 939 | |
92e78233 MT |
940 | // FAT32 |
941 | } else if (fs == HW_FS_FAT32) { | |
942 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.vfat %s", path); | |
25fcce25 MT |
943 | } |
944 | ||
945 | assert(*cmd); | |
946 | ||
0465449e | 947 | r = mysystem(output, cmd); |
25fcce25 MT |
948 | |
949 | return r; | |
950 | } | |
951 | ||
46b56e20 | 952 | int hw_create_filesystems(struct hw_destination* dest, const char* output) { |
25fcce25 MT |
953 | int r; |
954 | ||
955 | // boot | |
956 | if (*dest->part_boot) { | |
46b56e20 | 957 | r = hw_format_filesystem(dest->part_boot, dest->filesystem, output); |
25fcce25 MT |
958 | if (r) |
959 | return r; | |
960 | } | |
961 | ||
92e78233 MT |
962 | // ESP |
963 | if (*dest->part_boot_efi) { | |
964 | r = hw_format_filesystem(dest->part_boot_efi, HW_FS_FAT32, output); | |
965 | if (r) | |
966 | return r; | |
967 | } | |
968 | ||
25fcce25 MT |
969 | // swap |
970 | if (*dest->part_swap) { | |
46b56e20 | 971 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP, output); |
25fcce25 MT |
972 | if (r) |
973 | return r; | |
974 | } | |
975 | ||
976 | // root | |
46b56e20 | 977 | r = hw_format_filesystem(dest->part_root, dest->filesystem, output); |
25fcce25 MT |
978 | if (r) |
979 | return r; | |
980 | ||
25fcce25 MT |
981 | return 0; |
982 | } | |
983 | ||
42849040 | 984 | static int hw_mount_btrfs_subvolumes(const char* source) { |
0465449e | 985 | const struct btrfs_subvolumes* subvolume = NULL; |
42849040 MT |
986 | char path[STRING_SIZE]; |
987 | char options[STRING_SIZE]; | |
0465449e SS |
988 | int r; |
989 | ||
42849040 MT |
990 | // Loop through the list of known subvolumes. |
991 | for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) { | |
992 | // Assign subvolume path. | |
993 | r = snprintf(path, sizeof(path), "%s%s", DESTINATION_MOUNT_PATH, subvolume->mount_path); | |
994 | if (r < 0) | |
995 | return r; | |
0465449e | 996 | |
42849040 MT |
997 | // Assign subvolume name. |
998 | r = snprintf(options, sizeof(options), "subvol=%s,%s", subvolume->name, BTRFS_MOUNT_OPTIONS); | |
999 | if (r < 0) | |
0465449e | 1000 | return r; |
0465449e | 1001 | |
42849040 | 1002 | // Create the directory. |
0e0346cc | 1003 | r = hw_mkdir(path); |
0465449e | 1004 | |
42849040 MT |
1005 | // Abort if the directory could not be created. |
1006 | if(r != 0 && errno != EEXIST) | |
0465449e | 1007 | return r; |
0465449e | 1008 | |
42849040 MT |
1009 | // Print log message |
1010 | fprintf(flog, "Mounting subvolume %s to %s\n", subvolume->name, subvolume->mount_path); | |
0465449e | 1011 | |
42849040 | 1012 | // Try to mount the subvolume. |
1415e83a | 1013 | r = mount(source, path, "btrfs", 0, options); |
42849040 MT |
1014 | if (r) |
1015 | return r; | |
0465449e SS |
1016 | } |
1017 | ||
1018 | return 0; | |
1019 | } | |
1020 | ||
25fcce25 MT |
1021 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { |
1022 | char target[STRING_SIZE]; | |
e7740eaf | 1023 | int r; |
25fcce25 MT |
1024 | |
1025 | assert(*prefix == '/'); | |
1026 | ||
1027 | const char* filesystem; | |
1028 | switch (dest->filesystem) { | |
25fcce25 MT |
1029 | case HW_FS_EXT4: |
1030 | case HW_FS_EXT4_WO_JOURNAL: | |
1031 | filesystem = "ext4"; | |
1032 | break; | |
1033 | ||
70a44b52 MT |
1034 | case HW_FS_XFS: |
1035 | filesystem = "xfs"; | |
1036 | break; | |
1037 | ||
130815d3 SS |
1038 | case HW_FS_BTRFS: |
1039 | filesystem = "btrfs"; | |
1040 | break; | |
1041 | ||
92e78233 MT |
1042 | case HW_FS_FAT32: |
1043 | filesystem = "vfat"; | |
1044 | break; | |
1045 | ||
25fcce25 MT |
1046 | default: |
1047 | assert(0); | |
1048 | } | |
1049 | ||
1050 | // root | |
e7740eaf SS |
1051 | if (dest->filesystem == HW_FS_BTRFS) { |
1052 | r = hw_mount_btrfs_subvolumes(dest->part_root); | |
e7740eaf SS |
1053 | if (r) |
1054 | return r; | |
1055 | } else { | |
1056 | r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
e7740eaf SS |
1057 | if (r) |
1058 | return r; | |
1059 | } | |
25fcce25 MT |
1060 | |
1061 | // boot | |
fbeac096 SS |
1062 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); |
1063 | r = mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
1064 | ||
1065 | if (r) { | |
1066 | hw_umount_filesystems(dest, prefix); | |
25fcce25 | 1067 | |
fbeac096 SS |
1068 | return r; |
1069 | } | |
1070 | ||
1071 | if (*dest->part_boot) { | |
25fcce25 MT |
1072 | r = hw_mount(dest->part_boot, target, filesystem, 0); |
1073 | if (r) { | |
1074 | hw_umount_filesystems(dest, prefix); | |
1075 | ||
1076 | return r; | |
1077 | } | |
1078 | } | |
1079 | ||
92e78233 MT |
1080 | // ESP |
1081 | if (*dest->part_boot_efi) { | |
1082 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT_EFI); | |
1083 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
1084 | ||
1085 | r = hw_mount(dest->part_boot_efi, target, "vfat", 0); | |
1086 | if (r) { | |
1087 | hw_umount_filesystems(dest, prefix); | |
1088 | ||
1089 | return r; | |
1090 | } | |
1091 | } | |
1092 | ||
25fcce25 MT |
1093 | // swap |
1094 | if (*dest->part_swap) { | |
1095 | r = swapon(dest->part_swap, 0); | |
1096 | if (r) { | |
1097 | hw_umount_filesystems(dest, prefix); | |
1098 | ||
1099 | return r; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | // bind-mount misc filesystems | |
ced15fdf MT |
1104 | r = hw_bind_mount("/dev", prefix); |
1105 | if (r) | |
1106 | return r; | |
25fcce25 | 1107 | |
ced15fdf MT |
1108 | r = hw_bind_mount("/proc", prefix); |
1109 | if (r) | |
1110 | return r; | |
25fcce25 | 1111 | |
ced15fdf MT |
1112 | r = hw_bind_mount("/sys", prefix); |
1113 | if (r) | |
1114 | return r; | |
25fcce25 | 1115 | |
ced15fdf MT |
1116 | r = hw_bind_mount("/sys/firmware/efi/efivars", prefix); |
1117 | if (r && errno != ENOENT) | |
1118 | return r; | |
25fcce25 MT |
1119 | |
1120 | return 0; | |
1121 | } | |
1122 | ||
42849040 | 1123 | static int hw_umount_btrfs_layout() { |
e7740eaf SS |
1124 | const struct btrfs_subvolumes* subvolume = NULL; |
1125 | char path[STRING_SIZE]; | |
42849040 MT |
1126 | int counter = 0; |
1127 | int retry = 1; | |
e7740eaf SS |
1128 | int r; |
1129 | ||
42849040 MT |
1130 | do { |
1131 | // Reset the retry marker | |
1132 | retry = 0; | |
e7740eaf | 1133 | |
42849040 MT |
1134 | // Loop through the list of subvolumes |
1135 | for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) { | |
1136 | // Abort if the subvolume path could not be assigned. | |
1137 | r = snprintf(path, sizeof(path), "%s%s", DESTINATION_MOUNT_PATH, subvolume->mount_path); | |
1138 | if (r < 0) | |
1139 | return r; | |
e7740eaf | 1140 | |
42849040 MT |
1141 | // Try to umount the subvolume. |
1142 | r = umount2(path, 0); | |
e7740eaf | 1143 | |
42849040 MT |
1144 | // Handle return codes. |
1145 | if (r) { | |
1146 | switch(errno) { | |
1147 | case EBUSY: | |
1148 | // Set marker to retry the umount. | |
1149 | retry = 1; | |
e7740eaf | 1150 | |
42849040 MT |
1151 | // Ignore if the subvolume could not be unmounted yet, |
1152 | // because it is still used. | |
1153 | continue; | |
e7740eaf | 1154 | |
42849040 MT |
1155 | case EINVAL: |
1156 | // Ignore if the subvolume already has been unmounted | |
1157 | continue; | |
1158 | case ENOENT: | |
1159 | // Ignore if the directory does not longer exist. | |
1160 | continue; | |
1161 | default: | |
1162 | fprintf(flog, "Could not umount %s from %s - Error: %d\n", subvolume->name, path, r); | |
1163 | return r; | |
1164 | } | |
1165 | } | |
e7740eaf | 1166 | |
42849040 MT |
1167 | // Print log message |
1168 | fprintf(flog, "Umounted %s from %s\n", subvolume->name, path); | |
e7740eaf | 1169 | } |
42849040 MT |
1170 | |
1171 | // Abort loop if all mountpoins got umounted | |
1172 | if (retry == 0) | |
1173 | return 0; | |
1174 | ||
1175 | // Abort after five failed umount attempts | |
1176 | if (counter == 5) | |
1177 | return -1; | |
1178 | ||
1179 | // Increment counter. | |
1180 | counter++; | |
1181 | } while (1); | |
e7740eaf SS |
1182 | } |
1183 | ||
25fcce25 | 1184 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { |
ade96ba8 | 1185 | int r; |
ade96ba8 | 1186 | |
ddd32a5c MT |
1187 | // Write all buffers to disk before umounting |
1188 | hw_sync(); | |
1189 | ||
92e78233 MT |
1190 | // ESP |
1191 | if (*dest->part_boot_efi) { | |
ced15fdf | 1192 | r = hw_umount(HW_PATH_BOOT_EFI, prefix); |
92e78233 MT |
1193 | if (r) |
1194 | return -1; | |
1195 | } | |
1196 | ||
25fcce25 MT |
1197 | // boot |
1198 | if (*dest->part_boot) { | |
ced15fdf | 1199 | r = hw_umount(HW_PATH_BOOT, prefix); |
ade96ba8 MT |
1200 | if (r) |
1201 | return -1; | |
25fcce25 MT |
1202 | } |
1203 | ||
25fcce25 MT |
1204 | // swap |
1205 | if (*dest->part_swap) { | |
1206 | swapoff(dest->part_swap); | |
1207 | } | |
1208 | ||
1209 | // misc filesystems | |
ced15fdf MT |
1210 | r = hw_umount("/sys/firmware/efi/efivars", prefix); |
1211 | if (r) | |
1212 | return -1; | |
1213 | ||
1214 | r = hw_umount("/sys", prefix); | |
1215 | if (r) | |
1216 | return -1; | |
1217 | ||
1218 | r = hw_umount("/proc", prefix); | |
1219 | if (r) | |
1220 | return -1; | |
1221 | ||
1222 | r = hw_umount("/dev", prefix); | |
1223 | if (r) | |
1224 | return -1; | |
25fcce25 | 1225 | |
ade96ba8 | 1226 | // root |
2a981396 SS |
1227 | if(dest->filesystem == HW_FS_BTRFS) { |
1228 | r = hw_umount_btrfs_layout(); | |
1229 | } else { | |
1230 | r = hw_umount(prefix, NULL); | |
1231 | } | |
1232 | ||
ade96ba8 MT |
1233 | if (r) |
1234 | return -1; | |
1235 | ||
25fcce25 MT |
1236 | return 0; |
1237 | } | |
4a0d9bef | 1238 | |
d78fffa0 | 1239 | int hw_destroy_raid_superblocks(const struct hw_destination* dest, const char* output) { |
d2fafe03 MT |
1240 | char cmd[STRING_SIZE]; |
1241 | ||
46b56e20 MT |
1242 | hw_stop_all_raid_arrays(output); |
1243 | hw_stop_all_raid_arrays(output); | |
d2fafe03 MT |
1244 | |
1245 | if (dest->disk1) { | |
46b56e20 MT |
1246 | snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk1->path); |
1247 | mysystem(output, cmd); | |
d2fafe03 MT |
1248 | } |
1249 | ||
1250 | if (dest->disk2) { | |
46b56e20 MT |
1251 | snprintf(cmd, sizeof(cmd), "/sbin/mdadm --zero-superblock %s", dest->disk2->path); |
1252 | mysystem(output, cmd); | |
d2fafe03 MT |
1253 | } |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
46b56e20 | 1258 | int hw_setup_raid(struct hw_destination* dest, const char* output) { |
4a0d9bef | 1259 | char* cmd = NULL; |
7b4790d3 | 1260 | int r; |
4a0d9bef MT |
1261 | |
1262 | assert(dest->is_raid); | |
1263 | ||
d2fafe03 MT |
1264 | // Stop all RAID arrays that might be around (again). |
1265 | // It seems that there is some sort of race-condition with udev re-enabling | |
1266 | // the raid arrays and therefore locking the disks. | |
46b56e20 | 1267 | r = hw_destroy_raid_superblocks(dest, output); |
d2fafe03 | 1268 | |
7b4790d3 MT |
1269 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=%s --auto=mdp %s", |
1270 | RAID_METADATA, dest->path); | |
4a0d9bef MT |
1271 | |
1272 | switch (dest->raid_level) { | |
1273 | case 1: | |
1274 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
1275 | break; | |
1276 | ||
1277 | default: | |
1278 | assert(0); | |
1279 | } | |
1280 | ||
1281 | if (dest->disk1) { | |
1282 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
d2f993a7 MT |
1283 | |
1284 | // Clear all data at the beginning | |
1285 | r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10)); | |
1286 | if (r <= 0) | |
1287 | return r; | |
4a0d9bef MT |
1288 | } |
1289 | ||
1290 | if (dest->disk2) { | |
1291 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
d2f993a7 MT |
1292 | |
1293 | // Clear all data at the beginning | |
1294 | r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10)); | |
1295 | if (r <= 0) | |
1296 | return r; | |
4a0d9bef MT |
1297 | } |
1298 | ||
46b56e20 | 1299 | r = mysystem(output, cmd); |
4a0d9bef MT |
1300 | free(cmd); |
1301 | ||
1302 | // Wait a moment until the device has been properly brought up | |
1303 | if (r == 0) { | |
1304 | unsigned int counter = 10; | |
1305 | while (counter-- > 0) { | |
1306 | sleep(1); | |
1307 | ||
fde37387 MT |
1308 | // If the raid device has not yet been properly brought up, |
1309 | // opening it will fail with the message: Device or resource busy | |
1310 | // Hence we will wait a bit until it becomes usable. | |
0e491487 | 1311 | if (try_open(dest->path) == 0) |
4a0d9bef MT |
1312 | break; |
1313 | } | |
1314 | } | |
1315 | ||
1316 | return r; | |
1317 | } | |
1318 | ||
46b56e20 MT |
1319 | int hw_stop_all_raid_arrays(const char* output) { |
1320 | return mysystem(output, "/sbin/mdadm --stop --scan --verbose"); | |
4a0d9bef | 1321 | } |
f5007e9c | 1322 | |
92e78233 | 1323 | int hw_install_bootloader(struct hw* hw, struct hw_destination* dest, const char* output) { |
f5007e9c | 1324 | char cmd[STRING_SIZE]; |
f5007e9c | 1325 | |
12034118 MT |
1326 | snprintf(cmd, sizeof(cmd), "/usr/bin/install-bootloader %s", dest->path); |
1327 | int r = system_chroot(output, DESTINATION_MOUNT_PATH, cmd); | |
92e78233 MT |
1328 | if (r) |
1329 | return r; | |
1330 | ||
ade96ba8 MT |
1331 | hw_sync(); |
1332 | ||
92e78233 | 1333 | return 0; |
f5007e9c | 1334 | } |
7f69d8a4 MT |
1335 | |
1336 | static char* hw_get_uuid(const char* dev) { | |
1337 | blkid_probe p = blkid_new_probe_from_filename(dev); | |
1338 | const char* buffer = NULL; | |
1339 | char* uuid = NULL; | |
1340 | ||
1341 | if (!p) | |
1342 | return NULL; | |
1343 | ||
1344 | blkid_do_probe(p); | |
1345 | blkid_probe_lookup_value(p, "UUID", &buffer, NULL); | |
1346 | ||
1347 | if (buffer) | |
1348 | uuid = strdup(buffer); | |
1349 | ||
1350 | blkid_free_probe(p); | |
1351 | ||
1352 | return uuid; | |
1353 | } | |
1354 | ||
335c5bd1 MT |
1355 | #define FSTAB_FMT "UUID=%s %-8s %-4s %-10s %d %d\n" |
1356 | ||
7f69d8a4 | 1357 | int hw_write_fstab(struct hw_destination* dest) { |
15be1e1b | 1358 | const struct btrfs_subvolumes* subvolume = NULL; |
7f69d8a4 MT |
1359 | FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w"); |
1360 | if (!f) | |
1361 | return -1; | |
1362 | ||
7f69d8a4 | 1363 | char* uuid = NULL; |
15be1e1b | 1364 | char mount_options[STRING_SIZE]; |
7f69d8a4 MT |
1365 | |
1366 | // boot | |
1367 | if (*dest->part_boot) { | |
1368 | uuid = hw_get_uuid(dest->part_boot); | |
1369 | ||
1370 | if (uuid) { | |
e404dab5 | 1371 | fprintf(f, FSTAB_FMT, uuid, "/boot", "auto", "defaults,nodev,noexec,nosuid", 1, 2); |
7f69d8a4 MT |
1372 | free(uuid); |
1373 | } | |
1374 | } | |
1375 | ||
92e78233 MT |
1376 | // ESP |
1377 | if (*dest->part_boot_efi) { | |
1378 | uuid = hw_get_uuid(dest->part_boot_efi); | |
1379 | ||
1380 | if (uuid) { | |
1381 | fprintf(f, FSTAB_FMT, uuid, "/boot/efi", "auto", "defaults", 1, 2); | |
1382 | free(uuid); | |
1383 | } | |
1384 | } | |
1385 | ||
1386 | ||
7f69d8a4 MT |
1387 | // swap |
1388 | if (*dest->part_swap) { | |
1389 | uuid = hw_get_uuid(dest->part_swap); | |
1390 | ||
1391 | if (uuid) { | |
335c5bd1 | 1392 | fprintf(f, FSTAB_FMT, uuid, "swap", "swap", "defaults,pri=1", 0, 0); |
7f69d8a4 MT |
1393 | free(uuid); |
1394 | } | |
1395 | } | |
1396 | ||
1397 | // root | |
1398 | uuid = hw_get_uuid(dest->part_root); | |
1399 | if (uuid) { | |
15be1e1b SS |
1400 | if(dest->filesystem == HW_FS_BTRFS) { |
1401 | // Loop through the list of subvolumes | |
1402 | for (subvolume = btrfs_subvolumes; subvolume->name; subvolume++) { | |
1403 | // Abort if the mount options could not be assigned | |
1404 | int r = snprintf(mount_options, sizeof(mount_options), "defaults,%s,subvol=%s", BTRFS_MOUNT_OPTIONS, subvolume->name); | |
1405 | if (r < 0) { | |
1406 | return r; | |
1407 | } | |
1408 | ||
1409 | // Write the entry to the file | |
1410 | fprintf(f, FSTAB_FMT, uuid, subvolume->mount_path, "btrfs", mount_options, 1, 1); | |
1411 | } | |
1412 | } else { | |
1413 | fprintf(f, FSTAB_FMT, uuid, "/", "auto", "defaults", 1, 1); | |
1414 | } | |
1415 | ||
7f69d8a4 MT |
1416 | free(uuid); |
1417 | } | |
1418 | ||
7f69d8a4 MT |
1419 | fclose(f); |
1420 | ||
1421 | return 0; | |
1422 | } | |
282ec35e MT |
1423 | |
1424 | void hw_sync() { | |
1425 | sync(); | |
1426 | sync(); | |
1427 | sync(); | |
1428 | } | |
7d114284 MT |
1429 | |
1430 | int hw_start_networking(const char* output) { | |
1431 | return mysystem(output, "/usr/bin/start-networking.sh"); | |
1432 | } | |
38c6822d MT |
1433 | |
1434 | char* hw_find_backup_file(const char* output, const char* search_path) { | |
1435 | char path[STRING_SIZE]; | |
1436 | ||
1437 | snprintf(path, sizeof(path), "%s/backup.ipf", search_path); | |
1438 | int r = access(path, R_OK); | |
1439 | ||
1440 | if (r == 0) | |
1441 | return strdup(path); | |
1442 | ||
1443 | return NULL; | |
1444 | } | |
1445 | ||
1446 | int hw_restore_backup(const char* output, const char* backup_path, const char* destination) { | |
1447 | char command[STRING_SIZE]; | |
1448 | ||
3f8e70f6 MT |
1449 | snprintf(command, sizeof(command), "/bin/tar xzpf %s -C %s " |
1450 | "--exclude-from=%s/var/ipfire/backup/exclude --exclude-from=%s/var/ipfire/backup/exclude.user", | |
1451 | backup_path, destination, destination, destination); | |
38c6822d MT |
1452 | int rc = mysystem(output, command); |
1453 | ||
1454 | if (rc) | |
1455 | return -1; | |
1456 | ||
1457 | return 0; | |
1458 | } |