]>
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 MT |
26 | #include <blkid/blkid.h> |
27 | #include <fcntl.h> | |
f0fa1795 | 28 | #include <libudev.h> |
d7dd283b | 29 | #include <math.h> |
f0fa1795 MT |
30 | #include <stdio.h> |
31 | #include <stdlib.h> | |
32 | #include <string.h> | |
d7dd283b | 33 | #include <sys/ioctl.h> |
f0fa1795 | 34 | #include <sys/mount.h> |
25fcce25 | 35 | #include <sys/swap.h> |
f0fa1795 MT |
36 | #include <unistd.h> |
37 | ||
d7dd283b MT |
38 | #include <linux/fs.h> |
39 | ||
5315fae6 MT |
40 | #include <libsmooth.h> |
41 | ||
f0fa1795 | 42 | #include "hw.h" |
25fcce25 MT |
43 | |
44 | const char* other_filesystems[] = { | |
45 | "/dev", | |
46 | "/proc", | |
47 | "/sys", | |
48 | NULL | |
49 | }; | |
f0fa1795 | 50 | |
f5007e9c MT |
51 | static int system_chroot(const char* path, const char* cmd) { |
52 | char chroot_cmd[STRING_SIZE]; | |
53 | ||
54 | snprintf(chroot_cmd, sizeof(chroot_cmd), "/usr/sbin/chroot %s %s", path, cmd); | |
55 | ||
56 | return mysystem(chroot_cmd); | |
57 | } | |
58 | ||
f0fa1795 MT |
59 | struct hw* hw_init() { |
60 | struct hw* hw = malloc(sizeof(*hw)); | |
61 | assert(hw); | |
62 | ||
63 | // Initialize libudev | |
64 | hw->udev = udev_new(); | |
65 | if (!hw->udev) { | |
66 | fprintf(stderr, "Could not create udev instance\n"); | |
67 | exit(1); | |
68 | } | |
69 | ||
70 | return hw; | |
71 | } | |
72 | ||
73 | void hw_free(struct hw* hw) { | |
74 | if (hw->udev) | |
75 | udev_unref(hw->udev); | |
76 | ||
77 | free(hw); | |
78 | } | |
79 | ||
80 | static int strstartswith(const char* a, const char* b) { | |
81 | return (strncmp(a, b, strlen(b)) == 0); | |
82 | } | |
83 | ||
25fcce25 | 84 | int hw_mount(const char* source, const char* target, const char* fs, int flags) { |
85f2892b MT |
85 | // Create target if it does not exist |
86 | if (access(target, X_OK) != 0) | |
87 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
88 | ||
25fcce25 | 89 | return mount(source, target, fs, flags, NULL); |
f0fa1795 MT |
90 | } |
91 | ||
92 | int hw_umount(const char* target) { | |
a8d8b557 | 93 | return umount2(target, 0); |
f0fa1795 MT |
94 | } |
95 | ||
96 | static int hw_test_source_medium(const char* path) { | |
25fcce25 | 97 | int ret = hw_mount(path, SOURCE_MOUNT_PATH, "iso9660", MS_RDONLY); |
f0fa1795 MT |
98 | |
99 | // If the source could not be mounted we | |
100 | // cannot proceed. | |
5315fae6 | 101 | if (ret != 0) |
f0fa1795 MT |
102 | return ret; |
103 | ||
104 | // Check if the test file exists. | |
5315fae6 | 105 | ret = access(SOURCE_TEST_FILE, R_OK); |
f0fa1795 MT |
106 | |
107 | // Umount the test device. | |
108 | hw_umount(SOURCE_MOUNT_PATH); | |
109 | ||
5315fae6 | 110 | return (ret == 0); |
f0fa1795 MT |
111 | } |
112 | ||
113 | char* hw_find_source_medium(struct hw* hw) { | |
114 | char* ret = NULL; | |
115 | ||
116 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); | |
117 | ||
118 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
119 | udev_enumerate_scan_devices(enumerate); | |
120 | ||
121 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
122 | ||
123 | struct udev_list_entry* dev_list_entry; | |
124 | udev_list_entry_foreach(dev_list_entry, devices) { | |
125 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
126 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
127 | ||
128 | const char* dev_path = udev_device_get_devnode(dev); | |
129 | ||
130 | // Skip everything what we cannot work with | |
131 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef | 132 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/md")) |
f0fa1795 MT |
133 | continue; |
134 | ||
5315fae6 | 135 | if (hw_test_source_medium(dev_path) == 0) { |
f0fa1795 MT |
136 | ret = strdup(dev_path); |
137 | } | |
138 | ||
139 | udev_device_unref(dev); | |
140 | ||
141 | // If a suitable device was found the search will end. | |
142 | if (ret) | |
143 | break; | |
144 | } | |
145 | ||
146 | udev_enumerate_unref(enumerate); | |
147 | ||
148 | return ret; | |
149 | } | |
d7dd283b MT |
150 | |
151 | static struct hw_disk** hw_create_disks() { | |
152 | struct hw_disk** ret = malloc(sizeof(*ret) * (HW_MAX_DISKS + 1)); | |
153 | ||
154 | return ret; | |
155 | } | |
156 | ||
157 | static unsigned long long hw_block_device_get_size(const char* dev) { | |
158 | int fd = open(dev, O_RDONLY); | |
159 | if (fd < 0) | |
160 | return 0; | |
161 | ||
162 | unsigned long long size = blkid_get_dev_size(fd); | |
163 | close(fd); | |
164 | ||
165 | return size; | |
166 | } | |
167 | ||
168 | struct hw_disk** hw_find_disks(struct hw* hw) { | |
169 | struct hw_disk** ret = hw_create_disks(); | |
170 | struct hw_disk** disks = ret; | |
171 | ||
172 | struct udev_enumerate* enumerate = udev_enumerate_new(hw->udev); | |
173 | ||
174 | udev_enumerate_add_match_subsystem(enumerate, "block"); | |
175 | udev_enumerate_scan_devices(enumerate); | |
176 | ||
177 | struct udev_list_entry* devices = udev_enumerate_get_list_entry(enumerate); | |
178 | ||
179 | struct udev_list_entry* dev_list_entry; | |
180 | unsigned int i = HW_MAX_DISKS; | |
181 | udev_list_entry_foreach(dev_list_entry, devices) { | |
182 | const char* path = udev_list_entry_get_name(dev_list_entry); | |
183 | struct udev_device* dev = udev_device_new_from_syspath(hw->udev, path); | |
184 | ||
185 | const char* dev_path = udev_device_get_devnode(dev); | |
186 | ||
187 | // Skip everything what we cannot work with | |
188 | if (strstartswith(dev_path, "/dev/loop") || strstartswith(dev_path, "/dev/fd") || | |
4a0d9bef MT |
189 | strstartswith(dev_path, "/dev/ram") || strstartswith(dev_path, "/dev/sr") || |
190 | strstartswith(dev_path, "/dev/md")) { | |
d7dd283b MT |
191 | udev_device_unref(dev); |
192 | continue; | |
193 | } | |
194 | ||
195 | // DEVTYPE must be disk (otherwise we will see all sorts of partitions here) | |
196 | const char* devtype = udev_device_get_property_value(dev, "DEVTYPE"); | |
197 | if (devtype && (strcmp(devtype, "disk") != 0)) { | |
198 | udev_device_unref(dev); | |
199 | continue; | |
200 | } | |
201 | ||
202 | // Skip all source mediums | |
203 | if (hw_test_source_medium(dev_path) == 0) { | |
204 | udev_device_unref(dev); | |
205 | continue; | |
206 | } | |
207 | ||
208 | // Skip devices with a size of zero | |
209 | unsigned long long size = hw_block_device_get_size(dev_path); | |
210 | if (size == 0) { | |
211 | udev_device_unref(dev); | |
212 | continue; | |
213 | } | |
214 | ||
215 | struct hw_disk* disk = malloc(sizeof(*disk)); | |
216 | if (disk == NULL) | |
217 | return NULL; | |
218 | ||
219 | disk->ref = 1; | |
220 | ||
221 | strncpy(disk->path, dev_path, sizeof(disk->path)); | |
222 | ||
223 | disk->size = size; | |
224 | ||
225 | // Vendor | |
226 | const char* vendor = udev_device_get_property_value(dev, "ID_VENDOR"); | |
227 | if (!vendor) | |
228 | vendor = udev_device_get_sysattr_value(dev, "vendor"); | |
229 | if (!vendor) | |
230 | vendor = udev_device_get_sysattr_value(dev, "manufacturer"); | |
d7dd283b | 231 | |
4c812463 MT |
232 | if (vendor) |
233 | strncpy(disk->vendor, vendor, sizeof(disk->vendor)); | |
234 | else | |
235 | *disk->vendor = '\0'; | |
d7dd283b MT |
236 | |
237 | // Model | |
238 | const char* model = udev_device_get_property_value(dev, "ID_MODEL"); | |
239 | if (!model) | |
240 | model = udev_device_get_sysattr_value(dev, "model"); | |
241 | if (!model) | |
242 | model = udev_device_get_sysattr_value(dev, "product"); | |
d7dd283b | 243 | |
4c812463 MT |
244 | if (model) |
245 | strncpy(disk->model, model, sizeof(disk->model)); | |
246 | else | |
247 | *disk->model = '\0'; | |
248 | ||
249 | // Format description | |
250 | char size_str[STRING_SIZE]; | |
251 | snprintf(size_str, sizeof(size_str), "%4.1fGB", (double)disk->size / pow(1024, 3)); | |
d7dd283b | 252 | |
4c812463 MT |
253 | if (*disk->vendor && *disk->model) { |
254 | snprintf(disk->description, sizeof(disk->description), | |
255 | "%s | %s - %s", size_str, disk->vendor, disk->model); | |
256 | ||
257 | } else if (*disk->vendor || *disk->model) { | |
258 | snprintf(disk->description, sizeof(disk->description), | |
259 | "%s | %s", size_str, (*disk->vendor) ? disk->vendor : disk->model); | |
260 | ||
261 | } else { | |
262 | snprintf(disk->description, sizeof(disk->description), | |
263 | "%s | N/A", size_str); | |
264 | } | |
d7dd283b MT |
265 | |
266 | *disks++ = disk; | |
267 | ||
268 | if (--i == 0) | |
269 | break; | |
270 | ||
271 | udev_device_unref(dev); | |
272 | } | |
273 | ||
274 | udev_enumerate_unref(enumerate); | |
275 | ||
276 | *disks = NULL; | |
277 | ||
278 | return ret; | |
279 | } | |
280 | ||
281 | void hw_free_disks(struct hw_disk** disks) { | |
282 | struct hw_disk** disk = disks; | |
283 | ||
284 | while (*disk != NULL) { | |
285 | if (--(*disk)->ref == 0) | |
286 | free(*disk); | |
287 | ||
288 | disk++; | |
289 | } | |
290 | ||
291 | free(disks); | |
292 | } | |
293 | ||
294 | unsigned int hw_count_disks(struct hw_disk** disks) { | |
295 | unsigned int ret = 0; | |
296 | ||
297 | while (*disks++) | |
298 | ret++; | |
299 | ||
300 | return ret; | |
301 | } | |
302 | ||
303 | struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) { | |
304 | struct hw_disk** ret = hw_create_disks(); | |
305 | struct hw_disk** selected_disks = ret; | |
306 | ||
307 | unsigned int num_disks = hw_count_disks(disks); | |
308 | ||
309 | for (unsigned int i = 0; i < num_disks; i++) { | |
310 | if (selection && selection[i]) { | |
311 | struct hw_disk *selected_disk = disks[i]; | |
312 | selected_disk->ref++; | |
313 | ||
314 | *selected_disks++ = selected_disk; | |
315 | } | |
316 | } | |
317 | ||
318 | // Set sentinel | |
319 | *selected_disks = NULL; | |
320 | ||
321 | return ret; | |
322 | } | |
323 | ||
25fcce25 MT |
324 | static unsigned long long hw_swap_size(struct hw_destination* dest) { |
325 | unsigned long long memory = hw_memory(); | |
326 | ||
327 | unsigned long long swap_size = memory / 4; | |
328 | ||
329 | // Min. swap size is 128MB | |
330 | if (swap_size < MB2BYTES(128)) | |
331 | swap_size = MB2BYTES(128); | |
332 | ||
333 | // Cap swap size to 1GB | |
334 | else if (swap_size > MB2BYTES(1024)) | |
335 | swap_size = MB2BYTES(1024); | |
336 | ||
337 | return swap_size; | |
338 | } | |
339 | ||
340 | static unsigned long long hw_root_size(struct hw_destination* dest) { | |
341 | unsigned long long root_size; | |
342 | ||
343 | if (dest->size < MB2BYTES(2048)) | |
344 | root_size = MB2BYTES(1024); | |
345 | ||
346 | else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072)) | |
347 | root_size = MB2BYTES(1536); | |
348 | ||
349 | else | |
350 | root_size = MB2BYTES(2048); | |
351 | ||
352 | return root_size; | |
353 | } | |
354 | ||
355 | static unsigned long long hw_boot_size(struct hw_destination* dest) { | |
356 | return MB2BYTES(64); | |
357 | } | |
358 | ||
359 | static int hw_calculate_partition_table(struct hw_destination* dest) { | |
48d6a112 MT |
360 | char path[DEV_SIZE]; |
361 | int part_idx = 1; | |
362 | ||
363 | snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : ""); | |
364 | dest->part_boot_idx = 0; | |
365 | ||
25fcce25 MT |
366 | // Determine the size of the target block device |
367 | if (dest->is_raid) { | |
368 | dest->size = (dest->disk1->size >= dest->disk2->size) ? | |
5315fae6 | 369 | dest->disk2->size : dest->disk1->size; |
22581f51 MT |
370 | |
371 | // The RAID will install some metadata at the end of the disk | |
372 | // and we will save up some space for that. | |
373 | dest->size -= MB2BYTES(2); | |
25fcce25 MT |
374 | } else { |
375 | dest->size = dest->disk1->size; | |
376 | } | |
377 | ||
802a123b MT |
378 | // As we add some extra space before the beginning of the first |
379 | // partition, we need to substract that here. | |
380 | dest->size -= MB2BYTES(1); | |
381 | ||
382 | // Add some more space for partition tables, etc. | |
383 | dest->size -= MB2BYTES(1); | |
384 | ||
48d6a112 MT |
385 | // Determine partition table |
386 | dest->part_table = HW_PART_TABLE_MSDOS; | |
387 | ||
388 | // Disks over 2TB need to use GPT | |
389 | if (dest->size >= MB2BYTES(2047 * 1024)) | |
390 | dest->part_table = HW_PART_TABLE_GPT; | |
391 | ||
392 | // We also use GPT on raid disks by default | |
393 | else if (dest->is_raid) | |
394 | dest->part_table = HW_PART_TABLE_GPT; | |
395 | ||
396 | // When using GPT, GRUB2 needs a little bit of space to put | |
397 | // itself in. | |
5689fc72 | 398 | if (dest->part_table == HW_PART_TABLE_GPT) { |
48d6a112 MT |
399 | snprintf(dest->part_bootldr, sizeof(dest->part_bootldr), |
400 | "%s%d", path, part_idx); | |
401 | ||
402 | dest->size_bootldr = MB2BYTES(4); | |
403 | ||
404 | dest->part_boot_idx = part_idx++; | |
405 | } else { | |
406 | *dest->part_bootldr = '\0'; | |
407 | dest->size_bootldr = 0; | |
408 | } | |
409 | ||
25fcce25 MT |
410 | dest->size_boot = hw_boot_size(dest); |
411 | dest->size_swap = hw_swap_size(dest); | |
412 | dest->size_root = hw_root_size(dest); | |
413 | ||
414 | // Determine the size of the data partition. | |
48d6a112 MT |
415 | unsigned long long used_space = dest->size_bootldr + dest->size_boot |
416 | + dest->size_swap + dest->size_root; | |
25fcce25 MT |
417 | |
418 | // Disk is way too small | |
419 | if (used_space >= dest->size) | |
420 | return -1; | |
421 | ||
422 | dest->size_data = dest->size - used_space; | |
423 | ||
424 | // If it gets too small, we remove the swap space. | |
425 | if (dest->size_data <= MB2BYTES(256)) { | |
426 | dest->size_data += dest->size_swap; | |
427 | dest->size_swap = 0; | |
428 | } | |
429 | ||
430 | // Set partition names | |
25fcce25 | 431 | if (dest->size_boot > 0) { |
48d6a112 MT |
432 | if (dest->part_boot_idx == 0) |
433 | dest->part_boot_idx = part_idx; | |
25fcce25 MT |
434 | |
435 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
436 | } else | |
437 | *dest->part_boot = '\0'; | |
438 | ||
439 | if (dest->size_swap > 0) | |
440 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
441 | else | |
442 | *dest->part_swap = '\0'; | |
443 | ||
444 | // There is always a root partition | |
48d6a112 | 445 | if (dest->part_boot_idx == 0) |
25fcce25 MT |
446 | dest->part_boot_idx = part_idx; |
447 | ||
448 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
449 | ||
450 | if (dest->size_data > 0) | |
451 | snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++); | |
452 | else | |
453 | *dest->part_data = '\0'; | |
454 | ||
25fcce25 MT |
455 | return 0; |
456 | } | |
457 | ||
d7dd283b MT |
458 | struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) { |
459 | struct hw_destination* dest = malloc(sizeof(*dest)); | |
460 | ||
461 | if (part_type == HW_PART_TYPE_NORMAL) { | |
462 | dest->disk1 = *disks; | |
463 | dest->disk2 = NULL; | |
464 | ||
465 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
466 | ||
467 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
468 | dest->disk1 = *disks++; | |
469 | dest->disk2 = *disks; | |
4a0d9bef | 470 | dest->raid_level = 1; |
d7dd283b MT |
471 | |
472 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
473 | } | |
474 | ||
475 | // Is this a RAID device? | |
476 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
477 | ||
25fcce25 MT |
478 | int r = hw_calculate_partition_table(dest); |
479 | if (r) | |
480 | return NULL; | |
d7dd283b | 481 | |
25fcce25 MT |
482 | // Set default filesystem |
483 | dest->filesystem = HW_FS_DEFAULT; | |
d7dd283b MT |
484 | |
485 | return dest; | |
486 | } | |
c4e96674 MT |
487 | |
488 | unsigned long long hw_memory() { | |
489 | FILE* handle = NULL; | |
490 | char line[STRING_SIZE]; | |
491 | ||
492 | unsigned long long memory = 0; | |
493 | ||
494 | /* Calculate amount of memory in machine */ | |
495 | if ((handle = fopen("/proc/meminfo", "r"))) { | |
496 | while (fgets(line, sizeof(line), handle)) { | |
25fcce25 | 497 | if (!sscanf (line, "MemTotal: %llu kB", &memory)) { |
c4e96674 MT |
498 | memory = 0; |
499 | } | |
500 | } | |
501 | ||
502 | fclose(handle); | |
503 | } | |
504 | ||
505 | return memory * 1024; | |
506 | } | |
25fcce25 MT |
507 | |
508 | int hw_create_partitions(struct hw_destination* dest) { | |
509 | char* cmd = NULL; | |
510 | ||
511 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); | |
512 | ||
513 | // Set partition type | |
514 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
515 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
516 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
517 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
518 | ||
802a123b | 519 | unsigned long long part_start = MB2BYTES(1); |
25fcce25 | 520 | |
48d6a112 | 521 | if (*dest->part_bootldr) { |
802a123b | 522 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
48d6a112 | 523 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary", |
802a123b | 524 | part_start, part_start + dest->size_bootldr - 1); |
48d6a112 MT |
525 | |
526 | part_start += dest->size_bootldr; | |
527 | } | |
528 | ||
25fcce25 | 529 | if (*dest->part_boot) { |
802a123b | 530 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 531 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", |
802a123b | 532 | part_start, part_start + dest->size_boot - 1); |
25fcce25 MT |
533 | |
534 | part_start += dest->size_boot; | |
535 | } | |
536 | ||
537 | if (*dest->part_swap) { | |
802a123b | 538 | asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd, |
25fcce25 | 539 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", |
802a123b | 540 | part_start, part_start + dest->size_swap - 1); |
25fcce25 MT |
541 | |
542 | part_start += dest->size_swap; | |
543 | } | |
544 | ||
545 | if (*dest->part_root) { | |
802a123b | 546 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 547 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", |
802a123b | 548 | part_start, part_start + dest->size_root - 1); |
25fcce25 MT |
549 | |
550 | part_start += dest->size_root; | |
551 | } | |
552 | ||
553 | if (*dest->part_data) { | |
802a123b | 554 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 555 | (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary", |
802a123b | 556 | part_start, part_start + dest->size_data - 1); |
25fcce25 MT |
557 | |
558 | part_start += dest->size_data; | |
559 | } | |
560 | ||
561 | if (dest->part_table == HW_PART_TABLE_MSDOS && dest->part_boot_idx > 0) { | |
562 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); | |
563 | ||
564 | } else if (dest->part_table == HW_PART_TABLE_GPT) { | |
48d6a112 MT |
565 | if (*dest->part_bootldr) { |
566 | asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx); | |
567 | } | |
25fcce25 MT |
568 | asprintf(&cmd, "%s disk_set pmbr_boot on", cmd); |
569 | } | |
570 | ||
571 | int r = mysystem(cmd); | |
572 | ||
268090a8 MT |
573 | // Wait until the system re-read the partition table |
574 | if (r == 0) { | |
575 | unsigned int counter = 10; | |
576 | ||
577 | while (counter-- > 0) { | |
578 | sleep(1); | |
579 | ||
48d6a112 MT |
580 | if (*dest->part_bootldr && (access(dest->part_bootldr, R_OK) != 0)) |
581 | continue; | |
582 | ||
268090a8 MT |
583 | if (*dest->part_boot && (access(dest->part_boot, R_OK) != 0)) |
584 | continue; | |
585 | ||
586 | if (*dest->part_swap && (access(dest->part_swap, R_OK) != 0)) | |
587 | continue; | |
588 | ||
589 | if (*dest->part_root && (access(dest->part_root, R_OK) != 0)) | |
590 | continue; | |
591 | ||
592 | if (*dest->part_data && (access(dest->part_data, R_OK) != 0)) | |
593 | continue; | |
594 | ||
595 | // All partitions do exist, exiting the loop. | |
596 | break; | |
597 | } | |
598 | } | |
599 | ||
25fcce25 MT |
600 | if (cmd) |
601 | free(cmd); | |
602 | ||
603 | return r; | |
604 | } | |
605 | ||
606 | static int hw_format_filesystem(const char* path, int fs) { | |
607 | char cmd[STRING_SIZE] = "\0"; | |
608 | ||
609 | // Swap | |
610 | if (fs == HW_FS_SWAP) { | |
611 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
612 | // ReiserFS | |
613 | } else if (fs == HW_FS_REISERFS) { | |
614 | snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path); | |
615 | ||
616 | // EXT4 | |
617 | } else if (fs == HW_FS_EXT4) { | |
618 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path); | |
619 | ||
620 | // EXT4 w/o journal | |
621 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
622 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path); | |
70a44b52 MT |
623 | |
624 | // XFS | |
625 | } else if (fs == HW_FS_XFS) { | |
626 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path); | |
25fcce25 MT |
627 | } |
628 | ||
629 | assert(*cmd); | |
630 | ||
631 | int r = mysystem(cmd); | |
632 | ||
633 | return r; | |
634 | } | |
635 | ||
636 | int hw_create_filesystems(struct hw_destination* dest) { | |
637 | int r; | |
638 | ||
639 | // boot | |
640 | if (*dest->part_boot) { | |
641 | r = hw_format_filesystem(dest->part_boot, dest->filesystem); | |
642 | if (r) | |
643 | return r; | |
644 | } | |
645 | ||
646 | // swap | |
647 | if (*dest->part_swap) { | |
648 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP); | |
649 | if (r) | |
650 | return r; | |
651 | } | |
652 | ||
653 | // root | |
654 | r = hw_format_filesystem(dest->part_root, dest->filesystem); | |
655 | if (r) | |
656 | return r; | |
657 | ||
658 | // data | |
659 | if (*dest->part_data) { | |
660 | r = hw_format_filesystem(dest->part_data, dest->filesystem); | |
661 | if (r) | |
662 | return r; | |
663 | } | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
669 | char target[STRING_SIZE]; | |
670 | ||
671 | assert(*prefix == '/'); | |
672 | ||
673 | const char* filesystem; | |
674 | switch (dest->filesystem) { | |
675 | case HW_FS_REISERFS: | |
676 | filesystem = "reiserfs"; | |
677 | break; | |
678 | ||
679 | case HW_FS_EXT4: | |
680 | case HW_FS_EXT4_WO_JOURNAL: | |
681 | filesystem = "ext4"; | |
682 | break; | |
683 | ||
70a44b52 MT |
684 | case HW_FS_XFS: |
685 | filesystem = "xfs"; | |
686 | break; | |
687 | ||
25fcce25 MT |
688 | default: |
689 | assert(0); | |
690 | } | |
691 | ||
692 | // root | |
693 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
694 | if (r) | |
695 | return r; | |
696 | ||
697 | // boot | |
698 | if (*dest->part_boot) { | |
699 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
700 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
701 | ||
702 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
703 | if (r) { | |
704 | hw_umount_filesystems(dest, prefix); | |
705 | ||
706 | return r; | |
707 | } | |
708 | } | |
709 | ||
710 | // data | |
711 | if (*dest->part_data) { | |
712 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA); | |
713 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
714 | ||
715 | r = hw_mount(dest->part_data, target, filesystem, 0); | |
716 | if (r) { | |
717 | hw_umount_filesystems(dest, prefix); | |
718 | ||
719 | return r; | |
720 | } | |
721 | } | |
722 | ||
723 | // swap | |
724 | if (*dest->part_swap) { | |
725 | r = swapon(dest->part_swap, 0); | |
726 | if (r) { | |
727 | hw_umount_filesystems(dest, prefix); | |
728 | ||
729 | return r; | |
730 | } | |
731 | } | |
732 | ||
733 | // bind-mount misc filesystems | |
734 | char** otherfs = other_filesystems; | |
735 | while (*otherfs) { | |
736 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs); | |
737 | ||
738 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
739 | r = hw_mount(*otherfs, target, NULL, MS_BIND); | |
740 | if (r) { | |
741 | hw_umount_filesystems(dest, prefix); | |
742 | ||
743 | return r; | |
744 | } | |
745 | ||
746 | otherfs++; | |
747 | } | |
748 | ||
749 | return 0; | |
750 | } | |
751 | ||
752 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
753 | // boot | |
754 | if (*dest->part_boot) { | |
755 | hw_umount(dest->part_boot); | |
756 | } | |
757 | ||
758 | // data | |
759 | if (*dest->part_data) { | |
760 | hw_umount(dest->part_data); | |
761 | } | |
762 | ||
763 | // root | |
764 | hw_umount(dest->part_root); | |
765 | ||
766 | // swap | |
767 | if (*dest->part_swap) { | |
768 | swapoff(dest->part_swap); | |
769 | } | |
770 | ||
771 | // misc filesystems | |
772 | char target[STRING_SIZE]; | |
773 | char** otherfs = other_filesystems; | |
774 | ||
775 | while (*otherfs) { | |
776 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++); | |
777 | hw_umount(target); | |
778 | } | |
779 | ||
780 | return 0; | |
781 | } | |
4a0d9bef MT |
782 | |
783 | int hw_setup_raid(struct hw_destination* dest) { | |
784 | char* cmd = NULL; | |
785 | ||
786 | assert(dest->is_raid); | |
787 | ||
f5007e9c | 788 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest->path); |
4a0d9bef MT |
789 | |
790 | switch (dest->raid_level) { | |
791 | case 1: | |
792 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
793 | break; | |
794 | ||
795 | default: | |
796 | assert(0); | |
797 | } | |
798 | ||
799 | if (dest->disk1) { | |
800 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
801 | } | |
802 | ||
803 | if (dest->disk2) { | |
804 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
805 | } | |
806 | ||
807 | int r = mysystem(cmd); | |
808 | free(cmd); | |
809 | ||
810 | // Wait a moment until the device has been properly brought up | |
811 | if (r == 0) { | |
812 | unsigned int counter = 10; | |
813 | while (counter-- > 0) { | |
814 | sleep(1); | |
815 | ||
fde37387 MT |
816 | // If the raid device has not yet been properly brought up, |
817 | // opening it will fail with the message: Device or resource busy | |
818 | // Hence we will wait a bit until it becomes usable. | |
819 | FILE* f = fopen(dest->path, "r"); | |
820 | if (f) { | |
821 | fclose(f); | |
4a0d9bef | 822 | break; |
fde37387 | 823 | } |
4a0d9bef MT |
824 | } |
825 | } | |
826 | ||
827 | return r; | |
828 | } | |
829 | ||
830 | int hw_stop_all_raid_arrays() { | |
831 | return mysystem("/sbin/mdadm --stop --scan"); | |
832 | } | |
f5007e9c MT |
833 | |
834 | int hw_install_bootloader(struct hw_destination* dest) { | |
835 | char cmd[STRING_SIZE]; | |
836 | int r; | |
837 | ||
838 | // Generate configuration file | |
839 | snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg"); | |
840 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
841 | if (r) | |
842 | return r; | |
843 | ||
844 | char cmd_grub[STRING_SIZE]; | |
845 | snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck"); | |
846 | ||
48d6a112 | 847 | if (dest->is_raid && (dest->part_table == HW_PART_TABLE_MSDOS)) { |
f5007e9c MT |
848 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path); |
849 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
850 | if (r) | |
851 | return r; | |
852 | ||
853 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path); | |
854 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
855 | } else { | |
856 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path); | |
857 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
858 | } | |
859 | ||
860 | return r; | |
861 | } | |
7f69d8a4 MT |
862 | |
863 | static char* hw_get_uuid(const char* dev) { | |
864 | blkid_probe p = blkid_new_probe_from_filename(dev); | |
865 | const char* buffer = NULL; | |
866 | char* uuid = NULL; | |
867 | ||
868 | if (!p) | |
869 | return NULL; | |
870 | ||
871 | blkid_do_probe(p); | |
872 | blkid_probe_lookup_value(p, "UUID", &buffer, NULL); | |
873 | ||
874 | if (buffer) | |
875 | uuid = strdup(buffer); | |
876 | ||
877 | blkid_free_probe(p); | |
878 | ||
879 | return uuid; | |
880 | } | |
881 | ||
882 | int hw_write_fstab(struct hw_destination* dest) { | |
883 | FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w"); | |
884 | if (!f) | |
885 | return -1; | |
886 | ||
887 | const char* fmt = "UUID=%s %-8s %-4s %-10s %d %d\n"; | |
888 | char* uuid = NULL; | |
889 | ||
890 | // boot | |
891 | if (*dest->part_boot) { | |
892 | uuid = hw_get_uuid(dest->part_boot); | |
893 | ||
894 | if (uuid) { | |
895 | fprintf(f, fmt, uuid, "/boot", "auto", "defaults", 1, 2); | |
896 | free(uuid); | |
897 | } | |
898 | } | |
899 | ||
900 | // swap | |
901 | if (*dest->part_swap) { | |
902 | uuid = hw_get_uuid(dest->part_swap); | |
903 | ||
904 | if (uuid) { | |
905 | fprintf(f, fmt, uuid, "swap", "swap", "defaults,pri=1", 0, 0); | |
906 | free(uuid); | |
907 | } | |
908 | } | |
909 | ||
910 | // root | |
911 | uuid = hw_get_uuid(dest->part_root); | |
912 | if (uuid) { | |
913 | fprintf(f, fmt, uuid, "/", "auto", "defaults", 1, 1); | |
914 | free(uuid); | |
915 | } | |
916 | ||
917 | // data | |
918 | if (*dest->part_data) { | |
919 | uuid = hw_get_uuid(dest->part_data); | |
920 | ||
921 | if (uuid) { | |
922 | fprintf(f, fmt, uuid, "/var", "auto", "defaults", 1, 1); | |
923 | free(uuid); | |
924 | } | |
925 | } | |
926 | ||
927 | fclose(f); | |
928 | ||
929 | return 0; | |
930 | } |