2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
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
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.
27 #include <sys/types.h>
29 #include <sys/mount.h>
37 #include <libubox/ulog.h>
38 #include <libubox/list.h>
39 #include <libubox/vlist.h>
40 #include <libubox/blobmsg_json.h>
41 #include <libubox/avl-cmp.h>
46 #include "libubi/libubi.h"
55 struct vlist_node node;
71 static struct vlist_tree mounts;
72 static struct blob_buf b;
73 static LIST_HEAD(devices);
74 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
75 static unsigned int delay_root;
87 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
88 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
89 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
90 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
91 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
92 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
93 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
106 static const struct uci_blob_param_list config_attr_list = {
107 .n_params = __CFG_MAX,
108 .params = config_policy,
111 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
112 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
113 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
114 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
115 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
116 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
117 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
120 static const struct uci_blob_param_list mount_attr_list = {
121 .n_params = __MOUNT_MAX,
122 .params = mount_policy,
134 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
135 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
136 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
137 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
138 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
139 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
142 static const struct uci_blob_param_list swap_attr_list = {
143 .n_params = __SWAP_MAX,
144 .params = swap_policy,
152 static const struct mount_flag mount_flags[] = {
153 { "sync", MS_SYNCHRONOUS },
154 { "async", ~MS_SYNCHRONOUS },
155 { "dirsync", MS_DIRSYNC },
156 { "mand", MS_MANDLOCK },
157 { "nomand", ~MS_MANDLOCK },
158 { "atime", ~MS_NOATIME },
159 { "noatime", MS_NOATIME },
160 { "dev", ~MS_NODEV },
161 { "nodev", MS_NODEV },
162 { "diratime", ~MS_NODIRATIME },
163 { "nodiratime", MS_NODIRATIME },
164 { "exec", ~MS_NOEXEC },
165 { "noexec", MS_NOEXEC },
166 { "suid", ~MS_NOSUID },
167 { "nosuid", MS_NOSUID },
168 { "rw", ~MS_RDONLY },
170 { "relatime", MS_RELATIME },
171 { "norelatime", ~MS_RELATIME },
172 { "strictatime", MS_STRICTATIME },
173 { "acl", MS_POSIXACL },
174 { "noacl", ~MS_POSIXACL },
175 { "nouser_xattr", MS_NOUSER },
176 { "user_xattr", ~MS_NOUSER },
179 static char *blobmsg_get_strdup(struct blob_attr *attr)
184 return strdup(blobmsg_get_string(attr));
187 static char *blobmsg_get_basename(struct blob_attr *attr)
192 return strdup(basename(blobmsg_get_string(attr)));
195 static void parse_mount_options(struct mount *m, char *optstr)
199 char *p, *opts, *last;
204 if (!optstr || !*optstr)
207 m->options = opts = calloc(1, strlen(optstr) + 1);
220 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
221 if (!strcmp(last, mount_flags[i].name)) {
222 if (mount_flags[i].flag < 0)
223 m->flags &= (uint32_t)mount_flags[i].flag;
225 m->flags |= (uint32_t)mount_flags[i].flag;
232 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
241 static int mount_add(struct uci_section *s)
243 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
246 blob_buf_init(&b, 0);
247 uci_to_blob(&b, s, &mount_attr_list);
248 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
250 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
253 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
256 m = malloc(sizeof(struct mount));
257 m->type = TYPE_MOUNT;
258 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
259 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
260 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
261 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
263 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
265 m->overlay = m->extroot = 0;
266 if (m->target && !strcmp(m->target, "/"))
268 if (m->target && !strcmp(m->target, "/overlay"))
269 m->extroot = m->overlay = 1;
271 if (m->target && *m->target != '/') {
272 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
273 s->e.name, m->target);
279 vlist_add(&mounts, &m->node, m->uuid);
281 vlist_add(&mounts, &m->node, m->label);
283 vlist_add(&mounts, &m->node, m->device);
288 static int swap_add(struct uci_section *s)
290 struct blob_attr *tb[__SWAP_MAX] = { 0 };
293 blob_buf_init(&b, 0);
294 uci_to_blob(&b, s, &swap_attr_list);
295 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
297 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
300 m = malloc(sizeof(struct mount));
301 memset(m, 0, sizeof(struct mount));
303 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
304 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
305 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
307 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
309 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
311 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
312 /* store complete swap path */
314 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
317 vlist_add(&mounts, &m->node, m->uuid);
319 vlist_add(&mounts, &m->node, m->label);
321 vlist_add(&mounts, &m->node, m->device);
327 static int global_add(struct uci_section *s)
329 struct blob_attr *tb[__CFG_MAX] = { 0 };
331 blob_buf_init(&b, 0);
332 uci_to_blob(&b, s, &config_attr_list);
333 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
335 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
337 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
340 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
342 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
345 if (tb[CFG_DELAY_ROOT])
346 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
348 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
354 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
358 vlist_for_each_element(&mounts, m, node) {
359 if (m->type != TYPE_SWAP)
361 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
363 if (label && m->label && !strcmp(m->label, label))
365 if (device && m->device && !strcmp(m->device, device))
372 static struct mount* find_block(const char *uuid, const char *label, const char *device,
377 vlist_for_each_element(&mounts, m, node) {
378 if (m->type != TYPE_MOUNT)
380 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
382 if (m->label && label && !strcmp(m->label, label))
384 if (m->target && target && !strcmp(m->target, target))
386 if (m->device && device && !strcmp(m->device, device))
393 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
394 struct vlist_node *node_old)
398 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
400 char *file = basename(path);
401 char *dir = dirname(path);
403 struct uci_package *pkg;
405 uci_set_confdir(ctx, dir);
406 ULOG_INFO("attempting to load %s/%s\n", dir, file);
408 if (uci_load(ctx, file, &pkg)) {
409 uci_get_errorstr(ctx, &err, file);
410 ULOG_ERR("unable to load configuration (%s)\n", err);
419 static int config_load(char *cfg)
421 struct uci_context *ctx = uci_alloc_context();
422 struct uci_package *pkg = NULL;
423 struct uci_element *e;
426 vlist_init(&mounts, avl_strcmp, mounts_update);
429 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
430 pkg = config_try_load(ctx, path);
433 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
434 pkg = config_try_load(ctx, path);
439 snprintf(path, sizeof(path), "/etc/config/fstab");
440 pkg = config_try_load(ctx, path);
444 ULOG_ERR("no usable configuration\n");
448 vlist_update(&mounts);
449 uci_foreach_element(&pkg->sections, e) {
450 struct uci_section *s = uci_to_section(e);
452 if (!strcmp(s->type, "mount"))
454 if (!strcmp(s->type, "swap"))
456 if (!strcmp(s->type, "global"))
459 vlist_flush(&mounts);
464 static struct probe_info* _probe_path(char *path)
466 struct probe_info *pr;
469 /* skip ubi device if ubiblock device is present */
470 if (path[5] == 'u' && path[6] == 'b' && path[7] == 'i' &&
471 path[8] >= '0' && path[8] <= '9' ) {
472 snprintf(tmppath, sizeof(tmppath), "/dev/ubiblock%s", path + 8);
473 list_for_each_entry(pr, &devices, list)
474 if (!strcasecmp(pr->dev, tmppath))
478 return probe_path(path);
481 static int _cache_load(const char *path)
483 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
487 if (glob(path, gl_flags, NULL, &gl) < 0)
490 for (j = 0; j < gl.gl_pathc; j++) {
491 struct probe_info *pr = _probe_path(gl.gl_pathv[j]);
493 list_add_tail(&pr->list, &devices);
501 static void cache_load(int mtd)
504 _cache_load("/dev/mtdblock*");
505 _cache_load("/dev/ubiblock*");
506 _cache_load("/dev/ubi[0-9]*");
508 _cache_load("/dev/loop*");
509 _cache_load("/dev/mmcblk*");
510 _cache_load("/dev/sd*");
511 _cache_load("/dev/hd*");
512 _cache_load("/dev/md*");
513 _cache_load("/dev/vd*");
514 _cache_load("/dev/mapper/*");
518 static int print_block_uci(struct probe_info *pr)
520 if (!strcmp(pr->type, "swap")) {
521 printf("config 'swap'\n");
523 printf("config 'mount'\n");
524 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
527 printf("\toption\tuuid\t'%s'\n", pr->uuid);
529 printf("\toption\tdevice\t'%s'\n", pr->dev);
530 printf("\toption\tenabled\t'0'\n\n");
535 static struct probe_info* find_block_info(char *uuid, char *label, char *path)
537 struct probe_info *pr = NULL;
540 list_for_each_entry(pr, &devices, list)
541 if (pr->uuid && !strcasecmp(pr->uuid, uuid))
545 list_for_each_entry(pr, &devices, list)
546 if (pr->label && !strcmp(pr->label, label))
550 list_for_each_entry(pr, &devices, list)
551 if (pr->dev && !strcmp(basename(pr->dev), basename(path)))
557 static char* find_mount_point(char *block)
559 FILE *fp = fopen("/proc/self/mountinfo", "r");
560 static char line[256];
561 int len = strlen(block);
562 char *point = NULL, *pos, *tmp, *cpoint, *devname;
565 unsigned int minor, major;
570 rstat = stat(block, &s);
572 while (fgets(line, sizeof(line), fp)) {
573 pos = strchr(line, ' ');
577 pos = strchr(pos + 1, ' ');
582 pos = strchr(pos, ':');
589 pos = strchr(pos, ' ');
595 pos = strchr(pos + 1, ' ');
600 pos = strchr(pos, ' ');
606 pos = strchr(pos + 1, ' ');
610 pos = strchr(pos + 1, ' ');
614 pos = strchr(pos + 1, ' ');
619 pos = strchr(pos, ' ');
625 if (!strncmp(block, devname, len)) {
626 point = strdup(cpoint);
633 if (!S_ISBLK(s.st_mode))
636 if (major == major(s.st_rdev) &&
637 minor == minor(s.st_rdev)) {
638 point = strdup(cpoint);
648 static int print_block_info(struct probe_info *pr)
652 mp = find_mount_point(pr->dev);
653 printf("%s:", pr->dev);
655 printf(" UUID=\"%s\"", pr->uuid);
658 printf(" LABEL=\"%s\"", pr->label);
661 printf(" NAME=\"%s\"", pr->name);
664 printf(" VERSION=\"%s\"", pr->version);
667 printf(" MOUNT=\"%s\"", mp);
671 printf(" TYPE=\"%s\"\n", pr->type);
676 static void mkdir_p(char *dir)
678 char *l = strrchr(dir, '/');
688 static void check_filesystem(struct probe_info *pr)
692 const char *e2fsck = "/usr/sbin/e2fsck";
693 const char *dosfsck = "/usr/sbin/dosfsck";
696 /* UBIFS does not need stuff like fsck */
697 if (!strncmp(pr->type, "ubifs", 5))
700 if (!strncmp(pr->type, "vfat", 4)) {
702 } else if (!strncmp(pr->type, "ext", 3)) {
705 ULOG_ERR("check_filesystem: %s is not supported\n", pr->type);
709 if (stat(ckfs, &statbuf) < 0) {
710 ULOG_ERR("check_filesystem: %s not found\n", ckfs);
716 execl(ckfs, ckfs, "-p", pr->dev, NULL);
718 } else if (pid > 0) {
721 waitpid(pid, &status, 0);
722 if (WEXITSTATUS(status))
723 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs, WEXITSTATUS(status));
727 static void handle_swapfiles(bool on)
731 struct probe_info *pr;
733 vlist_for_each_element(&mounts, m, node)
735 if (m->type != TYPE_SWAP || !m->target)
738 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
741 pr = _probe_path(m->target);
746 if (!strcmp(pr->type, "swap")) {
748 swapon(pr->dev, m->prio);
757 static int mount_device(struct probe_info *pr, int hotplug)
766 device = basename(pr->dev);
768 if (!strcmp(pr->type, "swap")) {
769 if (hotplug && !auto_swap)
771 m = find_swap(pr->uuid, pr->label, device);
773 swapon(pr->dev, (m) ? (m->prio) : (0));
778 if (hotplug && !auto_mount)
781 mp = find_mount_point(pr->dev);
783 ULOG_ERR("%s is already mounted on %s\n", pr->dev, mp);
788 m = find_block(pr->uuid, pr->label, device, NULL);
793 char *target = m->target;
798 snprintf(_target, sizeof(_target), "/mnt/%s", device);
804 check_filesystem(pr);
806 err = mount(pr->dev, target, pr->type, m->flags,
807 (m->options) ? (m->options) : (""));
809 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
810 pr->dev, pr->type, target, err, strerror(err));
812 handle_swapfiles(true);
820 snprintf(target, sizeof(target), "/mnt/%s", device);
824 check_filesystem(pr);
826 err = mount(pr->dev, target, pr->type, 0, "");
828 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
829 pr->dev, pr->type, target, err, strerror(err));
831 handle_swapfiles(true);
838 static int umount_device(struct probe_info *pr)
841 char *device = basename(pr->dev);
848 if (!strcmp(pr->type, "swap"))
851 mp = find_mount_point(pr->dev);
855 m = find_block(pr->uuid, pr->label, device, NULL);
859 err = umount2(mp, MNT_DETACH);
861 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
862 pr->dev, mp, err, strerror(err));
864 ULOG_INFO("unmounted %s (%s)\n",
871 static int main_hotplug(int argc, char **argv)
874 char *action, *device, *mount_point;
876 action = getenv("ACTION");
877 device = getenv("DEVNAME");
879 if (!action || !device)
881 snprintf(path, sizeof(path), "/dev/%s", device);
883 if (!strcmp(action, "remove")) {
885 mount_point = find_mount_point(path);
887 err = umount2(mount_point, MNT_DETACH);
890 ULOG_ERR("umount of %s failed (%d) - %s\n",
891 mount_point, err, strerror(err));
895 } else if (strcmp(action, "add")) {
896 ULOG_ERR("Unkown action %s\n", action);
901 if (config_load(NULL))
905 return mount_device(find_block_info(NULL, NULL, path), 1);
908 static int find_block_mtd(char *name, char *part, int plen)
910 FILE *fp = fopen("/proc/mtd", "r");
911 static char line[256];
917 while (!index && fgets(line, sizeof(line), fp)) {
918 if (strstr(line, name)) {
919 char *eol = strstr(line, ":");
934 snprintf(part, plen, "/dev/mtdblock%s", index);
940 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
944 while (ubi_dev_present(libubi, dev))
946 struct ubi_dev_info dev_info;
947 struct ubi_vol_info vol_info;
949 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
951 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
954 *dev_num = dev_info.dev_num;
955 *vol_id = vol_info.vol_id;
963 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
969 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
971 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
976 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
982 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
984 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
991 static int find_root_dev(char *buf, int len)
1001 if (!(d = opendir("/dev")))
1006 while ((e = readdir(d)) != NULL) {
1007 snprintf(buf, len, "/dev/%s", e->d_name);
1009 if (stat(buf, &s) || s.st_rdev != root)
1022 static int test_fs_support(const char *name)
1028 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
1029 while (fgets(line, sizeof(line), f)) {
1030 p = strtok(line, "\t\n");
1032 if (p && !strcmp(p, "nodev"))
1033 p = strtok(NULL, "\t\n");
1035 if (p && !strcmp(p, name)) {
1046 static int check_extroot(char *path)
1048 struct probe_info *pr = NULL;
1051 #ifdef UBIFS_EXTROOT
1052 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1056 libubi = libubi_open();
1057 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1058 libubi_close(libubi);
1063 if (find_block_mtd("\"rootfs\"", devpath, sizeof(devpath))) {
1064 if (find_root_dev(devpath, sizeof(devpath))) {
1065 ULOG_ERR("extroot: unable to determine root device\n");
1071 list_for_each_entry(pr, &devices, list) {
1072 if (!strcmp(pr->dev, devpath)) {
1076 char uuid[64] = { 0 };
1078 snprintf(tag, sizeof(tag), "%s/etc", path);
1082 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1083 if (stat(tag, &s)) {
1084 fp = fopen(tag, "w+");
1086 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1087 tag, errno, strerror(errno));
1088 /* return 0 to continue boot regardless of error */
1091 fputs(pr->uuid, fp);
1096 fp = fopen(tag, "r");
1098 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1099 tag, errno, strerror(errno));
1103 if (!fgets(uuid, sizeof(uuid), fp))
1104 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1105 tag, errno, strerror(errno));
1108 if (*uuid && !strcasecmp(uuid, pr->uuid))
1111 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1112 pr->uuid, basename(path), uuid);
1117 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1122 * Read info about extroot from UCI (using prefix) and mount it.
1124 static int mount_extroot(char *cfg)
1126 char overlay[] = "/tmp/extroot/overlay";
1127 char mnt[] = "/tmp/extroot/mnt";
1129 struct probe_info *pr;
1133 /* Load @cfg/etc/config/fstab */
1134 if (config_load(cfg))
1137 /* See if there is extroot-specific mount config */
1138 m = find_block(NULL, NULL, NULL, "/");
1140 m = find_block(NULL, NULL, NULL, "/overlay");
1142 if (!m || !m->extroot)
1144 ULOG_INFO("extroot: not configured\n");
1148 /* Find block device pointed by the mount config */
1149 pr = find_block_info(m->uuid, m->label, m->device);
1151 if (!pr && delay_root){
1152 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1156 pr = find_block_info(m->uuid, m->label, m->device);
1159 if (strncmp(pr->type, "ext", 3) &&
1160 strncmp(pr->type, "ubifs", 5)) {
1161 ULOG_ERR("extroot: unsupported filesystem %s, try ext4\n", pr->type);
1165 if (test_fs_support(pr->type)) {
1166 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->type);
1175 check_filesystem(pr);
1177 err = mount(pr->dev, path, pr->type, m->flags,
1178 (m->options) ? (m->options) : (""));
1181 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1182 pr->dev, pr->type, path, err, strerror(err));
1183 } else if (m->overlay) {
1184 err = check_extroot(path);
1189 ULOG_ERR("extroot: cannot find device %s%s\n",
1190 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1191 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1197 static int main_extroot(int argc, char **argv)
1199 struct probe_info *pr;
1200 char blkdev_path[32] = { 0 };
1202 #ifdef UBIFS_EXTROOT
1206 if (!getenv("PREINIT"))
1210 ULOG_ERR("Usage: block extroot\n");
1217 /* enable LOG_INFO messages */
1218 ulog_threshold(LOG_INFO);
1221 * Look for "rootfs_data". We will want to mount it and check for
1222 * extroot configuration.
1225 /* Start with looking for MTD partition */
1226 find_block_mtd("\"rootfs_data\"", blkdev_path, sizeof(blkdev_path));
1227 if (blkdev_path[0]) {
1228 pr = find_block_info(NULL, NULL, blkdev_path);
1229 if (pr && !strcmp(pr->type, "jffs2")) {
1230 char cfg[] = "/tmp/jffs_cfg";
1233 * Mount MTD part and try extroot (using
1234 * /etc/config/fstab from that partition)
1237 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1238 err = mount_extroot(cfg);
1239 umount2(cfg, MNT_DETACH);
1242 rmdir("/tmp/overlay");
1248 #ifdef UBIFS_EXTROOT
1249 /* ... but it also could be an UBI volume */
1250 memset(blkdev_path, 0, sizeof(blkdev_path));
1251 libubi = libubi_open();
1252 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1253 libubi_close(libubi);
1254 if (blkdev_path[0]) {
1255 char cfg[] = "/tmp/ubifs_cfg";
1257 /* Mount volume and try extroot (using fstab from that vol) */
1259 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1260 err = mount_extroot(cfg);
1261 umount2(cfg, MNT_DETACH);
1264 rmdir("/tmp/overlay");
1270 return mount_extroot(NULL);
1273 static int main_mount(int argc, char **argv)
1275 struct probe_info *pr;
1277 if (config_load(NULL))
1281 list_for_each_entry(pr, &devices, list)
1282 mount_device(pr, 0);
1284 handle_swapfiles(true);
1289 static int main_umount(int argc, char **argv)
1291 struct probe_info *pr;
1293 if (config_load(NULL))
1296 handle_swapfiles(false);
1299 list_for_each_entry(pr, &devices, list)
1305 static int main_detect(int argc, char **argv)
1307 struct probe_info *pr;
1310 printf("config 'global'\n");
1311 printf("\toption\tanon_swap\t'0'\n");
1312 printf("\toption\tanon_mount\t'0'\n");
1313 printf("\toption\tauto_swap\t'1'\n");
1314 printf("\toption\tauto_mount\t'1'\n");
1315 printf("\toption\tdelay_root\t'5'\n");
1316 printf("\toption\tcheck_fs\t'0'\n\n");
1317 list_for_each_entry(pr, &devices, list)
1318 print_block_uci(pr);
1323 static int main_info(int argc, char **argv)
1326 struct probe_info *pr;
1330 list_for_each_entry(pr, &devices, list)
1331 print_block_info(pr);
1336 for (i = 2; i < argc; i++) {
1339 if (stat(argv[i], &s)) {
1340 ULOG_ERR("failed to stat %s\n", argv[i]);
1343 if (!S_ISBLK(s.st_mode) && !(S_ISCHR(s.st_mode) && major(s.st_rdev) == 250)) {
1344 ULOG_ERR("%s is not a block device\n", argv[i]);
1347 pr = find_block_info(NULL, NULL, argv[i]);
1349 print_block_info(pr);
1355 static int swapon_usage(void)
1357 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1358 "\tStart swapping on [DEVICE]\n"
1359 " -a\tStart swapping on all swap devices\n"
1360 " -p pri\tSet priority of swap device\n"
1361 " -s\tShow summary\n");
1365 static int main_swapon(int argc, char **argv)
1371 struct probe_info *pr;
1377 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1380 fp = fopen("/proc/swaps", "r");
1384 ULOG_ERR("failed to open /proc/swaps\n");
1387 while (getline(&lineptr, &s, fp) > 0)
1388 printf("%s", lineptr);
1395 list_for_each_entry(pr, &devices, list) {
1396 if (strcmp(pr->type, "swap"))
1398 if (swapon(pr->dev, 0))
1399 ULOG_ERR("failed to swapon %s\n", pr->dev);
1405 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1408 return swapon_usage();
1413 if (optind != (argc - 1))
1414 return swapon_usage();
1416 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1417 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1420 err = swapon(argv[optind], flags);
1422 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1429 static int main_swapoff(int argc, char **argv)
1432 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1433 "\tStop swapping on DEVICE\n"
1434 " -a\tStop swapping on all swap devices\n");
1438 if (!strcmp(argv[1], "-a")) {
1439 FILE *fp = fopen("/proc/swaps", "r");
1443 ULOG_ERR("failed to open /proc/swaps\n");
1446 if (fgets(line, sizeof(line), fp))
1447 while (fgets(line, sizeof(line), fp)) {
1448 char *end = strchr(line, ' ');
1454 err = swapoff(line);
1456 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1463 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1464 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1467 err = swapoff(argv[1]);
1469 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1477 int main(int argc, char **argv)
1479 char *base = basename(*argv);
1483 ulog_open(-1, -1, "block");
1484 ulog_threshold(LOG_NOTICE);
1486 if (!strcmp(base, "swapon"))
1487 return main_swapon(argc, argv);
1489 if (!strcmp(base, "swapoff"))
1490 return main_swapoff(argc, argv);
1492 if ((argc > 1) && !strcmp(base, "block")) {
1493 if (!strcmp(argv[1], "info"))
1494 return main_info(argc, argv);
1496 if (!strcmp(argv[1], "detect"))
1497 return main_detect(argc, argv);
1499 if (!strcmp(argv[1], "hotplug"))
1500 return main_hotplug(argc, argv);
1502 if (!strcmp(argv[1], "extroot"))
1503 return main_extroot(argc, argv);
1505 if (!strcmp(argv[1], "mount"))
1506 return main_mount(argc, argv);
1508 if (!strcmp(argv[1], "umount"))
1509 return main_umount(argc, argv);
1511 if (!strcmp(argv[1], "remount")) {
1512 int ret = main_umount(argc, argv);
1515 ret = main_mount(argc, argv);
1520 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");