block: umount: skip / unless -a is given
[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 int print_block_uci(struct probe_info *pr)
540 {
541 if (!strcmp(pr->type, "swap")) {
542 printf("config 'swap'\n");
543 } else {
544 printf("config 'mount'\n");
545 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
546 }
547 if (pr->uuid)
548 printf("\toption\tuuid\t'%s'\n", pr->uuid);
549 else
550 printf("\toption\tdevice\t'%s'\n", pr->dev);
551 printf("\toption\tenabled\t'0'\n\n");
552
553 return 0;
554 }
555
556 static struct probe_info* find_block_info(char *uuid, char *label, char *path)
557 {
558 struct probe_info *pr = NULL;
559
560 if (uuid)
561 list_for_each_entry(pr, &devices, list)
562 if (pr->uuid && !strcasecmp(pr->uuid, uuid))
563 return pr;
564
565 if (label)
566 list_for_each_entry(pr, &devices, list)
567 if (pr->label && !strcmp(pr->label, label))
568 return pr;
569
570 if (path)
571 list_for_each_entry(pr, &devices, list)
572 if (pr->dev && !strcmp(basename(pr->dev), basename(path)))
573 return pr;
574
575 return NULL;
576 }
577
578 static char* find_mount_point(char *block)
579 {
580 FILE *fp = fopen("/proc/self/mountinfo", "r");
581 static char line[256];
582 char *point = NULL, *pos, *tmp, *cpoint, *devname;
583 struct stat s;
584 int rstat;
585 unsigned int minor, major;
586
587 if (!fp)
588 return NULL;
589
590 rstat = stat(block, &s);
591
592 while (fgets(line, sizeof(line), fp)) {
593 pos = strchr(line, ' ');
594 if (!pos)
595 continue;
596
597 pos = strchr(pos + 1, ' ');
598 if (!pos)
599 continue;
600
601 tmp = ++pos;
602 pos = strchr(pos, ':');
603 if (!pos)
604 continue;
605
606 *pos = '\0';
607 major = atoi(tmp);
608 tmp = ++pos;
609 pos = strchr(pos, ' ');
610 if (!pos)
611 continue;
612
613 *pos = '\0';
614 minor = atoi(tmp);
615 pos = strchr(pos + 1, ' ');
616 if (!pos)
617 continue;
618 tmp = ++pos;
619
620 pos = strchr(pos, ' ');
621 if (!pos)
622 continue;
623 *pos = '\0';
624 cpoint = tmp;
625
626 pos = strchr(pos + 1, ' ');
627 if (!pos)
628 continue;
629
630 pos = strchr(pos + 1, ' ');
631 if (!pos)
632 continue;
633
634 pos = strchr(pos + 1, ' ');
635 if (!pos)
636 continue;
637
638 tmp = ++pos;
639 pos = strchr(pos, ' ');
640 if (!pos)
641 continue;
642
643 *pos = '\0';
644 devname = tmp;
645 if (!strcmp(block, devname)) {
646 point = strdup(cpoint);
647 break;
648 }
649
650 if (rstat)
651 continue;
652
653 if (!S_ISBLK(s.st_mode))
654 continue;
655
656 if (major == major(s.st_rdev) &&
657 minor == minor(s.st_rdev)) {
658 point = strdup(cpoint);
659 break;
660 }
661 }
662
663 fclose(fp);
664
665 return point;
666 }
667
668 static int print_block_info(struct probe_info *pr)
669 {
670 static char *mp;
671
672 mp = find_mount_point(pr->dev);
673 printf("%s:", pr->dev);
674 if (pr->uuid)
675 printf(" UUID=\"%s\"", pr->uuid);
676
677 if (pr->label)
678 printf(" LABEL=\"%s\"", pr->label);
679
680 if (pr->version)
681 printf(" VERSION=\"%s\"", pr->version);
682
683 if (mp) {
684 printf(" MOUNT=\"%s\"", mp);
685 free(mp);
686 }
687
688 printf(" TYPE=\"%s\"\n", pr->type);
689
690 return 0;
691 }
692
693 static void mkdir_p(char *dir)
694 {
695 char *l = strrchr(dir, '/');
696
697 if (l) {
698 *l = '\0';
699 mkdir_p(dir);
700 *l = '/';
701 mkdir(dir, 0755);
702 }
703 }
704
705 static void check_filesystem(struct probe_info *pr)
706 {
707 pid_t pid;
708 struct stat statbuf;
709 const char *e2fsck = "/usr/sbin/e2fsck";
710 const char *f2fsck = "/usr/sbin/fsck.f2fs";
711 const char *fatfsck = "/usr/sbin/fsck.fat";
712 const char *btrfsck = "/usr/bin/btrfsck";
713 const char *ntfsck = "/usr/bin/ntfsfix";
714 const char *ckfs;
715
716 /* UBIFS does not need stuff like fsck */
717 if (!strncmp(pr->type, "ubifs", 5))
718 return;
719
720 if (!strncmp(pr->type, "vfat", 4)) {
721 ckfs = fatfsck;
722 } else if (!strncmp(pr->type, "f2fs", 4)) {
723 ckfs = f2fsck;
724 } else if (!strncmp(pr->type, "ext", 3)) {
725 ckfs = e2fsck;
726 } else if (!strncmp(pr->type, "btrfs", 5)) {
727 ckfs = btrfsck;
728 } else if (!strncmp(pr->type, "ntfs", 4)) {
729 ckfs = ntfsck;
730 } else {
731 ULOG_ERR("check_filesystem: %s is not supported\n", pr->type);
732 return;
733 }
734
735 if (stat(ckfs, &statbuf) < 0) {
736 ULOG_ERR("check_filesystem: %s not found\n", ckfs);
737 return;
738 }
739
740 pid = fork();
741 if (!pid) {
742 if(!strncmp(pr->type, "f2fs", 4)) {
743 execl(ckfs, ckfs, "-f", pr->dev, NULL);
744 exit(EXIT_FAILURE);
745 } else if(!strncmp(pr->type, "btrfs", 5)) {
746 execl(ckfs, ckfs, "--repair", pr->dev, NULL);
747 exit(EXIT_FAILURE);
748 } else if(!strncmp(pr->type, "ntfs", 4)) {
749 execl(ckfs, ckfs, "-b", pr->dev, NULL);
750 exit(EXIT_FAILURE);
751 } else {
752 execl(ckfs, ckfs, "-p", pr->dev, NULL);
753 exit(EXIT_FAILURE);
754 }
755 } else if (pid > 0) {
756 int status;
757
758 waitpid(pid, &status, 0);
759 if (WIFEXITED(status) && WEXITSTATUS(status))
760 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
761 if (WIFSIGNALED(status))
762 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs, strsignal(WTERMSIG(status)));
763 }
764 }
765
766 static void handle_swapfiles(bool on)
767 {
768 struct stat s;
769 struct mount *m;
770 struct probe_info *pr;
771
772 vlist_for_each_element(&mounts, m, node)
773 {
774 if (m->type != TYPE_SWAP || !m->target)
775 continue;
776
777 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
778 continue;
779
780 pr = _probe_path(m->target);
781
782 if (!pr)
783 continue;
784
785 if (!strcmp(pr->type, "swap")) {
786 if (on)
787 swapon(pr->dev, m->prio);
788 else
789 swapoff(pr->dev);
790 }
791
792 free(pr);
793 }
794 }
795
796 static void to_devnull(int fd)
797 {
798 int devnull = open("/dev/null", fd ? O_WRONLY : O_RDONLY);
799
800 if (devnull >= 0)
801 dup2(devnull, fd);
802
803 if (devnull > STDERR_FILENO)
804 close(devnull);
805 }
806
807 static int exec_mount(const char *source, const char *target,
808 const char *fstype, const char *options)
809 {
810 pid_t pid;
811 struct stat s;
812 FILE *mount_fd;
813 int err, status, pfds[2];
814 char errmsg[128], cmd[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
815
816 snprintf(cmd, sizeof(cmd), "/sbin/mount.%s", fstype);
817
818 if (stat(cmd, &s) < 0 || !S_ISREG(s.st_mode) || !(s.st_mode & S_IXUSR)) {
819 ULOG_ERR("No \"mount.%s\" utility available\n", fstype);
820 return -1;
821 }
822
823 if (pipe(pfds) < 0)
824 return -1;
825
826 fcntl(pfds[0], F_SETFD, fcntl(pfds[0], F_GETFD) | FD_CLOEXEC);
827 fcntl(pfds[1], F_SETFD, fcntl(pfds[1], F_GETFD) | FD_CLOEXEC);
828
829 pid = vfork();
830
831 switch (pid) {
832 case -1:
833 close(pfds[0]);
834 close(pfds[1]);
835
836 return -1;
837
838 case 0:
839 to_devnull(STDIN_FILENO);
840 to_devnull(STDOUT_FILENO);
841
842 dup2(pfds[1], STDERR_FILENO);
843 close(pfds[0]);
844 close(pfds[1]);
845
846 if (options && *options)
847 execl(cmd, cmd, "-o", options, source, target, NULL);
848 else
849 execl(cmd, cmd, source, target, NULL);
850
851 return -1;
852
853 default:
854 close(pfds[1]);
855
856 mount_fd = fdopen(pfds[0], "r");
857
858 while (fgets(errmsg, sizeof(errmsg), mount_fd))
859 ULOG_ERR("mount.%s: %s", fstype, errmsg);
860
861 fclose(mount_fd);
862
863 err = waitpid(pid, &status, 0);
864
865 if (err != -1) {
866 if (status != 0) {
867 ULOG_ERR("mount.%s: failed with status %d\n", fstype, status);
868 errno = EINVAL;
869 err = -1;
870 } else {
871 errno = 0;
872 err = 0;
873 }
874 }
875
876 break;
877 }
878
879 return err;
880 }
881
882 static int hotplug_call_mount(const char *action, const char *device)
883 {
884 pid_t pid;
885 int err = 0;
886
887 pid = fork();
888 if (!pid) {
889 char * const argv[] = { "hotplug-call", "mount", NULL };
890
891 setenv("ACTION", action, 1);
892 setenv("DEVICE", device, 1);
893
894 execv("/sbin/hotplug-call", argv);
895 exit(-1);
896 } else if (pid > 0) {
897 int status;
898
899 pid = waitpid(pid, &status, 0);
900 if (pid <= 0 || !WIFEXITED(status) || WEXITSTATUS(status)) {
901 err = -ENOEXEC;
902 ULOG_ERR("hotplug-call call failed\n");
903 }
904 } else {
905 err = -errno;
906 }
907
908 return err;
909 }
910
911 static int handle_mount(const char *source, const char *target,
912 const char *fstype, struct mount *m)
913 {
914 int i, err;
915 size_t mount_opts_len;
916 char *mount_opts = NULL, *ptr;
917
918 err = mount(source, target, fstype, m ? m->flags : 0,
919 (m && m->options) ? m->options : "");
920
921 /* Requested file system type is not available in kernel,
922 attempt to call mount helper. */
923 if (err == -1 && errno == ENODEV) {
924 if (m) {
925 /* Convert mount flags back into string representation,
926 first calculate needed length of string buffer... */
927 mount_opts_len = 1 + (m->options ? strlen(m->options) : 0);
928
929 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
930 if ((mount_flags[i].flag > 0) &&
931 (mount_flags[i].flag < INT_MAX) &&
932 (m->flags & (uint32_t)mount_flags[i].flag))
933 mount_opts_len += strlen(mount_flags[i].name) + 1;
934
935 /* ... then now allocate and fill it ... */
936 ptr = mount_opts = calloc(1, mount_opts_len);
937
938 if (!ptr) {
939 errno = ENOMEM;
940 return -1;
941 }
942
943 if (m->options)
944 ptr += sprintf(ptr, "%s,", m->options);
945
946 for (i = 0; i < ARRAY_SIZE(mount_flags); i++)
947 if ((mount_flags[i].flag > 0) &&
948 (mount_flags[i].flag < INT_MAX) &&
949 (m->flags & (uint32_t)mount_flags[i].flag))
950 ptr += sprintf(ptr, "%s,", mount_flags[i].name);
951
952 mount_opts[mount_opts_len - 1] = 0;
953 }
954
955 /* ... and now finally invoke the external mount program */
956 err = exec_mount(source, target, fstype, mount_opts);
957 }
958
959 return err;
960 }
961
962 static int blockd_notify(char *device, struct mount *m, struct probe_info *pr)
963 {
964 struct ubus_context *ctx = ubus_connect(NULL);
965 uint32_t id;
966 int err;
967
968 if (!ctx)
969 return -ENXIO;
970
971 if (!ubus_lookup_id(ctx, "block", &id)) {
972 struct blob_buf buf = { 0 };
973 char *d = strrchr(device, '/');
974
975 if (d)
976 d++;
977 else
978 d = device;
979
980 blob_buf_init(&buf, 0);
981
982 if (m) {
983
984 blobmsg_add_string(&buf, "device", d);
985 if (m->uuid)
986 blobmsg_add_string(&buf, "uuid", m->uuid);
987 if (m->label)
988 blobmsg_add_string(&buf, "label", m->label);
989 if (m->target)
990 blobmsg_add_string(&buf, "target", m->target);
991 if (m->options)
992 blobmsg_add_string(&buf, "options", m->options);
993 if (m->autofs)
994 blobmsg_add_u32(&buf, "autofs", m->autofs);
995 if (pr->type)
996 blobmsg_add_string(&buf, "type", pr->type);
997 if (pr->version)
998 blobmsg_add_string(&buf, "version", pr->version);
999 } else if (pr) {
1000 blobmsg_add_string(&buf, "device", d);
1001 if (pr->uuid)
1002 blobmsg_add_string(&buf, "uuid", pr->uuid);
1003 if (pr->label)
1004 blobmsg_add_string(&buf, "label", pr->label);
1005 if (pr->type)
1006 blobmsg_add_string(&buf, "type", pr->type);
1007 if (pr->version)
1008 blobmsg_add_string(&buf, "version", pr->version);
1009 blobmsg_add_u32(&buf, "anon", 1);
1010 } else {
1011 blobmsg_add_string(&buf, "device", d);
1012 blobmsg_add_u32(&buf, "remove", 1);
1013 }
1014
1015 err = ubus_invoke(ctx, id, "hotplug", buf.head, NULL, NULL, 3000);
1016 } else {
1017 err = -ENOENT;
1018 }
1019
1020 ubus_free(ctx);
1021
1022 return err;
1023 }
1024
1025 static int mount_device(struct probe_info *pr, int type)
1026 {
1027 struct mount *m;
1028 char _target[32];
1029 char *target;
1030 char *device;
1031 char *mp;
1032 int err;
1033
1034 if (!pr)
1035 return -1;
1036
1037 device = basename(pr->dev);
1038
1039 if (!strcmp(pr->type, "swap")) {
1040 if ((type == TYPE_HOTPLUG) && !auto_swap)
1041 return -1;
1042 m = find_swap(pr->uuid, pr->label, device);
1043 if (m || anon_swap)
1044 swapon(pr->dev, (m) ? (m->prio) : (0));
1045
1046 return 0;
1047 }
1048
1049 mp = find_mount_point(pr->dev);
1050 if (mp && (type != TYPE_HOTPLUG)) {
1051 ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
1052 free(mp);
1053 return -1;
1054 }
1055
1056 m = find_block(pr->uuid, pr->label, device, NULL);
1057 if (m && m->extroot)
1058 return -1;
1059
1060 if (type == TYPE_HOTPLUG)
1061 blockd_notify(device, m, pr);
1062
1063 /* Check if device should be mounted & set the target directory */
1064 if (m) {
1065 switch (type) {
1066 case TYPE_HOTPLUG:
1067 if (m->autofs)
1068 return 0;
1069 if (!auto_mount)
1070 return -1;
1071 break;
1072 case TYPE_AUTOFS:
1073 if (!m->autofs)
1074 return -1;
1075 break;
1076 case TYPE_DEV:
1077 if (m->autofs)
1078 return -1;
1079 break;
1080 }
1081
1082 if (m->autofs) {
1083 snprintf(_target, sizeof(_target), "/tmp/run/blockd/%s", device);
1084 target = _target;
1085 } else if (m->target) {
1086 target = m->target;
1087 } else {
1088 snprintf(_target, sizeof(_target), "/mnt/%s", device);
1089 target = _target;
1090 }
1091 } else if (anon_mount) {
1092 snprintf(_target, sizeof(_target), "/mnt/%s", device);
1093 target = _target;
1094 } else {
1095 /* No reason to mount this device */
1096 return 0;
1097 }
1098
1099 /* Mount the device */
1100
1101 if (check_fs)
1102 check_filesystem(pr);
1103
1104 mkdir_p(target);
1105
1106 err = handle_mount(pr->dev, target, pr->type, m);
1107 if (err) {
1108 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1109 pr->dev, pr->type, target, errno);
1110 return err;
1111 }
1112
1113 handle_swapfiles(true);
1114
1115 if (type != TYPE_AUTOFS)
1116 hotplug_call_mount("add", device);
1117
1118 return 0;
1119 }
1120
1121 static int umount_device(char *path, int type, bool all)
1122 {
1123 char *mp;
1124 int err;
1125
1126 mp = find_mount_point(path);
1127 if (!mp)
1128 return -1;
1129 if (!strcmp(mp, "/") && !all)
1130 return 0;
1131
1132 if (type != TYPE_AUTOFS)
1133 hotplug_call_mount("remove", basename(path));
1134
1135 err = umount2(mp, MNT_DETACH);
1136 if (err) {
1137 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path, mp,
1138 errno);
1139 } else {
1140 ULOG_INFO("unmounted %s (%s)\n", path, mp);
1141 rmdir(mp);
1142 }
1143
1144 free(mp);
1145 return err;
1146 }
1147
1148 static int mount_action(char *action, char *device, int type)
1149 {
1150 char path[32];
1151
1152 if (!action || !device)
1153 return -1;
1154 snprintf(path, sizeof(path), "/dev/%s", device);
1155
1156 if (!strcmp(action, "remove")) {
1157 if (type == TYPE_HOTPLUG)
1158 blockd_notify(device, NULL, NULL);
1159
1160 umount_device(path, type, true);
1161
1162 return 0;
1163 } else if (strcmp(action, "add")) {
1164 ULOG_ERR("Unkown action %s\n", action);
1165
1166 return -1;
1167 }
1168
1169 if (config_load(NULL))
1170 return -1;
1171 cache_load(0);
1172
1173 return mount_device(find_block_info(NULL, NULL, path), type);
1174 }
1175
1176 static int main_hotplug(int argc, char **argv)
1177 {
1178 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG);
1179 }
1180
1181 static int main_autofs(int argc, char **argv)
1182 {
1183 int err = 0;
1184
1185 if (argc < 3)
1186 return -1;
1187
1188 if (!strcmp(argv[2], "start")) {
1189 struct probe_info *pr;
1190
1191 if (config_load(NULL))
1192 return -1;
1193
1194 cache_load(0);
1195 list_for_each_entry(pr, &devices, list) {
1196 struct mount *m;
1197
1198 if (!strcmp(pr->type, "swap"))
1199 continue;
1200
1201 m = find_block(pr->uuid, pr->label, NULL, NULL);
1202 if (m && m->extroot)
1203 continue;
1204
1205 blockd_notify(pr->dev, m, pr);
1206 }
1207 } else if (!strcmp(argv[2], "available")) {
1208 err = hotplug_call_mount("add", argv[3]);
1209 } else if (!strcmp(argv[2], "unavailable")) {
1210 err = hotplug_call_mount("remove", argv[3]);
1211 } else {
1212 if (argc < 4)
1213 return -EINVAL;
1214
1215 err = mount_action(argv[2], argv[3], TYPE_AUTOFS);
1216 }
1217
1218 if (err) {
1219 ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv[2], err);
1220 }
1221
1222 return err;
1223 }
1224
1225 static int find_block_mtd(char *name, char *part, int plen)
1226 {
1227 FILE *fp = fopen("/proc/mtd", "r");
1228 static char line[256];
1229 char *index = NULL;
1230
1231 if(!fp)
1232 return -1;
1233
1234 while (!index && fgets(line, sizeof(line), fp)) {
1235 if (strstr(line, name)) {
1236 char *eol = strstr(line, ":");
1237
1238 if (!eol)
1239 continue;
1240
1241 *eol = '\0';
1242 index = &line[3];
1243 }
1244 }
1245
1246 fclose(fp);
1247
1248 if (!index)
1249 return -1;
1250
1251 snprintf(part, plen, "/dev/mtdblock%s", index);
1252
1253 return 0;
1254 }
1255
1256 #ifdef UBIFS_EXTROOT
1257 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
1258 {
1259 int dev = 0;
1260
1261 while (ubi_dev_present(libubi, dev))
1262 {
1263 struct ubi_dev_info dev_info;
1264 struct ubi_vol_info vol_info;
1265
1266 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
1267 continue;
1268 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
1269 continue;
1270
1271 *dev_num = dev_info.dev_num;
1272 *vol_id = vol_info.vol_id;
1273
1274 return 0;
1275 }
1276
1277 return -1;
1278 }
1279
1280 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
1281 {
1282 int dev_num;
1283 int vol_id;
1284 int err = -1;
1285
1286 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1287 if (!err)
1288 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
1289
1290 return err;
1291 }
1292
1293 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
1294 {
1295 int dev_num;
1296 int vol_id;
1297 int err = -1;
1298
1299 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
1300 if (!err)
1301 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
1302
1303 return err;
1304 }
1305
1306 #else
1307
1308 static int find_root_dev(char *buf, int len)
1309 {
1310 DIR *d;
1311 dev_t root;
1312 struct stat s;
1313 struct dirent *e;
1314
1315 if (stat("/", &s))
1316 return -1;
1317
1318 if (!(d = opendir("/dev")))
1319 return -1;
1320
1321 root = s.st_dev;
1322
1323 while ((e = readdir(d)) != NULL) {
1324 snprintf(buf, len, "/dev/%s", e->d_name);
1325
1326 if (stat(buf, &s) || s.st_rdev != root)
1327 continue;
1328
1329 closedir(d);
1330 return 0;
1331 }
1332
1333 closedir(d);
1334 return -1;
1335 }
1336
1337 #endif
1338
1339 static int test_fs_support(const char *name)
1340 {
1341 char line[128], *p;
1342 int rv = -1;
1343 FILE *f;
1344
1345 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
1346 while (fgets(line, sizeof(line), f)) {
1347 p = strtok(line, "\t\n");
1348
1349 if (p && !strcmp(p, "nodev"))
1350 p = strtok(NULL, "\t\n");
1351
1352 if (p && !strcmp(p, name)) {
1353 rv = 0;
1354 break;
1355 }
1356 }
1357 fclose(f);
1358 }
1359
1360 return rv;
1361 }
1362
1363 static int check_extroot(char *path)
1364 {
1365 struct probe_info *pr = NULL;
1366 char devpath[32];
1367
1368 #ifdef UBIFS_EXTROOT
1369 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1370 int err = -1;
1371 libubi_t libubi;
1372
1373 libubi = libubi_open();
1374 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1375 libubi_close(libubi);
1376 if (err)
1377 return -1;
1378 }
1379 #else
1380 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1381 if (find_root_dev(devpath, sizeof(devpath))) {
1382 ULOG_ERR("extroot: unable to determine root device\n");
1383 return -1;
1384 }
1385 }
1386 #endif
1387
1388 list_for_each_entry(pr, &devices, list) {
1389 if (!strcmp(pr->dev, devpath)) {
1390 struct stat s;
1391 FILE *fp = NULL;
1392 char tag[64];
1393 char uuid[64] = { 0 };
1394
1395 snprintf(tag, sizeof(tag), "%s/etc", path);
1396 if (stat(tag, &s))
1397 mkdir_p(tag);
1398
1399 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1400 if (stat(tag, &s)) {
1401 fp = fopen(tag, "w+");
1402 if (!fp) {
1403 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1404 tag, errno);
1405 /* return 0 to continue boot regardless of error */
1406 return 0;
1407 }
1408 fputs(pr->uuid, fp);
1409 fclose(fp);
1410 return 0;
1411 }
1412
1413 fp = fopen(tag, "r");
1414 if (!fp) {
1415 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1416 tag, errno);
1417 return -1;
1418 }
1419
1420 if (!fgets(uuid, sizeof(uuid), fp))
1421 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1422 tag, errno);
1423 fclose(fp);
1424
1425 if (*uuid && !strcasecmp(uuid, pr->uuid))
1426 return 0;
1427
1428 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1429 pr->uuid, basename(path), uuid);
1430 return -1;
1431 }
1432 }
1433
1434 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1435 return -1;
1436 }
1437
1438 /*
1439 * Read info about extroot from UCI (using prefix) and mount it.
1440 */
1441 static int mount_extroot(char *cfg)
1442 {
1443 char overlay[] = "/tmp/extroot/overlay";
1444 char mnt[] = "/tmp/extroot/mnt";
1445 char *path = mnt;
1446 struct probe_info *pr;
1447 struct mount *m;
1448 int err = -1;
1449
1450 /* Load @cfg/etc/config/fstab */
1451 if (config_load(cfg))
1452 return -2;
1453
1454 /* See if there is extroot-specific mount config */
1455 m = find_block(NULL, NULL, NULL, "/");
1456 if (!m)
1457 m = find_block(NULL, NULL, NULL, "/overlay");
1458
1459 if (!m || !m->extroot)
1460 {
1461 ULOG_INFO("extroot: not configured\n");
1462 return -1;
1463 }
1464
1465 /* Find block device pointed by the mount config */
1466 pr = find_block_info(m->uuid, m->label, m->device);
1467
1468 if (!pr && delay_root){
1469 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1470 sleep(delay_root);
1471 make_devs();
1472 cache_load(0);
1473 pr = find_block_info(m->uuid, m->label, m->device);
1474 }
1475 if (pr) {
1476 if (strncmp(pr->type, "ext", 3) &&
1477 strncmp(pr->type, "f2fs", 4) &&
1478 strncmp(pr->type, "btrfs", 5) &&
1479 strncmp(pr->type, "ntfs", 4) &&
1480 strncmp(pr->type, "ubifs", 5)) {
1481 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr->type);
1482 return -1;
1483 }
1484
1485 if (test_fs_support(pr->type)) {
1486 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->type);
1487 return -1;
1488 }
1489
1490 if (m->overlay)
1491 path = overlay;
1492 mkdir_p(path);
1493
1494 if (check_fs)
1495 check_filesystem(pr);
1496
1497 err = mount(pr->dev, path, pr->type, m->flags,
1498 (m->options) ? (m->options) : (""));
1499
1500 if (err) {
1501 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1502 pr->dev, pr->type, path, errno);
1503 } else if (m->overlay) {
1504 err = check_extroot(path);
1505 if (err)
1506 umount(path);
1507 }
1508 } else {
1509 ULOG_ERR("extroot: cannot find device %s%s\n",
1510 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1511 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1512 }
1513
1514 return err;
1515 }
1516
1517 static int main_extroot(int argc, char **argv)
1518 {
1519 struct probe_info *pr;
1520 char blkdev_path[32] = { 0 };
1521 int err = -1;
1522 #ifdef UBIFS_EXTROOT
1523 libubi_t libubi;
1524 #endif
1525
1526 if (!getenv("PREINIT"))
1527 return -1;
1528
1529 if (argc != 2) {
1530 ULOG_ERR("Usage: block extroot\n");
1531 return -1;
1532 }
1533
1534 make_devs();
1535 cache_load(1);
1536
1537 /* enable LOG_INFO messages */
1538 ulog_threshold(LOG_INFO);
1539
1540 /*
1541 * Look for "rootfs_data". We will want to mount it and check for
1542 * extroot configuration.
1543 */
1544
1545 /* Start with looking for MTD partition */
1546 find_block_mtd("\"rootfs_data\"", blkdev_path, sizeof(blkdev_path));
1547 if (blkdev_path[0]) {
1548 pr = find_block_info(NULL, NULL, blkdev_path);
1549 if (pr && !strcmp(pr->type, "jffs2")) {
1550 char cfg[] = "/tmp/jffs_cfg";
1551
1552 /*
1553 * Mount MTD part and try extroot (using
1554 * /etc/config/fstab from that partition)
1555 */
1556 mkdir_p(cfg);
1557 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1558 err = mount_extroot(cfg);
1559 umount2(cfg, MNT_DETACH);
1560 }
1561 if (err < 0)
1562 rmdir("/tmp/overlay");
1563 rmdir(cfg);
1564 return err;
1565 }
1566 }
1567
1568 #ifdef UBIFS_EXTROOT
1569 /* ... but it also could be an UBI volume */
1570 memset(blkdev_path, 0, sizeof(blkdev_path));
1571 libubi = libubi_open();
1572 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1573 libubi_close(libubi);
1574 if (blkdev_path[0]) {
1575 char cfg[] = "/tmp/ubifs_cfg";
1576
1577 /* Mount volume and try extroot (using fstab from that vol) */
1578 mkdir_p(cfg);
1579 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1580 err = mount_extroot(cfg);
1581 umount2(cfg, MNT_DETACH);
1582 }
1583 if (err < 0)
1584 rmdir("/tmp/overlay");
1585 rmdir(cfg);
1586 return err;
1587 }
1588 #endif
1589
1590 return mount_extroot(NULL);
1591 }
1592
1593 static int main_mount(int argc, char **argv)
1594 {
1595 struct probe_info *pr;
1596
1597 if (config_load(NULL))
1598 return -1;
1599
1600 cache_load(1);
1601 list_for_each_entry(pr, &devices, list)
1602 mount_device(pr, TYPE_DEV);
1603
1604 handle_swapfiles(true);
1605
1606 return 0;
1607 }
1608
1609 static int main_umount(int argc, char **argv)
1610 {
1611 struct probe_info *pr;
1612 bool all = false;
1613
1614 if (config_load(NULL))
1615 return -1;
1616
1617 handle_swapfiles(false);
1618
1619 cache_load(0);
1620
1621 if (argc == 3)
1622 all = !strcmp(argv[2], "-a");
1623
1624 list_for_each_entry(pr, &devices, list) {
1625 struct mount *m;
1626
1627 if (!strcmp(pr->type, "swap"))
1628 continue;
1629
1630 m = find_block(pr->uuid, pr->label, basename(pr->dev), NULL);
1631 if (m && m->extroot)
1632 continue;
1633
1634 umount_device(pr->dev, TYPE_DEV, all);
1635 }
1636
1637 return 0;
1638 }
1639
1640 static int main_detect(int argc, char **argv)
1641 {
1642 struct probe_info *pr;
1643
1644 cache_load(0);
1645 printf("config 'global'\n");
1646 printf("\toption\tanon_swap\t'0'\n");
1647 printf("\toption\tanon_mount\t'0'\n");
1648 printf("\toption\tauto_swap\t'1'\n");
1649 printf("\toption\tauto_mount\t'1'\n");
1650 printf("\toption\tdelay_root\t'5'\n");
1651 printf("\toption\tcheck_fs\t'0'\n\n");
1652 list_for_each_entry(pr, &devices, list)
1653 print_block_uci(pr);
1654
1655 return 0;
1656 }
1657
1658 static int main_info(int argc, char **argv)
1659 {
1660 int i;
1661 struct probe_info *pr;
1662
1663 cache_load(1);
1664 if (argc == 2) {
1665 list_for_each_entry(pr, &devices, list)
1666 print_block_info(pr);
1667
1668 return 0;
1669 };
1670
1671 for (i = 2; i < argc; i++) {
1672 struct stat s;
1673
1674 if (stat(argv[i], &s)) {
1675 ULOG_ERR("failed to stat %s\n", argv[i]);
1676 continue;
1677 }
1678 if (!S_ISBLK(s.st_mode) && !(S_ISCHR(s.st_mode) && major(s.st_rdev) == 250)) {
1679 ULOG_ERR("%s is not a block device\n", argv[i]);
1680 continue;
1681 }
1682 pr = find_block_info(NULL, NULL, argv[i]);
1683 if (pr)
1684 print_block_info(pr);
1685 }
1686
1687 return 0;
1688 }
1689
1690 static int swapon_usage(void)
1691 {
1692 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1693 "\tStart swapping on [DEVICE]\n"
1694 " -a\tStart swapping on all swap devices\n"
1695 " -p pri\tSet priority of swap device\n"
1696 " -s\tShow summary\n");
1697 return -1;
1698 }
1699
1700 static int main_swapon(int argc, char **argv)
1701 {
1702 int ch;
1703 FILE *fp;
1704 char *lineptr;
1705 size_t s;
1706 struct probe_info *pr;
1707 int flags = 0;
1708 int pri;
1709 struct stat st;
1710 int err;
1711
1712 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1713 switch(ch) {
1714 case 's':
1715 fp = fopen("/proc/swaps", "r");
1716 lineptr = NULL;
1717
1718 if (!fp) {
1719 ULOG_ERR("failed to open /proc/swaps\n");
1720 return -1;
1721 }
1722 while (getline(&lineptr, &s, fp) > 0)
1723 printf("%s", lineptr);
1724 if (lineptr)
1725 free(lineptr);
1726 fclose(fp);
1727 return 0;
1728 case 'a':
1729 cache_load(0);
1730 list_for_each_entry(pr, &devices, list) {
1731 if (strcmp(pr->type, "swap"))
1732 continue;
1733 if (swapon(pr->dev, 0))
1734 ULOG_ERR("failed to swapon %s\n", pr->dev);
1735 }
1736 return 0;
1737 case 'p':
1738 pri = atoi(optarg);
1739 if (pri >= 0)
1740 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1741 break;
1742 default:
1743 return swapon_usage();
1744 }
1745 }
1746
1747 if (optind != (argc - 1))
1748 return swapon_usage();
1749
1750 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1751 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1752 return -1;
1753 }
1754 err = swapon(argv[optind], flags);
1755 if (err) {
1756 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1757 return err;
1758 }
1759
1760 return 0;
1761 }
1762
1763 static int main_swapoff(int argc, char **argv)
1764 {
1765 if (argc != 2) {
1766 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1767 "\tStop swapping on DEVICE\n"
1768 " -a\tStop swapping on all swap devices\n");
1769 return -1;
1770 }
1771
1772 if (!strcmp(argv[1], "-a")) {
1773 FILE *fp = fopen("/proc/swaps", "r");
1774 char line[256];
1775
1776 if (!fp) {
1777 ULOG_ERR("failed to open /proc/swaps\n");
1778 return -1;
1779 }
1780 if (fgets(line, sizeof(line), fp))
1781 while (fgets(line, sizeof(line), fp)) {
1782 char *end = strchr(line, ' ');
1783 int err;
1784
1785 if (!end)
1786 continue;
1787 *end = '\0';
1788 err = swapoff(line);
1789 if (err)
1790 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1791 }
1792 fclose(fp);
1793 } else {
1794 struct stat s;
1795 int err;
1796
1797 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1798 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1799 return -1;
1800 }
1801 err = swapoff(argv[1]);
1802 if (err) {
1803 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1804 return err;
1805 }
1806 }
1807
1808 return 0;
1809 }
1810
1811 int main(int argc, char **argv)
1812 {
1813 char *base = basename(*argv);
1814
1815 umask(0);
1816
1817 ulog_open(-1, -1, "block");
1818 ulog_threshold(LOG_NOTICE);
1819
1820 if (!strcmp(base, "swapon"))
1821 return main_swapon(argc, argv);
1822
1823 if (!strcmp(base, "swapoff"))
1824 return main_swapoff(argc, argv);
1825
1826 if ((argc > 1) && !strcmp(base, "block")) {
1827 if (!strcmp(argv[1], "info"))
1828 return main_info(argc, argv);
1829
1830 if (!strcmp(argv[1], "detect"))
1831 return main_detect(argc, argv);
1832
1833 if (!strcmp(argv[1], "hotplug"))
1834 return main_hotplug(argc, argv);
1835
1836 if (!strcmp(argv[1], "autofs"))
1837 return main_autofs(argc, argv);
1838
1839 if (!strcmp(argv[1], "extroot"))
1840 return main_extroot(argc, argv);
1841
1842 if (!strcmp(argv[1], "mount"))
1843 return main_mount(argc, argv);
1844
1845 if (!strcmp(argv[1], "umount"))
1846 return main_umount(argc, argv);
1847
1848 if (!strcmp(argv[1], "remount")) {
1849 int ret = main_umount(argc, argv);
1850
1851 if (!ret)
1852 ret = main_mount(argc, argv);
1853 return ret;
1854 }
1855 }
1856
1857 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");
1858
1859 return -1;
1860 }