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/list.h>
36 #include <libubox/vlist.h>
37 #include <libubox/blobmsg_json.h>
38 #include <libubox/avl-cmp.h>
40 #include "libblkid-tiny/libblkid-tiny.h"
43 #include "libubi/libubi.h"
48 static void klog(int prio, const char *fmt, ...)
51 FILE *f = fopen("/dev/kmsg", "w");
54 fprintf(f, "<%d>", prio);
64 #define KINFO(fmt, ...) do { \
65 if (kmsg) klog(LOG_INFO, "block: " fmt, ## __VA_ARGS__); \
68 #define ERROR(fmt, ...) do { \
70 klog(LOG_ERR, "block: " fmt, ## __VA_ARGS__); \
72 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
73 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
83 struct vlist_node node;
99 static struct vlist_tree mounts;
100 static struct blob_buf b;
101 static LIST_HEAD(devices);
102 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
103 static unsigned int delay_root;
115 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
116 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
117 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
118 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
119 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
120 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
121 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
134 static const struct uci_blob_param_list config_attr_list = {
135 .n_params = __CFG_MAX,
136 .params = config_policy,
139 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
140 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
141 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
142 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
143 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
144 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
145 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
148 static const struct uci_blob_param_list mount_attr_list = {
149 .n_params = __MOUNT_MAX,
150 .params = mount_policy,
162 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
163 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
164 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
165 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
166 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
167 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
170 static const struct uci_blob_param_list swap_attr_list = {
171 .n_params = __SWAP_MAX,
172 .params = swap_policy,
181 # define MS_DIRSYNC (1 << 7)
185 # define MS_RELATIME (1 << 21)
188 #ifndef MS_STRICTATIME
189 # define MS_STRICTATIME (1 << 24)
192 static const struct mount_flag mount_flags[] = {
193 { "sync", MS_SYNCHRONOUS },
194 { "async", ~MS_SYNCHRONOUS },
195 { "dirsync", MS_DIRSYNC },
196 { "mand", MS_MANDLOCK },
197 { "nomand", ~MS_MANDLOCK },
198 { "atime", ~MS_NOATIME },
199 { "noatime", MS_NOATIME },
200 { "dev", ~MS_NODEV },
201 { "nodev", MS_NODEV },
202 { "diratime", ~MS_NODIRATIME },
203 { "nodiratime", MS_NODIRATIME },
204 { "exec", ~MS_NOEXEC },
205 { "noexec", MS_NOEXEC },
206 { "suid", ~MS_NOSUID },
207 { "nosuid", MS_NOSUID },
208 { "rw", ~MS_RDONLY },
210 { "relatime", MS_RELATIME },
211 { "norelatime", ~MS_RELATIME },
212 { "strictatime", MS_STRICTATIME },
215 static char *blobmsg_get_strdup(struct blob_attr *attr)
220 return strdup(blobmsg_get_string(attr));
223 static char *blobmsg_get_basename(struct blob_attr *attr)
228 return strdup(basename(blobmsg_get_string(attr)));
231 static void parse_mount_options(struct mount *m, char *optstr)
235 char *p, *opts, *last;
240 if (!optstr || !*optstr)
243 m->options = opts = calloc(1, strlen(optstr) + 1);
256 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
257 if (!strcmp(last, mount_flags[i].name)) {
258 if (mount_flags[i].flag < 0)
259 m->flags &= (uint32_t)mount_flags[i].flag;
261 m->flags |= (uint32_t)mount_flags[i].flag;
268 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
277 static int mount_add(struct uci_section *s)
279 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
282 blob_buf_init(&b, 0);
283 uci_to_blob(&b, s, &mount_attr_list);
284 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
286 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
289 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
292 m = malloc(sizeof(struct mount));
293 m->type = TYPE_MOUNT;
294 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
295 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
296 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
297 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
299 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
301 m->overlay = m->extroot = 0;
302 if (m->target && !strcmp(m->target, "/"))
304 if (m->target && !strcmp(m->target, "/overlay"))
305 m->extroot = m->overlay = 1;
308 vlist_add(&mounts, &m->node, m->uuid);
310 vlist_add(&mounts, &m->node, m->label);
312 vlist_add(&mounts, &m->node, m->device);
317 static int swap_add(struct uci_section *s)
319 struct blob_attr *tb[__SWAP_MAX] = { 0 };
322 blob_buf_init(&b, 0);
323 uci_to_blob(&b, s, &swap_attr_list);
324 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
326 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
329 m = malloc(sizeof(struct mount));
330 memset(m, 0, sizeof(struct mount));
332 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
333 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
334 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
336 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
338 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
340 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
341 /* store complete swap path */
343 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
346 vlist_add(&mounts, &m->node, m->uuid);
348 vlist_add(&mounts, &m->node, m->label);
350 vlist_add(&mounts, &m->node, m->device);
356 static int global_add(struct uci_section *s)
358 struct blob_attr *tb[__CFG_MAX] = { 0 };
360 blob_buf_init(&b, 0);
361 uci_to_blob(&b, s, &config_attr_list);
362 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
364 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
366 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
369 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
371 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
374 if (tb[CFG_DELAY_ROOT])
375 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
377 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
383 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
387 vlist_for_each_element(&mounts, m, node) {
388 if (m->type != TYPE_SWAP)
390 if (uuid && m->uuid && !strcasecmp(m->uuid, uuid))
392 if (label && m->label && !strcmp(m->label, label))
394 if (device && m->device && !strcmp(m->device, device))
401 static struct mount* find_block(const char *uuid, const char *label, const char *device,
406 vlist_for_each_element(&mounts, m, node) {
407 if (m->type != TYPE_MOUNT)
409 if (m->uuid && uuid && !strcasecmp(m->uuid, uuid))
411 if (m->label && label && !strcmp(m->label, label))
413 if (m->target && target && !strcmp(m->target, target))
415 if (m->device && device && !strcmp(m->device, device))
422 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
423 struct vlist_node *node_old)
427 static struct uci_package * config_try_load(struct uci_context *ctx, char *path)
429 char *file = basename(path);
430 char *dir = dirname(path);
432 struct uci_package *pkg;
434 uci_set_confdir(ctx, dir);
435 KINFO("attempting to load %s/%s\n", dir, file);
437 if (uci_load(ctx, file, &pkg)) {
438 uci_get_errorstr(ctx, &err, file);
439 ERROR("unable to load configuration (%s)\n", err);
448 static int config_load(char *cfg)
450 struct uci_context *ctx = uci_alloc_context();
451 struct uci_package *pkg = NULL;
452 struct uci_element *e;
455 vlist_init(&mounts, avl_strcmp, mounts_update);
458 snprintf(path, sizeof(path), "%s/upper/etc/config/fstab", cfg);
459 pkg = config_try_load(ctx, path);
462 snprintf(path, sizeof(path), "%s/etc/config/fstab", cfg);
463 pkg = config_try_load(ctx, path);
468 snprintf(path, sizeof(path), "/etc/config/fstab");
469 pkg = config_try_load(ctx, path);
473 ERROR("no usable configuration\n");
477 vlist_update(&mounts);
478 uci_foreach_element(&pkg->sections, e) {
479 struct uci_section *s = uci_to_section(e);
481 if (!strcmp(s->type, "mount"))
483 if (!strcmp(s->type, "swap"))
485 if (!strcmp(s->type, "global"))
488 vlist_flush(&mounts);
493 static struct blkid_struct_probe* _probe_path(char *path)
495 struct blkid_struct_probe *pr;
497 pr = malloc(sizeof(*pr));
502 memset(pr, 0, sizeof(*pr));
503 probe_block(path, pr);
505 if (pr->err || !pr->id) {
513 static int _cache_load(const char *path)
515 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
519 if (glob(path, gl_flags, NULL, &gl) < 0)
522 for (j = 0; j < gl.gl_pathc; j++) {
523 struct blkid_struct_probe *pr = _probe_path(gl.gl_pathv[j]);
525 list_add_tail(&pr->list, &devices);
533 static void cache_load(int mtd)
536 _cache_load("/dev/mtdblock*");
537 _cache_load("/dev/ubiblock*");
538 _cache_load("/dev/ubi?*_?*");
540 _cache_load("/dev/mmcblk*");
541 _cache_load("/dev/sd*");
542 _cache_load("/dev/hd*");
543 _cache_load("/dev/md*");
544 _cache_load("/dev/vd*");
545 _cache_load("/dev/mapper/*");
548 static int print_block_info(struct blkid_struct_probe *pr)
550 printf("%s:", pr->dev);
552 printf(" UUID=\"%s\"", pr->uuid);
555 printf(" LABEL=\"%s\"", pr->label);
558 printf(" NAME=\"%s\"", pr->name);
561 printf(" VERSION=\"%s\"", pr->version);
563 printf(" TYPE=\"%s\"\n", pr->id->name);
568 static int print_block_uci(struct blkid_struct_probe *pr)
570 if (!strcmp(pr->id->name, "swap")) {
571 printf("config 'swap'\n");
573 printf("config 'mount'\n");
574 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
577 printf("\toption\tuuid\t'%s'\n", pr->uuid);
579 printf("\toption\tdevice\t'%s'\n", pr->dev);
580 printf("\toption\tenabled\t'0'\n\n");
585 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
587 struct blkid_struct_probe *pr = NULL;
590 list_for_each_entry(pr, &devices, list)
591 if (!strcasecmp(pr->uuid, uuid))
595 list_for_each_entry(pr, &devices, list)
596 if (!strcmp(pr->label, label))
600 list_for_each_entry(pr, &devices, list)
601 if (!strcmp(basename(pr->dev), basename(path)))
607 static char* find_mount_point(char *block)
609 FILE *fp = fopen("/proc/mounts", "r");
610 static char line[256];
611 int len = strlen(block);
617 while (fgets(line, sizeof(line), fp)) {
618 if (!strncmp(line, block, len)) {
619 char *p = &line[len + 1];
620 char *t = strstr(p, " ");
637 static void mkdir_p(char *dir)
639 char *l = strrchr(dir, '/');
649 static void check_filesystem(struct blkid_struct_probe *pr)
653 char *e2fsck = "/usr/sbin/e2fsck";
655 /* UBIFS does not need stuff like fsck */
656 if (!strncmp(pr->id->name, "ubifs", 5))
659 if (strncmp(pr->id->name, "ext", 3)) {
660 ERROR("check_filesystem: %s is not supported\n", pr->id->name);
664 if (stat(e2fsck, &statbuf) < 0) {
665 ERROR("check_filesystem: %s not found\n", e2fsck);
671 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
673 } else if (pid > 0) {
676 waitpid(pid, &status, 0);
677 if (WEXITSTATUS(status))
678 ERROR("check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
682 static void handle_swapfiles(bool on)
686 struct blkid_struct_probe *pr;
688 vlist_for_each_element(&mounts, m, node)
690 if (m->type != TYPE_SWAP || !m->target)
693 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
696 pr = _probe_path(m->target);
701 if (!strcmp(pr->id->name, "swap")) {
703 swapon(pr->dev, m->prio);
712 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
720 device = basename(pr->dev);
722 if (!strcmp(pr->id->name, "swap")) {
723 if (hotplug && !auto_swap)
725 m = find_swap(pr->uuid, pr->label, device);
727 swapon(pr->dev, (m) ? (m->prio) : (0));
732 if (hotplug && !auto_mount)
735 if (find_mount_point(pr->dev)) {
736 ERROR("%s is already mounted\n", pr->dev);
740 m = find_block(pr->uuid, pr->label, device, NULL);
745 char *target = m->target;
750 snprintf(_target, sizeof(_target), "/mnt/%s", device);
756 check_filesystem(pr);
758 err = mount(pr->dev, target, pr->id->name, m->flags,
759 (m->options) ? (m->options) : (""));
761 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
762 pr->dev, pr->id->name, target, err, strerror(err));
764 handle_swapfiles(true);
769 char target[] = "/mnt/mmcblk123";
772 snprintf(target, sizeof(target), "/mnt/%s", device);
776 check_filesystem(pr);
778 err = mount(pr->dev, target, pr->id->name, 0, "");
780 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
781 pr->dev, pr->id->name, target, err, strerror(err));
783 handle_swapfiles(true);
790 static int umount_device(struct blkid_struct_probe *pr)
793 char *device = basename(pr->dev);
800 if (!strcmp(pr->id->name, "swap"))
803 mp = find_mount_point(pr->dev);
807 m = find_block(pr->uuid, pr->label, device, NULL);
811 err = umount2(mp, MNT_DETACH);
813 ERROR("unmounting %s (%s) failed (%d) - %s\n",
814 pr->dev, mp, err, strerror(err));
816 ERROR("unmounted %s (%s)\n",
822 static int main_hotplug(int argc, char **argv)
825 char *action, *device, *mount_point;
827 action = getenv("ACTION");
828 device = getenv("DEVNAME");
830 if (!action || !device)
832 snprintf(path, sizeof(path), "/dev/%s", device);
834 if (!strcmp(action, "remove")) {
836 mount_point = find_mount_point(path);
838 err = umount2(mount_point, MNT_DETACH);
841 ERROR("umount of %s failed (%d) - %s\n",
842 mount_point, err, strerror(err));
845 } else if (strcmp(action, "add")) {
846 ERROR("Unkown action %s\n", action);
851 if (config_load(NULL))
855 return mount_device(find_block_info(NULL, NULL, path), 1);
858 static int find_block_mtd(char *name, char *part, int plen)
860 FILE *fp = fopen("/proc/mtd", "r");
861 static char line[256];
867 while (!index && fgets(line, sizeof(line), fp)) {
868 if (strstr(line, name)) {
869 char *eol = strstr(line, ":");
884 snprintf(part, plen, "/dev/mtdblock%s", index);
890 static int find_ubi_vol(libubi_t libubi, char *name, int *dev_num, int *vol_id)
894 while (ubi_dev_present(libubi, dev))
896 struct ubi_dev_info dev_info;
897 struct ubi_vol_info vol_info;
899 if (ubi_get_dev_info1(libubi, dev++, &dev_info))
901 if (ubi_get_vol_info1_nm(libubi, dev_info.dev_num, name, &vol_info))
904 *dev_num = dev_info.dev_num;
905 *vol_id = vol_info.vol_id;
913 static int find_block_ubi(libubi_t libubi, char *name, char *part, int plen)
919 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
921 snprintf(part, plen, "/dev/ubi%d_%d", dev_num, vol_id);
926 static int find_block_ubi_RO(libubi_t libubi, char *name, char *part, int plen)
932 err = find_ubi_vol(libubi, name, &dev_num, &vol_id);
934 snprintf(part, plen, "/dev/ubiblock%d_%d", dev_num, vol_id);
941 static int find_root_dev(char *buf, int len)
951 if (!(d = opendir("/dev")))
956 while ((e = readdir(d)) != NULL) {
957 snprintf(buf, len, "/dev/%s", e->d_name);
959 if (stat(buf, &s) || s.st_rdev != root)
972 static int test_fs_support(const char *name)
978 if ((f = fopen("/proc/filesystems", "r")) != NULL) {
979 while (fgets(line, sizeof(line), f)) {
980 p = strtok(line, "\t\n");
982 if (p && !strcmp(p, "nodev"))
983 p = strtok(NULL, "\t\n");
985 if (p && !strcmp(p, name)) {
996 static int check_extroot(char *path)
998 struct blkid_struct_probe *pr = NULL;
1001 #ifdef UBIFS_EXTROOT
1002 if (find_block_mtd("rootfs", devpath, sizeof(devpath))) {
1006 libubi = libubi_open();
1007 err = find_block_ubi_RO(libubi, "rootfs", devpath, sizeof(devpath));
1008 libubi_close(libubi);
1013 if (find_block_mtd("rootfs", devpath, sizeof(devpath))) {
1014 if (find_root_dev(devpath, sizeof(devpath))) {
1015 ERROR("extroot: unable to determine root device\n");
1021 list_for_each_entry(pr, &devices, list) {
1022 if (!strcmp(pr->dev, devpath)) {
1026 char uuid[64] = { 0 };
1028 snprintf(tag, sizeof(tag), "%s/etc", path);
1032 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
1033 if (stat(tag, &s)) {
1034 fp = fopen(tag, "w+");
1036 ERROR("extroot: failed to write UUID to %s: %d (%s)\n",
1037 tag, errno, strerror(errno));
1038 /* return 0 to continue boot regardless of error */
1041 fputs(pr->uuid, fp);
1046 fp = fopen(tag, "r");
1048 ERROR("extroot: failed to read UUID from %s: %d (%s)\n",
1049 tag, errno, strerror(errno));
1053 fgets(uuid, sizeof(uuid), fp);
1056 if (!strcasecmp(uuid, pr->uuid))
1059 ERROR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1060 pr->uuid, basename(path), uuid);
1065 ERROR("extroot: unable to lookup root device %s\n", devpath);
1070 * Read info about extroot from UCI (using prefix) and mount it.
1072 static int mount_extroot(char *cfg)
1074 char overlay[] = "/tmp/extroot/overlay";
1075 char mnt[] = "/tmp/extroot/mnt";
1077 struct blkid_struct_probe *pr;
1081 /* Load @cfg/etc/config/fstab */
1082 if (config_load(cfg))
1085 /* See if there is extroot-specific mount config */
1086 m = find_block(NULL, NULL, NULL, "/");
1088 m = find_block(NULL, NULL, NULL, "/overlay");
1090 if (!m || !m->extroot)
1092 KINFO("extroot: not configured\n");
1096 /* Find block device pointed by the mount config */
1097 pr = find_block_info(m->uuid, m->label, m->device);
1099 if (!pr && delay_root){
1100 KINFO("extroot: device not present, retrying in %u seconds\n", delay_root);
1104 pr = find_block_info(m->uuid, m->label, m->device);
1107 if (strncmp(pr->id->name, "ext", 3) &&
1108 strncmp(pr->id->name, "ubifs", 5)) {
1109 ERROR("extroot: unsupported filesystem %s, try ext4\n", pr->id->name);
1113 if (test_fs_support(pr->id->name)) {
1114 ERROR("extroot: filesystem %s not supported by kernel\n", pr->id->name);
1123 check_filesystem(pr);
1125 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
1128 ERROR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1129 pr->dev, pr->id->name, path, err, strerror(err));
1130 } else if (m->overlay) {
1131 err = check_extroot(path);
1136 ERROR("extroot: cannot find device %s%s\n",
1137 (m->uuid ? "with UUID " : (m->label ? "with label " : "")),
1138 (m->uuid ? m->uuid : (m->label ? m->label : m->device)));
1144 static int main_extroot(int argc, char **argv)
1146 struct blkid_struct_probe *pr;
1147 char blkdev_path[32] = { 0 };
1149 #ifdef UBIFS_EXTROOT
1153 if (!getenv("PREINIT"))
1157 fprintf(stderr, "Usage: block extroot\n");
1167 * Look for "rootfs_data". We will want to mount it and check for
1168 * extroot configuration.
1171 /* Start with looking for MTD partition */
1172 find_block_mtd("rootfs_data", blkdev_path, sizeof(blkdev_path));
1173 if (blkdev_path[0]) {
1174 pr = find_block_info(NULL, NULL, blkdev_path);
1175 if (pr && !strcmp(pr->id->name, "jffs2")) {
1176 char cfg[] = "/tmp/jffs_cfg";
1179 * Mount MTD part and try extroot (using
1180 * /etc/config/fstab from that partition)
1183 if (!mount(blkdev_path, cfg, "jffs2", MS_NOATIME, NULL)) {
1184 err = mount_extroot(cfg);
1185 umount2(cfg, MNT_DETACH);
1188 rmdir("/tmp/overlay");
1194 #ifdef UBIFS_EXTROOT
1195 /* ... but it also could be an UBI volume */
1196 memset(blkdev_path, 0, sizeof(blkdev_path));
1197 libubi = libubi_open();
1198 find_block_ubi(libubi, "rootfs_data", blkdev_path, sizeof(blkdev_path));
1199 libubi_close(libubi);
1200 if (blkdev_path[0]) {
1201 char cfg[] = "/tmp/ubifs_cfg";
1203 /* Mount volume and try extroot (using fstab from that vol) */
1205 if (!mount(blkdev_path, cfg, "ubifs", MS_NOATIME, NULL)) {
1206 err = mount_extroot(cfg);
1207 umount2(cfg, MNT_DETACH);
1210 rmdir("/tmp/overlay");
1216 return mount_extroot(NULL);
1219 static int main_mount(int argc, char **argv)
1221 struct blkid_struct_probe *pr;
1223 if (config_load(NULL))
1227 list_for_each_entry(pr, &devices, list)
1228 mount_device(pr, 0);
1230 handle_swapfiles(true);
1235 static int main_umount(int argc, char **argv)
1237 struct blkid_struct_probe *pr;
1239 if (config_load(NULL))
1242 handle_swapfiles(false);
1245 list_for_each_entry(pr, &devices, list)
1251 static int main_detect(int argc, char **argv)
1253 struct blkid_struct_probe *pr;
1256 printf("config 'global'\n");
1257 printf("\toption\tanon_swap\t'0'\n");
1258 printf("\toption\tanon_mount\t'0'\n");
1259 printf("\toption\tauto_swap\t'1'\n");
1260 printf("\toption\tauto_mount\t'1'\n");
1261 printf("\toption\tdelay_root\t'5'\n");
1262 printf("\toption\tcheck_fs\t'0'\n\n");
1263 list_for_each_entry(pr, &devices, list)
1264 print_block_uci(pr);
1269 static int main_info(int argc, char **argv)
1272 struct blkid_struct_probe *pr;
1276 list_for_each_entry(pr, &devices, list)
1277 print_block_info(pr);
1282 for (i = 2; i < argc; i++) {
1285 if (stat(argv[i], &s)) {
1286 ERROR("failed to stat %s\n", argv[i]);
1289 if (!S_ISBLK(s.st_mode)) {
1290 ERROR("%s is not a block device\n", argv[i]);
1293 pr = find_block_info(NULL, NULL, argv[i]);
1295 print_block_info(pr);
1301 static int swapon_usage(void)
1303 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1304 "\tStart swapping on [DEVICE]\n"
1305 " -a\tStart swapping on all swap devices\n"
1306 " -p pri\tSet priority of swap device\n"
1307 " -s\tShow summary\n");
1311 static int main_swapon(int argc, char **argv)
1317 struct blkid_struct_probe *pr;
1323 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1326 fp = fopen("/proc/swaps", "r");
1330 ERROR("failed to open /proc/swaps\n");
1333 while (getline(&lineptr, &s, fp) > 0)
1341 list_for_each_entry(pr, &devices, list) {
1342 if (strcmp(pr->id->name, "swap"))
1344 if (swapon(pr->dev, 0))
1345 ERROR("failed to swapon %s\n", pr->dev);
1351 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1354 return swapon_usage();
1359 if (optind != (argc - 1))
1360 return swapon_usage();
1362 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1363 ERROR("%s is not a block device or file\n", argv[optind]);
1366 err = swapon(argv[optind], flags);
1368 ERROR("failed to swapon %s (%d)\n", argv[optind], err);
1375 static int main_swapoff(int argc, char **argv)
1378 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1379 "\tStop swapping on DEVICE\n"
1380 " -a\tStop swapping on all swap devices\n");
1384 if (!strcmp(argv[1], "-a")) {
1385 FILE *fp = fopen("/proc/swaps", "r");
1389 ERROR("failed to open /proc/swaps\n");
1392 fgets(line, sizeof(line), fp);
1393 while (fgets(line, sizeof(line), fp)) {
1394 char *end = strchr(line, ' ');
1400 err = swapoff(line);
1402 ERROR("failed to swapoff %s (%d)\n", line, err);
1409 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1410 ERROR("%s is not a block device or file\n", argv[1]);
1413 err = swapoff(argv[1]);
1415 ERROR("fsiled to swapoff %s (%d)\n", argv[1], err);
1423 int main(int argc, char **argv)
1425 char *base = basename(*argv);
1429 if (!strcmp(base, "swapon"))
1430 return main_swapon(argc, argv);
1432 if (!strcmp(base, "swapoff"))
1433 return main_swapoff(argc, argv);
1435 if ((argc > 1) && !strcmp(base, "block")) {
1436 if (!strcmp(argv[1], "info"))
1437 return main_info(argc, argv);
1439 if (!strcmp(argv[1], "detect"))
1440 return main_detect(argc, argv);
1442 if (!strcmp(argv[1], "hotplug"))
1443 return main_hotplug(argc, argv);
1445 if (!strcmp(argv[1], "extroot"))
1446 return main_extroot(argc, argv);
1448 if (!strcmp(argv[1], "mount"))
1449 return main_mount(argc, argv);
1451 if (!strcmp(argv[1], "umount"))
1452 return main_umount(argc, argv);
1455 fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");