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