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