edfa62759efe2ead96bf3d3dd5968ad17fb42126
[project/ubox.git] / block.c
1 /*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <syslog.h>
18 #include <libgen.h>
19 #include <glob.h>
20
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <sys/swap.h>
24 #include <sys/mount.h>
25 #include <sys/wait.h>
26
27 #include <uci.h>
28 #include <uci_blob.h>
29
30 #include <libubox/list.h>
31 #include <libubox/vlist.h>
32 #include <libubox/blobmsg_json.h>
33 #include <libubox/avl-cmp.h>
34
35 #include "libblkid-tiny/libblkid-tiny.h"
36
37 enum {
38 TYPE_MOUNT,
39 TYPE_SWAP,
40 };
41
42 struct mount {
43 struct vlist_node node;
44 int type;
45
46 char *target;
47 char *path;
48 char *options;
49 char *uuid;
50 char *label;
51 char *device;
52 int extroot;
53 int overlay;
54 int disabled_fsck;
55 unsigned int prio;
56 };
57
58 static struct vlist_tree mounts;
59 static struct blob_buf b;
60 static LIST_HEAD(devices);
61 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
62 static unsigned int delay_root;
63
64 enum {
65 CFG_ANON_MOUNT,
66 CFG_ANON_SWAP,
67 CFG_AUTO_MOUNT,
68 CFG_AUTO_SWAP,
69 CFG_DELAY_ROOT,
70 CFG_CHECK_FS,
71 __CFG_MAX
72 };
73
74 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
75 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
76 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
77 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
78 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
79 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
80 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
81 };
82
83 enum {
84 MOUNT_UUID,
85 MOUNT_LABEL,
86 MOUNT_ENABLE,
87 MOUNT_TARGET,
88 MOUNT_DEVICE,
89 MOUNT_OPTIONS,
90 __MOUNT_MAX
91 };
92
93 static const struct uci_blob_param_list config_attr_list = {
94 .n_params = __CFG_MAX,
95 .params = config_policy,
96 };
97
98 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
99 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
100 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
101 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
102 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
103 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
104 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
105 };
106
107 static const struct uci_blob_param_list mount_attr_list = {
108 .n_params = __MOUNT_MAX,
109 .params = mount_policy,
110 };
111
112 enum {
113 SWAP_ENABLE,
114 SWAP_UUID,
115 SWAP_DEVICE,
116 SWAP_PRIO,
117 __SWAP_MAX
118 };
119
120 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
121 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
122 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
123 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
124 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
125 };
126
127 static const struct uci_blob_param_list swap_attr_list = {
128 .n_params = __SWAP_MAX,
129 .params = swap_policy,
130 };
131
132 static char *blobmsg_get_strdup(struct blob_attr *attr)
133 {
134 if (!attr)
135 return NULL;
136
137 return strdup(blobmsg_get_string(attr));
138 }
139
140 static char *blobmsg_get_basename(struct blob_attr *attr)
141 {
142 if (!attr)
143 return NULL;
144
145 return strdup(basename(blobmsg_get_string(attr)));
146 }
147
148 static int mount_add(struct uci_section *s)
149 {
150 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
151 struct mount *m;
152
153 blob_buf_init(&b, 0);
154 uci_to_blob(&b, s, &mount_attr_list);
155 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
156
157 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
158 return -1;
159
160 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
161 return -1;
162
163 m = malloc(sizeof(struct mount));
164 m->type = TYPE_MOUNT;
165 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
166 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
167 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
168 m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
169 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
170 m->overlay = m->extroot = 0;
171 if (m->target && !strcmp(m->target, "/"))
172 m->extroot = 1;
173 if (m->target && !strcmp(m->target, "/overlay"))
174 m->extroot = m->overlay = 1;
175
176 if (m->uuid)
177 vlist_add(&mounts, &m->node, m->uuid);
178 else if (m->label)
179 vlist_add(&mounts, &m->node, m->label);
180 else if (m->device)
181 vlist_add(&mounts, &m->node, m->device);
182
183 return 0;
184 }
185
186 static int swap_add(struct uci_section *s)
187 {
188 struct blob_attr *tb[__SWAP_MAX] = { 0 };
189 struct mount *m;
190
191 blob_buf_init(&b, 0);
192 uci_to_blob(&b, s, &swap_attr_list);
193 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
194
195 if (!tb[SWAP_UUID] && !tb[SWAP_DEVICE])
196 return -1;
197
198 m = malloc(sizeof(struct mount));
199 memset(m, 0, sizeof(struct mount));
200 m->type = TYPE_SWAP;
201 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
202 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
203 if (tb[SWAP_PRIO])
204 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
205 if (m->prio)
206 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
207
208 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
209 vlist_add(&mounts, &m->node, (m->uuid) ? (m->uuid) : (m->device));
210
211 return 0;
212 }
213
214 static int global_add(struct uci_section *s)
215 {
216 struct blob_attr *tb[__CFG_MAX] = { 0 };
217
218 blob_buf_init(&b, 0);
219 uci_to_blob(&b, s, &config_attr_list);
220 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
221
222 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
223 anon_mount = 1;
224 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
225 anon_swap = 1;
226
227 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
228 auto_mount = 1;
229 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
230 auto_swap = 1;
231
232 if (tb[CFG_DELAY_ROOT])
233 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
234
235 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
236 check_fs = 1;
237
238 return 0;
239 }
240
241 static struct mount* find_swap(const char *uuid, const char *device)
242 {
243 struct mount *m;
244
245 vlist_for_each_element(&mounts, m, node) {
246 if (m->type != TYPE_SWAP)
247 continue;
248 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
249 return m;
250 if (device && m->device && !strcmp(m->device, device))
251 return m;
252 }
253
254 return NULL;
255 }
256
257 static struct mount* find_block(const char *uuid, const char *label, const char *device,
258 const char *target)
259 {
260 struct mount *m;
261
262 vlist_for_each_element(&mounts, m, node) {
263 if (m->type != TYPE_MOUNT)
264 continue;
265 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
266 return m;
267 if (m->label && label && !strcmp(m->label, label))
268 return m;
269 if (m->target && target && !strcmp(m->target, target))
270 return m;
271 if (m->device && device && !strcmp(m->device, device))
272 return m;
273 }
274
275 return NULL;
276 }
277
278 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
279 struct vlist_node *node_old)
280 {
281 }
282
283 static int config_load(char *cfg)
284 {
285 struct uci_context *ctx;
286 struct uci_package *pkg;
287 struct uci_element *e;
288
289 vlist_init(&mounts, avl_strcmp, mounts_update);
290
291 ctx = uci_alloc_context();
292 if (cfg) {
293 char path[32];
294 snprintf(path, 32, "%s/etc/config", cfg);
295 uci_set_confdir(ctx, path);
296 }
297
298 if (uci_load(ctx, "fstab", &pkg))
299 return -1;
300
301 vlist_update(&mounts);
302 uci_foreach_element(&pkg->sections, e) {
303 struct uci_section *s = uci_to_section(e);
304
305 if (!strcmp(s->type, "mount"))
306 mount_add(s);
307 if (!strcmp(s->type, "swap"))
308 swap_add(s);
309 if (!strcmp(s->type, "global"))
310 global_add(s);
311 }
312 vlist_flush(&mounts);
313
314 return 0;
315 }
316
317 static int _cache_load(const char *path)
318 {
319 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
320 int j;
321 glob_t gl;
322
323 if (glob(path, gl_flags, NULL, &gl) < 0)
324 return -1;
325
326 for (j = 0; j < gl.gl_pathc; j++) {
327 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
328 memset(pr, 0, sizeof(struct blkid_struct_probe));
329 probe_block(gl.gl_pathv[j], pr);
330 if (pr->err || !pr->id)
331 free(pr);
332 else
333 list_add_tail(&pr->list, &devices);
334 }
335
336 globfree(&gl);
337
338 return 0;
339 }
340
341 static void cache_load(int mtd)
342 {
343 if (mtd)
344 _cache_load("/dev/mtdblock*");
345 _cache_load("/dev/mmcblk*");
346 _cache_load("/dev/sd*");
347 _cache_load("/dev/sdc*");
348 _cache_load("/dev/hd*");
349 _cache_load("/dev/md*");
350 _cache_load("/dev/mapper/*");
351 }
352
353 static int print_block_info(struct blkid_struct_probe *pr)
354 {
355 printf("%s:", pr->dev);
356 if (pr->uuid[0])
357 printf(" UUID=\"%s\"", pr->uuid);
358
359 if (pr->label[0])
360 printf(" LABEL=\"%s\"", pr->label);
361
362 if (pr->name[0])
363 printf(" NAME=\"%s\"", pr->name);
364
365 if (pr->version[0])
366 printf(" VERSION=\"%s\"", pr->version);
367
368 printf(" TYPE=\"%s\"\n", pr->id->name);
369
370 return 0;
371 }
372
373 static int print_block_uci(struct blkid_struct_probe *pr)
374 {
375 if (!strcmp(pr->id->name, "swap")) {
376 printf("config 'swap'\n");
377 } else {
378 printf("config 'mount'\n");
379 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
380 }
381 if (pr->uuid[0])
382 printf("\toption\tuuid\t'%s'\n", pr->uuid);
383 else
384 printf("\toption\tdevice\t'%s'\n", pr->dev);
385 printf("\toption\tenabled\t'0'\n\n");
386
387 return 0;
388 }
389
390 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
391 {
392 struct blkid_struct_probe *pr = NULL;
393
394 if (uuid)
395 list_for_each_entry(pr, &devices, list)
396 if (!strcmp(pr->uuid, uuid))
397 return pr;
398
399 if (label)
400 list_for_each_entry(pr, &devices, list)
401 if (strcmp(pr->label, label))
402 return pr;
403
404 if (path)
405 list_for_each_entry(pr, &devices, list)
406 if (!strcmp(pr->dev, path))
407 return pr;
408
409 return NULL;
410 }
411
412 static char* find_mount_point(char *block)
413 {
414 FILE *fp = fopen("/proc/mounts", "r");
415 static char line[256];
416 int len = strlen(block);
417 char *point = NULL;
418
419 if(!fp)
420 return NULL;
421
422 while (fgets(line, sizeof(line), fp)) {
423 if (!strncmp(line, block, len)) {
424 char *p = &line[len + 1];
425 char *t = strstr(p, " ");
426
427 if (!t)
428 return NULL;
429 *t = '\0';
430 point = p;
431 break;
432 }
433 }
434
435 fclose(fp);
436
437 return point;
438 }
439
440 static void mkdir_p(char *dir)
441 {
442 char *l = strrchr(dir, '/');
443
444 if (l) {
445 *l = '\0';
446 mkdir_p(dir);
447 *l = '/';
448 mkdir(dir, 0755);
449 }
450 }
451
452 static void check_filesystem(struct blkid_struct_probe *pr)
453 {
454 pid_t pid;
455 struct stat statbuf;
456 char *e2fsck = "/usr/sbin/e2fsck";
457
458 if (strncmp(pr->id->name, "ext", 3)) {
459 fprintf(stderr, "check_filesystem: %s is not supported\n", pr->id->name);
460 return;
461 }
462
463 if (stat(e2fsck, &statbuf) < 0) {
464 fprintf(stderr, "check_filesystem: %s not found\n", e2fsck);
465 return;
466 }
467
468 pid = fork();
469 if (!pid) {
470 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
471 exit(-1);
472 } else if (pid > 0) {
473 int status;
474
475 waitpid(pid, &status, 0);
476 if (WEXITSTATUS(status))
477 fprintf(stderr, "check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
478 }
479 }
480
481 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
482 {
483 struct mount *m;
484 char *device = basename(pr->dev);
485
486 if (!pr)
487 return -1;
488
489 if (!strcmp(pr->id->name, "swap")) {
490 if (hotplug && !auto_swap)
491 return -1;
492 m = find_swap(pr->uuid, device);
493 if (m || anon_swap)
494 swapon(pr->dev, (m) ? (m->prio) : (0));
495
496 return 0;
497 }
498
499 if (hotplug && !auto_mount)
500 return -1;
501
502 if (find_mount_point(pr->dev)) {
503 fprintf(stderr, "%s is already mounted\n", pr->dev);
504 return -1;
505 }
506
507 m = find_block(pr->uuid, pr->label, device, NULL);
508 if (m && m->extroot)
509 return -1;
510
511 if (m) {
512 char *target = m->target;
513 char _target[] = "/mnt/mmcblk123";
514 int err = 0;
515
516 if (!target) {
517 snprintf(_target, sizeof(_target), "/mnt/%s", device);
518 target = _target;
519 }
520 mkdir_p(target);
521
522 if (check_fs)
523 check_filesystem(pr);
524
525 err = mount(pr->dev, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
526 if (err)
527 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
528 pr->dev, pr->id->name, target, err, strerror(err));
529 return err;
530 }
531
532 if (anon_mount) {
533 char target[] = "/mnt/mmcblk123";
534 int err = 0;
535
536 snprintf(target, sizeof(target), "/mnt/%s", device);
537 mkdir_p(target);
538
539 if (check_fs)
540 check_filesystem(pr);
541
542 err = mount(pr->dev, target, pr->id->name, 0, "");
543 if (err)
544 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
545 pr->dev, pr->id->name, target, err, strerror(err));
546 return err;
547 }
548
549 return 0;
550 }
551
552 static int umount_device(struct blkid_struct_probe *pr)
553 {
554 struct mount *m;
555 char *device = basename(pr->dev);
556 char *mp;
557 int err;
558
559 if (!pr)
560 return -1;
561
562 if (!strcmp(pr->id->name, "swap"))
563 return -1;
564
565 mp = find_mount_point(pr->dev);
566 if (!mp)
567 return -1;
568
569 m = find_block(pr->uuid, pr->label, device, NULL);
570 if (m && m->extroot)
571 return -1;
572
573 err = umount2(mp, MNT_DETACH);
574 if (err)
575 fprintf(stderr, "unmounting %s (%s) failed (%d) - %s\n",
576 pr->dev, mp, err, strerror(err));
577 else
578 fprintf(stderr, "unmounted %s (%s)\n",
579 pr->dev, mp);
580
581 return err;
582 }
583
584 static int main_hotplug(int argc, char **argv)
585 {
586 char path[32];
587 char *action, *device, *mount_point;
588
589 action = getenv("ACTION");
590 device = getenv("DEVNAME");
591
592 if (!action || !device)
593 return -1;
594 snprintf(path, sizeof(path), "/dev/%s", device);
595
596 if (!strcmp(action, "remove")) {
597 int err = 0;
598 mount_point = find_mount_point(path);
599 if (mount_point)
600 err = umount2(mount_point, MNT_DETACH);
601
602 if (err)
603 fprintf(stderr, "umount of %s failed (%d) - %s\n",
604 mount_point, err, strerror(err));
605
606 return 0;
607 } else if (strcmp(action, "add")) {
608 fprintf(stderr, "Unkown action %s\n", action);
609
610 return -1;
611 }
612
613 if (config_load(NULL))
614 return -1;
615 cache_load(0);
616
617 return mount_device(find_block_info(NULL, NULL, path), 1);
618 }
619
620 static int find_block_mtd(char *name, char *part, int plen)
621 {
622 FILE *fp = fopen("/proc/mtd", "r");
623 static char line[256];
624 char *index = NULL;
625
626 if(!fp)
627 return -1;
628
629 while (!index && fgets(line, sizeof(line), fp)) {
630 if (strstr(line, name)) {
631 char *eol = strstr(line, ":");
632
633 if (!eol)
634 continue;
635
636 *eol = '\0';
637 index = &line[3];
638 }
639 }
640
641 fclose(fp);
642
643 if (!index)
644 return -1;
645
646 snprintf(part, plen, "/dev/mtdblock%s", index);
647
648 return 0;
649 }
650
651 static int check_extroot(char *path)
652 {
653 struct blkid_struct_probe *pr = NULL;
654 char fs[32];
655
656 if (find_block_mtd("rootfs", fs, sizeof(fs)))
657 return -1;
658
659 list_for_each_entry(pr, &devices, list) {
660 if (!strcmp(pr->dev, fs)) {
661 struct stat s;
662 FILE *fp = NULL;
663 char tag[32];
664 char uuid[32] = { 0 };
665
666 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
667 if (stat(tag, &s)) {
668 fp = fopen(tag, "w+");
669 if (!fp) {
670 fprintf(stderr, "extroot: failed to write uuid tag file\n");
671 /* return 0 to continue boot regardless of error */
672 return 0;
673 }
674 fputs(pr->uuid, fp);
675 fclose(fp);
676 return 0;
677 }
678
679 fp = fopen(tag, "r");
680 if (!fp) {
681 fprintf(stderr, "extroot: failed to open uuid tag file\n");
682 return -1;
683 }
684
685 fgets(uuid, sizeof(uuid), fp);
686 fclose(fp);
687 if (!strcmp(uuid, pr->uuid))
688 return 0;
689 fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
690 }
691 }
692 return -1;
693 }
694
695 static int mount_extroot(char *cfg)
696 {
697 char overlay[] = "/tmp/overlay";
698 char mnt[] = "/tmp/mnt";
699 char *path = mnt;
700 struct blkid_struct_probe *pr;
701 struct mount *m;
702 int err = -1;
703
704 if (config_load(cfg))
705 return -2;
706
707 m = find_block(NULL, NULL, NULL, "/");
708 if (!m)
709 m = find_block(NULL, NULL, NULL, "/overlay");
710
711 if (!m || !m->extroot)
712 return -1;
713
714 pr = find_block_info(m->uuid, m->label, NULL);
715
716 if (!pr && delay_root){
717 fprintf(stderr, "extroot: is not ready yet, retrying in %ui seconds\n", delay_root);
718 sleep(delay_root);
719 mkblkdev();
720 cache_load(0);
721 pr = find_block_info(m->uuid, m->label, NULL);
722 }
723 if (pr) {
724 if (strncmp(pr->id->name, "ext", 3)) {
725 fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
726 return -1;
727 }
728 if (m->overlay)
729 path = overlay;
730 mkdir_p(path);
731
732 if (check_fs)
733 check_filesystem(pr);
734
735 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
736
737 if (err) {
738 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
739 pr->dev, pr->id->name, path, err, strerror(err));
740 } else if (m->overlay) {
741 err = check_extroot(path);
742 if (err)
743 umount(path);
744 }
745 }
746
747 return err;
748 }
749
750 static int main_extroot(int argc, char **argv)
751 {
752 struct blkid_struct_probe *pr;
753 char fs[32] = { 0 };
754 char fs_data[32] = { 0 };
755 int err = -1;
756
757 if (!getenv("PREINIT"))
758 return -1;
759
760 if (argc != 2) {
761 fprintf(stderr, "Usage: block extroot mountpoint\n");
762 return -1;
763 }
764
765 mkblkdev();
766 cache_load(1);
767
768 find_block_mtd("rootfs", fs, sizeof(fs));
769 if (!fs[0])
770 return -2;
771
772 pr = find_block_info(NULL, NULL, fs);
773 if (!pr)
774 return -3;
775
776 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
777 if (fs_data[0]) {
778 pr = find_block_info(NULL, NULL, fs_data);
779 if (pr && !strcmp(pr->id->name, "jffs2")) {
780 char cfg[] = "/tmp/jffs_cfg";
781
782 mkdir_p(cfg);
783 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
784 err = mount_extroot(cfg);
785 umount2(cfg, MNT_DETACH);
786 }
787 if (err < 0)
788 rmdir("/tmp/overlay");
789 rmdir(cfg);
790 return err;
791 }
792 }
793
794 return mount_extroot(NULL);
795 }
796
797 static int main_mount(int argc, char **argv)
798 {
799 struct blkid_struct_probe *pr;
800
801 if (config_load(NULL))
802 return -1;
803
804 cache_load(0);
805 list_for_each_entry(pr, &devices, list)
806 mount_device(pr, 0);
807
808 return 0;
809 }
810
811 static int main_umount(int argc, char **argv)
812 {
813 struct blkid_struct_probe *pr;
814
815 if (config_load(NULL))
816 return -1;
817
818 cache_load(0);
819 list_for_each_entry(pr, &devices, list)
820 umount_device(pr);
821
822 return 0;
823 }
824
825 static int main_detect(int argc, char **argv)
826 {
827 struct blkid_struct_probe *pr;
828
829 cache_load(0);
830 printf("config 'global'\n");
831 printf("\toption\tanon_swap\t'0'\n");
832 printf("\toption\tanon_mount\t'0'\n");
833 printf("\toption\tauto_swap\t'1'\n");
834 printf("\toption\tauto_mount\t'1'\n");
835 printf("\toption\tdelay_root\t'0'\n");
836 printf("\toption\tcheck_fs\t'0'\n\n");
837 list_for_each_entry(pr, &devices, list)
838 print_block_uci(pr);
839
840 return 0;
841 }
842
843 static int main_info(int argc, char **argv)
844 {
845 int i;
846 struct blkid_struct_probe *pr;
847
848 cache_load(1);
849 if (argc == 2) {
850 list_for_each_entry(pr, &devices, list)
851 print_block_info(pr);
852
853 return 0;
854 };
855
856 for (i = 2; i < argc; i++) {
857 struct stat s;
858
859 if (stat(argv[i], &s)) {
860 fprintf(stderr, "failed to stat %s\n", argv[i]);
861 continue;
862 }
863 if (!S_ISBLK(s.st_mode)) {
864 fprintf(stderr, "%s is not a block device\n", argv[i]);
865 continue;
866 }
867 pr = find_block_info(NULL, NULL, argv[i]);
868 if (pr)
869 print_block_info(pr);
870 }
871
872 return 0;
873 }
874
875 static int main_swapon(int argc, char **argv)
876 {
877 if (argc != 2) {
878 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
879 return -1;
880 }
881
882 if (!strcmp(argv[1], "-a")) {
883 struct blkid_struct_probe *pr;
884
885 cache_load(0);
886 list_for_each_entry(pr, &devices, list) {
887 if (strcmp(pr->id->name, "swap"))
888 continue;
889 if (swapon(pr->dev, 0))
890 fprintf(stderr, "failed to swapon %s\n", pr->dev);
891 }
892 } else {
893 struct stat s;
894 int err;
895
896 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
897 fprintf(stderr, "%s is not a block device\n", argv[1]);
898 return -1;
899 }
900 err = swapon(argv[1], 0);
901 if (err) {
902 fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
903 return err;
904 }
905 }
906
907 return 0;
908 }
909
910 static int main_swapoff(int argc, char **argv)
911 {
912 if (argc != 2) {
913 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
914 return -1;
915 }
916
917 if (!strcmp(argv[1], "-a")) {
918 FILE *fp = fopen("/proc/swaps", "r");
919 char line[256];
920
921 if (!fp) {
922 fprintf(stderr, "failed to open /proc/swaps\n");
923 return -1;
924 }
925 fgets(line, sizeof(line), fp);
926 while (fgets(line, sizeof(line), fp)) {
927 char *end = strchr(line, ' ');
928 int err;
929
930 if (!end)
931 continue;
932 *end = '\0';
933 err = swapoff(line);
934 if (err)
935 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
936 }
937 fclose(fp);
938 } else {
939 struct stat s;
940 int err;
941
942 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
943 fprintf(stderr, "%s is not a block device\n", argv[1]);
944 return -1;
945 }
946 err = swapoff(argv[1]);
947 if (err) {
948 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
949 return err;
950 }
951 }
952
953 return 0;
954 }
955
956 int main(int argc, char **argv)
957 {
958 char *base = basename(*argv);
959
960 umask(0);
961
962 if (!strcmp(base, "swapon"))
963 return main_swapon(argc, argv);
964
965 if (!strcmp(base, "swapoff"))
966 return main_swapoff(argc, argv);
967
968 if ((argc > 1) && !strcmp(base, "block")) {
969 if (!strcmp(argv[1], "info"))
970 return main_info(argc, argv);
971
972 if (!strcmp(argv[1], "detect"))
973 return main_detect(argc, argv);
974
975 if (!strcmp(argv[1], "hotplug"))
976 return main_hotplug(argc, argv);
977
978 if (!strcmp(argv[1], "extroot"))
979 return main_extroot(argc, argv);
980
981 if (!strcmp(argv[1], "mount"))
982 return main_mount(argc, argv);
983
984 if (!strcmp(argv[1], "umount"))
985 return main_umount(argc, argv);
986 }
987
988 fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");
989
990 return -1;
991 }