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