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