]>
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 | 507 | |
d2f993a7 MT |
508 | static int hw_zero_out_device(const char* path, int bytes) { |
509 | char block[512]; | |
510 | memset(block, 0, sizeof(block)); | |
511 | ||
512 | int blocks = bytes / sizeof(block); | |
513 | ||
514 | int fd = open(path, O_WRONLY); | |
515 | if (fd < 0) | |
516 | return -1; | |
517 | ||
518 | unsigned int bytes_written = 0; | |
519 | while (blocks-- > 0) { | |
520 | bytes_written += write(fd, block, sizeof(block)); | |
521 | } | |
522 | ||
523 | fsync(fd); | |
524 | close(fd); | |
525 | ||
526 | return bytes_written; | |
527 | } | |
528 | ||
25fcce25 | 529 | int hw_create_partitions(struct hw_destination* dest) { |
d2f993a7 MT |
530 | // Before we write a new partition table to the disk, we will erase |
531 | // the first couple of megabytes at the beginning of the device to | |
532 | // get rid of all left other things like bootloaders and partition tables. | |
533 | // This solves some problems when changing from MBR to GPT partitions or | |
534 | // the other way around. | |
535 | int r = hw_zero_out_device(dest->path, MB2BYTES(10)); | |
536 | if (r <= 0) | |
537 | return r; | |
25fcce25 | 538 | |
d2f993a7 | 539 | char* cmd = NULL; |
25fcce25 MT |
540 | asprintf(&cmd, "/usr/sbin/parted -s %s -a optimal", dest->path); |
541 | ||
542 | // Set partition type | |
543 | if (dest->part_table == HW_PART_TABLE_MSDOS) | |
544 | asprintf(&cmd, "%s mklabel msdos", cmd); | |
545 | else if (dest->part_table == HW_PART_TABLE_GPT) | |
546 | asprintf(&cmd, "%s mklabel gpt", cmd); | |
547 | ||
802a123b | 548 | unsigned long long part_start = MB2BYTES(1); |
25fcce25 | 549 | |
48d6a112 | 550 | if (*dest->part_bootldr) { |
802a123b | 551 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
48d6a112 | 552 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOTLDR" : "primary", |
802a123b | 553 | part_start, part_start + dest->size_bootldr - 1); |
48d6a112 MT |
554 | |
555 | part_start += dest->size_bootldr; | |
556 | } | |
557 | ||
25fcce25 | 558 | if (*dest->part_boot) { |
802a123b | 559 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 560 | (dest->part_table == HW_PART_TABLE_GPT) ? "BOOT" : "primary", |
802a123b | 561 | part_start, part_start + dest->size_boot - 1); |
25fcce25 MT |
562 | |
563 | part_start += dest->size_boot; | |
564 | } | |
565 | ||
566 | if (*dest->part_swap) { | |
802a123b | 567 | asprintf(&cmd, "%s mkpart %s linux-swap %lluB %lluB", cmd, |
25fcce25 | 568 | (dest->part_table == HW_PART_TABLE_GPT) ? "SWAP" : "primary", |
802a123b | 569 | part_start, part_start + dest->size_swap - 1); |
25fcce25 MT |
570 | |
571 | part_start += dest->size_swap; | |
572 | } | |
573 | ||
574 | if (*dest->part_root) { | |
802a123b | 575 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 576 | (dest->part_table == HW_PART_TABLE_GPT) ? "ROOT" : "primary", |
802a123b | 577 | part_start, part_start + dest->size_root - 1); |
25fcce25 MT |
578 | |
579 | part_start += dest->size_root; | |
580 | } | |
581 | ||
582 | if (*dest->part_data) { | |
802a123b | 583 | asprintf(&cmd, "%s mkpart %s ext2 %lluB %lluB", cmd, |
25fcce25 | 584 | (dest->part_table == HW_PART_TABLE_GPT) ? "DATA" : "primary", |
802a123b | 585 | part_start, part_start + dest->size_data - 1); |
25fcce25 MT |
586 | |
587 | part_start += dest->size_data; | |
588 | } | |
589 | ||
590 | if (dest->part_table == HW_PART_TABLE_MSDOS && dest->part_boot_idx > 0) { | |
591 | asprintf(&cmd, "%s set %d boot on", cmd, dest->part_boot_idx); | |
592 | ||
593 | } else if (dest->part_table == HW_PART_TABLE_GPT) { | |
48d6a112 MT |
594 | if (*dest->part_bootldr) { |
595 | asprintf(&cmd, "%s set %d bios_grub on", cmd, dest->part_boot_idx); | |
596 | } | |
25fcce25 MT |
597 | asprintf(&cmd, "%s disk_set pmbr_boot on", cmd); |
598 | } | |
599 | ||
600 | int r = mysystem(cmd); | |
601 | ||
268090a8 MT |
602 | // Wait until the system re-read the partition table |
603 | if (r == 0) { | |
604 | unsigned int counter = 10; | |
605 | ||
606 | while (counter-- > 0) { | |
607 | sleep(1); | |
608 | ||
48d6a112 MT |
609 | if (*dest->part_bootldr && (access(dest->part_bootldr, R_OK) != 0)) |
610 | continue; | |
611 | ||
268090a8 MT |
612 | if (*dest->part_boot && (access(dest->part_boot, R_OK) != 0)) |
613 | continue; | |
614 | ||
615 | if (*dest->part_swap && (access(dest->part_swap, R_OK) != 0)) | |
616 | continue; | |
617 | ||
618 | if (*dest->part_root && (access(dest->part_root, R_OK) != 0)) | |
619 | continue; | |
620 | ||
621 | if (*dest->part_data && (access(dest->part_data, R_OK) != 0)) | |
622 | continue; | |
623 | ||
624 | // All partitions do exist, exiting the loop. | |
625 | break; | |
626 | } | |
627 | } | |
628 | ||
25fcce25 MT |
629 | if (cmd) |
630 | free(cmd); | |
631 | ||
632 | return r; | |
633 | } | |
634 | ||
635 | static int hw_format_filesystem(const char* path, int fs) { | |
636 | char cmd[STRING_SIZE] = "\0"; | |
637 | ||
638 | // Swap | |
639 | if (fs == HW_FS_SWAP) { | |
640 | snprintf(cmd, sizeof(cmd), "/sbin/mkswap -v1 %s &>/dev/null", path); | |
641 | // ReiserFS | |
642 | } else if (fs == HW_FS_REISERFS) { | |
643 | snprintf(cmd, sizeof(cmd), "/sbin/mkreiserfs -f %s ", path); | |
644 | ||
645 | // EXT4 | |
646 | } else if (fs == HW_FS_EXT4) { | |
647 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 %s", path); | |
648 | ||
649 | // EXT4 w/o journal | |
650 | } else if (fs == HW_FS_EXT4_WO_JOURNAL) { | |
651 | snprintf(cmd, sizeof(cmd), "/sbin/mke2fs -T ext4 -O ^has_journal %s", path); | |
70a44b52 MT |
652 | |
653 | // XFS | |
654 | } else if (fs == HW_FS_XFS) { | |
655 | snprintf(cmd, sizeof(cmd), "/sbin/mkfs.xfs -f %s", path); | |
25fcce25 MT |
656 | } |
657 | ||
658 | assert(*cmd); | |
659 | ||
660 | int r = mysystem(cmd); | |
661 | ||
662 | return r; | |
663 | } | |
664 | ||
665 | int hw_create_filesystems(struct hw_destination* dest) { | |
666 | int r; | |
667 | ||
668 | // boot | |
669 | if (*dest->part_boot) { | |
670 | r = hw_format_filesystem(dest->part_boot, dest->filesystem); | |
671 | if (r) | |
672 | return r; | |
673 | } | |
674 | ||
675 | // swap | |
676 | if (*dest->part_swap) { | |
677 | r = hw_format_filesystem(dest->part_swap, HW_FS_SWAP); | |
678 | if (r) | |
679 | return r; | |
680 | } | |
681 | ||
682 | // root | |
683 | r = hw_format_filesystem(dest->part_root, dest->filesystem); | |
684 | if (r) | |
685 | return r; | |
686 | ||
687 | // data | |
688 | if (*dest->part_data) { | |
689 | r = hw_format_filesystem(dest->part_data, dest->filesystem); | |
690 | if (r) | |
691 | return r; | |
692 | } | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | int hw_mount_filesystems(struct hw_destination* dest, const char* prefix) { | |
698 | char target[STRING_SIZE]; | |
699 | ||
700 | assert(*prefix == '/'); | |
701 | ||
702 | const char* filesystem; | |
703 | switch (dest->filesystem) { | |
704 | case HW_FS_REISERFS: | |
705 | filesystem = "reiserfs"; | |
706 | break; | |
707 | ||
708 | case HW_FS_EXT4: | |
709 | case HW_FS_EXT4_WO_JOURNAL: | |
710 | filesystem = "ext4"; | |
711 | break; | |
712 | ||
70a44b52 MT |
713 | case HW_FS_XFS: |
714 | filesystem = "xfs"; | |
715 | break; | |
716 | ||
25fcce25 MT |
717 | default: |
718 | assert(0); | |
719 | } | |
720 | ||
721 | // root | |
722 | int r = hw_mount(dest->part_root, prefix, filesystem, 0); | |
723 | if (r) | |
724 | return r; | |
725 | ||
726 | // boot | |
727 | if (*dest->part_boot) { | |
728 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_BOOT); | |
729 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
730 | ||
731 | r = hw_mount(dest->part_boot, target, filesystem, 0); | |
732 | if (r) { | |
733 | hw_umount_filesystems(dest, prefix); | |
734 | ||
735 | return r; | |
736 | } | |
737 | } | |
738 | ||
739 | // data | |
740 | if (*dest->part_data) { | |
741 | snprintf(target, sizeof(target), "%s%s", prefix, HW_PATH_DATA); | |
742 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
743 | ||
744 | r = hw_mount(dest->part_data, target, filesystem, 0); | |
745 | if (r) { | |
746 | hw_umount_filesystems(dest, prefix); | |
747 | ||
748 | return r; | |
749 | } | |
750 | } | |
751 | ||
752 | // swap | |
753 | if (*dest->part_swap) { | |
754 | r = swapon(dest->part_swap, 0); | |
755 | if (r) { | |
756 | hw_umount_filesystems(dest, prefix); | |
757 | ||
758 | return r; | |
759 | } | |
760 | } | |
761 | ||
762 | // bind-mount misc filesystems | |
763 | char** otherfs = other_filesystems; | |
764 | while (*otherfs) { | |
765 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs); | |
766 | ||
767 | mkdir(target, S_IRWXU|S_IRWXG|S_IRWXO); | |
768 | r = hw_mount(*otherfs, target, NULL, MS_BIND); | |
769 | if (r) { | |
770 | hw_umount_filesystems(dest, prefix); | |
771 | ||
772 | return r; | |
773 | } | |
774 | ||
775 | otherfs++; | |
776 | } | |
777 | ||
778 | return 0; | |
779 | } | |
780 | ||
781 | int hw_umount_filesystems(struct hw_destination* dest, const char* prefix) { | |
782 | // boot | |
783 | if (*dest->part_boot) { | |
784 | hw_umount(dest->part_boot); | |
785 | } | |
786 | ||
787 | // data | |
788 | if (*dest->part_data) { | |
789 | hw_umount(dest->part_data); | |
790 | } | |
791 | ||
792 | // root | |
793 | hw_umount(dest->part_root); | |
794 | ||
795 | // swap | |
796 | if (*dest->part_swap) { | |
797 | swapoff(dest->part_swap); | |
798 | } | |
799 | ||
800 | // misc filesystems | |
801 | char target[STRING_SIZE]; | |
802 | char** otherfs = other_filesystems; | |
803 | ||
804 | while (*otherfs) { | |
805 | snprintf(target, sizeof(target), "%s%s", prefix, *otherfs++); | |
806 | hw_umount(target); | |
807 | } | |
808 | ||
809 | return 0; | |
810 | } | |
4a0d9bef MT |
811 | |
812 | int hw_setup_raid(struct hw_destination* dest) { | |
813 | char* cmd = NULL; | |
814 | ||
815 | assert(dest->is_raid); | |
816 | ||
f5007e9c | 817 | asprintf(&cmd, "echo \"y\" | /sbin/mdadm --create --verbose --metadata=1.2 %s", dest->path); |
4a0d9bef MT |
818 | |
819 | switch (dest->raid_level) { | |
820 | case 1: | |
821 | asprintf(&cmd, "%s --level=1 --raid-devices=2", cmd); | |
822 | break; | |
823 | ||
824 | default: | |
825 | assert(0); | |
826 | } | |
827 | ||
828 | if (dest->disk1) { | |
829 | asprintf(&cmd, "%s %s", cmd, dest->disk1->path); | |
d2f993a7 MT |
830 | |
831 | // Clear all data at the beginning | |
832 | r = hw_zero_out_device(dest->disk1->path, MB2BYTES(10)); | |
833 | if (r <= 0) | |
834 | return r; | |
4a0d9bef MT |
835 | } |
836 | ||
837 | if (dest->disk2) { | |
838 | asprintf(&cmd, "%s %s", cmd, dest->disk2->path); | |
d2f993a7 MT |
839 | |
840 | // Clear all data at the beginning | |
841 | r = hw_zero_out_device(dest->disk2->path, MB2BYTES(10)); | |
842 | if (r <= 0) | |
843 | return r; | |
4a0d9bef MT |
844 | } |
845 | ||
846 | int r = mysystem(cmd); | |
847 | free(cmd); | |
848 | ||
849 | // Wait a moment until the device has been properly brought up | |
850 | if (r == 0) { | |
851 | unsigned int counter = 10; | |
852 | while (counter-- > 0) { | |
853 | sleep(1); | |
854 | ||
fde37387 MT |
855 | // If the raid device has not yet been properly brought up, |
856 | // opening it will fail with the message: Device or resource busy | |
857 | // Hence we will wait a bit until it becomes usable. | |
858 | FILE* f = fopen(dest->path, "r"); | |
859 | if (f) { | |
860 | fclose(f); | |
4a0d9bef | 861 | break; |
fde37387 | 862 | } |
4a0d9bef MT |
863 | } |
864 | } | |
865 | ||
866 | return r; | |
867 | } | |
868 | ||
869 | int hw_stop_all_raid_arrays() { | |
870 | return mysystem("/sbin/mdadm --stop --scan"); | |
871 | } | |
f5007e9c MT |
872 | |
873 | int hw_install_bootloader(struct hw_destination* dest) { | |
874 | char cmd[STRING_SIZE]; | |
875 | int r; | |
876 | ||
877 | // Generate configuration file | |
878 | snprintf(cmd, sizeof(cmd), "/usr/sbin/grub-mkconfig -o /boot/grub/grub.cfg"); | |
879 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
880 | if (r) | |
881 | return r; | |
882 | ||
883 | char cmd_grub[STRING_SIZE]; | |
884 | snprintf(cmd_grub, sizeof(cmd_grub), "/usr/sbin/grub-install --no-floppy --recheck"); | |
885 | ||
48d6a112 | 886 | if (dest->is_raid && (dest->part_table == HW_PART_TABLE_MSDOS)) { |
f5007e9c MT |
887 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk1->path); |
888 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
889 | if (r) | |
890 | return r; | |
891 | ||
892 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->disk2->path); | |
893 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
894 | } else { | |
895 | snprintf(cmd, sizeof(cmd), "%s %s", cmd_grub, dest->path); | |
896 | r = system_chroot(DESTINATION_MOUNT_PATH, cmd); | |
897 | } | |
898 | ||
899 | return r; | |
900 | } | |
7f69d8a4 MT |
901 | |
902 | static char* hw_get_uuid(const char* dev) { | |
903 | blkid_probe p = blkid_new_probe_from_filename(dev); | |
904 | const char* buffer = NULL; | |
905 | char* uuid = NULL; | |
906 | ||
907 | if (!p) | |
908 | return NULL; | |
909 | ||
910 | blkid_do_probe(p); | |
911 | blkid_probe_lookup_value(p, "UUID", &buffer, NULL); | |
912 | ||
913 | if (buffer) | |
914 | uuid = strdup(buffer); | |
915 | ||
916 | blkid_free_probe(p); | |
917 | ||
918 | return uuid; | |
919 | } | |
920 | ||
921 | int hw_write_fstab(struct hw_destination* dest) { | |
922 | FILE* f = fopen(DESTINATION_MOUNT_PATH "/etc/fstab", "w"); | |
923 | if (!f) | |
924 | return -1; | |
925 | ||
926 | const char* fmt = "UUID=%s %-8s %-4s %-10s %d %d\n"; | |
927 | char* uuid = NULL; | |
928 | ||
929 | // boot | |
930 | if (*dest->part_boot) { | |
931 | uuid = hw_get_uuid(dest->part_boot); | |
932 | ||
933 | if (uuid) { | |
934 | fprintf(f, fmt, uuid, "/boot", "auto", "defaults", 1, 2); | |
935 | free(uuid); | |
936 | } | |
937 | } | |
938 | ||
939 | // swap | |
940 | if (*dest->part_swap) { | |
941 | uuid = hw_get_uuid(dest->part_swap); | |
942 | ||
943 | if (uuid) { | |
944 | fprintf(f, fmt, uuid, "swap", "swap", "defaults,pri=1", 0, 0); | |
945 | free(uuid); | |
946 | } | |
947 | } | |
948 | ||
949 | // root | |
950 | uuid = hw_get_uuid(dest->part_root); | |
951 | if (uuid) { | |
952 | fprintf(f, fmt, uuid, "/", "auto", "defaults", 1, 1); | |
953 | free(uuid); | |
954 | } | |
955 | ||
956 | // data | |
957 | if (*dest->part_data) { | |
958 | uuid = hw_get_uuid(dest->part_data); | |
959 | ||
960 | if (uuid) { | |
961 | fprintf(f, fmt, uuid, "/var", "auto", "defaults", 1, 1); | |
962 | free(uuid); | |
963 | } | |
964 | } | |
965 | ||
966 | fclose(f); | |
967 | ||
968 | return 0; | |
969 | } |