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.
24 #include <sys/types.h>
26 #include <sys/mount.h>
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/avl-cmp.h>
37 #include "libblkid-tiny/libblkid-tiny.h"
39 #define ERROR(fmt, ...) do { \
40 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
41 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
50 struct vlist_node node;
66 static struct vlist_tree mounts;
67 static struct blob_buf b;
68 static LIST_HEAD(devices);
69 static int anon_mount, anon_swap, auto_mount, auto_swap, check_fs;
70 static unsigned int delay_root;
82 static const struct blobmsg_policy config_policy[__CFG_MAX] = {
83 [CFG_ANON_SWAP] = { .name = "anon_swap", .type = BLOBMSG_TYPE_INT32 },
84 [CFG_ANON_MOUNT] = { .name = "anon_mount", .type = BLOBMSG_TYPE_INT32 },
85 [CFG_AUTO_SWAP] = { .name = "auto_swap", .type = BLOBMSG_TYPE_INT32 },
86 [CFG_AUTO_MOUNT] = { .name = "auto_mount", .type = BLOBMSG_TYPE_INT32 },
87 [CFG_DELAY_ROOT] = { .name = "delay_root", .type = BLOBMSG_TYPE_INT32 },
88 [CFG_CHECK_FS] = { .name = "check_fs", .type = BLOBMSG_TYPE_INT32 },
101 static const struct uci_blob_param_list config_attr_list = {
102 .n_params = __CFG_MAX,
103 .params = config_policy,
106 static const struct blobmsg_policy mount_policy[__MOUNT_MAX] = {
107 [MOUNT_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
108 [MOUNT_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
109 [MOUNT_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
110 [MOUNT_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
111 [MOUNT_OPTIONS] = { .name = "options", .type = BLOBMSG_TYPE_STRING },
112 [MOUNT_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
115 static const struct uci_blob_param_list mount_attr_list = {
116 .n_params = __MOUNT_MAX,
117 .params = mount_policy,
129 static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
130 [SWAP_ENABLE] = { .name = "enabled", .type = BLOBMSG_TYPE_INT32 },
131 [SWAP_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
132 [SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
133 [SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
134 [SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
137 static const struct uci_blob_param_list swap_attr_list = {
138 .n_params = __SWAP_MAX,
139 .params = swap_policy,
148 # define MS_DIRSYNC (1 << 7)
152 # define MS_RELATIME (1 << 21)
155 #ifndef MS_STRICTATIME
156 # define MS_STRICTATIME (1 << 24)
159 static const struct mount_flag mount_flags[] = {
160 { "sync", MS_SYNCHRONOUS },
161 { "async", ~MS_SYNCHRONOUS },
162 { "dirsync", MS_DIRSYNC },
163 { "mand", MS_MANDLOCK },
164 { "nomand", ~MS_MANDLOCK },
165 { "atime", ~MS_NOATIME },
166 { "noatime", MS_NOATIME },
167 { "dev", ~MS_NODEV },
168 { "nodev", MS_NODEV },
169 { "diratime", ~MS_NODIRATIME },
170 { "nodiratime", MS_NODIRATIME },
171 { "exec", ~MS_NOEXEC },
172 { "noexec", MS_NOEXEC },
173 { "suid", ~MS_NOSUID },
174 { "nosuid", MS_NOSUID },
175 { "rw", ~MS_RDONLY },
177 { "relatime", MS_RELATIME },
178 { "norelatime", ~MS_RELATIME },
179 { "strictatime", MS_STRICTATIME },
182 static char *blobmsg_get_strdup(struct blob_attr *attr)
187 return strdup(blobmsg_get_string(attr));
190 static char *blobmsg_get_basename(struct blob_attr *attr)
195 return strdup(basename(blobmsg_get_string(attr)));
198 static void parse_mount_options(struct mount *m, char *optstr)
202 char *p, *opts, *last;
207 if (!optstr || !*optstr)
210 m->options = opts = calloc(1, strlen(optstr) + 1);
223 for (i = 0, is_flag = false; i < ARRAY_SIZE(mount_flags); i++) {
224 if (!strcmp(last, mount_flags[i].name)) {
225 if (mount_flags[i].flag < 0)
226 m->flags &= (uint32_t)mount_flags[i].flag;
228 m->flags |= (uint32_t)mount_flags[i].flag;
235 opts += sprintf(opts, "%s%s", (opts > m->options) ? "," : "", last);
244 static int mount_add(struct uci_section *s)
246 struct blob_attr *tb[__MOUNT_MAX] = { 0 };
249 blob_buf_init(&b, 0);
250 uci_to_blob(&b, s, &mount_attr_list);
251 blobmsg_parse(mount_policy, __MOUNT_MAX, tb, blob_data(b.head), blob_len(b.head));
253 if (!tb[MOUNT_LABEL] && !tb[MOUNT_UUID] && !tb[MOUNT_DEVICE])
256 if (tb[MOUNT_ENABLE] && !blobmsg_get_u32(tb[MOUNT_ENABLE]))
259 m = malloc(sizeof(struct mount));
260 m->type = TYPE_MOUNT;
261 m->uuid = blobmsg_get_strdup(tb[MOUNT_UUID]);
262 m->label = blobmsg_get_strdup(tb[MOUNT_LABEL]);
263 m->target = blobmsg_get_strdup(tb[MOUNT_TARGET]);
264 m->device = blobmsg_get_basename(tb[MOUNT_DEVICE]);
266 parse_mount_options(m, blobmsg_get_strdup(tb[MOUNT_OPTIONS]));
268 m->overlay = m->extroot = 0;
269 if (m->target && !strcmp(m->target, "/"))
271 if (m->target && !strcmp(m->target, "/overlay"))
272 m->extroot = m->overlay = 1;
275 vlist_add(&mounts, &m->node, m->uuid);
277 vlist_add(&mounts, &m->node, m->label);
279 vlist_add(&mounts, &m->node, m->device);
284 static int swap_add(struct uci_section *s)
286 struct blob_attr *tb[__SWAP_MAX] = { 0 };
289 blob_buf_init(&b, 0);
290 uci_to_blob(&b, s, &swap_attr_list);
291 blobmsg_parse(swap_policy, __SWAP_MAX, tb, blob_data(b.head), blob_len(b.head));
293 if (!tb[SWAP_UUID] && !tb[SWAP_LABEL] && !tb[SWAP_DEVICE])
296 m = malloc(sizeof(struct mount));
297 memset(m, 0, sizeof(struct mount));
299 m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
300 m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
301 m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
303 m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
305 m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
307 if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
308 /* store complete swap path */
310 m->target = blobmsg_get_strdup(tb[SWAP_DEVICE]);
313 vlist_add(&mounts, &m->node, m->uuid);
315 vlist_add(&mounts, &m->node, m->label);
317 vlist_add(&mounts, &m->node, m->device);
323 static int global_add(struct uci_section *s)
325 struct blob_attr *tb[__CFG_MAX] = { 0 };
327 blob_buf_init(&b, 0);
328 uci_to_blob(&b, s, &config_attr_list);
329 blobmsg_parse(config_policy, __CFG_MAX, tb, blob_data(b.head), blob_len(b.head));
331 if ((tb[CFG_ANON_MOUNT]) && blobmsg_get_u32(tb[CFG_ANON_MOUNT]))
333 if ((tb[CFG_ANON_SWAP]) && blobmsg_get_u32(tb[CFG_ANON_SWAP]))
336 if ((tb[CFG_AUTO_MOUNT]) && blobmsg_get_u32(tb[CFG_AUTO_MOUNT]))
338 if ((tb[CFG_AUTO_SWAP]) && blobmsg_get_u32(tb[CFG_AUTO_SWAP]))
341 if (tb[CFG_DELAY_ROOT])
342 delay_root = blobmsg_get_u32(tb[CFG_DELAY_ROOT]);
344 if ((tb[CFG_CHECK_FS]) && blobmsg_get_u32(tb[CFG_CHECK_FS]))
350 static struct mount* find_swap(const char *uuid, const char *label, const char *device)
354 vlist_for_each_element(&mounts, m, node) {
355 if (m->type != TYPE_SWAP)
357 if (uuid && m->uuid && !strcmp(m->uuid, uuid))
359 if (label && m->label && !strcmp(m->label, label))
361 if (device && m->device && !strcmp(m->device, device))
368 static struct mount* find_block(const char *uuid, const char *label, const char *device,
373 vlist_for_each_element(&mounts, m, node) {
374 if (m->type != TYPE_MOUNT)
376 if (m->uuid && uuid && !strcmp(m->uuid, uuid))
378 if (m->label && label && !strcmp(m->label, label))
380 if (m->target && target && !strcmp(m->target, target))
382 if (m->device && device && !strcmp(m->device, device))
389 static void mounts_update(struct vlist_tree *tree, struct vlist_node *node_new,
390 struct vlist_node *node_old)
394 static int config_load(char *cfg)
396 struct uci_context *ctx;
397 struct uci_package *pkg;
398 struct uci_element *e;
400 vlist_init(&mounts, avl_strcmp, mounts_update);
402 ctx = uci_alloc_context();
405 snprintf(path, 32, "%s/etc/config", cfg);
406 uci_set_confdir(ctx, path);
409 if (uci_load(ctx, "fstab", &pkg))
412 uci_get_errorstr(ctx, &err, "fstab");
413 ERROR("extroot: failed to load %s/etc/config/%s\n",
414 cfg ? cfg : "", err);
419 vlist_update(&mounts);
420 uci_foreach_element(&pkg->sections, e) {
421 struct uci_section *s = uci_to_section(e);
423 if (!strcmp(s->type, "mount"))
425 if (!strcmp(s->type, "swap"))
427 if (!strcmp(s->type, "global"))
430 vlist_flush(&mounts);
435 static struct blkid_struct_probe* _probe_path(char *path)
437 struct blkid_struct_probe *pr;
439 pr = malloc(sizeof(*pr));
444 memset(pr, 0, sizeof(*pr));
445 probe_block(path, pr);
447 if (pr->err || !pr->id) {
455 static int _cache_load(const char *path)
457 int gl_flags = GLOB_NOESCAPE | GLOB_MARK;
461 if (glob(path, gl_flags, NULL, &gl) < 0)
464 for (j = 0; j < gl.gl_pathc; j++) {
465 struct blkid_struct_probe *pr = _probe_path(gl.gl_pathv[j]);
467 list_add_tail(&pr->list, &devices);
475 static void cache_load(int mtd)
478 _cache_load("/dev/mtdblock*");
479 _cache_load("/dev/ubiblock*");
481 _cache_load("/dev/mmcblk*");
482 _cache_load("/dev/sd*");
483 _cache_load("/dev/hd*");
484 _cache_load("/dev/md*");
485 _cache_load("/dev/mapper/*");
488 static int print_block_info(struct blkid_struct_probe *pr)
490 printf("%s:", pr->dev);
492 printf(" UUID=\"%s\"", pr->uuid);
495 printf(" LABEL=\"%s\"", pr->label);
498 printf(" NAME=\"%s\"", pr->name);
501 printf(" VERSION=\"%s\"", pr->version);
503 printf(" TYPE=\"%s\"\n", pr->id->name);
508 static int print_block_uci(struct blkid_struct_probe *pr)
510 if (!strcmp(pr->id->name, "swap")) {
511 printf("config 'swap'\n");
513 printf("config 'mount'\n");
514 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr->dev));
517 printf("\toption\tuuid\t'%s'\n", pr->uuid);
519 printf("\toption\tdevice\t'%s'\n", pr->dev);
520 printf("\toption\tenabled\t'0'\n\n");
525 static struct blkid_struct_probe* find_block_info(char *uuid, char *label, char *path)
527 struct blkid_struct_probe *pr = NULL;
530 list_for_each_entry(pr, &devices, list)
531 if (!strcmp(pr->uuid, uuid))
535 list_for_each_entry(pr, &devices, list)
536 if (!strcmp(pr->label, label))
540 list_for_each_entry(pr, &devices, list)
541 if (!strcmp(basename(pr->dev), basename(path)))
547 static char* find_mount_point(char *block)
549 FILE *fp = fopen("/proc/mounts", "r");
550 static char line[256];
551 int len = strlen(block);
557 while (fgets(line, sizeof(line), fp)) {
558 if (!strncmp(line, block, len)) {
559 char *p = &line[len + 1];
560 char *t = strstr(p, " ");
577 static void mkdir_p(char *dir)
579 char *l = strrchr(dir, '/');
589 static void check_filesystem(struct blkid_struct_probe *pr)
593 char *e2fsck = "/usr/sbin/e2fsck";
595 if (strncmp(pr->id->name, "ext", 3)) {
596 ERROR("check_filesystem: %s is not supported\n", pr->id->name);
600 if (stat(e2fsck, &statbuf) < 0) {
601 ERROR("check_filesystem: %s not found\n", e2fsck);
607 execl(e2fsck, e2fsck, "-p", pr->dev, NULL);
609 } else if (pid > 0) {
612 waitpid(pid, &status, 0);
613 if (WEXITSTATUS(status))
614 ERROR("check_filesystem: %s returned %d\n", e2fsck, WEXITSTATUS(status));
618 static void handle_swapfiles(bool on)
622 struct blkid_struct_probe *pr;
624 vlist_for_each_element(&mounts, m, node)
626 if (m->type != TYPE_SWAP || !m->target)
629 if (stat(m->target, &s) || !S_ISREG(s.st_mode))
632 pr = _probe_path(m->target);
637 if (!strcmp(pr->id->name, "swap")) {
639 swapon(pr->dev, m->prio);
648 static int mount_device(struct blkid_struct_probe *pr, int hotplug)
656 device = basename(pr->dev);
658 if (!strcmp(pr->id->name, "swap")) {
659 if (hotplug && !auto_swap)
661 m = find_swap(pr->uuid, pr->label, device);
663 swapon(pr->dev, (m) ? (m->prio) : (0));
668 if (hotplug && !auto_mount)
671 if (find_mount_point(pr->dev)) {
672 ERROR("%s is already mounted\n", pr->dev);
676 m = find_block(pr->uuid, pr->label, device, NULL);
681 char *target = m->target;
686 snprintf(_target, sizeof(_target), "/mnt/%s", device);
692 check_filesystem(pr);
694 err = mount(pr->dev, target, pr->id->name, m->flags,
695 (m->options) ? (m->options) : (""));
697 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
698 pr->dev, pr->id->name, target, err, strerror(err));
700 handle_swapfiles(true);
705 char target[] = "/mnt/mmcblk123";
708 snprintf(target, sizeof(target), "/mnt/%s", device);
712 check_filesystem(pr);
714 err = mount(pr->dev, target, pr->id->name, 0, "");
716 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
717 pr->dev, pr->id->name, target, err, strerror(err));
719 handle_swapfiles(true);
726 static int umount_device(struct blkid_struct_probe *pr)
729 char *device = basename(pr->dev);
736 if (!strcmp(pr->id->name, "swap"))
739 mp = find_mount_point(pr->dev);
743 m = find_block(pr->uuid, pr->label, device, NULL);
747 err = umount2(mp, MNT_DETACH);
749 ERROR("unmounting %s (%s) failed (%d) - %s\n",
750 pr->dev, mp, err, strerror(err));
752 ERROR("unmounted %s (%s)\n",
758 static int main_hotplug(int argc, char **argv)
761 char *action, *device, *mount_point;
763 action = getenv("ACTION");
764 device = getenv("DEVNAME");
766 if (!action || !device)
768 snprintf(path, sizeof(path), "/dev/%s", device);
770 if (!strcmp(action, "remove")) {
772 mount_point = find_mount_point(path);
774 err = umount2(mount_point, MNT_DETACH);
777 ERROR("umount of %s failed (%d) - %s\n",
778 mount_point, err, strerror(err));
781 } else if (strcmp(action, "add")) {
782 ERROR("Unkown action %s\n", action);
787 if (config_load(NULL))
791 return mount_device(find_block_info(NULL, NULL, path), 1);
794 static int find_block_mtd(char *name, char *part, int plen)
796 FILE *fp = fopen("/proc/mtd", "r");
797 static char line[256];
803 while (!index && fgets(line, sizeof(line), fp)) {
804 if (strstr(line, name)) {
805 char *eol = strstr(line, ":");
820 snprintf(part, plen, "/dev/mtdblock%s", index);
825 static int check_extroot(char *path)
827 struct blkid_struct_probe *pr = NULL;
830 if (find_block_mtd("rootfs", fs, sizeof(fs)))
833 list_for_each_entry(pr, &devices, list) {
834 if (!strcmp(pr->dev, fs)) {
838 char uuid[64] = { 0 };
840 snprintf(tag, sizeof(tag), "%s/etc/.extroot-uuid", path);
842 fp = fopen(tag, "w+");
844 ERROR("extroot: failed to write uuid tag file\n");
845 /* return 0 to continue boot regardless of error */
853 fp = fopen(tag, "r");
855 ERROR("extroot: failed to open uuid tag file\n");
859 fgets(uuid, sizeof(uuid), fp);
861 if (!strcmp(uuid, pr->uuid))
863 ERROR("extroot: uuid tag does not match rom uuid\n");
869 static int mount_extroot(char *cfg)
871 char overlay[] = "/tmp/extroot/overlay";
872 char mnt[] = "/tmp/extroot/mnt";
874 struct blkid_struct_probe *pr;
878 if (config_load(cfg))
881 m = find_block(NULL, NULL, NULL, "/");
883 m = find_block(NULL, NULL, NULL, "/overlay");
885 if (!m || !m->extroot)
887 ERROR("extroot: no root or overlay mount defined\n");
891 pr = find_block_info(m->uuid, m->label, m->device);
893 if (!pr && delay_root){
894 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root);
898 pr = find_block_info(m->uuid, m->label, m->device);
901 if (strncmp(pr->id->name, "ext", 3)) {
902 ERROR("extroot: %s is not supported, try ext4\n", pr->id->name);
910 check_filesystem(pr);
912 err = mount(pr->dev, path, pr->id->name, 0, (m->options) ? (m->options) : (""));
915 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
916 pr->dev, pr->id->name, path, err, strerror(err));
917 } else if (m->overlay) {
918 err = check_extroot(path);
923 ERROR("extroot: cannot find block device\n");
929 static int main_extroot(int argc, char **argv)
931 struct blkid_struct_probe *pr;
933 char fs_data[32] = { 0 };
936 if (!getenv("PREINIT"))
940 fprintf(stderr, "Usage: block extroot mountpoint\n");
947 find_block_mtd("rootfs", fs, sizeof(fs));
949 ERROR("extroot: unable to locate rootfs mtdblock\n");
953 pr = find_block_info(NULL, NULL, fs);
955 ERROR("extroot: unable to retrieve rootfs information\n");
959 find_block_mtd("rootfs_data", fs_data, sizeof(fs_data));
961 pr = find_block_info(NULL, NULL, fs_data);
962 if (pr && !strcmp(pr->id->name, "jffs2")) {
963 char cfg[] = "/tmp/jffs_cfg";
966 if (!mount(fs_data, cfg, "jffs2", MS_NOATIME, NULL)) {
967 err = mount_extroot(cfg);
968 umount2(cfg, MNT_DETACH);
971 rmdir("/tmp/overlay");
977 return mount_extroot(NULL);
980 static int main_mount(int argc, char **argv)
982 struct blkid_struct_probe *pr;
984 if (config_load(NULL))
988 list_for_each_entry(pr, &devices, list)
991 handle_swapfiles(true);
996 static int main_umount(int argc, char **argv)
998 struct blkid_struct_probe *pr;
1000 if (config_load(NULL))
1003 handle_swapfiles(false);
1006 list_for_each_entry(pr, &devices, list)
1012 static int main_detect(int argc, char **argv)
1014 struct blkid_struct_probe *pr;
1017 printf("config 'global'\n");
1018 printf("\toption\tanon_swap\t'0'\n");
1019 printf("\toption\tanon_mount\t'0'\n");
1020 printf("\toption\tauto_swap\t'1'\n");
1021 printf("\toption\tauto_mount\t'1'\n");
1022 printf("\toption\tdelay_root\t'5'\n");
1023 printf("\toption\tcheck_fs\t'0'\n\n");
1024 list_for_each_entry(pr, &devices, list)
1025 print_block_uci(pr);
1030 static int main_info(int argc, char **argv)
1033 struct blkid_struct_probe *pr;
1037 list_for_each_entry(pr, &devices, list)
1038 print_block_info(pr);
1043 for (i = 2; i < argc; i++) {
1046 if (stat(argv[i], &s)) {
1047 ERROR("failed to stat %s\n", argv[i]);
1050 if (!S_ISBLK(s.st_mode)) {
1051 ERROR("%s is not a block device\n", argv[i]);
1054 pr = find_block_info(NULL, NULL, argv[i]);
1056 print_block_info(pr);
1062 static int swapon_usage(void)
1064 fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1065 "\tStart swapping on [DEVICE]\n"
1066 " -a\tStart swapping on all swap devices\n"
1067 " -p pri\tSet priority of swap device\n"
1068 " -s\tShow summary\n");
1072 static int main_swapon(int argc, char **argv)
1078 struct blkid_struct_probe *pr;
1084 while ((ch = getopt(argc, argv, "ap:s")) != -1) {
1087 fp = fopen("/proc/swaps", "r");
1091 ERROR("failed to open /proc/swaps\n");
1094 while (getline(&lineptr, &s, fp) > 0)
1102 list_for_each_entry(pr, &devices, list) {
1103 if (strcmp(pr->id->name, "swap"))
1105 if (swapon(pr->dev, 0))
1106 ERROR("failed to swapon %s\n", pr->dev);
1112 flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
1115 return swapon_usage();
1120 if (optind != (argc - 1))
1121 return swapon_usage();
1123 if (stat(argv[optind], &st) || (!S_ISBLK(st.st_mode) && !S_ISREG(st.st_mode))) {
1124 ERROR("%s is not a block device or file\n", argv[optind]);
1127 err = swapon(argv[optind], flags);
1129 ERROR("failed to swapon %s (%d)\n", argv[optind], err);
1136 static int main_swapoff(int argc, char **argv)
1139 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1140 "\tStop swapping on DEVICE\n"
1141 " -a\tStop swapping on all swap devices\n");
1145 if (!strcmp(argv[1], "-a")) {
1146 FILE *fp = fopen("/proc/swaps", "r");
1150 ERROR("failed to open /proc/swaps\n");
1153 fgets(line, sizeof(line), fp);
1154 while (fgets(line, sizeof(line), fp)) {
1155 char *end = strchr(line, ' ');
1161 err = swapoff(line);
1163 ERROR("failed to swapoff %s (%d)\n", line, err);
1170 if (stat(argv[1], &s) || (!S_ISBLK(s.st_mode) && !S_ISREG(s.st_mode))) {
1171 ERROR("%s is not a block device or file\n", argv[1]);
1174 err = swapoff(argv[1]);
1176 ERROR("fsiled to swapoff %s (%d)\n", argv[1], err);
1184 int main(int argc, char **argv)
1186 char *base = basename(*argv);
1190 if (!strcmp(base, "swapon"))
1191 return main_swapon(argc, argv);
1193 if (!strcmp(base, "swapoff"))
1194 return main_swapoff(argc, argv);
1196 if ((argc > 1) && !strcmp(base, "block")) {
1197 if (!strcmp(argv[1], "info"))
1198 return main_info(argc, argv);
1200 if (!strcmp(argv[1], "detect"))
1201 return main_detect(argc, argv);
1203 if (!strcmp(argv[1], "hotplug"))
1204 return main_hotplug(argc, argv);
1206 if (!strcmp(argv[1], "extroot"))
1207 return main_extroot(argc, argv);
1209 if (!strcmp(argv[1], "mount"))
1210 return main_mount(argc, argv);
1212 if (!strcmp(argv[1], "umount"))
1213 return main_umount(argc, argv);
1216 fprintf(stderr, "Usage: block <info|mount|umount|detect>\n");