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