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>
35 #include <libubox/ulog.h>
36 #include <libubox/list.h>
37 #include <libubox/vlist.h>
38 #include <libubox/blobmsg_json.h>
39 #include <libubox/avl-cmp.h>
41 #include "libblkid-tiny/libblkid-tiny.h"
44 #include "libubi/libubi.h"
53 struct vlist_node node;
69 static struct vlist_tree mounts;
70 static struct blob_buf b;
71 static LIST_HEAD(devices);
72 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
73 static unsigned int delay_root;
85 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
86 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
87 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
88 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
89 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
90 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
91 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
104 static const struct uci_blob_param_list config_attr_list = {
105 .n_params = __CFG_MAX,
106 .params = config_policy,
109 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
110 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
111 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
112 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
113 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
114 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
115 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
118 static const struct uci_blob_param_list mount_attr_list = {
119 .n_params = __MOUNT_MAX,
120 .params = mount_policy,
132 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
133 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
134 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
135 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
136 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
137 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
140 static const struct uci_blob_param_list swap_attr_list = {
141 .n_params = __SWAP_MAX,
142 .params = swap_policy,
151 # define MS_DIRSYNC (1 << 7)
155 # define MS_RELATIME (1 << 21)
158 #ifndef MS_STRICTATIME
159 # define MS_STRICTATIME (1 << 24)
162 static const struct mount_flag mount_flags[] = {
163 { "sync", MS_SYNCHRONOUS },
164 { "async", ~MS_SYNCHRONOUS },
165 { "dirsync", MS_DIRSYNC },
166 { "mand", MS_MANDLOCK },
167 { "nomand", ~MS_MANDLOCK },
168 { "atime", ~MS_NOATIME },
169 { "noatime", MS_NOATIME },
170 { "dev", ~MS_NODEV },
171 { "nodev", MS_NODEV },
172 { "diratime", ~MS_NODIRATIME },
173 { "nodiratime", MS_NODIRATIME },
174 { "exec", ~MS_NOEXEC },
175 { "noexec", MS_NOEXEC },
176 { "suid", ~MS_NOSUID },
177 { "nosuid", MS_NOSUID },
178 { "rw", ~MS_RDONLY },
180 { "relatime", MS_RELATIME },
181 { "norelatime", ~MS_RELATIME },
182 { "strictatime", MS_STRICTATIME },
185 static char *blobmsg_get_strdup(struct blob_attr *attr)
190 return strdup(blobmsg_get_string(attr));
193 static char *blobmsg_get_basename(struct blob_attr *attr)
198 return strdup(basename(blobmsg_get_string(attr)));
201 static void parse_mount_options(struct mount *m, char *optstr)
205 char *p, *opts, *last;
210 if (!optstr || !*optstr)
213 m->options = opts = calloc(1, strlen(optstr) + 1);
226 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
227 if (!strcmp(last, mount_flags[i].name)) {
228 if (mount_flags[i].flag < 0)
229 m->flags &= (uint32_t)mount_flags[i].flag;
231 m->flags |= (uint32_t)mount_flags[i].flag;
238 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
247 static int mount_add(struct uci_section *s)
249 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
252 blob_buf_init(&b, 0);
253 uci_to_blob(&b, s, &mount_attr_list);
254 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
256 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
259 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
262 m = malloc(sizeof(struct mount));
263 m->type = TYPE_MOUNT;
264 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
265 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
266 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
267 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
269 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
271 m->overlay = m->extroot = 0;
272 if (m->target && !strcmp(m->target, "/"))
274 if (m->target && !strcmp(m->target, "/overlay"))
275 m->extroot = m->overlay = 1;
278 vlist_add(&mounts, &m->node, m->uuid);
280 vlist_add(&mounts, &m->node, m->label);
282 vlist_add(&mounts, &m->node, m->device);
287 static int swap_add(struct uci_section *s)
289 struct blob_attr *tb[__SWAP_MAX] = { 0 };
292 blob_buf_init(&b, 0);
293 uci_to_blob(&b, s, &swap_attr_list);
294 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
296 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
299 m = malloc(sizeof(struct mount));
300 memset(m, 0, sizeof(struct mount));
302 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
303 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
304 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
306 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
308 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
310 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
311 /* store complete swap path */
313 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
316 vlist_add(&mounts, &m->node, m->uuid);
318 vlist_add(&mounts, &m->node, m->label);
320 vlist_add(&mounts, &m->node, m->device);
326 static int global_add(struct uci_section *s)
328 struct blob_attr *tb[__CFG_MAX] = { 0 };
330 blob_buf_init(&b, 0);
331 uci_to_blob(&b, s, &config_attr_list);
332 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
334 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
336 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
339 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
341 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
344 if (tb[CFG_DELAY_ROOT])
345 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
347 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
353 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
357 vlist_for_each_element(&mounts, m, node) {
358 if (m->type != TYPE_SWAP)
360 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
362 if (label && m->label && !strcmp(m->label, label))
364 if (device && m->device && !strcmp(m->device, device))
371 static struct mount* find_block(const char *uuid, const char *label, const char *device,
376 vlist_for_each_element(&mounts, m, node) {
377 if (m->type != TYPE_MOUNT)
379 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
381 if (m->label && label && !strcmp(m->label, label))
383 if (m->target && target && !strcmp(m->target, target))
385 if (m->device && device && !strcmp(m->device, device))
392 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
393 struct vlist_node *node_old)
397 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
399 char *file = basename(path);
400 char *dir = dirname(path);
402 struct uci_package *pkg;
404 uci_set_confdir(ctx, dir);
405 ULOG_INFO("attempting to load %s/%s\n", dir, file);
407 if (uci_load(ctx, file, &pkg)) {
408 uci_get_errorstr(ctx, &err, file);
409 ULOG_ERR("unable to load configuration (%s)\n", err);
418 static int config_load(char *cfg)
420 struct uci_context *ctx = uci_alloc_context();
421 struct uci_package *pkg = NULL;
422 struct uci_element *e;
425 vlist_init(&mounts, avl_strcmp, mounts_update);
428 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
429 pkg = config_try_load(ctx, path);
432 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
433 pkg = config_try_load(ctx, path);
438 snprintf(path, sizeof(path), "/etc/config/fstab");
439 pkg = config_try_load(ctx, path);
443 ULOG_ERR("no usable configuration\n");
447 vlist_update(&mounts);
448 uci_foreach_element(&pkg->sections, e) {
449 struct uci_section *s = uci_to_section(e);
451 if (!strcmp(s->type, "mount"))
453 if (!strcmp(s->type, "swap"))
455 if (!strcmp(s->type, "global"))
458 vlist_flush(&mounts);
463 static struct blkid_struct_probe* _probe_path(char *path)
465 struct blkid_struct_probe *pr;
467 pr = malloc(sizeof(*pr));
472 memset(pr, 0, sizeof(*pr));
473 probe_block(path, pr);
475 if (pr->err || !pr->id) {
483 static int _cache_load(const char *path)
485 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
489 if (glob(path, gl_flags, NULL, &gl) < 0)
492 for (j = 0; j < gl.gl_pathc; j++) {
493 struct blkid_struct_probe *pr = _probe_path(gl.gl_pathv[j]);
495 list_add_tail(&pr->list, &devices);
503 static void cache_load(int mtd)
506 _cache_load("/dev/mtdblock*");
507 _cache_load("/dev/ubiblock*");
508 _cache_load("/dev/ubi?*_?*");
510 _cache_load("/dev/mmcblk*");
511 _cache_load("/dev/sd*");
512 _cache_load("/dev/hd*");
513 _cache_load("/dev/md*");
514 _cache_load("/dev/vd*");
515 _cache_load("/dev/mapper/*");
518 static int print_block_info(struct blkid_struct_probe *pr)
520 printf("%s:", pr->dev);
522 printf(" UUID=\"%s\"", pr->uuid);
525 printf(" LABEL=\"%s\"", pr->label);
528 printf(" NAME=\"%s\"", pr->name);
531 printf(" VERSION=\"%s\"", pr->version);
533 printf(" TYPE=\"%s\"\n", pr->id->name);
538 static int print_block_uci(struct blkid_struct_probe *pr)
540 if (!strcmp(pr->id->name, "swap")) {
541 printf("config 'swap'\n");
543 printf("config 'mount'\n");
544 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
547 printf("\toption\tuuid\t'%s'\n", pr->uuid);
549 printf("\toption\tdevice\t'%s'\n", pr->dev);
550 printf("\toption\tenabled\t'0'\n\n");
555 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
557 struct blkid_struct_probe *pr = NULL;
560 list_for_each_entry(pr, &devices, list)
561 if (!strcasecmp(pr->uuid, uuid))
565 list_for_each_entry(pr, &devices, list)
566 if (!strcmp(pr->label, label))
570 list_for_each_entry(pr, &devices, list)
571 if (!strcmp(basename(pr->dev), basename(path)))
577 static char* find_mount_point(char *block)
579 FILE *fp = fopen("/proc/mounts", "r");
580 static char line[256];
581 int len = strlen(block);
587 while (fgets(line, sizeof(line), fp)) {
588 if (!strncmp(line, block, len)) {
589 char *p = &line[len + 1];
590 char *t = strstr(p, " ");
607 static void mkdir_p(char *dir)
609 char *l = strrchr(dir, '/');
619 static void check_filesystem(struct blkid_struct_probe *pr)
623 char *e2fsck = "/usr/sbin/e2fsck";
625 /* UBIFS does not need stuff like fsck */
626 if (!strncmp(pr->id->name, "ubifs", 5))
629 if (strncmp(pr->id->name, "ext", 3)) {
630 ULOG_ERR("check_filesystem: %s is not supported\n", pr->id->name);
634 if (stat(e2fsck, &statbuf) < 0) {
635 ULOG_ERR("check_filesystem: %s not found\n", e2fsck);
641 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
643 } else if (pid > 0) {
646 waitpid(pid, &status, 0);
647 if (WEXITSTATUS(status))
648 ULOG_ERR("check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
652 static void handle_swapfiles(bool on)
656 struct blkid_struct_probe *pr;
658 vlist_for_each_element(&mounts, m, node)
660 if (m->type != TYPE_SWAP || !m->target)
663 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
666 pr = _probe_path(m->target);
671 if (!strcmp(pr->id->name, "swap")) {
673 swapon(pr->dev, m->prio);
682 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
690 device = basename(pr->dev);
692 if (!strcmp(pr->id->name, "swap")) {
693 if (hotplug && !auto_swap)
695 m = find_swap(pr->uuid, pr->label, device);
697 swapon(pr->dev, (m) ? (m->prio) : (0));
702 if (hotplug && !auto_mount)
705 if (find_mount_point(pr->dev)) {
706 ULOG_ERR("%s is already mounted\n", pr->dev);
710 m = find_block(pr->uuid, pr->label, device, NULL);
715 char *target = m->target;
720 snprintf(_target, sizeof(_target), "/mnt/%s", device);
726 check_filesystem(pr);
728 err = mount(pr->dev, target, pr->id->name, m->flags,
729 (m->options) ? (m->options) : (""));
731 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
732 pr->dev, pr->id->name, target, err, strerror(err));
734 handle_swapfiles(true);
739 char target[] = "/mnt/mmcblk123";
742 snprintf(target, sizeof(target), "/mnt/%s", device);
746 check_filesystem(pr);
748 err = mount(pr->dev, target, pr->id->name, 0, "");
750 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
751 pr->dev, pr->id->name, target, err, strerror(err));
753 handle_swapfiles(true);
760 static int umount_device(struct blkid_struct_probe *pr)
763 char *device = basename(pr->dev);
770 if (!strcmp(pr->id->name, "swap"))
773 mp = find_mount_point(pr->dev);
777 m = find_block(pr->uuid, pr->label, device, NULL);
781 err = umount2(mp, MNT_DETACH);
783 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
784 pr->dev, mp, err, strerror(err));
786 ULOG_INFO("unmounted %s (%s)\n",
792 static int main_hotplug(int argc, char **argv)
795 char *action, *device, *mount_point;
797 action = getenv("ACTION");
798 device = getenv("DEVNAME");
800 if (!action || !device)
802 snprintf(path, sizeof(path), "/dev/%s", device);
804 if (!strcmp(action, "remove")) {
806 mount_point = find_mount_point(path);
808 err = umount2(mount_point, MNT_DETACH);
811 ULOG_ERR("umount of %s failed (%d) - %s\n",
812 mount_point, err, strerror(err));
815 } else if (strcmp(action, "add")) {
816 ULOG_ERR("Unkown action %s\n", action);
821 if (config_load(NULL))
825 return mount_device(find_block_info(NULL, NULL, path), 1);
828 static int find_block_mtd(char *name, char *part, int plen)
830 FILE *fp = fopen("/proc/mtd", "r");
831 static char line[256];
837 while (!index && fgets(line, sizeof(line), fp)) {
838 if (strstr(line, name)) {
839 char *eol = strstr(line, ":");
854 snprintf(part, plen, "/dev/mtdblock%s", index);
860 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
864 while (ubi_dev_present(libubi, dev))
866 struct ubi_dev_info dev_info;
867 struct ubi_vol_info vol_info;
869 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
871 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
874 *dev_num = dev_info.dev_num;
875 *vol_id = vol_info.vol_id;
883 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
889 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
891 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
896 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
902 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
904 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
911 static int find_root_dev(char *buf, int len)
921 if (!(d = opendir("/dev")))
926 while ((e = readdir(d)) != NULL) {
927 snprintf(buf, len, "/dev/%s", e->d_name);
929 if (stat(buf, &s) || s.st_rdev != root)
942 static int test_fs_support(const char *name)
948 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
949 while (fgets(line, sizeof(line), f)) {
950 p = strtok(line, "\t\n");
952 if (p && !strcmp(p, "nodev"))
953 p = strtok(NULL, "\t\n");
955 if (p && !strcmp(p, name)) {
966 static int check_extroot(char *path)
968 struct blkid_struct_probe *pr = NULL;
972 if (find_block_mtd("rootfs", devpath, sizeof(devpath))) {
976 libubi = libubi_open();
977 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
978 libubi_close(libubi);
983 if (find_block_mtd("rootfs", devpath, sizeof(devpath))) {
984 if (find_root_dev(devpath, sizeof(devpath))) {
985 ULOG_ERR("extroot: unable to determine root device\n");
991 list_for_each_entry(pr, &devices, list) {
992 if (!strcmp(pr->dev, devpath)) {
996 char uuid[64] = { 0 };
998 snprintf(tag, sizeof(tag), "%s/etc", path);
1002 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1003 if (stat(tag, &s)) {
1004 fp = fopen(tag, "w+");
1006 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1007 tag, errno, strerror(errno));
1008 /* return 0 to continue boot regardless of error */
1011 fputs(pr->uuid, fp);
1016 fp = fopen(tag, "r");
1018 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1019 tag, errno, strerror(errno));
1023 if (!fgets(uuid, sizeof(uuid), fp))
1024 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1025 tag, errno, strerror(errno));
1028 if (*uuid || !strcasecmp(uuid, pr->uuid))
1031 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1032 pr->uuid, basename(path), uuid);
1037 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath);
1042 * Read info about extroot from UCI (using prefix) and mount it.
1044 static int mount_extroot(char *cfg)
1046 char overlay[] = "/tmp/extroot/overlay";
1047 char mnt[] = "/tmp/extroot/mnt";
1049 struct blkid_struct_probe *pr;
1053 /* Load @cfg/etc/config/fstab */
1054 if (config_load(cfg))
1057 /* See if there is extroot-specific mount config */
1058 m = find_block(NULL, NULL, NULL, "/");
1060 m = find_block(NULL, NULL, NULL, "/overlay");
1062 if (!m || !m->extroot)
1064 ULOG_INFO("extroot: not configured\n");
1068 /* Find block device pointed by the mount config */
1069 pr = find_block_info(m->uuid, m->label, m->device);
1071 if (!pr && delay_root){
1072 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1076 pr = find_block_info(m->uuid, m->label, m->device);
1079 if (strncmp(pr->id->name, "ext", 3) &&
1080 strncmp(pr->id->name, "ubifs", 5)) {
1081 ULOG_ERR("extroot: unsupported filesystem %s, try ext4\n", pr->id->name);
1085 if (test_fs_support(pr->id->name)) {
1086 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr->id->name);
1095 check_filesystem(pr);
1097 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
1100 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1101 pr->dev, pr->id->name, path, err, strerror(err));
1102 } else if (m->overlay) {
1103 err = check_extroot(path);
1108 ULOG_ERR("extroot: cannot find device %s%s\n",
1109 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1110 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1116 static int main_extroot(int argc, char **argv)
1118 struct blkid_struct_probe *pr;
1119 char blkdev_path[32] = { 0 };
1121 #ifdef UBIFS_EXTROOT
1125 if (!getenv("PREINIT"))
1129 ULOG_ERR("Usage: block extroot\n");
1136 /* enable LOG_INFO messages */
1137 ulog_threshold(LOG_INFO);
1140 * Look for "rootfs_data". We will want to mount it and check for
1141 * extroot configuration.
1144 /* Start with looking for MTD partition */
1145 find_block_mtd("rootfs_data", blkdev_path, sizeof(blkdev_path));
1146 if (blkdev_path[0]) {
1147 pr = find_block_info(NULL, NULL, blkdev_path);
1148 if (pr && !strcmp(pr->id->name, "jffs2")) {
1149 char cfg[] = "/tmp/jffs_cfg";
1152 * Mount MTD part and try extroot (using
1153 * /etc/config/fstab from that partition)
1156 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1157 err = mount_extroot(cfg);
1158 umount2(cfg, MNT_DETACH);
1161 rmdir("/tmp/overlay");
1167 #ifdef UBIFS_EXTROOT
1168 /* ... but it also could be an UBI volume */
1169 memset(blkdev_path, 0, sizeof(blkdev_path));
1170 libubi = libubi_open();
1171 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1172 libubi_close(libubi);
1173 if (blkdev_path[0]) {
1174 char cfg[] = "/tmp/ubifs_cfg";
1176 /* Mount volume and try extroot (using fstab from that vol) */
1178 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1179 err = mount_extroot(cfg);
1180 umount2(cfg, MNT_DETACH);
1183 rmdir("/tmp/overlay");
1189 return mount_extroot(NULL);
1192 static int main_mount(int argc, char **argv)
1194 struct blkid_struct_probe *pr;
1196 if (config_load(NULL))
1200 list_for_each_entry(pr, &devices, list)
1201 mount_device(pr, 0);
1203 handle_swapfiles(true);
1208 static int main_umount(int argc, char **argv)
1210 struct blkid_struct_probe *pr;
1212 if (config_load(NULL))
1215 handle_swapfiles(false);
1218 list_for_each_entry(pr, &devices, list)
1224 static int main_detect(int argc, char **argv)
1226 struct blkid_struct_probe *pr;
1229 printf("config 'global'\n");
1230 printf("\toption\tanon_swap\t'0'\n");
1231 printf("\toption\tanon_mount\t'0'\n");
1232 printf("\toption\tauto_swap\t'1'\n");
1233 printf("\toption\tauto_mount\t'1'\n");
1234 printf("\toption\tdelay_root\t'5'\n");
1235 printf("\toption\tcheck_fs\t'0'\n\n");
1236 list_for_each_entry(pr, &devices, list)
1237 print_block_uci(pr);
1242 static int main_info(int argc, char **argv)
1245 struct blkid_struct_probe *pr;
1249 list_for_each_entry(pr, &devices, list)
1250 print_block_info(pr);
1255 for (i = 2; i < argc; i++) {
1258 if (stat(argv[i], &s)) {
1259 ULOG_ERR("failed to stat %s\n", argv[i]);
1262 if (!S_ISBLK(s.st_mode)) {
1263 ULOG_ERR("%s is not a block device\n", argv[i]);
1266 pr = find_block_info(NULL, NULL, argv[i]);
1268 print_block_info(pr);
1274 static int swapon_usage(void)
1276 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1277 "\tStart swapping on [DEVICE]\n"
1278 " -a\tStart swapping on all swap devices\n"
1279 " -p pri\tSet priority of swap device\n"
1280 " -s\tShow summary\n");
1284 static int main_swapon(int argc, char **argv)
1290 struct blkid_struct_probe *pr;
1296 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1299 fp = fopen("/proc/swaps", "r");
1303 ULOG_ERR("failed to open /proc/swaps\n");
1306 while (getline(&lineptr, &s, fp) > 0)
1314 list_for_each_entry(pr, &devices, list) {
1315 if (strcmp(pr->id->name, "swap"))
1317 if (swapon(pr->dev, 0))
1318 ULOG_ERR("failed to swapon %s\n", pr->dev);
1324 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1327 return swapon_usage();
1332 if (optind != (argc - 1))
1333 return swapon_usage();
1335 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1336 ULOG_ERR("%s is not a block device or file\n", argv[optind]);
1339 err = swapon(argv[optind], flags);
1341 ULOG_ERR("failed to swapon %s (%d)\n", argv[optind], err);
1348 static int main_swapoff(int argc, char **argv)
1351 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1352 "\tStop swapping on DEVICE\n"
1353 " -a\tStop swapping on all swap devices\n");
1357 if (!strcmp(argv[1], "-a")) {
1358 FILE *fp = fopen("/proc/swaps", "r");
1362 ULOG_ERR("failed to open /proc/swaps\n");
1365 if (fgets(line, sizeof(line), fp))
1366 while (fgets(line, sizeof(line), fp)) {
1367 char *end = strchr(line, ' ');
1373 err = swapoff(line);
1375 ULOG_ERR("failed to swapoff %s (%d)\n", line, err);
1382 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1383 ULOG_ERR("%s is not a block device or file\n", argv[1]);
1386 err = swapoff(argv[1]);
1388 ULOG_ERR("failed to swapoff %s (%d)\n", argv[1], err);
1396 int main(int argc, char **argv)
1398 char *base = basename(*argv);
1402 ulog_open(-1, -1, "block");
1403 ulog_threshold(LOG_NOTICE);
1405 if (!strcmp(base, "swapon"))
1406 return main_swapon(argc, argv);
1408 if (!strcmp(base, "swapoff"))
1409 return main_swapoff(argc, argv);
1411 if ((argc > 1) && !strcmp(base, "block")) {
1412 if (!strcmp(argv[1], "info"))
1413 return main_info(argc, argv);
1415 if (!strcmp(argv[1], "detect"))
1416 return main_detect(argc, argv);
1418 if (!strcmp(argv[1], "hotplug"))
1419 return main_hotplug(argc, argv);
1421 if (!strcmp(argv[1], "extroot"))
1422 return main_extroot(argc, argv);
1424 if (!strcmp(argv[1], "mount"))
1425 return main_mount(argc, argv);
1427 if (!strcmp(argv[1], "umount"))
1428 return main_umount(argc, argv);
1431 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");