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;
68 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
69 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
70 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
83 static const struct uci_blob_param_list config_attr_list = {
84 .n_params = __CFG_MAX,
85 .params = config_policy,
88 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
89 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
90 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
91 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
92 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
93 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
94 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
97 static const struct uci_blob_param_list mount_attr_list = {
98 .n_params = __MOUNT_MAX,
99 .params = mount_policy,
110 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
111 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
112 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
113 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
114 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
117 static const struct uci_blob_param_list swap_attr_list = {
118 .n_params = __SWAP_MAX,
119 .params = swap_policy,
122 static char *blobmsg_get_strdup(struct blob_attr *attr)
127 return strdup(blobmsg_get_string(attr));
130 static int mount_add(struct uci_section *s)
132 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
135 blob_buf_init(&b, 0);
136 uci_to_blob(&b, s, &mount_attr_list);
137 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
139 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
142 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
145 m = malloc(sizeof(struct mount));
146 m->type = TYPE_MOUNT;
147 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
148 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
149 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
150 m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
151 m->device = blobmsg_get_strdup(tb[MOUNT_DEVICE]);
152 m->overlay = m->extroot = 0;
153 if (m->target && !strcmp(m->target, "/"))
155 if (m->target && !strcmp(m->target, "/overlay"))
156 m->extroot = m->overlay = 1;
159 vlist_add(&mounts, &m->node, m->uuid);
161 vlist_add(&mounts, &m->node, m->label);
163 vlist_add(&mounts, &m->node, m->device);
168 static int swap_add(struct uci_section *s)
170 struct blob_attr *tb[__SWAP_MAX] = { 0 };
173 blob_buf_init(&b, 0);
174 uci_to_blob(&b, s, &swap_attr_list);
175 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
177 if (!tb[SWAP_UUID] && !tb[SWAP_DEVICE])
180 m = malloc(sizeof(struct mount));
181 memset(m, 0, sizeof(struct mount));
183 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
184 m->device = blobmsg_get_strdup(tb[SWAP_DEVICE]);
186 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
188 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
190 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
191 vlist_add(&mounts, &m->node, (m->uuid) ? (m->uuid) : (m->device));
196 static int global_add(struct uci_section *s)
198 struct blob_attr *tb[__CFG_MAX] = { 0 };
200 blob_buf_init(&b, 0);
201 uci_to_blob(&b, s, &config_attr_list);
202 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
204 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
206 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
212 static struct mount* find_swap(const char *uuid, const char *device)
216 vlist_for_each_element(&mounts, m, node) {
217 if (m->type != TYPE_SWAP)
219 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
221 if (device && m->device && !strcmp(m->device, device))
228 static struct mount* find_block(const char *uuid, const char *label, const char *device,
233 vlist_for_each_element(&mounts, m, node) {
234 if (m->type != TYPE_MOUNT)
236 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
238 if (m->label && label && !strcmp(m->label, label))
240 if (m->target && target && !strcmp(m->target, target))
242 if (m->device && device && !strcmp(m->device, device))
249 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
250 struct vlist_node *node_old)
254 static int config_load(char *cfg)
256 struct uci_context *ctx;
257 struct uci_package *pkg;
258 struct uci_element *e;
260 vlist_init(&mounts, avl_strcmp, mounts_update);
262 ctx = uci_alloc_context();
265 snprintf(path, 32, "%s/etc/config", cfg);
266 uci_set_confdir(ctx, path);
269 if (uci_load(ctx, "fstab", &pkg))
272 vlist_update(&mounts);
273 uci_foreach_element(&pkg->sections, e) {
274 struct uci_section *s = uci_to_section(e);
276 if (!strcmp(s->type, "mount"))
278 if (!strcmp(s->type, "swap"))
280 if (!strcmp(s->type, "global"))
283 vlist_flush(&mounts);
288 static int _cache_load(const char *path)
290 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
294 if (glob(path, gl_flags, NULL, &gl) < 0)
297 for (j = 0; j < gl.gl_pathc; j++) {
298 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
299 memset(pr, 0, sizeof(struct blkid_struct_probe));
300 probe_block(gl.gl_pathv[j], pr);
304 list_add_tail(&pr->list, &devices);
312 static void cache_load(int mtd)
315 _cache_load("/dev/mtdblock*");
316 _cache_load("/dev/mmcblk*");
317 _cache_load("/dev/sd*");
320 static int print_block_info(struct blkid_struct_probe *pr)
322 printf("%s:", pr->dev);
324 printf(" UUID=\"%s\"", pr->uuid);
327 printf(" LABEL=\"%s\"", pr->label);
330 printf(" NAME=\"%s\"", pr->name);
333 printf(" VERSION=\"%s\"", pr->version);
335 printf(" TYPE=\"%s\"\n", pr->id->name);
340 static int print_block_uci(struct blkid_struct_probe *pr)
342 if (!strcmp(pr->id->name, "swap")) {
343 printf("config 'swap'\n");
345 printf("config 'mount'\n");
346 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
349 printf("\toption\tuuid\t'%s'\n", pr->uuid);
351 printf("\toption\tdevice\t'%s'\n", basename(pr->dev));
352 printf("\toption\tenabled\t'0'\n\n");
357 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
359 struct blkid_struct_probe *pr = NULL;
362 list_for_each_entry(pr, &devices, list)
363 if (!strcmp(pr->uuid, uuid))
367 list_for_each_entry(pr, &devices, list)
368 if (strcmp(pr->label, label))
372 list_for_each_entry(pr, &devices, list)
373 if (!strcmp(pr->dev, path))
379 static char* find_mount_point(char *block)
381 FILE *fp = fopen("/proc/mounts", "r");
382 static char line[256];
383 int len = strlen(block);
389 while (fgets(line, sizeof(line), fp)) {
390 if (!strncmp(line, block, len)) {
391 char *p = &line[len + 1];
392 char *t = strstr(p, " ");
407 static void mkdir_p(char *dir)
409 char *l = strrchr(dir, '/');
419 static int main_hotplug(int argc, char **argv)
423 char *action, *device, *mount_point;
424 struct blkid_struct_probe *pr;
426 action = getenv("ACTION");
427 device = getenv("DEVNAME");
429 if (!action || !device)
431 snprintf(path, sizeof(path), "/dev/%s", device);
433 if (!strcmp(action, "remove")) {
435 mount_point = find_mount_point(path);
437 err = umount2(mount_point, MNT_DETACH);
440 fprintf(stderr, "unmount of %s failed (%d) - %s\n",
441 mount_point, err, strerror(err));
444 } else if (strcmp(action, "add")) {
445 fprintf(stderr, "Unkown action %s\n", action);
450 if (config_load(NULL))
454 pr = find_block_info(NULL, NULL, path);
456 fprintf(stderr, "failed to read blockinfo for %s\n", path);
460 if (!strcmp(pr->id->name, "swap")) {
461 m = find_swap(pr->uuid, device);
462 if (m || anon_swap) {
463 if (!strcmp(action, "add"))
464 swapon(path, (m) ? (m->prio) : (0));
471 m = find_block(pr->uuid, pr->label, device, NULL);
472 if (m && !m->extroot) {
473 char *target = m->target;
474 char _target[] = "/mnt/mmcblk123";
478 snprintf(_target, sizeof(_target), "/mnt/%s", device);
482 err = mount(path, target, pr->id->name, 0, (m->options) ? (m->options) : (""));
484 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
485 path, pr->id->name, target, err, strerror(err));
490 char target[] = "/mnt/mmcblk123";
493 snprintf(target, sizeof(target), "/mnt/%s", device);
495 err = mount(path, target, pr->id->name, 0, "");
497 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
498 path, pr->id->name, target, err, strerror(err));
505 static int find_block_mtd(char *name, char *part, int plen)
507 FILE *fp = fopen("/proc/mtd", "r");
508 static char line[256];
514 while (!index && fgets(line, sizeof(line), fp)) {
515 if (strstr(line, name)) {
516 char *eol = strstr(line, ":");
531 snprintf(part, plen, "/dev/mtdblock%s", index);
536 static int check_extroot(char *path)
538 struct blkid_struct_probe *pr = NULL;
541 if (find_block_mtd("rootfs", fs, sizeof(fs)))
544 list_for_each_entry(pr, &devices, list) {
545 if (!strcmp(pr->dev, fs)) {
549 char uuid[32] = { 0 };
551 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
553 fp = fopen(tag, "w+");
555 fprintf(stderr, "extroot: failed to write uuid tag file\n");
556 /* return 0 to continue boot regardless of error */
564 fp = fopen(tag, "r");
566 fprintf(stderr, "extroot: failed to open uuid tag file\n");
570 fgets(uuid, sizeof(uuid), fp);
572 if (!strcmp(uuid, pr->uuid))
574 fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
580 static int mount_extroot(char *cfg)
582 char overlay[] = "/tmp/overlay";
583 char mnt[] = "/tmp/mnt";
585 struct blkid_struct_probe *pr;
589 if (config_load(cfg))
592 m = find_block(NULL, NULL, NULL, "/");
594 m = find_block(NULL, NULL, NULL, "/overlay");
596 if (!m || !m->extroot)
599 pr = find_block_info(m->uuid, m->label, NULL);
601 if (strncmp(pr->id->name, "ext", 3)) {
602 fprintf(stderr, "extroot: %s is not supported, try ext4\n", pr->id->name);
609 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
612 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
613 pr->dev, pr->id->name, path, err, strerror(err));
614 } else if (m->overlay) {
615 err = check_extroot(path);
624 static int main_extroot(int argc, char **argv)
626 struct blkid_struct_probe *pr;
628 char fs_data[32] = { 0 };
631 if (!getenv("PREINIT"))
635 fprintf(stderr, "Usage: block extroot mountpoint\n");
642 find_block_mtd("rootfs", fs, sizeof(fs));
646 pr = find_block_info(NULL, NULL, fs);
650 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
652 pr = find_block_info(NULL, NULL, fs_data);
653 if (pr && !strcmp(pr->id->name, "jffs2")) {
654 char cfg[] = "/tmp/jffs_cfg";
657 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
658 err = mount_extroot(cfg);
659 umount2(cfg, MNT_DETACH);
662 rmdir("/tmp/overlay");
668 return mount_extroot(NULL);
671 static int main_detect(int argc, char **argv)
673 struct blkid_struct_probe *pr;
676 list_for_each_entry(pr, &devices, list)
682 static int main_info(int argc, char **argv)
685 struct blkid_struct_probe *pr;
689 list_for_each_entry(pr, &devices, list)
690 print_block_info(pr);
695 for (i = 2; i < argc; i++) {
698 if (stat(argv[i], &s)) {
699 fprintf(stderr, "failed to stat %s\n", argv[i]);
702 if (!S_ISBLK(s.st_mode)) {
703 fprintf(stderr, "%s is not a block device\n", argv[i]);
706 pr = find_block_info(NULL, NULL, argv[i]);
708 print_block_info(pr);
714 static int main_swapon(int argc, char **argv)
717 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
721 if (!strcmp(argv[1], "-a")) {
722 struct blkid_struct_probe *pr;
725 list_for_each_entry(pr, &devices, list) {
726 if (strcmp(pr->id->name, "swap"))
728 if (swapon(pr->dev, 0))
729 fprintf(stderr, "failed to swapon %s\n", pr->dev);
735 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
736 fprintf(stderr, "%s is not a block device\n", argv[1]);
739 err = swapon(argv[1], 0);
741 fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
749 static int main_swapoff(int argc, char **argv)
752 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
756 if (!strcmp(argv[1], "-a")) {
757 FILE *fp = fopen("/proc/swaps", "r");
761 fprintf(stderr, "failed to open /proc/swaps\n");
764 fgets(line, sizeof(line), fp);
765 while (fgets(line, sizeof(line), fp)) {
766 char *end = strchr(line, ' ');
774 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
781 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
782 fprintf(stderr, "%s is not a block device\n", argv[1]);
785 err = swapoff(argv[1]);
787 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
795 int main(int argc, char **argv)
797 char *base = basename(*argv);
801 if (!strcmp(base, "swapon"))
802 return main_swapon(argc, argv);
804 if (!strcmp(base, "swapoff"))
805 return main_swapoff(argc, argv);
807 if ((argc > 1) && !strcmp(base, "block")) {
808 if (!strcmp(argv[1], "info"))
809 return main_info(argc, argv);
811 if (!strcmp(argv[1], "detect"))
812 return main_detect(argc, argv);
814 if (!strcmp(argv[1], "hotplug"))
815 return main_hotplug(argc, argv);
817 if (!strcmp(argv[1], "extroot"))
818 return main_extroot(argc, argv);
821 fprintf(stderr, "Usage: block <info|detect>\n");