Revert "block: support hierarchical mount/umount"
[project/fstools.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 <getopt.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <libgen.h>
21 #include <glob.h>
22 #include <dirent.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <limits.h>
28
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/swap.h>
32 #include <sys/mount.h>
33 #include <sys/wait.h>
34 #include <sys/sysmacros.h>
35
36 #include <linux/fs.h>
37
38 #include <uci.h>
39 #include <uci_blob.h>
40
41 #include <libubox/ulog.h>
42 #include <libubox/list.h>
43 #include <libubox/vlist.h>
44 #include <libubox/blobmsg_json.h>
45 #include <libubox/avl-cmp.h>
46 #include <libubus.h>
47
48 #include "probe.h"
49
50 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
51
52 #ifdef UBIFS_EXTROOT
53 #include "libubi/libubi.h"
54 #endif
55
56 enum {
57 TYPE_MOUNT,
58 TYPE_SWAP,
59 };
60
61 enum {
62 TYPE_DEV,
63 TYPE_HOTPLUG,
64 TYPE_AUTOFS,
65 };
66
67 struct mount {
68 struct vlist_node node;
69 int type;
70
71 char *target;
72 char *path;
73 char *options;
74 uint32_t flags;
75 char *uuid;
76 char *label;
77 char *device;
78 int extroot;
79 int autofs;
80 int overlay;
81 int disabled_fsck;
82 unsigned int prio;
83 };
84
85 static struct vlist_tree mounts;
86 static struct blob_buf b;
87 static LIST_HEAD(devices);
88 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
89 static unsigned int delay_root;
90
91 enum {
92 CFG_ANON_MOUNT,
93 CFG_ANON_SWAP,
94 CFG_AUTO_MOUNT,
95 CFG_AUTO_SWAP,
96 CFG_DELAY_ROOT,
97 CFG_CHECK_FS,
98 __CFG_MAX
99 };
100
101 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
102 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
103 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
104 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
105 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
106 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
107 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
108 };
109
110 enum {
111 MOUNT_UUID,
112 MOUNT_LABEL,
113 MOUNT_ENABLE,
114 MOUNT_TARGET,
115 MOUNT_DEVICE,
116 MOUNT_OPTIONS,
117 MOUNT_AUTOFS,
118 __MOUNT_MAX
119 };
120
121 static const struct uci_blob_param_list config_attr_list = {
122 .n_params = __CFG_MAX,
123 .params = config_policy,
124 };
125
126 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
127 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
128 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
129 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
130 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
131 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
132 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
133 [MOUNT_AUTOFS] = { .name = "autofs", .type = BLOBMSG_TYPE_INT32 },
134 };
135
136 static const struct uci_blob_param_list mount_attr_list = {
137 .n_params = __MOUNT_MAX,
138 .params = mount_policy,
139 };
140
141 enum {
142 SWAP_ENABLE,
143 SWAP_UUID,
144 SWAP_LABEL,
145 SWAP_DEVICE,
146 SWAP_PRIO,
147 __SWAP_MAX
148 };
149
150 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
151 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
152 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
153 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
154 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
155 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
156 };
157
158 static const struct uci_blob_param_list swap_attr_list = {
159 .n_params = __SWAP_MAX,
160 .params = swap_policy,
161 };
162
163 struct mount_flag {
164 const char *name;
165 int32_t flag;
166 };
167
168 static const struct mount_flag mount_flags[] = {
169 { "sync", MS_SYNCHRONOUS },
170 { "async", ~MS_SYNCHRONOUS },
171 { "dirsync", MS_DIRSYNC },
172 { "mand", MS_MANDLOCK },
173 { "nomand", ~MS_MANDLOCK },
174 { "atime", ~MS_NOATIME },
175 { "noatime", MS_NOATIME },
176 { "dev", ~MS_NODEV },
177 { "nodev", MS_NODEV },
178 { "diratime", ~MS_NODIRATIME },
179 { "nodiratime", MS_NODIRATIME },
180 { "exec", ~MS_NOEXEC },
181 { "noexec", MS_NOEXEC },
182 { "suid", ~MS_NOSUID },
183 { "nosuid", MS_NOSUID },
184 { "rw", ~MS_RDONLY },
185 { "ro", MS_RDONLY },
186 { "relatime", MS_RELATIME },
187 { "norelatime", ~MS_RELATIME },
188 { "strictatime", MS_STRICTATIME },
189 { "acl", MS_POSIXACL },
190 { "noacl", ~MS_POSIXACL },
191 { "nouser_xattr", MS_NOUSER },
192 { "user_xattr", ~MS_NOUSER },
193 };
194
195 static char *blobmsg_get_strdup(struct blob_attr *attr)
196 {
197 if (!attr)
198 return NULL;
199
200 return strdup(blobmsg_get_string(attr));
201 }
202
203 static char *blobmsg_get_basename(struct blob_attr *attr)
204 {
205 if (!attr)
206 return NULL;
207
208 return strdup(basename(blobmsg_get_string(attr)));
209 }
210
211 static void parse_mount_options(struct mount *m, char *optstr)
212 {
213 int i;
214 bool is_flag;
215 char *p, *opts, *last;
216
217 m->flags = 0;
218 m->options = NULL;
219
220 if (!optstr || !*optstr)
221 return;
222
223 m->options = opts = calloc(1, strlen(optstr) + 1);
224
225 if (!m->options)
226 return;
227
228 p = last = optstr;
229
230 do {
231 p = strchr(p, ',');
232
233 if (p)
234 *p++ = 0;
235
236 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
237 if (!strcmp(last, mount_flags[i].name)) {
238 if (mount_flags[i].flag < 0)
239 m->flags &= (uint32_t)mount_flags[i].flag;
240 else
241 m->flags |= (uint32_t)mount_flags[i].flag;
242 is_flag = true;
243 break;
244 }
245 }
246
247 if (!is_flag)
248 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
249
250 last = p;
251
252 } while (p);
253
254 free(optstr);
255 }
256
257 static int mount_add(struct uci_section *s)
258 {
259 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
260 struct mount *m;
261
262 blob_buf_init(&b, 0);
263 uci_to_blob(&b, s, &mount_attr_list);
264 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
265
266 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
267 return -1;
268
269 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
270 return -1;
271
272 m = malloc(sizeof(struct mount));
273 m->type = TYPE_MOUNT;
274 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
275 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
276 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
277 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
278 if (tb[MOUNT_AUTOFS])
279 m->autofs = blobmsg_get_u32(tb[MOUNT_AUTOFS]);
280 else
281 m->autofs = 0;
282 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
283
284 m->overlay = m->extroot = 0;
285 if (m->target && !strcmp(m->target, "/"))
286 m->extroot = 1;
287 if (m->target && !strcmp(m->target, "/overlay"))
288 m->extroot = m->overlay = 1;
289
290 if (m->target && *m->target != '/') {
291 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
292 s->e.name, m->target);
293 free(m);
294 return -1;
295 }
296
297 if (m->uuid)
298 vlist_add(&mounts, &m->node, m->uuid);
299 else if (m->label)
300 vlist_add(&mounts, &m->node, m->label);
301 else if (m->device)
302 vlist_add(&mounts, &m->node, m->device);
303
304 return 0;
305 }
306
307 static int swap_add(struct uci_section *s)
308 {
309 struct blob_attr *tb[__SWAP_MAX] = { 0 };
310 struct mount *m;
311
312 blob_buf_init(&b, 0);
313 uci_to_blob(&b, s, &swap_attr_list);
314 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
315
316 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
317 return -1;
318
319 m = malloc(sizeof(struct mount));
320 memset(m, 0, sizeof(struct mount));
321 m->type = TYPE_SWAP;
322 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
323 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
324 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
325 if (tb[SWAP_PRIO])
326 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
327 if (m->prio)
328 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
329
330 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
331 /* store complete swap path */
332 if (tb[SWAP_DEVICE])
333 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
334
335 if (m->uuid)
336 vlist_add(&mounts, &m->node, m->uuid);
337 else if (m->label)
338 vlist_add(&mounts, &m->node, m->label);
339 else if (m->device)
340 vlist_add(&mounts, &m->node, m->device);
341 }
342
343 return 0;
344 }
345
346 static int global_add(struct uci_section *s)
347 {
348 struct blob_attr *tb[__CFG_MAX] = { 0 };
349
350 blob_buf_init(&b, 0);
351 uci_to_blob(&b, s, &config_attr_list);
352 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
353
354 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
355 anon_mount = 1;
356 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
357 anon_swap = 1;
358
359 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
360 auto_mount = 1;
361 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
362 auto_swap = 1;
363
364 if (tb[CFG_DELAY_ROOT])
365 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
366
367 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
368 check_fs = 1;
369
370 return 0;
371 }
372
373 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
374 {
375 struct mount *m;
376
377 vlist_for_each_element(&mounts, m, node) {
378 if (m->type != TYPE_SWAP)
379 continue;
380 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
381 return m;
382 if (label && m->label && !strcmp(m->label, label))
383 return m;
384 if (device && m->device && !strcmp(m->device, device))
385 return m;
386 }
387
388 return NULL;
389 }
390
391 static struct mount* find_block(const char *uuid, const char *label, const char *device,
392 const char *target)
393 {
394 struct mount *m;
395
396 vlist_for_each_element(&mounts, m, node) {
397 if (m->type != TYPE_MOUNT)
398 continue;
399 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
400 return m;
401 if (m->label && label && !strcmp(m->label, label))
402 return m;
403 if (m->target && target && !strcmp(m->target, target))
404 return m;
405 if (m->device && device && !strcmp(m->device, device))
406 return m;
407 }
408
409 return NULL;
410 }
411
412 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
413 struct vlist_node *node_old)
414 {
415 }
416
417 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
418 {
419 char *file = basename(path);
420 char *dir = dirname(path);
421 char *err;
422 struct uci_package *pkg;
423
424 uci_set_confdir(ctx, dir);
425 ULOG_INFO("attempting to load %s/%s\n", dir, file);
426
427 if (uci_load(ctx, file, &pkg)) {
428 uci_get_errorstr(ctx, &err, file);
429 ULOG_ERR("unable to load configuration (%s)\n", err);
430
431 free(err);
432 return NULL;
433 }
434
435 return pkg;
436 }
437
438 static int config_load(char *cfg)
439 {
440 struct uci_context *ctx = uci_alloc_context();
441 struct uci_package *pkg = NULL;
442 struct uci_element *e;
443 char path[64];
444
445 vlist_init(&mounts, avl_strcmp, mounts_update);
446
447 if (cfg) {
448 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
449 pkg = config_try_load(ctx, path);
450
451 if (!pkg) {
452 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
453 pkg = config_try_load(ctx, path);
454 }
455 }
456
457 if (!pkg) {
458 snprintf(path, sizeof(path), "/etc/config/fstab");
459 pkg = config_try_load(ctx, path);
460 }
461
462 if (!pkg) {
463 ULOG_ERR("no usable configuration\n");
464 return -1;
465 }
466
467 vlist_update(&mounts);
468 uci_foreach_element(&pkg->sections, e) {
469 struct uci_section *s = uci_to_section(e);
470
471 if (!strcmp(s->type, "mount"))
472 mount_add(s);
473 if (!strcmp(s->type, "swap"))
474 swap_add(s);
475 if (!strcmp(s->type, "global"))
476 global_add(s);
477 }
478 vlist_flush(&mounts);
479
480 return 0;
481 }
482
483 static struct probe_info* _probe_path(char *path)
484 {
485 struct probe_info *pr;
486 char tmppath[64];
487
488 /* skip ubi device if ubiblock device is present */
489 if (path[5] == 'u' && path[6] == 'b' && path[7] == 'i' &&
490 path[8] >= '0' && path[8] <= '9' ) {
491 snprintf(tmppath, sizeof(tmppath), "/dev/ubiblock%s", path + 8);
492 list_for_each_entry(pr, &devices, list)
493 if (!strcasecmp(pr->dev, tmppath))
494 return NULL;
495 }
496
497 return probe_path(path);
498 }
499
500 static int _cache_load(const char *path)
501 {
502 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
503 int j;
504 glob_t gl;
505
506 if (glob(path, gl_flags, NULL, &gl) < 0)
507 return -1;
508
509 for (j = 0; j < gl.gl_pathc; j++) {
510 struct probe_info *pr = _probe_path(gl.gl_pathv[j]);
511 if (pr)
512 list_add_tail(&pr->list, &devices);
513 }
514
515 globfree(&gl);
516
517 return 0;
518 }
519
520 static void cache_load(int mtd)
521 {
522 if (mtd) {
523 _cache_load("/dev/mtdblock*");
524 _cache_load("/dev/ubiblock*");
525 _cache_load("/dev/ubi[0-9]*");
526 }
527 _cache_load("/dev/loop*");
528 _cache_load("/dev/mmcblk*");
529 _cache_load("/dev/sd*");
530 _cache_load("/dev/hd*");
531 _cache_load("/dev/md*");
532 _cache_load("/dev/nvme*");
533 _cache_load("/dev/vd*");
534 _cache_load("/dev/xvd*");
535 _cache_load("/dev/mapper/*");
536 }
537
538
539 static struct probe_info* find_block_info(char *uuid, char *label, char *path)
540 {
541 struct probe_info *pr = NULL;
542
543 if (uuid)
544 list_for_each_entry(pr, &devices, list)
545 if (pr->uuid && !strcasecmp(pr->uuid, uuid))
546 return pr;
547
548 if (label)
549 list_for_each_entry(pr, &devices, list)
550 if (pr->label && !strcmp(pr->label, label))
551 return pr;
552
553 if (path)
554 list_for_each_entry(pr, &devices, list)
555 if (pr->dev && !strcmp(basename(pr->dev), basename(path)))
556 return pr;
557
558 return NULL;
559 }
560
561 static char* find_mount_point(char *block)
562 {
563 FILE *fp = fopen("/proc/self/mountinfo", "r");
564 static char line[256];
565 char *point = NULL, *pos, *tmp, *cpoint, *devname;
566 struct stat s;
567 int rstat;
568 unsigned int minor, major;
569
570 if (!fp)
571 return NULL;
572
573 rstat = stat(block, &s);
574
575 while (fgets(line, sizeof(line), fp)) {
576 pos = strchr(line, ' ');
577 if (!pos)
578 continue;
579
580 pos = strchr(pos + 1, ' ');
581 if (!pos)
582 continue;
583
584 tmp = ++pos;
585 pos = strchr(pos, ':');
586 if (!pos)
587 continue;
588
589 *pos = '\0';
590 major = atoi(tmp);
591 tmp = ++pos;
592 pos = strchr(pos, ' ');
593 if (!pos)
594 continue;
595
596 *pos = '\0';
597 minor = atoi(tmp);
598 pos = strchr(pos + 1, ' ');
599 if (!pos)
600 continue;
601 tmp = ++pos;
602
603 pos = strchr(pos, ' ');
604 if (!pos)
605 continue;
606 *pos = '\0';
607 cpoint = tmp;
608
609 pos = strchr(pos + 1, ' ');
610 if (!pos)
611 continue;
612
613 pos = strchr(pos + 1, ' ');
614 if (!pos)
615 continue;
616
617 pos = strchr(pos + 1, ' ');
618 if (!pos)
619 continue;
620
621 tmp = ++pos;
622 pos = strchr(pos, ' ');
623 if (!pos)
624 continue;
625
626 *pos = '\0';
627 devname = tmp;
628 if (!strcmp(block, devname)) {
629 point = strdup(cpoint);
630 break;
631 }
632
633 if (rstat)
634 continue;
635
636 if (!S_ISBLK(s.st_mode))
637 continue;
638
639 if (major == major(s.st_rdev) &&
640 minor == minor(s.st_rdev)) {
641 point = strdup(cpoint);
642 break;
643 }
644 }
645
646 fclose(fp);
647
648 return point;
649 }
650
651 static int print_block_uci(struct probe_info *pr)
652 {
653 if (!strcmp(pr->type, "swap")) {
654 printf("config 'swap'\n");
655 } else {
656 char *mp = find_mount_point(pr->dev);
657
658 printf("config 'mount'\n");
659 if (mp) {
660 printf("\toption\ttarget\t'%s'\n", mp);
661 free(mp);
662 } else {
663 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
664 }
665 }
666 if (pr->uuid)
667 printf("\toption\tuuid\t'%s'\n", pr->uuid);
668 else
669 printf("\toption\tdevice\t'%s'\n", pr->dev);
670 printf("\toption\tenabled\t'0'\n\n");
671
672 return 0;
673 }
674
675 static int print_block_info(struct probe_info *pr)
676 {
677 static char *mp;
678
679 mp = find_mount_point(pr->dev);
680 printf("%s:", pr->dev);
681 if (pr->uuid)
682 printf(" UUID=\"%s\"", pr->uuid);
683
684 if (pr->label)
685 printf(" LABEL=\"%s\"", pr->label);
686
687 if (pr->version)
688 printf(" VERSION=\"%s\"", pr->version);
689
690 if (mp) {
691 printf(" MOUNT=\"%s\"", mp);
692 free(mp);
693 }
694
695 printf(" TYPE=\"%s\"\n", pr->type);
696
697 return 0;
698 }
699
700 static void mkdir_p(char *dir)
701 {
702 char *l = strrchr(dir, '/');
703
704 if (l) {
705 *l = '\0';
706 mkdir_p(dir);
707 *l = '/';
708 mkdir(dir, 0755);
709 }
710 }
711
712 static void check_filesystem(struct probe_info *pr)
713 {
714 pid_t pid;
715 struct stat statbuf;
716 const char *e2fsck = "/usr/sbin/e2fsck";
717 const char *f2fsck = "/usr/sbin/fsck.f2fs";
718 const char *fatfsck = "/usr/sbin/fsck.fat";
719 const char *btrfsck = "/usr/bin/btrfsck";
720 const char *ntfsck = "/usr/bin/ntfsfix";
721 const char *ckfs;
722
723 /* UBIFS does not need stuff like fsck */
724 if (!strncmp(pr->type, "ubifs", 5))
725 return;
726
727 if (!strncmp(pr->type, "vfat", 4)) {
728 ckfs = fatfsck;
729 } else if (!strncmp(pr->type, "f2fs", 4)) {
730 ckfs = f2fsck;
731 } else if (!strncmp(pr->type, "ext", 3)) {
732 ckfs = e2fsck;
733 } else if (!strncmp(pr->type, "btrfs", 5)) {
734 ckfs = btrfsck;
735 } else if (!strncmp(pr->type, "ntfs", 4)) {
736 ckfs = ntfsck;
737 } else {
738 ULOG_ERR("check_filesystem: %s is not supported\n", pr->type);
739 return;
740 }
741
742 if (stat(ckfs, &statbuf) < 0) {
743 ULOG_ERR("check_filesystem: %s not found\n", ckfs);
744 return;
745 }
746
747 pid = fork();
748 if (!pid) {
749 if(!strncmp(pr->type, "f2fs", 4)) {
750 execl(ckfs, ckfs, "-f", pr->dev, NULL);
751 exit(EXIT_FAILURE);
752 } else if(!strncmp(pr->type, "btrfs", 5)) {
753 execl(ckfs, ckfs, "--repair", pr->dev, NULL);
754 exit(EXIT_FAILURE);
755 } else if(!strncmp(pr->type, "ntfs", 4)) {
756 execl(ckfs, ckfs, "-b", pr->dev, NULL);
757 exit(EXIT_FAILURE);
758 } else {
759 execl(ckfs, ckfs, "-p", pr->dev, NULL);
760 exit(EXIT_FAILURE);
761 }
762 } else if (pid > 0) {
763 int status;
764
765 waitpid(pid, &status, 0);
766 if (WIFEXITED(status) && WEXITSTATUS(status))
767 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
768 if (WIFSIGNALED(status))
769 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs, strsignal(WTERMSIG(status)));
770 }
771 }
772
773 static void handle_swapfiles(bool on)
774 {
775 struct stat s;
776 struct mount *m;
777 struct probe_info *pr;
778
779 vlist_for_each_element(&mounts, m, node)
780 {
781 if (m->type != TYPE_SWAP || !m->target)
782 continue;
783
784 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
785 continue;
786
787 pr = _probe_path(m->target);
788
789 if (!pr)
790 continue;
791
792 if (!strcmp(pr->type, "swap")) {
793 if (on)
794 swapon(pr->dev, m->prio);
795 else
796 swapoff(pr->dev);
797 }
798
799 free(pr);
800 }
801 }
802
803 static void to_devnull(int fd)
804 {
805 int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY);
806
807 if (devnull >= 0)
808 dup2(devnull, fd);
809
810 if (devnull > STDERR_FILENO)
811 close(devnull);
812 }
813
814 static int exec_mount(const char *source, const char *target,
815 const char *fstype, const char *options)
816 {
817 pid_t pid;
818 struct stat s;
819 FILE *mount_fd;
820 int err, status, pfds[2];
821 char errmsg[128], cmd[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
822
823 snprintf(cmd, sizeof(cmd), "/sbin/mount.%s", fstype);
824
825 if (stat(cmd, &s) < 0 || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR)) {
826 ULOG_ERR("No \"mount.%s\" utility available\n", fstype);
827 return -1;
828 }
829
830 if (pipe(pfds) < 0)
831 return -1;
832
833 fcntl(pfds[0], F_SETFD, fcntl(pfds[0], F_GETFD) | FD_CLOEXEC);
834 fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
835
836 pid = vfork();
837
838 switch (pid) {
839 case -1:
840 close(pfds[0]);
841 close(pfds[1]);
842
843 return -1;
844
845 case 0:
846 to_devnull(STDIN_FILENO);
847 to_devnull(STDOUT_FILENO);
848
849 dup2(pfds[1], STDERR_FILENO);
850 close(pfds[0]);
851 close(pfds[1]);
852
853 if (options && *options)
854 execl(cmd, cmd, "-o", options, source, target, NULL);
855 else
856 execl(cmd, cmd, source, target, NULL);
857
858 return -1;
859
860 default:
861 close(pfds[1]);
862
863 mount_fd = fdopen(pfds[0], "r");
864
865 while (fgets(errmsg, sizeof(errmsg), mount_fd))
866 ULOG_ERR("mount.%s: %s", fstype, errmsg);
867
868 fclose(mount_fd);
869
870 err = waitpid(pid, &status, 0);
871
872 if (err != -1) {
873 if (status != 0) {
874 ULOG_ERR("mount.%s: failed with status %d\n", fstype, status);
875 errno = EINVAL;
876 err = -1;
877 } else {
878 errno = 0;
879 err = 0;
880 }
881 }
882
883 break;
884 }
885
886 return err;
887 }
888
889 static int hotplug_call_mount(const char *action, const char *device)
890 {
891 pid_t pid;
892 int err = 0;
893
894 pid = fork();
895 if (!pid) {
896 char * const argv[] = { "hotplug-call", "mount", NULL };
897
898 setenv("ACTION", action, 1);
899 setenv("DEVICE", device, 1);
900
901 execv("/sbin/hotplug-call", argv);
902 exit(-1);
903 } else if (pid > 0) {
904 int status;
905
906 pid = waitpid(pid, &status, 0);
907 if (pid <= 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
908 err = -ENOEXEC;
909 ULOG_ERR("hotplug-call call failed\n");
910 }
911 } else {
912 err = -errno;
913 }
914
915 return err;
916 }
917
918 static int handle_mount(const char *source, const char *target,
919 const char *fstype, struct mount *m)
920 {
921 int i, err;
922 size_t mount_opts_len;
923 char *mount_opts = NULL, *ptr;
924
925 err = mount(source, target, fstype, m ? m->flags : 0,
926 (m && m->options) ? m->options : "");
927
928 /* Requested file system type is not available in kernel,
929 attempt to call mount helper. */
930 if (err == -1 && errno == ENODEV) {
931 if (m) {
932 /* Convert mount flags back into string representation,
933 first calculate needed length of string buffer... */
934 mount_opts_len = 1 + (m->options ? strlen(m->options) : 0);
935
936 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
937 if ((mount_flags[i].flag > 0) &&
938 (mount_flags[i].flag < INT_MAX) &&
939 (m->flags & (uint32_t)mount_flags[i].flag))
940 mount_opts_len += strlen(mount_flags[i].name) + 1;
941
942 /* ... then now allocate and fill it ... */
943 ptr = mount_opts = calloc(1, mount_opts_len);
944
945 if (!ptr) {
946 errno = ENOMEM;
947 return -1;
948 }
949
950 if (m->options)
951 ptr += sprintf(ptr, "%s,", m->options);
952
953 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
954 if ((mount_flags[i].flag > 0) &&
955 (mount_flags[i].flag < INT_MAX) &&
956 (m->flags & (uint32_t)mount_flags[i].flag))
957 ptr += sprintf(ptr, "%s,", mount_flags[i].name);
958
959 mount_opts[mount_opts_len - 1] = 0;
960 }
961
962 /* ... and now finally invoke the external mount program */
963 err = exec_mount(source, target, fstype, mount_opts);
964 }
965
966 return err;
967 }
968
969 static int blockd_notify(char *device, struct mount *m, struct probe_info *pr)
970 {
971 struct ubus_context *ctx = ubus_connect(NULL);
972 uint32_t id;
973 int err;
974
975 if (!ctx)
976 return -ENXIO;
977
978 if (!ubus_lookup_id(ctx, "block", &id)) {
979 struct blob_buf buf = { 0 };
980 char *d = strrchr(device, '/');
981
982 if (d)
983 d++;
984 else
985 d = device;
986
987 blob_buf_init(&buf, 0);
988
989 if (m) {
990
991 blobmsg_add_string(&buf, "device", d);
992 if (m->uuid)
993 blobmsg_add_string(&buf, "uuid", m->uuid);
994 if (m->label)
995 blobmsg_add_string(&buf, "label", m->label);
996 if (m->target)
997 blobmsg_add_string(&buf, "target", m->target);
998 if (m->options)
999 blobmsg_add_string(&buf, "options", m->options);
1000 if (m->autofs)
1001 blobmsg_add_u32(&buf, "autofs", m->autofs);
1002 if (pr->type)
1003 blobmsg_add_string(&buf, "type", pr->type);
1004 if (pr->version)
1005 blobmsg_add_string(&buf, "version", pr->version);
1006 } else if (pr) {
1007 blobmsg_add_string(&buf, "device", d);
1008 if (pr->uuid)
1009 blobmsg_add_string(&buf, "uuid", pr->uuid);
1010 if (pr->label)
1011 blobmsg_add_string(&buf, "label", pr->label);
1012 if (pr->type)
1013 blobmsg_add_string(&buf, "type", pr->type);
1014 if (pr->version)
1015 blobmsg_add_string(&buf, "version", pr->version);
1016 blobmsg_add_u32(&buf, "anon", 1);
1017 } else {
1018 blobmsg_add_string(&buf, "device", d);
1019 blobmsg_add_u32(&buf, "remove", 1);
1020 }
1021
1022 err = ubus_invoke(ctx, id, "hotplug", buf.head, NULL, NULL, 3000);
1023 } else {
1024 err = -ENOENT;
1025 }
1026
1027 ubus_free(ctx);
1028
1029 return err;
1030 }
1031
1032 static int mount_device(struct probe_info *pr, int type)
1033 {
1034 struct mount *m;
1035 struct stat st;
1036 char _target[32];
1037 char *target;
1038 char *device;
1039 char *mp;
1040 int err;
1041
1042 if (!pr)
1043 return -1;
1044
1045 device = basename(pr->dev);
1046
1047 if (!strcmp(pr->type, "swap")) {
1048 if ((type == TYPE_HOTPLUG) && !auto_swap)
1049 return -1;
1050 m = find_swap(pr->uuid, pr->label, device);
1051 if (m || anon_swap)
1052 swapon(pr->dev, (m) ? (m->prio) : (0));
1053
1054 return 0;
1055 }
1056
1057 m = find_block(pr->uuid, pr->label, device, NULL);
1058 if (m && m->extroot)
1059 return -1;
1060
1061 mp = find_mount_point(pr->dev);
1062 if (mp) {
1063 if (m && m->type == TYPE_MOUNT && strcmp(m->target, mp)) {
1064 ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
1065 err = -1;
1066 } else
1067 err = 0;
1068 free(mp);
1069 return err;
1070 }
1071
1072 if (type == TYPE_HOTPLUG)
1073 blockd_notify(device, m, pr);
1074
1075 /* Check if device should be mounted & set the target directory */
1076 if (m) {
1077 switch (type) {
1078 case TYPE_HOTPLUG:
1079 if (m->autofs)
1080 return 0;
1081 if (!auto_mount)
1082 return -1;
1083 break;
1084 case TYPE_AUTOFS:
1085 if (!m->autofs)
1086 return -1;
1087 break;
1088 case TYPE_DEV:
1089 if (m->autofs)
1090 return -1;
1091 break;
1092 }
1093
1094 if (m->autofs) {
1095 snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
1096 target = _target;
1097 } else if (m->target) {
1098 target = m->target;
1099 } else {
1100 snprintf(_target, sizeof(_target), "/mnt/%s", device);
1101 target = _target;
1102 }
1103 } else if (anon_mount) {
1104 snprintf(_target, sizeof(_target), "/mnt/%s", device);
1105 target = _target;
1106 } else {
1107 /* No reason to mount this device */
1108 return 0;
1109 }
1110
1111 /* Mount the device */
1112
1113 if (check_fs)
1114 check_filesystem(pr);
1115
1116 mkdir_p(target);
1117 if (!lstat(target, &st) && S_ISLNK(st.st_mode))
1118 unlink(target);
1119
1120 err = handle_mount(pr->dev, target, pr->type, m);
1121 if (err) {
1122 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1123 pr->dev, pr->type, target, errno);
1124 return err;
1125 }
1126
1127 handle_swapfiles(true);
1128
1129 if (type != TYPE_AUTOFS)
1130 hotplug_call_mount("add", device);
1131
1132 return 0;
1133 }
1134
1135 static int umount_device(char *path, int type, bool all)
1136 {
1137 char *mp;
1138 int err;
1139
1140 mp = find_mount_point(path);
1141 if (!mp)
1142 return -1;
1143 if (!strcmp(mp, "/") && !all)
1144 return 0;
1145
1146 if (type != TYPE_AUTOFS)
1147 hotplug_call_mount("remove", basename(path));
1148
1149 err = umount2(mp, MNT_DETACH);
1150 if (err) {
1151 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,
1152 errno);
1153 } else {
1154 ULOG_INFO("unmounted %s (%s)\n", path, mp);
1155 rmdir(mp);
1156 }
1157
1158 free(mp);
1159 return err;
1160 }
1161
1162 static int mount_action(char *action, char *device, int type)
1163 {
1164 char path[32];
1165
1166 if (!action || !device)
1167 return -1;
1168 snprintf(path, sizeof(path), "/dev/%s", device);
1169
1170 if (!strcmp(action, "remove")) {
1171 if (type == TYPE_HOTPLUG)
1172 blockd_notify(device, NULL, NULL);
1173
1174 umount_device(path, type, true);
1175
1176 return 0;
1177 } else if (strcmp(action, "add")) {
1178 ULOG_ERR("Unkown action %s\n", action);
1179
1180 return -1;
1181 }
1182
1183 if (config_load(NULL))
1184 return -1;
1185 cache_load(0);
1186
1187 return mount_device(find_block_info(NULL, NULL, path), type);
1188 }
1189
1190 static int main_hotplug(int argc, char **argv)
1191 {
1192 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG);
1193 }
1194
1195 static int main_autofs(int argc, char **argv)
1196 {
1197 int err = 0;
1198
1199 if (argc < 3)
1200 return -1;
1201
1202 if (!strcmp(argv[2], "start")) {
1203 struct probe_info *pr;
1204
1205 if (config_load(NULL))
1206 return -1;
1207
1208 cache_load(0);
1209 list_for_each_entry(pr, &devices, list) {
1210 struct mount *m;
1211
1212 if (!strcmp(pr->type, "swap"))
1213 continue;
1214
1215 m = find_block(pr->uuid, pr->label, NULL, NULL);
1216 if (m && m->extroot)
1217 continue;
1218
1219 blockd_notify(pr->dev, m, pr);
1220 }
1221 } else if (!strcmp(argv[2], "available")) {
1222 err = hotplug_call_mount("add", argv[3]);
1223 } else if (!strcmp(argv[2], "unavailable")) {
1224 err = hotplug_call_mount("remove", argv[3]);
1225 } else {
1226 if (argc < 4)
1227 return -EINVAL;
1228
1229 err = mount_action(argv[2], argv[3], TYPE_AUTOFS);
1230 }
1231
1232 if (err) {
1233 ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv[2], err);
1234 }
1235
1236 return err;
1237 }
1238
1239 static int find_block_mtd(char *name, char *part, int plen)
1240 {
1241 FILE *fp = fopen("/proc/mtd", "r");
1242 static char line[256];
1243 char *index = NULL;
1244
1245 if(!fp)
1246 return -1;
1247
1248 while (!index && fgets(line, sizeof(line), fp)) {
1249 if (strstr(line, name)) {
1250 char *eol = strstr(line, ":");
1251
1252 if (!eol)
1253 continue;
1254
1255 *eol = '\0';
1256 index = &line[3];
1257 }
1258 }
1259
1260 fclose(fp);
1261
1262 if (!index)
1263 return -1;
1264
1265 snprintf(part, plen, "/dev/mtdblock%s", index);
1266
1267 return 0;
1268 }
1269
1270 #ifdef UBIFS_EXTROOT
1271 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
1272 {
1273 int dev = 0;
1274
1275 while (ubi_dev_present(libubi, dev))
1276 {
1277 struct ubi_dev_info dev_info;
1278 struct ubi_vol_info vol_info;
1279
1280 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
1281 continue;
1282 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
1283 continue;
1284
1285 *dev_num = dev_info.dev_num;
1286 *vol_id = vol_info.vol_id;
1287
1288 return 0;
1289 }
1290
1291 return -1;
1292 }
1293
1294 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
1295 {
1296 int dev_num;
1297 int vol_id;
1298 int err = -1;
1299
1300 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1301 if (!err)
1302 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
1303
1304 return err;
1305 }
1306
1307 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
1308 {
1309 int dev_num;
1310 int vol_id;
1311 int err = -1;
1312
1313 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1314 if (!err)
1315 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
1316
1317 return err;
1318 }
1319
1320 #else
1321
1322 static int find_root_dev(char *buf, int len)
1323 {
1324 DIR *d;
1325 dev_t root;
1326 struct stat s;
1327 struct dirent *e;
1328
1329 if (stat("/", &s))
1330 return -1;
1331
1332 if (!(d = opendir("/dev")))
1333 return -1;
1334
1335 root = s.st_dev;
1336
1337 while ((e = readdir(d)) != NULL) {
1338 snprintf(buf, len, "/dev/%s", e->d_name);
1339
1340 if (stat(buf, &s) || s.st_rdev != root)
1341 continue;
1342
1343 closedir(d);
1344 return 0;
1345 }
1346
1347 closedir(d);
1348 return -1;
1349 }
1350
1351 #endif
1352
1353 static int test_fs_support(const char *name)
1354 {
1355 char line[128], *p;
1356 int rv = -1;
1357 FILE *f;
1358
1359 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
1360 while (fgets(line, sizeof(line), f)) {
1361 p = strtok(line, "\t\n");
1362
1363 if (p && !strcmp(p, "nodev"))
1364 p = strtok(NULL, "\t\n");
1365
1366 if (p && !strcmp(p, name)) {
1367 rv = 0;
1368 break;
1369 }
1370 }
1371 fclose(f);
1372 }
1373
1374 return rv;
1375 }
1376
1377 static int check_extroot(char *path)
1378 {
1379 struct probe_info *pr = NULL;
1380 char devpath[32];
1381
1382 #ifdef UBIFS_EXTROOT
1383 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1384 int err = -1;
1385 libubi_t libubi;
1386
1387 libubi = libubi_open();
1388 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1389 libubi_close(libubi);
1390 if (err)
1391 return -1;
1392 }
1393 #else
1394 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1395 if (find_root_dev(devpath, sizeof(devpath))) {
1396 ULOG_ERR("extroot: unable to determine root device\n");
1397 return -1;
1398 }
1399 }
1400 #endif
1401
1402 list_for_each_entry(pr, &devices, list) {
1403 if (!strcmp(pr->dev, devpath)) {
1404 struct stat s;
1405 FILE *fp = NULL;
1406 char tag[64];
1407 char uuid[64] = { 0 };
1408
1409 snprintf(tag, sizeof(tag), "%s/etc", path);
1410 if (stat(tag, &s))
1411 mkdir_p(tag);
1412
1413 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1414 if (stat(tag, &s)) {
1415 fp = fopen(tag, "w+");
1416 if (!fp) {
1417 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1418 tag, errno);
1419 /* return 0 to continue boot regardless of error */
1420 return 0;
1421 }
1422 fputs(pr->uuid, fp);
1423 fclose(fp);
1424 return 0;
1425 }
1426
1427 fp = fopen(tag, "r");
1428 if (!fp) {
1429 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1430 tag, errno);
1431 return -1;
1432 }
1433
1434 if (!fgets(uuid, sizeof(uuid), fp))
1435 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1436 tag, errno);
1437 fclose(fp);
1438
1439 if (*uuid && !strcasecmp(uuid, pr->uuid))
1440 return 0;
1441
1442 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1443 pr->uuid, basename(path), uuid);
1444 return -1;
1445 }
1446 }
1447
1448 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1449 return -1;
1450 }
1451
1452 /*
1453 * Read info about extroot from UCI (using prefix) and mount it.
1454 */
1455 static int mount_extroot(char *cfg)
1456 {
1457 char overlay[] = "/tmp/extroot/overlay";
1458 char mnt[] = "/tmp/extroot/mnt";
1459 char *path = mnt;
1460 struct probe_info *pr;
1461 struct mount *m;
1462 int err = -1;
1463
1464 /* Load @cfg/etc/config/fstab */
1465 if (config_load(cfg))
1466 return -2;
1467
1468 /* See if there is extroot-specific mount config */
1469 m = find_block(NULL, NULL, NULL, "/");
1470 if (!m)
1471 m = find_block(NULL, NULL, NULL, "/overlay");
1472
1473 if (!m || !m->extroot)
1474 {
1475 ULOG_INFO("extroot: not configured\n");
1476 return -1;
1477 }
1478
1479 /* Find block device pointed by the mount config */
1480 pr = find_block_info(m->uuid, m->label, m->device);
1481
1482 if (!pr && delay_root){
1483 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1484 sleep(delay_root);
1485 make_devs();
1486 cache_load(1);
1487 pr = find_block_info(m->uuid, m->label, m->device);
1488 }
1489 if (pr) {
1490 if (strncmp(pr->type, "ext", 3) &&
1491 strncmp(pr->type, "f2fs", 4) &&
1492 strncmp(pr->type, "btrfs", 5) &&
1493 strncmp(pr->type, "ntfs", 4) &&
1494 strncmp(pr->type, "ubifs", 5)) {
1495 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr->type);
1496 return -1;
1497 }
1498
1499 if (test_fs_support(pr->type)) {
1500 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->type);
1501 return -1;
1502 }
1503
1504 if (m->overlay)
1505 path = overlay;
1506 mkdir_p(path);
1507
1508 if (check_fs)
1509 check_filesystem(pr);
1510
1511 err = mount(pr->dev, path, pr->type, m->flags,
1512 (m->options) ? (m->options) : (""));
1513
1514 if (err) {
1515 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1516 pr->dev, pr->type, path, errno);
1517 } else if (m->overlay) {
1518 err = check_extroot(path);
1519 if (err)
1520 umount(path);
1521 }
1522 } else {
1523 ULOG_ERR("extroot: cannot find device %s%s\n",
1524 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1525 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1526 }
1527
1528 return err;
1529 }
1530
1531 static int main_extroot(int argc, char **argv)
1532 {
1533 struct probe_info *pr;
1534 char blkdev_path[32] = { 0 };
1535 int err = -1;
1536 #ifdef UBIFS_EXTROOT
1537 libubi_t libubi;
1538 #endif
1539
1540 if (!getenv("PREINIT"))
1541 return -1;
1542
1543 if (argc != 2) {
1544 ULOG_ERR("Usage: block extroot\n");
1545 return -1;
1546 }
1547
1548 make_devs();
1549 cache_load(1);
1550
1551 /* enable LOG_INFO messages */
1552 ulog_threshold(LOG_INFO);
1553
1554 /*
1555 * Look for "rootfs_data". We will want to mount it and check for
1556 * extroot configuration.
1557 */
1558
1559 /* Start with looking for MTD partition */
1560 find_block_mtd("\"rootfs_data\"", blkdev_path, sizeof(blkdev_path));
1561 if (blkdev_path[0]) {
1562 pr = find_block_info(NULL, NULL, blkdev_path);
1563 if (pr && !strcmp(pr->type, "jffs2")) {
1564 char cfg[] = "/tmp/jffs_cfg";
1565
1566 /*
1567 * Mount MTD part and try extroot (using
1568 * /etc/config/fstab from that partition)
1569 */
1570 mkdir_p(cfg);
1571 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1572 err = mount_extroot(cfg);
1573 umount2(cfg, MNT_DETACH);
1574 }
1575 if (err < 0)
1576 rmdir("/tmp/overlay");
1577 rmdir(cfg);
1578 return err;
1579 }
1580 }
1581
1582 #ifdef UBIFS_EXTROOT
1583 /* ... but it also could be an UBI volume */
1584 memset(blkdev_path, 0, sizeof(blkdev_path));
1585 libubi = libubi_open();
1586 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1587 libubi_close(libubi);
1588 if (blkdev_path[0]) {
1589 char cfg[] = "/tmp/ubifs_cfg";
1590
1591 /* Mount volume and try extroot (using fstab from that vol) */
1592 mkdir_p(cfg);
1593 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1594 err = mount_extroot(cfg);
1595 umount2(cfg, MNT_DETACH);
1596 }
1597 if (err < 0)
1598 rmdir("/tmp/overlay");
1599 rmdir(cfg);
1600 return err;
1601 }
1602 #endif
1603
1604 return mount_extroot(NULL);
1605 }
1606
1607 static int main_mount(int argc, char **argv)
1608 {
1609 struct probe_info *pr;
1610
1611 if (config_load(NULL))
1612 return -1;
1613
1614 cache_load(1);
1615 list_for_each_entry(pr, &devices, list)
1616 mount_device(pr, TYPE_DEV);
1617
1618 handle_swapfiles(true);
1619
1620 return 0;
1621 }
1622
1623 static int main_umount(int argc, char **argv)
1624 {
1625 struct probe_info *pr;
1626 bool all = false;
1627
1628 if (config_load(NULL))
1629 return -1;
1630
1631 handle_swapfiles(false);
1632
1633 cache_load(0);
1634
1635 if (argc == 3)
1636 all = !strcmp(argv[2], "-a");
1637
1638 list_for_each_entry(pr, &devices, list) {
1639 struct mount *m;
1640
1641 if (!strcmp(pr->type, "swap"))
1642 continue;
1643
1644 m = find_block(pr->uuid, pr->label, basename(pr->dev), NULL);
1645 if (m && m->extroot)
1646 continue;
1647
1648 umount_device(pr->dev, TYPE_DEV, all);
1649 }
1650
1651 return 0;
1652 }
1653
1654 static int main_detect(int argc, char **argv)
1655 {
1656 struct probe_info *pr;
1657
1658 cache_load(0);
1659 printf("config 'global'\n");
1660 printf("\toption\tanon_swap\t'0'\n");
1661 printf("\toption\tanon_mount\t'0'\n");
1662 printf("\toption\tauto_swap\t'1'\n");
1663 printf("\toption\tauto_mount\t'1'\n");
1664 printf("\toption\tdelay_root\t'5'\n");
1665 printf("\toption\tcheck_fs\t'0'\n\n");
1666 list_for_each_entry(pr, &devices, list)
1667 print_block_uci(pr);
1668
1669 return 0;
1670 }
1671
1672 static int main_info(int argc, char **argv)
1673 {
1674 int i;
1675 struct probe_info *pr;
1676
1677 cache_load(1);
1678 if (argc == 2) {
1679 list_for_each_entry(pr, &devices, list)
1680 print_block_info(pr);
1681
1682 return 0;
1683 };
1684
1685 for (i = 2; i < argc; i++) {
1686 struct stat s;
1687
1688 if (stat(argv[i], &s)) {
1689 ULOG_ERR("failed to stat %s\n", argv[i]);
1690 continue;
1691 }
1692 if (!S_ISBLK(s.st_mode) && !(S_ISCHR(s.st_mode) && major(s.st_rdev) == 250)) {
1693 ULOG_ERR("%s is not a block device\n", argv[i]);
1694 continue;
1695 }
1696 pr = find_block_info(NULL, NULL, argv[i]);
1697 if (pr)
1698 print_block_info(pr);
1699 }
1700
1701 return 0;
1702 }
1703
1704 static int swapon_usage(void)
1705 {
1706 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1707 "\tStart swapping on [DEVICE]\n"
1708 " -a\tStart swapping on all swap devices\n"
1709 " -p pri\tSet priority of swap device\n"
1710 " -s\tShow summary\n");
1711 return -1;
1712 }
1713
1714 static int main_swapon(int argc, char **argv)
1715 {
1716 int ch;
1717 FILE *fp;
1718 char *lineptr;
1719 size_t s;
1720 struct probe_info *pr;
1721 int flags = 0;
1722 int pri;
1723 struct stat st;
1724 int err;
1725
1726 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1727 switch(ch) {
1728 case 's':
1729 fp = fopen("/proc/swaps", "r");
1730 lineptr = NULL;
1731
1732 if (!fp) {
1733 ULOG_ERR("failed to open /proc/swaps\n");
1734 return -1;
1735 }
1736 while (getline(&lineptr, &s, fp) > 0)
1737 printf("%s", lineptr);
1738 if (lineptr)
1739 free(lineptr);
1740 fclose(fp);
1741 return 0;
1742 case 'a':
1743 cache_load(0);
1744 list_for_each_entry(pr, &devices, list) {
1745 if (strcmp(pr->type, "swap"))
1746 continue;
1747 if (swapon(pr->dev, 0))
1748 ULOG_ERR("failed to swapon %s\n", pr->dev);
1749 }
1750 return 0;
1751 case 'p':
1752 pri = atoi(optarg);
1753 if (pri >= 0)
1754 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1755 break;
1756 default:
1757 return swapon_usage();
1758 }
1759 }
1760
1761 if (optind != (argc - 1))
1762 return swapon_usage();
1763
1764 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1765 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1766 return -1;
1767 }
1768 err = swapon(argv[optind], flags);
1769 if (err) {
1770 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1771 return err;
1772 }
1773
1774 return 0;
1775 }
1776
1777 static int main_swapoff(int argc, char **argv)
1778 {
1779 if (argc != 2) {
1780 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1781 "\tStop swapping on DEVICE\n"
1782 " -a\tStop swapping on all swap devices\n");
1783 return -1;
1784 }
1785
1786 if (!strcmp(argv[1], "-a")) {
1787 FILE *fp = fopen("/proc/swaps", "r");
1788 char line[256];
1789
1790 if (!fp) {
1791 ULOG_ERR("failed to open /proc/swaps\n");
1792 return -1;
1793 }
1794 if (fgets(line, sizeof(line), fp))
1795 while (fgets(line, sizeof(line), fp)) {
1796 char *end = strchr(line, ' ');
1797 int err;
1798
1799 if (!end)
1800 continue;
1801 *end = '\0';
1802 err = swapoff(line);
1803 if (err)
1804 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1805 }
1806 fclose(fp);
1807 } else {
1808 struct stat s;
1809 int err;
1810
1811 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1812 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1813 return -1;
1814 }
1815 err = swapoff(argv[1]);
1816 if (err) {
1817 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1818 return err;
1819 }
1820 }
1821
1822 return 0;
1823 }
1824
1825 int main(int argc, char **argv)
1826 {
1827 char *base = basename(*argv);
1828
1829 umask(0);
1830
1831 ulog_open(-1, -1, "block");
1832 ulog_threshold(LOG_NOTICE);
1833
1834 if (!strcmp(base, "swapon"))
1835 return main_swapon(argc, argv);
1836
1837 if (!strcmp(base, "swapoff"))
1838 return main_swapoff(argc, argv);
1839
1840 if ((argc > 1) && !strcmp(base, "block")) {
1841 if (!strcmp(argv[1], "info"))
1842 return main_info(argc, argv);
1843
1844 if (!strcmp(argv[1], "detect"))
1845 return main_detect(argc, argv);
1846
1847 if (!strcmp(argv[1], "hotplug"))
1848 return main_hotplug(argc, argv);
1849
1850 if (!strcmp(argv[1], "autofs"))
1851 return main_autofs(argc, argv);
1852
1853 if (!strcmp(argv[1], "extroot"))
1854 return main_extroot(argc, argv);
1855
1856 if (!strcmp(argv[1], "mount"))
1857 return main_mount(argc, argv);
1858
1859 if (!strcmp(argv[1], "umount"))
1860 return main_umount(argc, argv);
1861
1862 if (!strcmp(argv[1], "remount")) {
1863 int ret = main_umount(argc, argv);
1864
1865 if (!ret)
1866 ret = main_mount(argc, argv);
1867 return ret;
1868 }
1869 }
1870
1871 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");
1872
1873 return -1;
1874 }