]>
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) { | |
93 | return umount2(target, MNT_DETACH); | |
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"); | |
231 | if (!vendor) | |
232 | vendor = "N/A"; | |
233 | ||
234 | strncpy(disk->vendor, vendor, sizeof(disk->vendor)); | |
235 | ||
236 | // Model | |
237 | const char* model = udev_device_get_property_value(dev, "ID_MODEL"); | |
238 | if (!model) | |
239 | model = udev_device_get_sysattr_value(dev, "model"); | |
240 | if (!model) | |
241 | model = udev_device_get_sysattr_value(dev, "product"); | |
242 | if (!model) | |
243 | model = "N/A"; | |
244 | ||
245 | strncpy(disk->model, model, sizeof(disk->model)); | |
246 | ||
247 | snprintf(disk->description, sizeof(disk->description), | |
248 | "%4.1fGB %s - %s", (double)disk->size / pow(1024, 3), | |
249 | disk->vendor, disk->model); | |
250 | ||
251 | *disks++ = disk; | |
252 | ||
253 | if (--i == 0) | |
254 | break; | |
255 | ||
256 | udev_device_unref(dev); | |
257 | } | |
258 | ||
259 | udev_enumerate_unref(enumerate); | |
260 | ||
261 | *disks = NULL; | |
262 | ||
263 | return ret; | |
264 | } | |
265 | ||
266 | void hw_free_disks(struct hw_disk** disks) { | |
267 | struct hw_disk** disk = disks; | |
268 | ||
269 | while (*disk != NULL) { | |
270 | if (--(*disk)->ref == 0) | |
271 | free(*disk); | |
272 | ||
273 | disk++; | |
274 | } | |
275 | ||
276 | free(disks); | |
277 | } | |
278 | ||
279 | unsigned int hw_count_disks(struct hw_disk** disks) { | |
280 | unsigned int ret = 0; | |
281 | ||
282 | while (*disks++) | |
283 | ret++; | |
284 | ||
285 | return ret; | |
286 | } | |
287 | ||
288 | struct hw_disk** hw_select_disks(struct hw_disk** disks, int* selection) { | |
289 | struct hw_disk** ret = hw_create_disks(); | |
290 | struct hw_disk** selected_disks = ret; | |
291 | ||
292 | unsigned int num_disks = hw_count_disks(disks); | |
293 | ||
294 | for (unsigned int i = 0; i < num_disks; i++) { | |
295 | if (selection && selection[i]) { | |
296 | struct hw_disk *selected_disk = disks[i]; | |
297 | selected_disk->ref++; | |
298 | ||
299 | *selected_disks++ = selected_disk; | |
300 | } | |
301 | } | |
302 | ||
303 | // Set sentinel | |
304 | *selected_disks = NULL; | |
305 | ||
306 | return ret; | |
307 | } | |
308 | ||
25fcce25 MT |
309 | static unsigned long long hw_swap_size(struct hw_destination* dest) { |
310 | unsigned long long memory = hw_memory(); | |
311 | ||
312 | unsigned long long swap_size = memory / 4; | |
313 | ||
314 | // Min. swap size is 128MB | |
315 | if (swap_size < MB2BYTES(128)) | |
316 | swap_size = MB2BYTES(128); | |
317 | ||
318 | // Cap swap size to 1GB | |
319 | else if (swap_size > MB2BYTES(1024)) | |
320 | swap_size = MB2BYTES(1024); | |
321 | ||
322 | return swap_size; | |
323 | } | |
324 | ||
325 | static unsigned long long hw_root_size(struct hw_destination* dest) { | |
326 | unsigned long long root_size; | |
327 | ||
328 | if (dest->size < MB2BYTES(2048)) | |
329 | root_size = MB2BYTES(1024); | |
330 | ||
331 | else if (dest->size >= MB2BYTES(2048) && dest->size <= MB2BYTES(3072)) | |
332 | root_size = MB2BYTES(1536); | |
333 | ||
334 | else | |
335 | root_size = MB2BYTES(2048); | |
336 | ||
337 | return root_size; | |
338 | } | |
339 | ||
340 | static unsigned long long hw_boot_size(struct hw_destination* dest) { | |
341 | return MB2BYTES(64); | |
342 | } | |
343 | ||
344 | static int hw_calculate_partition_table(struct hw_destination* dest) { | |
48d6a112 MT |
345 | char path[DEV_SIZE]; |
346 | int part_idx = 1; | |
347 | ||
348 | snprintf(path, sizeof(path), "%s%s", dest->path, (dest->is_raid) ? "p" : ""); | |
349 | dest->part_boot_idx = 0; | |
350 | ||
25fcce25 MT |
351 | // Determine the size of the target block device |
352 | if (dest->is_raid) { | |
353 | dest->size = (dest->disk1->size >= dest->disk2->size) ? | |
5315fae6 | 354 | dest->disk2->size : dest->disk1->size; |
22581f51 MT |
355 | |
356 | // The RAID will install some metadata at the end of the disk | |
357 | // and we will save up some space for that. | |
358 | dest->size -= MB2BYTES(2); | |
25fcce25 MT |
359 | } else { |
360 | dest->size = dest->disk1->size; | |
361 | } | |
362 | ||
802a123b MT |
363 | // As we add some extra space before the beginning of the first |
364 | // partition, we need to substract that here. | |
365 | dest->size -= MB2BYTES(1); | |
366 | ||
367 | // Add some more space for partition tables, etc. | |
368 | dest->size -= MB2BYTES(1); | |
369 | ||
48d6a112 MT |
370 | // Determine partition table |
371 | dest->part_table = HW_PART_TABLE_MSDOS; | |
372 | ||
373 | // Disks over 2TB need to use GPT | |
374 | if (dest->size >= MB2BYTES(2047 * 1024)) | |
375 | dest->part_table = HW_PART_TABLE_GPT; | |
376 | ||
377 | // We also use GPT on raid disks by default | |
378 | else if (dest->is_raid) | |
379 | dest->part_table = HW_PART_TABLE_GPT; | |
380 | ||
381 | // When using GPT, GRUB2 needs a little bit of space to put | |
382 | // itself in. | |
5689fc72 | 383 | if (dest->part_table == HW_PART_TABLE_GPT) { |
48d6a112 MT |
384 | snprintf(dest->part_bootldr, sizeof(dest->part_bootldr), |
385 | "%s%d", path, part_idx); | |
386 | ||
387 | dest->size_bootldr = MB2BYTES(4); | |
388 | ||
389 | dest->part_boot_idx = part_idx++; | |
390 | } else { | |
391 | *dest->part_bootldr = '\0'; | |
392 | dest->size_bootldr = 0; | |
393 | } | |
394 | ||
25fcce25 MT |
395 | dest->size_boot = hw_boot_size(dest); |
396 | dest->size_swap = hw_swap_size(dest); | |
397 | dest->size_root = hw_root_size(dest); | |
398 | ||
399 | // Determine the size of the data partition. | |
48d6a112 MT |
400 | unsigned long long used_space = dest->size_bootldr + dest->size_boot |
401 | + dest->size_swap + dest->size_root; | |
25fcce25 MT |
402 | |
403 | // Disk is way too small | |
404 | if (used_space >= dest->size) | |
405 | return -1; | |
406 | ||
407 | dest->size_data = dest->size - used_space; | |
408 | ||
409 | // If it gets too small, we remove the swap space. | |
410 | if (dest->size_data <= MB2BYTES(256)) { | |
411 | dest->size_data += dest->size_swap; | |
412 | dest->size_swap = 0; | |
413 | } | |
414 | ||
415 | // Set partition names | |
25fcce25 | 416 | if (dest->size_boot > 0) { |
48d6a112 MT |
417 | if (dest->part_boot_idx == 0) |
418 | dest->part_boot_idx = part_idx; | |
25fcce25 MT |
419 | |
420 | snprintf(dest->part_boot, sizeof(dest->part_boot), "%s%d", path, part_idx++); | |
421 | } else | |
422 | *dest->part_boot = '\0'; | |
423 | ||
424 | if (dest->size_swap > 0) | |
425 | snprintf(dest->part_swap, sizeof(dest->part_swap), "%s%d", path, part_idx++); | |
426 | else | |
427 | *dest->part_swap = '\0'; | |
428 | ||
429 | // There is always a root partition | |
48d6a112 | 430 | if (dest->part_boot_idx == 0) |
25fcce25 MT |
431 | dest->part_boot_idx = part_idx; |
432 | ||
433 | snprintf(dest->part_root, sizeof(dest->part_root), "%s%d", path, part_idx++); | |
434 | ||
435 | if (dest->size_data > 0) | |
436 | snprintf(dest->part_data, sizeof(dest->part_data), "%s%d", path, part_idx++); | |
437 | else | |
438 | *dest->part_data = '\0'; | |
439 | ||
25fcce25 MT |
440 | return 0; |
441 | } | |
442 | ||
d7dd283b MT |
443 | struct hw_destination* hw_make_destination(int part_type, struct hw_disk** disks) { |
444 | struct hw_destination* dest = malloc(sizeof(*dest)); | |
445 | ||
446 | if (part_type == HW_PART_TYPE_NORMAL) { | |
447 | dest->disk1 = *disks; | |
448 | dest->disk2 = NULL; | |
449 | ||
450 | strncpy(dest->path, dest->disk1->path, sizeof(dest->path)); | |
451 | ||
452 | } else if (part_type == HW_PART_TYPE_RAID1) { | |
453 | dest->disk1 = *disks++; | |
454 | dest->disk2 = *disks; | |
4a0d9bef | 455 | dest->raid_level = 1; |
d7dd283b MT |
456 | |
457 | snprintf(dest->path, sizeof(dest->path), "/dev/md0"); | |
458 | } | |
459 | ||
460 | // Is this a RAID device? | |
461 | dest->is_raid = (part_type > HW_PART_TYPE_NORMAL); | |
462 | ||
25fcce25 MT |
463 | int r = hw_calculate_partition_table(dest); |
464 | if (r) | |
465 | return NULL; | |
d7dd283b | 466 | |
25fcce25 MT |
467 | // Set default filesystem |
468 | dest->filesystem = HW_FS_DEFAULT; | |
d7dd283b MT |
469 | |
470 | return dest; | |
471 | } | |
c4e96674 MT |
472 | |
473 | unsigned long long hw_memory() { | |
474 | FILE* handle = NULL; | |
475 | char line[STRING_SIZE]; | |
476 | ||
477 | unsigned long long memory = 0; | |
478 | ||
479 | /* Calculate amount of memory in machine */ | |
480 | if ((handle = fopen("/proc/meminfo", "r"))) { | |
481 | while (fgets(line, sizeof(line), handle)) { | |
25fcce25 | 482 | if (!sscanf (line, "MemTotal: %llu kB", &memory)) { |
c4e96674 MT |
483 | memory = 0; |
484 | } | |
485 | } | |
486 | ||
487 | fclose(handle); | |
488 | } | |
489 | ||
490 | return memory * 1024; | |
491 | } | |
25fcce25 MT |
492 | |
493 | int hw_create_partitions(struct hw_destination* dest) { | |
494 | char* cmd = NULL; | |
495 | ||
496 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); | |
497 | ||
498 | // Set partition type | |
499 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
500 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
501 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
502 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
503 | ||
802a123b | 504 | unsigned long long part_start = MB2BYTES(1); |
25fcce25 | 505 | |
48d6a112 | 506 | if (*dest->part_bootldr) { |
802a123b | 507 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
48d6a112 | 508 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary", |
802a123b | 509 | part_start, part_start + dest->size_bootldr - 1); |
48d6a112 MT |
510 | |
511 | part_start += dest->size_bootldr; | |
512 | } | |
513 | ||
25fcce25 | 514 | if (*dest->part_boot) { |
802a123b | 515 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 516 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", |
802a123b | 517 | part_start, part_start + dest->size_boot - 1); |
25fcce25 MT |
518 | |
519 | part_start += dest->size_boot; | |
520 | } | |
521 | ||
522 | if (*dest->part_swap) { | |
802a123b | 523 | asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd, |
25fcce25 | 524 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", |
802a123b | 525 | part_start, part_start + dest->size_swap - 1); |
25fcce25 MT |
526 | |
527 | part_start += dest->size_swap; | |
528 | } | |
529 | ||
530 | if (*dest->part_root) { | |
802a123b | 531 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 532 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", |
802a123b | 533 | part_start, part_start + dest->size_root - 1); |
25fcce25 MT |
534 | |
535 | part_start += dest->size_root; | |
536 | } | |
537 | ||
538 | if (*dest->part_data) { | |
802a123b | 539 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 540 | (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary", |
802a123b | 541 | part_start, part_start + dest->size_data - 1); |
25fcce25 MT |
542 | |
543 | part_start += dest->size_data; | |
544 | } | |
545 | ||
546 | if (dest->part_table == HW_PART_TABLE_MSDOS && dest->part_boot_idx > 0) { | |
547 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); | |
548 | ||
549 | } else if (dest->part_table == HW_PART_TABLE_GPT) { | |
48d6a112 MT |
550 | if (*dest->part_bootldr) { |
551 | asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx); | |
552 | } | |
25fcce25 MT |
553 | asprintf(&cmd, "%s disk_set pmbr_boot on", cmd); |
554 | } | |
555 | ||
556 | int r = mysystem(cmd); | |
557 | ||
268090a8 MT |
558 | // Wait until the system re-read the partition table |
559 | if (r == 0) { | |
560 | unsigned int counter = 10; | |
561 | ||
562 | while (counter-- > 0) { | |
563 | sleep(1); | |
564 | ||
48d6a112 MT |
565 | if (*dest->part_bootldr && (access(dest->part_bootldr, R_OK) != 0)) |
566 | continue; | |
567 | ||
268090a8 MT |
568 | if (*dest->part_boot && (access(dest->part_boot, R_OK) != 0)) |
569 | continue; | |
570 | ||
571 | if (*dest->part_swap && (access(dest->part_swap, R_OK) != 0)) | |
572 | continue; | |
573 | ||
574 | if (*dest->part_root && (access(dest->part_root, R_OK) != 0)) | |
575 | continue; | |
576 | ||
577 | if (*dest->part_data && (access(dest->part_data, R_OK) != 0)) | |
578 | continue; | |
579 | ||
580 | // All partitions do exist, exiting the loop. | |
581 | break; | |
582 | } | |
583 | } | |
584 | ||
25fcce25 MT |
585 | if (cmd) |
586 | free(cmd); | |
587 | ||
588 | return r; | |
589 | } | |
590 | ||
591 | static int hw_format_filesystem(const char* path, int fs) { | |
592 | char cmd[STRING_SIZE] = "\0"; | |
593 | ||
594 | // Swap | |
595 | if (fs == HW_FS_SWAP) { | |
596 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
597 | // ReiserFS | |
598 | } else if (fs == HW_FS_REISERFS) { | |
599 | snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path); | |
600 | ||
601 | // EXT4 | |
602 | } else if (fs == HW_FS_EXT4) { | |
603 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path); | |
604 | ||
605 | // EXT4 w/o journal | |
606 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
607 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path); | |
70a44b52 MT |
608 | |
609 | // XFS | |
610 | } else if (fs == HW_FS_XFS) { | |
611 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path); | |
25fcce25 MT |
612 | } |
613 | ||
614 | assert(*cmd); | |
615 | ||
616 | int r = mysystem(cmd); | |
617 | ||
618 | return r; | |
619 | } | |
620 | ||
621 | int hw_create_filesystems(struct hw_destination* dest) { | |
622 | int r; | |
623 | ||
624 | // boot | |
625 | if (*dest->part_boot) { | |
626 | r = hw_format_filesystem(dest->part_boot, dest->filesystem); | |
627 | if (r) | |
628 | return r; | |
629 | } | |
630 | ||
631 | // swap | |
632 | if (*dest->part_swap) { | |
633 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP); | |
634 | if (r) | |
635 | return r; | |
636 | } | |
637 | ||
638 | // root | |
639 | r = hw_format_filesystem(dest->part_root, dest->filesystem); | |
640 | if (r) | |
641 | return r; | |
642 | ||
643 | // data | |
644 | if (*dest->part_data) { | |
645 | r = hw_format_filesystem(dest->part_data, dest->filesystem); | |
646 | if (r) | |
647 | return r; | |
648 | } | |
649 | ||
650 | return 0; | |
651 | } | |
652 | ||
653 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
654 | char target[STRING_SIZE]; | |
655 | ||
656 | assert(*prefix == '/'); | |
657 | ||
658 | const char* filesystem; | |
659 | switch (dest->filesystem) { | |
660 | case HW_FS_REISERFS: | |
661 | filesystem = "reiserfs"; | |
662 | break; | |
663 | ||
664 | case HW_FS_EXT4: | |
665 | case HW_FS_EXT4_WO_JOURNAL: | |
666 | filesystem = "ext4"; | |
667 | break; | |
668 | ||
70a44b52 MT |
669 | case HW_FS_XFS: |
670 | filesystem = "xfs"; | |
671 | break; | |
672 | ||
25fcce25 MT |
673 | default: |
674 | assert(0); | |
675 | } | |
676 | ||
677 | // root | |
678 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
679 | if (r) | |
680 | return r; | |
681 | ||
682 | // boot | |
683 | if (*dest->part_boot) { | |
684 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
685 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
686 | ||
687 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
688 | if (r) { | |
689 | hw_umount_filesystems(dest, prefix); | |
690 | ||
691 | return r; | |
692 | } | |
693 | } | |
694 | ||
695 | // data | |
696 | if (*dest->part_data) { | |
697 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA); | |
698 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
699 | ||
700 | r = hw_mount(dest->part_data, target, filesystem, 0); | |
701 | if (r) { | |
702 | hw_umount_filesystems(dest, prefix); | |
703 | ||
704 | return r; | |
705 | } | |
706 | } | |
707 | ||
708 | // swap | |
709 | if (*dest->part_swap) { | |
710 | r = swapon(dest->part_swap, 0); | |
711 | if (r) { | |
712 | hw_umount_filesystems(dest, prefix); | |
713 | ||
714 | return r; | |
715 | } | |
716 | } | |
717 | ||
718 | // bind-mount misc filesystems | |
719 | char** otherfs = other_filesystems; | |
720 | while (*otherfs) { | |
721 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs); | |
722 | ||
723 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
724 | r = hw_mount(*otherfs, target, NULL, MS_BIND); | |
725 | if (r) { | |
726 | hw_umount_filesystems(dest, prefix); | |
727 | ||
728 | return r; | |
729 | } | |
730 | ||
731 | otherfs++; | |
732 | } | |
733 | ||
734 | return 0; | |
735 | } | |
736 | ||
737 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
738 | // boot | |
739 | if (*dest->part_boot) { | |
740 | hw_umount(dest->part_boot); | |
741 | } | |
742 | ||
743 | // data | |
744 | if (*dest->part_data) { | |
745 | hw_umount(dest->part_data); | |
746 | } | |
747 | ||
748 | // root | |
749 | hw_umount(dest->part_root); | |
750 | ||
751 | // swap | |
752 | if (*dest->part_swap) { | |
753 | swapoff(dest->part_swap); | |
754 | } | |
755 | ||
756 | // misc filesystems | |
757 | char target[STRING_SIZE]; | |
758 | char** otherfs = other_filesystems; | |
759 | ||
760 | while (*otherfs) { | |
761 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++); | |
762 | hw_umount(target); | |
763 | } | |
764 | ||
765 | return 0; | |
766 | } | |
4a0d9bef MT |
767 | |
768 | int hw_setup_raid(struct hw_destination* dest) { | |
769 | char* cmd = NULL; | |
770 | ||
771 | assert(dest->is_raid); | |
772 | ||
f5007e9c | 773 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest->path); |
4a0d9bef MT |
774 | |
775 | switch (dest->raid_level) { | |
776 | case 1: | |
777 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
778 | break; | |
779 | ||
780 | default: | |
781 | assert(0); | |
782 | } | |
783 | ||
784 | if (dest->disk1) { | |
785 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
786 | } | |
787 | ||
788 | if (dest->disk2) { | |
789 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
790 | } | |
791 | ||
792 | int r = mysystem(cmd); | |
793 | free(cmd); | |
794 | ||
795 | // Wait a moment until the device has been properly brought up | |
796 | if (r == 0) { | |
797 | unsigned int counter = 10; | |
798 | while (counter-- > 0) { | |
799 | sleep(1); | |
800 | ||
fde37387 MT |
801 | // If the raid device has not yet been properly brought up, |
802 | // opening it will fail with the message: Device or resource busy | |
803 | // Hence we will wait a bit until it becomes usable. | |
804 | FILE* f = fopen(dest->path, "r"); | |
805 | if (f) { | |
806 | fclose(f); | |
4a0d9bef | 807 | break; |
fde37387 | 808 | } |
4a0d9bef MT |
809 | } |
810 | } | |
811 | ||
812 | return r; | |
813 | } | |
814 | ||
815 | int hw_stop_all_raid_arrays() { | |
816 | return mysystem("/sbin/mdadm --stop --scan"); | |
817 | } | |
f5007e9c MT |
818 | |
819 | int hw_install_bootloader(struct hw_destination* dest) { | |
820 | char cmd[STRING_SIZE]; | |
821 | int r; | |
822 | ||
823 | // Generate configuration file | |
824 | snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg"); | |
825 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
826 | if (r) | |
827 | return r; | |
828 | ||
829 | char cmd_grub[STRING_SIZE]; | |
830 | snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck"); | |
831 | ||
48d6a112 | 832 | if (dest->is_raid && (dest->part_table == HW_PART_TABLE_MSDOS)) { |
f5007e9c MT |
833 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path); |
834 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
835 | if (r) | |
836 | return r; | |
837 | ||
838 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path); | |
839 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
840 | } else { | |
841 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path); | |
842 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
843 | } | |
844 | ||
845 | return r; | |
846 | } | |
7f69d8a4 MT |
847 | |
848 | static char* hw_get_uuid(const char* dev) { | |
849 | blkid_probe p = blkid_new_probe_from_filename(dev); | |
850 | const char* buffer = NULL; | |
851 | char* uuid = NULL; | |
852 | ||
853 | if (!p) | |
854 | return NULL; | |
855 | ||
856 | blkid_do_probe(p); | |
857 | blkid_probe_lookup_value(p, "UUID", &buffer, NULL); | |
858 | ||
859 | if (buffer) | |
860 | uuid = strdup(buffer); | |
861 | ||
862 | blkid_free_probe(p); | |
863 | ||
864 | return uuid; | |
865 | } | |
866 | ||
867 | int hw_write_fstab(struct hw_destination* dest) { | |
868 | FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w"); | |
869 | if (!f) | |
870 | return -1; | |
871 | ||
872 | const char* fmt = "UUID=%s %-8s %-4s %-10s %d %d\n"; | |
873 | char* uuid = NULL; | |
874 | ||
875 | // boot | |
876 | if (*dest->part_boot) { | |
877 | uuid = hw_get_uuid(dest->part_boot); | |
878 | ||
879 | if (uuid) { | |
880 | fprintf(f, fmt, uuid, "/boot", "auto", "defaults", 1, 2); | |
881 | free(uuid); | |
882 | } | |
883 | } | |
884 | ||
885 | // swap | |
886 | if (*dest->part_swap) { | |
887 | uuid = hw_get_uuid(dest->part_swap); | |
888 | ||
889 | if (uuid) { | |
890 | fprintf(f, fmt, uuid, "swap", "swap", "defaults,pri=1", 0, 0); | |
891 | free(uuid); | |
892 | } | |
893 | } | |
894 | ||
895 | // root | |
896 | uuid = hw_get_uuid(dest->part_root); | |
897 | if (uuid) { | |
898 | fprintf(f, fmt, uuid, "/", "auto", "defaults", 1, 1); | |
899 | free(uuid); | |
900 | } | |
901 | ||
902 | // data | |
903 | if (*dest->part_data) { | |
904 | uuid = hw_get_uuid(dest->part_data); | |
905 | ||
906 | if (uuid) { | |
907 | fprintf(f, fmt, uuid, "/var", "auto", "defaults", 1, 1); | |
908 | free(uuid); | |
909 | } | |
910 | } | |
911 | ||
912 | fclose(f); | |
913 | ||
914 | return 0; | |
915 | } |