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*");
259 static int print_block_info(struct blkid_struct_probe *pr)
261 printf("%s:", pr->dev);
263 printf(" UUID=\"%s\"", pr->uuid);
266 printf(" LABEL=\"%s\"", pr->label);
269 printf(" NAME=\"%s\"", pr->name);
272 printf(" VERSION=\"%s\"", pr->version);
274 printf(" TYPE=\"%s\"\n", pr->id->name);
279 static int print_block_uci(struct blkid_struct_probe *pr)
284 if (!strcmp(pr->id->name, "swap")) {
285 printf("config 'swap'\n");
287 printf("config 'mount'\n");
288 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
290 printf("\toption\tuuid\t'%s'\n", pr->uuid);
291 printf("\toption\tenabled\t'0'\n\n");
296 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
298 struct blkid_struct_probe *pr = NULL;
301 list_for_each_entry(pr, &devices, list)
302 if (!strcmp(pr->uuid, uuid))
306 list_for_each_entry(pr, &devices, list)
307 if (strcmp(pr->label, label))
311 list_for_each_entry(pr, &devices, list)
312 if (!strcmp(pr->dev, path))
318 static char* find_mount_point(char *block)
320 FILE *fp = fopen("/proc/mounts", "r");
321 static char line[256];
322 int len = strlen(block);
328 while (fgets(line, sizeof(line), fp)) {
329 if (!strncmp(line, block, len)) {
330 char *p = &line[len + 1];
331 char *t = strstr(p, " ");
346 static void mkdir_p(char *dir)
348 char *l = strrchr(dir, '/');
358 static int main_hotplug(int argc, char **argv)
362 char *action, *device, *mount_point;
363 struct blkid_struct_probe *pr;
365 action = getenv("ACTION");
366 device = getenv("DEVNAME");
368 if (!action || !device)
370 snprintf(path, sizeof(path), "/dev/%s", device);
372 if (!strcmp(action, "remove")) {
374 mount_point = find_mount_point(path);
376 err = umount2(mount_point, MNT_DETACH);
379 fprintf(stderr, "unmount of %s failed (%d) - %s\n",
380 mount_point, err, strerror(err));
383 } else if (strcmp(action, "add")) {
384 fprintf(stderr, "Unkown action %s\n", action);
389 if (config_load(NULL))
393 pr = find_block_info(NULL, NULL, path);
394 if (!pr && pr->uuid) {
395 fprintf(stderr, "failed to read blockinfo for %s\n", path);
399 m = find_swap(pr->uuid);
401 if (!strcmp(action, "add"))
402 swapon(path, m->prio);
406 m = find_block(pr->uuid, pr->label, NULL);
407 if (m && strcmp(m->target, "/")) {
411 err = mount(path, m->target, pr->id->name, 0, (m->options) ? (m->options) : (""));
413 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
414 path, pr->id->name, m->target, err, strerror(err));
421 static int find_block_mtd(char *name, char *part, int plen)
423 FILE *fp = fopen("/proc/mtd", "r");
424 static char line[256];
430 while (!index && fgets(line, sizeof(line), fp)) {
431 if (strstr(line, name)) {
432 char *eol = strstr(line, ":");
447 snprintf(part, plen, "/dev/mtdblock%s", index);
452 static int mount_extroot(char *path, char *cfg)
454 struct blkid_struct_probe *pr;
458 if (config_load(cfg))
461 m = find_block(NULL, NULL, "/");
465 pr = find_block_info(m->uuid, m->label, NULL);
468 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
471 fprintf(stderr, "mounting %s (%s) as %s failed (%d) - %s\n",
472 pr->dev, pr->id->name, path, err, strerror(err));
478 static int main_extroot(int argc, char **argv)
480 struct blkid_struct_probe *pr;
482 char fs_data[32] = { 0 };
484 char extroot[] = "/tmp/overlay";
486 if (!getenv("PREINIT"))
490 fprintf(stderr, "Usage: block extroot mountpoint\n");
497 find_block_mtd("rootfs", fs, sizeof(fs));
501 pr = find_block_info(NULL, NULL, fs);
502 if (!pr || strcmp(pr->id->name, "squashfs"))
505 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
507 pr = find_block_info(NULL, NULL, fs_data);
508 if (pr && !strcmp(pr->id->name, "jffs2")) {
509 char cfg[] = "/tmp/jffs_cfg";
512 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
513 err = mount_extroot(extroot, cfg);
514 umount2(cfg, MNT_DETACH);
523 err = mount_extroot(extroot, NULL);
528 static int main_detect(int argc, char **argv)
530 struct blkid_struct_probe *pr;
533 list_for_each_entry(pr, &devices, list)
539 static int main_info(int argc, char **argv)
542 struct blkid_struct_probe *pr;
546 list_for_each_entry(pr, &devices, list)
547 print_block_info(pr);
552 for (i = 2; i < argc; i++) {
555 if (stat(argv[i], &s)) {
556 fprintf(stderr, "failed to stat %s\n", argv[i]);
559 if (!S_ISBLK(s.st_mode)) {
560 fprintf(stderr, "%s is not a block device\n", argv[i]);
563 pr = find_block_info(NULL, NULL, argv[i]);
565 print_block_info(pr);
571 static int main_swapon(int argc, char **argv)
574 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
578 if (!strcmp(argv[1], "-a")) {
579 struct blkid_struct_probe *pr;
582 list_for_each_entry(pr, &devices, list) {
583 if (strcmp(pr->id->name, "swap"))
585 if (swapon(pr->dev, 0))
586 fprintf(stderr, "failed to swapon %s\n", pr->dev);
592 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
593 fprintf(stderr, "%s is not a block device\n", argv[1]);
596 err = swapon(argv[1], 0);
598 fprintf(stderr, "failed to swapon %s (%d)\n", argv[1], err);
606 static int main_swapoff(int argc, char **argv)
609 fprintf(stderr, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
613 if (!strcmp(argv[1], "-a")) {
614 FILE *fp = fopen("/proc/swaps", "r");
618 fprintf(stderr, "failed to open /proc/swaps\n");
621 fgets(line, sizeof(line), fp);
622 while (fgets(line, sizeof(line), fp)) {
623 char *end = strchr(line, ' ');
631 fprintf(stderr, "failed to swapoff %s (%d)\n", line, err);
638 if (stat(argv[1], &s) || !S_ISBLK(s.st_mode)) {
639 fprintf(stderr, "%s is not a block device\n", argv[1]);
642 err = swapoff(argv[1]);
644 fprintf(stderr, "fsiled to swapoff %s (%d)\n", argv[1], err);
652 int main(int argc, char **argv)
654 char *base = basename(*argv);
656 if (!strcmp(base, "swapon"))
657 return main_swapon(argc, argv);
659 if (!strcmp(base, "swapoff"))
660 return main_swapoff(argc, argv);
662 if ((argc > 1) && !strcmp(base, "block")) {
663 if (!strcmp(argv[1], "info"))
664 return main_info(argc, argv);
666 if (!strcmp(argv[1], "detect"))
667 return main_detect(argc, argv);
669 if (!strcmp(argv[1], "hotplug"))
670 return main_hotplug(argc, argv);
672 if (!strcmp(argv[1], "extroot"))
673 return main_extroot(argc, argv);
676 fprintf(stderr, "Usage: block <info|detect>\n");