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;
54 static struct vlist_tree mounts;
55 static struct blob_buf b;
56 static LIST_HEAD(devices);
67 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
68 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
69 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
70 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
71 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
72 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
75 static const struct uci_blob_param_list mount_attr_list = {
76 .n_params = __MOUNT_MAX,
77 .params = mount_policy,
87 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
88 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
89 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
90 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
93 static const struct uci_blob_param_list swap_attr_list = {
94 .n_params = __SWAP_MAX,
95 .params = swap_policy,
98 static char *blobmsg_get_strdup(struct blob_attr *attr)
103 return strdup(blobmsg_get_string(attr));
106 static int mount_add(struct uci_section *s)
108 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
111 blob_buf_init(&b, 0);
112 uci_to_blob(&b, s, &mount_attr_list);
113 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
115 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID])
118 if (!tb[MOUNT_TARGET])
121 m = malloc(sizeof(struct mount));
122 m->type = TYPE_MOUNT;
123 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
124 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
125 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
126 m->options = blobmsg_get_strdup(tb[MOUNT_OPTIONS]);
128 if ((!tb[MOUNT_ENABLE]) || blobmsg_get_u32(tb[MOUNT_ENABLE]))
129 vlist_add(&mounts, &m->node, m->target);
134 static int swap_add(struct uci_section *s)
136 struct blob_attr *tb[__SWAP_MAX] = { 0 };
139 blob_buf_init(&b, 0);
140 uci_to_blob(&b, s, &swap_attr_list);
141 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
146 m = malloc(sizeof(struct mount));
147 memset(m, 0, sizeof(struct mount));
149 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
151 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
153 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
155 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE]))
156 vlist_add(&mounts, &m->node, m->uuid);
161 static struct mount* find_swap(const char *uuid)
168 vlist_for_each_element(&mounts, m, node)
169 if ((m->type == TYPE_SWAP) && m->uuid && !strcmp(m->uuid, uuid))
175 static struct mount* find_block(const char *uuid, const char *label, const char *target)
179 vlist_for_each_element(&mounts, m, node) {
180 if ((m->type == TYPE_MOUNT) && m->uuid && uuid && !strcmp(m->uuid, uuid))
182 if ((m->type == TYPE_MOUNT) && m->label && label && !strcmp(m->label, label))
184 if ((m->type == TYPE_MOUNT) && target && !strcmp(m->target, target))
191 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
192 struct vlist_node *node_old)
196 static int config_load(char *cfg)
198 struct uci_context *ctx;
199 struct uci_package *pkg;
200 struct uci_element *e;
202 vlist_init(&mounts, avl_strcmp, mounts_update);
204 ctx = uci_alloc_context();
207 snprintf(path, 32, "%s/etc/config", cfg);
208 uci_set_confdir(ctx, path);
211 if (uci_load(ctx, "fstab", &pkg))
214 vlist_update(&mounts);
215 uci_foreach_element(&pkg->sections, e) {
216 struct uci_section *s = uci_to_section(e);
218 if (!strcmp(s->type, "mount"))
220 if (!strcmp(s->type, "swap"))
223 vlist_flush(&mounts);
228 static int _cache_load(const char *path)
230 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
234 if (glob(path, gl_flags, NULL, &gl) < 0)
237 for (j = 0; j < gl.gl_pathc; j++) {
238 struct blkid_struct_probe *pr = malloc(sizeof(struct blkid_struct_probe));
239 memset(pr, 0, sizeof(struct blkid_struct_probe));
240 probe_block(gl.gl_pathv[j], pr);
244 list_add_tail(&pr->list, &devices);
252 static void cache_load(int mtd)
255 _cache_load("/dev/mtdblock*");
256 _cache_load("/dev/sd*");
257 _cache_load("/dev/mmcblk*");
260 static int print_block_info(struct blkid_struct_probe *pr)
262 printf("%s:", pr->dev);
264 printf(" UUID=\"%s\"", pr->uuid);
267 printf(" LABEL=\"%s\"", pr->label);
270 printf(" NAME=\"%s\"", pr->name);
273 printf(" VERSION=\"%s\"", pr->version);
275 printf(" TYPE=\"%s\"\n", pr->id->name);
280 static int print_block_uci(struct blkid_struct_probe *pr)
285 if (!strcmp(pr->id->name, "swap")) {
286 printf("config 'swap'\n");
288 printf("config 'mount'\n");
289 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
291 printf("\toption\tuuid\t'%s'\n", pr->uuid);
292 printf("\toption\tenabled\t'0'\n\n");
297 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
299 struct blkid_struct_probe *pr = NULL;
302 list_for_each_entry(pr, &devices, list)
303 if (!strcmp(pr->uuid, uuid))
307 list_for_each_entry(pr, &devices, list)
308 if (strcmp(pr->label, label))
312 list_for_each_entry(pr, &devices, list)
313 if (!strcmp(pr->dev, path))
319 static char* find_mount_point(char *block)
321 FILE *fp = fopen("/proc/mounts", "r");
322 static char line[256];
323 int len = strlen(block);
329 while (fgets(line, sizeof(line), fp)) {
330 if (!strncmp(line, block, len)) {
331 char *p = &line[len + 1];
332 char *t = strstr(p, " ");
347 static void mkdir_p(char *dir)
349 char *l = strrchr(dir, '/');
359 static int main_hotplug(int argc, char **argv)
363 char *action, *device, *mount_point;
364 struct blkid_struct_probe *pr;
366 action = getenv("ACTION");
367 device = getenv("DEVNAME");
369 if (!action || !device)
371 snprintf(path, sizeof(path), "/dev/%s", device);
373 if (!strcmp(action, "remove")) {
375 mount_point = find_mount_point(path);
377 err = umount2(mount_point, MNT_DETACH);
380 fprintf(stderr, "unmount of %s failed (%d) - %s\n",
381 mount_point, err, strerror(err));
384 } else if (strcmp(action, "add")) {
385 fprintf(stderr, "Unkown action %s\n", action);
390 if (config_load(NULL))
394 pr = find_block_info(NULL, NULL, path);
395 if (!pr && pr->uuid) {
396 fprintf(stderr, "failed to read blockinfo for %s\n", path);
400 m = find_swap(pr->uuid);
402 if (!strcmp(action, "add"))
403 swapon(path, m->prio);
407 m = find_block(pr->uuid, pr->label, NULL);
408 if (m && strcmp(m->target, "/")) {
412 err = mount(path, m->target, pr->id->name, 0, (m->options) ? (m->options) : (""));
414 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
415 path, pr->id->name, m->target, err, strerror(err));
422 static int find_block_mtd(char *name, char *part, int plen)
424 FILE *fp = fopen("/proc/mtd", "r");
425 static char line[256];
431 while (!index && fgets(line, sizeof(line), fp)) {
432 if (strstr(line, name)) {
433 char *eol = strstr(line, ":");
448 snprintf(part, plen, "/dev/mtdblock%s", index);
453 static int check_extroot(char *path)
455 struct blkid_struct_probe *pr = NULL;
458 if (find_block_mtd("rootfs", fs, sizeof(fs)))
461 list_for_each_entry(pr, &devices, list) {
462 if (!strcmp(pr->dev, fs)) {
466 char uuid[32] = { 0 };
468 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
470 fp = fopen(tag, "w+");
472 fprintf(stderr, "extroot: failed to write uuid tag file\n");
473 /* return 0 to continue boot regardless of error */
481 fp = fopen(tag, "r");
483 fprintf(stderr, "extroot: failed to open uuid tag file\n");
487 fgets(uuid, sizeof(uuid), fp);
489 if (!strcmp(uuid, pr->uuid))
491 fprintf(stderr, "extroot: uuid tag does not match rom uuid\n");
497 static int mount_extroot(char *path, char *cfg)
499 struct blkid_struct_probe *pr;
503 if (config_load(cfg))
506 m = find_block(NULL, NULL, "/");
510 pr = find_block_info(m->uuid, m->label, NULL);
513 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
516 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
517 pr->dev, pr->id->name, path, err, strerror(err));
519 err = check_extroot(path);
528 static int main_extroot(int argc, char **argv)
530 struct blkid_struct_probe *pr;
532 char fs_data[32] = { 0 };
534 char extroot[] = "/tmp/overlay";
536 if (!getenv("PREINIT"))
540 fprintf(stderr, "Usage: block extroot mountpoint\n");
547 find_block_mtd("rootfs", fs, sizeof(fs));
551 pr = find_block_info(NULL, NULL, fs);
552 if (!pr || strcmp(pr->id->name, "squashfs"))
555 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
557 pr = find_block_info(NULL, NULL, fs_data);
558 if (pr && !strcmp(pr->id->name, "jffs2")) {
559 char cfg[] = "/tmp/jffs_cfg";
562 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
563 err = mount_extroot(extroot, cfg);
564 umount2(cfg, MNT_DETACH);
573 err = mount_extroot(extroot, NULL);
578 static int main_detect(int argc, char **argv)
580 struct blkid_struct_probe *pr;
583 list_for_each_entry(pr, &devices, list)
589 static int main_info(int argc, char **argv)
592 struct blkid_struct_probe *pr;
596 list_for_each_entry(pr, &devices, list)
597 print_block_info(pr);
602 for (i = 2; i < argc; i++) {
605 if (stat(argv[i], &s)) {
606 fprintf(stderr, "failed to stat %s\n", argv[i]);
609 if (!S_ISBLK(s.st_mode)) {
610 fprintf(stderr, "%s is not a block device\n", argv[i]);
613 pr = find_block_info(NULL, NULL, argv[i]);
615 print_block_info(pr);
621 static int main_swapon(int argc, char **argv)
624 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
628 if (!strcmp(argv[1], "-a")) {
629 struct blkid_struct_probe *pr;
632 list_for_each_entry(pr, &devices, list) {
633 if (strcmp(pr->id->name, "swap"))
635 if (swapon(pr->dev, 0))
636 fprintf(stderr, "failed to swapon %s\n", pr->dev);
642 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
643 fprintf(stderr, "%s is not a block device\n", argv[1]);
646 err = swapon(argv[1], 0);
648 fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
656 static int main_swapoff(int argc, char **argv)
659 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
663 if (!strcmp(argv[1], "-a")) {
664 FILE *fp = fopen("/proc/swaps", "r");
668 fprintf(stderr, "failed to open /proc/swaps\n");
671 fgets(line, sizeof(line), fp);
672 while (fgets(line, sizeof(line), fp)) {
673 char *end = strchr(line, ' ');
681 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
688 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
689 fprintf(stderr, "%s is not a block device\n", argv[1]);
692 err = swapoff(argv[1]);
694 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
702 int main(int argc, char **argv)
704 char *base = basename(*argv);
706 if (!strcmp(base, "swapon"))
707 return main_swapon(argc, argv);
709 if (!strcmp(base, "swapoff"))
710 return main_swapoff(argc, argv);
712 if ((argc > 1) && !strcmp(base, "block")) {
713 if (!strcmp(argv[1], "info"))
714 return main_info(argc, argv);
716 if (!strcmp(argv[1], "detect"))
717 return main_detect(argc, argv);
719 if (!strcmp(argv[1], "hotplug"))
720 return main_hotplug(argc, argv);
722 if (!strcmp(argv[1], "extroot"))
723 return main_extroot(argc, argv);
726 fprintf(stderr, "Usage: block <info|detect>\n");