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.
22 #include <sys/types.h>
24 #include <sys/mount.h>
29 #include <libubox/list.h>
30 #include <libubox/vlist.h>
31 #include <libubox/blobmsg_json.h>
32 #include <libubox/avl-cmp.h>
34 #include "libblkid-tiny/libblkid-tiny.h"
42 struct vlist_node node;
57 static struct vlist_tree mounts;
58 static struct blob_buf b;
59 static LIST_HEAD(devices);
60 static int anon_mount, anon_swap, auto_mount, auto_swap;
70 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
71 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
72 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
73 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
74 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
87 static const struct uci_blob_param_list config_attr_list = {
88 .n_params = __CFG_MAX,
89 .params = config_policy,
92 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
93 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
94 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
95 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
96 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
97 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
98 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
101 static const struct uci_blob_param_list mount_attr_list = {
102 .n_params = __MOUNT_MAX,
103 .params = mount_policy,
114 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
115 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
116 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
117 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
118 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
121 static const struct uci_blob_param_list swap_attr_list = {
122 .n_params = __SWAP_MAX,
123 .params = swap_policy,
126 static char *blobmsg_get_strdup(struct blob_attr *attr)
131 return strdup(blobmsg_get_string(attr));
134 static int mount_add(struct uci_section *s)
136 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
139 blob_buf_init(&b, 0);
140 uci_to_blob(&b, s, &mount_attr_list);
141 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
143 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
146 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
149 m = malloc(sizeof(struct mount));
150 m->type = TYPE_MOUNT;
151 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
152 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
153 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
154 m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
155 m->device = blobmsg_get_strdup(tb[MOUNT_DEVICE]);
156 m->overlay = m->extroot = 0;
157 if (m->target && !strcmp(m->target, "/"))
159 if (m->target && !strcmp(m->target, "/overlay"))
160 m->extroot = m->overlay = 1;
163 vlist_add(&mounts, &m->node, m->uuid);
165 vlist_add(&mounts, &m->node, m->label);
167 vlist_add(&mounts, &m->node, m->device);
172 static int swap_add(struct uci_section *s)
174 struct blob_attr *tb[__SWAP_MAX] = { 0 };
177 blob_buf_init(&b, 0);
178 uci_to_blob(&b, s, &swap_attr_list);
179 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
181 if (!tb[SWAP_UUID] && !tb[SWAP_DEVICE])
184 m = malloc(sizeof(struct mount));
185 memset(m, 0, sizeof(struct mount));
187 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
188 m->device = blobmsg_get_strdup(tb[SWAP_DEVICE]);
190 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
192 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
194 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
195 vlist_add(&mounts, &m->node, (m->uuid) ? (m->uuid) : (m->device));
200 static int global_add(struct uci_section *s)
202 struct blob_attr *tb[__CFG_MAX] = { 0 };
204 blob_buf_init(&b, 0);
205 uci_to_blob(&b, s, &config_attr_list);
206 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
208 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
210 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
213 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
215 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
221 static struct mount* find_swap(const char *uuid, const char *device)
225 vlist_for_each_element(&mounts, m, node) {
226 if (m->type != TYPE_SWAP)
228 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
230 if (device && m->device && !strcmp(m->device, device))
237 static struct mount* find_block(const char *uuid, const char *label, const char *device,
242 vlist_for_each_element(&mounts, m, node) {
243 if (m->type != TYPE_MOUNT)
245 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
247 if (m->label && label && !strcmp(m->label, label))
249 if (m->target && target && !strcmp(m->target, target))
251 if (m->device && device && !strcmp(m->device, device))
258 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
259 struct vlist_node *node_old)
263 static int config_load(char *cfg)
265 struct uci_context *ctx;
266 struct uci_package *pkg;
267 struct uci_element *e;
269 vlist_init(&mounts, avl_strcmp, mounts_update);
271 ctx = uci_alloc_context();
274 snprintf(path, 32, "%s/etc/config", cfg);
275 uci_set_confdir(ctx, path);
278 if (uci_load(ctx, "fstab", &pkg))
281 vlist_update(&mounts);
282 uci_foreach_element(&pkg->sections, e) {
283 struct uci_section *s = uci_to_section(e);
285 if (!strcmp(s->type, "mount"))
287 if (!strcmp(s->type, "swap"))
289 if (!strcmp(s->type, "global"))
292 vlist_flush(&mounts);
297 static int _cache_load(const char *path)
299 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
303 if (glob(path, gl_flags, NULL, &gl) < 0)
306 for (j = 0; j < gl.gl_pathc; j++) {
307 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
308 memset(pr, 0, sizeof(struct blkid_struct_probe));
309 probe_block(gl.gl_pathv[j], pr);
313 list_add_tail(&pr->list, &devices);
321 static void cache_load(int mtd)
324 _cache_load("/dev/mtdblock*");
325 _cache_load("/dev/mmcblk*");
326 _cache_load("/dev/sd*");
329 static int print_block_info(struct blkid_struct_probe *pr)
331 printf("%s:", pr->dev);
333 printf(" UUID=\"%s\"", pr->uuid);
336 printf(" LABEL=\"%s\"", pr->label);
339 printf(" NAME=\"%s\"", pr->name);
342 printf(" VERSION=\"%s\"", pr->version);
344 printf(" TYPE=\"%s\"\n", pr->id->name);
349 static int print_block_uci(struct blkid_struct_probe *pr)
351 if (!strcmp(pr->id->name, "swap")) {
352 printf("config 'swap'\n");
354 printf("config 'mount'\n");
355 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
358 printf("\toption\tuuid\t'%s'\n", pr->uuid);
360 printf("\toption\tdevice\t'%s'\n", basename(pr->dev));
361 printf("\toption\tenabled\t'0'\n\n");
366 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
368 struct blkid_struct_probe *pr = NULL;
371 list_for_each_entry(pr, &devices, list)
372 if (!strcmp(pr->uuid, uuid))
376 list_for_each_entry(pr, &devices, list)
377 if (strcmp(pr->label, label))
381 list_for_each_entry(pr, &devices, list)
382 if (!strcmp(pr->dev, path))
388 static char* find_mount_point(char *block)
390 FILE *fp = fopen("/proc/mounts", "r");
391 static char line[256];
392 int len = strlen(block);
398 while (fgets(line, sizeof(line), fp)) {
399 if (!strncmp(line, block, len)) {
400 char *p = &line[len + 1];
401 char *t = strstr(p, " ");
416 static void mkdir_p(char *dir)
418 char *l = strrchr(dir, '/');
428 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
431 char *device = basename(pr->dev);
436 if (!strcmp(pr->id->name, "swap")) {
437 if (hotplug && !auto_swap)
439 m = find_swap(pr->uuid, device);
441 swapon(pr->dev, (m) ? (m->prio) : (0));
446 if (hotplug && !auto_mount)
449 if (find_mount_point(pr->dev)) {
450 fprintf(stderr, "%s is already mounted\n", pr->dev);
454 m = find_block(pr->uuid, pr->label, device, NULL);
459 char *target = m->target;
460 char _target[] = "/mnt/mmcblk123";
464 snprintf(_target, sizeof(_target), "/mnt/%s", device);
468 err = mount(pr->dev, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
470 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
471 pr->dev, pr->id->name, target, err, strerror(err));
476 char target[] = "/mnt/mmcblk123";
479 snprintf(target, sizeof(target), "/mnt/%s", device);
481 err = mount(pr->dev, target, pr->id->name, 0, "");
483 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
484 pr->dev, pr->id->name, target, err, strerror(err));
491 static int umount_device(struct blkid_struct_probe *pr)
494 char *device = basename(pr->dev);
501 if (!strcmp(pr->id->name, "swap"))
504 mp = find_mount_point(pr->dev);
508 m = find_block(pr->uuid, pr->label, device, NULL);
512 err = umount2(mp, MNT_DETACH);
514 fprintf(stderr, "unmounting %s (%s) failed (%d) - %s\n",
515 pr->dev, mp, err, strerror(err));
517 fprintf(stderr, "unmounted %s (%s)\n",
523 static int main_hotplug(int argc, char **argv)
526 char *action, *device, *mount_point;
528 action = getenv("ACTION");
529 device = getenv("DEVNAME");
531 if (!action || !device)
533 snprintf(path, sizeof(path), "/dev/%s", device);
535 if (!strcmp(action, "remove")) {
537 mount_point = find_mount_point(path);
539 err = umount2(mount_point, MNT_DETACH);
542 fprintf(stderr, "umount of %s failed (%d) - %s\n",
543 mount_point, err, strerror(err));
546 } else if (strcmp(action, "add")) {
547 fprintf(stderr, "Unkown action %s\n", action);
552 if (config_load(NULL))
556 return mount_device(find_block_info(NULL, NULL, path), 1);
559 static int find_block_mtd(char *name, char *part, int plen)
561 FILE *fp = fopen("/proc/mtd", "r");
562 static char line[256];
568 while (!index && fgets(line, sizeof(line), fp)) {
569 if (strstr(line, name)) {
570 char *eol = strstr(line, ":");
585 snprintf(part, plen, "/dev/mtdblock%s", index);
590 static int check_extroot(char *path)
592 struct blkid_struct_probe *pr = NULL;
595 if (find_block_mtd("rootfs", fs, sizeof(fs)))
598 list_for_each_entry(pr, &devices, list) {
599 if (!strcmp(pr->dev, fs)) {
603 char uuid[32] = { 0 };
605 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
607 fp = fopen(tag, "w+");
609 fprintf(stderr, "extroot: failed to write uuid tag file\n");
610 /* return 0 to continue boot regardless of error */
618 fp = fopen(tag, "r");
620 fprintf(stderr, "extroot: failed to open uuid tag file\n");
624 fgets(uuid, sizeof(uuid), fp);
626 if (!strcmp(uuid, pr->uuid))
628 fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
634 static int mount_extroot(char *cfg)
636 char overlay[] = "/tmp/overlay";
637 char mnt[] = "/tmp/mnt";
639 struct blkid_struct_probe *pr;
643 if (config_load(cfg))
646 m = find_block(NULL, NULL, NULL, "/");
648 m = find_block(NULL, NULL, NULL, "/overlay");
650 if (!m || !m->extroot)
653 pr = find_block_info(m->uuid, m->label, NULL);
655 if (strncmp(pr->id->name, "ext", 3)) {
656 fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
663 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
666 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
667 pr->dev, pr->id->name, path, err, strerror(err));
668 } else if (m->overlay) {
669 err = check_extroot(path);
678 static int main_extroot(int argc, char **argv)
680 struct blkid_struct_probe *pr;
682 char fs_data[32] = { 0 };
685 if (!getenv("PREINIT"))
689 fprintf(stderr, "Usage: block extroot mountpoint\n");
696 find_block_mtd("rootfs", fs, sizeof(fs));
700 pr = find_block_info(NULL, NULL, fs);
704 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
706 pr = find_block_info(NULL, NULL, fs_data);
707 if (pr && !strcmp(pr->id->name, "jffs2")) {
708 char cfg[] = "/tmp/jffs_cfg";
711 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
712 err = mount_extroot(cfg);
713 umount2(cfg, MNT_DETACH);
716 rmdir("/tmp/overlay");
722 return mount_extroot(NULL);
725 static int main_mount(int argc, char **argv)
727 struct blkid_struct_probe *pr;
729 if (config_load(NULL))
733 list_for_each_entry(pr, &devices, list)
739 static int main_umount(int argc, char **argv)
741 struct blkid_struct_probe *pr;
743 if (config_load(NULL))
747 list_for_each_entry(pr, &devices, list)
753 static int main_detect(int argc, char **argv)
755 struct blkid_struct_probe *pr;
758 printf("config 'global'\n");
759 printf("\toption\tanon_swap\t'0'\n");
760 printf("\toption\tanon_mount\t'0'\n");
761 printf("\toption\tauto_swap\t'1'\n");
762 printf("\toption\tauto_mount\t'1'\n\n");
763 list_for_each_entry(pr, &devices, list)
769 static int main_info(int argc, char **argv)
772 struct blkid_struct_probe *pr;
776 list_for_each_entry(pr, &devices, list)
777 print_block_info(pr);
782 for (i = 2; i < argc; i++) {
785 if (stat(argv[i], &s)) {
786 fprintf(stderr, "failed to stat %s\n", argv[i]);
789 if (!S_ISBLK(s.st_mode)) {
790 fprintf(stderr, "%s is not a block device\n", argv[i]);
793 pr = find_block_info(NULL, NULL, argv[i]);
795 print_block_info(pr);
801 static int main_swapon(int argc, char **argv)
804 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
808 if (!strcmp(argv[1], "-a")) {
809 struct blkid_struct_probe *pr;
812 list_for_each_entry(pr, &devices, list) {
813 if (strcmp(pr->id->name, "swap"))
815 if (swapon(pr->dev, 0))
816 fprintf(stderr, "failed to swapon %s\n", pr->dev);
822 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
823 fprintf(stderr, "%s is not a block device\n", argv[1]);
826 err = swapon(argv[1], 0);
828 fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
836 static int main_swapoff(int argc, char **argv)
839 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
843 if (!strcmp(argv[1], "-a")) {
844 FILE *fp = fopen("/proc/swaps", "r");
848 fprintf(stderr, "failed to open /proc/swaps\n");
851 fgets(line, sizeof(line), fp);
852 while (fgets(line, sizeof(line), fp)) {
853 char *end = strchr(line, ' ');
861 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
868 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
869 fprintf(stderr, "%s is not a block device\n", argv[1]);
872 err = swapoff(argv[1]);
874 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
882 int main(int argc, char **argv)
884 char *base = basename(*argv);
888 if (!strcmp(base, "swapon"))
889 return main_swapon(argc, argv);
891 if (!strcmp(base, "swapoff"))
892 return main_swapoff(argc, argv);
894 if ((argc > 1) && !strcmp(base, "block")) {
895 if (!strcmp(argv[1], "info"))
896 return main_info(argc, argv);
898 if (!strcmp(argv[1], "detect"))
899 return main_detect(argc, argv);
901 if (!strcmp(argv[1], "hotplug"))
902 return main_hotplug(argc, argv);
904 if (!strcmp(argv[1], "extroot"))
905 return main_extroot(argc, argv);
907 if (!strcmp(argv[1], "mount"))
908 return main_mount(argc, argv);
910 if (!strcmp(argv[1], "umount"))
911 return main_umount(argc, argv);
914 fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");