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.
30 #include <sys/types.h>
32 #include <sys/mount.h>
34 #include <sys/sysmacros.h>
41 #include <libubox/avl-cmp.h>
42 #include <libubox/blobmsg_json.h>
43 #include <libubox/list.h>
44 #include <libubox/ulog.h>
45 #include <libubox/utils.h>
46 #include <libubox/vlist.h>
51 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
54 #include "libubi/libubi.h"
69 struct vlist_node node
;
86 static struct vlist_tree mounts
;
87 static struct blob_buf b
;
88 static LIST_HEAD(devices
);
89 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
90 static unsigned int delay_root
;
102 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
103 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
104 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
105 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
106 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
107 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
108 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
122 static const struct uci_blob_param_list config_attr_list
= {
123 .n_params
= __CFG_MAX
,
124 .params
= config_policy
,
127 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
128 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
129 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
130 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
131 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
132 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
133 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
134 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
137 static const struct uci_blob_param_list mount_attr_list
= {
138 .n_params
= __MOUNT_MAX
,
139 .params
= mount_policy
,
151 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
152 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
153 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
154 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
155 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
156 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
159 static const struct uci_blob_param_list swap_attr_list
= {
160 .n_params
= __SWAP_MAX
,
161 .params
= swap_policy
,
169 static const struct mount_flag mount_flags
[] = {
170 { "sync", MS_SYNCHRONOUS
},
171 { "async", ~MS_SYNCHRONOUS
},
172 { "dirsync", MS_DIRSYNC
},
173 { "mand", MS_MANDLOCK
},
174 { "nomand", ~MS_MANDLOCK
},
175 { "atime", ~MS_NOATIME
},
176 { "noatime", MS_NOATIME
},
177 { "dev", ~MS_NODEV
},
178 { "nodev", MS_NODEV
},
179 { "diratime", ~MS_NODIRATIME
},
180 { "nodiratime", MS_NODIRATIME
},
181 { "exec", ~MS_NOEXEC
},
182 { "noexec", MS_NOEXEC
},
183 { "suid", ~MS_NOSUID
},
184 { "nosuid", MS_NOSUID
},
185 { "rw", ~MS_RDONLY
},
187 { "relatime", MS_RELATIME
},
188 { "norelatime", ~MS_RELATIME
},
189 { "strictatime", MS_STRICTATIME
},
190 { "acl", MS_POSIXACL
},
191 { "noacl", ~MS_POSIXACL
},
192 { "nouser_xattr", MS_NOUSER
},
193 { "user_xattr", ~MS_NOUSER
},
196 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
201 return strdup(blobmsg_get_string(attr
));
204 static char *blobmsg_get_basename(struct blob_attr
*attr
)
209 return strdup(basename(blobmsg_get_string(attr
)));
212 static void parse_mount_options(struct mount
*m
, char *optstr
)
216 char *p
, *opts
, *last
;
221 if (!optstr
|| !*optstr
)
224 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
237 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
238 if (!strcmp(last
, mount_flags
[i
].name
)) {
239 if (mount_flags
[i
].flag
< 0)
240 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
242 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
249 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
258 static int mount_add(struct uci_section
*s
)
260 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
263 blob_buf_init(&b
, 0);
264 uci_to_blob(&b
, s
, &mount_attr_list
);
265 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
267 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
270 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
273 m
= malloc(sizeof(struct mount
));
274 m
->type
= TYPE_MOUNT
;
275 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
276 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
277 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
278 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
279 if (tb
[MOUNT_AUTOFS
])
280 m
->autofs
= blobmsg_get_u32(tb
[MOUNT_AUTOFS
]);
283 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
285 m
->overlay
= m
->extroot
= 0;
286 if (m
->target
&& !strcmp(m
->target
, "/"))
288 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
289 m
->extroot
= m
->overlay
= 1;
291 if (m
->target
&& *m
->target
!= '/') {
292 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
293 s
->e
.name
, m
->target
);
299 vlist_add(&mounts
, &m
->node
, m
->uuid
);
301 vlist_add(&mounts
, &m
->node
, m
->label
);
303 vlist_add(&mounts
, &m
->node
, m
->device
);
308 static int swap_add(struct uci_section
*s
)
310 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
313 blob_buf_init(&b
, 0);
314 uci_to_blob(&b
, s
, &swap_attr_list
);
315 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
317 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
320 m
= malloc(sizeof(struct mount
));
321 memset(m
, 0, sizeof(struct mount
));
323 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
324 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
325 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
327 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
329 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
331 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
332 /* store complete swap path */
334 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
337 vlist_add(&mounts
, &m
->node
, m
->uuid
);
339 vlist_add(&mounts
, &m
->node
, m
->label
);
341 vlist_add(&mounts
, &m
->node
, m
->device
);
347 static int global_add(struct uci_section
*s
)
349 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
351 blob_buf_init(&b
, 0);
352 uci_to_blob(&b
, s
, &config_attr_list
);
353 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
355 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
357 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
360 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
362 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
365 if (tb
[CFG_DELAY_ROOT
])
366 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
368 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
374 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
378 vlist_for_each_element(&mounts
, m
, node
) {
379 if (m
->type
!= TYPE_SWAP
)
381 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
383 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
385 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
392 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
397 vlist_for_each_element(&mounts
, m
, node
) {
398 if (m
->type
!= TYPE_MOUNT
)
400 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
402 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
404 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
406 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
413 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
414 struct vlist_node
*node_old
)
418 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
420 char *file
= basename(path
);
421 char *dir
= dirname(path
);
423 struct uci_package
*pkg
;
425 uci_set_confdir(ctx
, dir
);
426 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
428 if (uci_load(ctx
, file
, &pkg
)) {
429 uci_get_errorstr(ctx
, &err
, file
);
430 ULOG_ERR("unable to load configuration (%s)\n", err
);
439 static int config_load(char *cfg
)
441 struct uci_context
*ctx
= uci_alloc_context();
442 struct uci_package
*pkg
= NULL
;
443 struct uci_element
*e
;
446 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
449 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
450 pkg
= config_try_load(ctx
, path
);
453 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
454 pkg
= config_try_load(ctx
, path
);
459 snprintf(path
, sizeof(path
), "/etc/config/fstab");
460 pkg
= config_try_load(ctx
, path
);
464 ULOG_ERR("no usable configuration\n");
468 vlist_update(&mounts
);
469 uci_foreach_element(&pkg
->sections
, e
) {
470 struct uci_section
*s
= uci_to_section(e
);
472 if (!strcmp(s
->type
, "mount"))
474 if (!strcmp(s
->type
, "swap"))
476 if (!strcmp(s
->type
, "global"))
479 vlist_flush(&mounts
);
484 static struct probe_info
* _probe_path(char *path
)
486 struct probe_info
*pr
, *epr
;
489 pr
= probe_path(path
);
493 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
494 path
[8] >= '0' && path
[8] <= '9' ) {
495 /* skip ubi device if not UBIFS (as it requires ubiblock) */
496 if (strcmp("ubifs", pr
->type
))
499 /* skip ubi device if ubiblock device is present */
500 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
501 list_for_each_entry(epr
, &devices
, list
)
502 if (!strcmp(epr
->dev
, tmppath
))
509 static int _cache_load(const char *path
)
511 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
515 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
518 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
519 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
521 list_add_tail(&pr
->list
, &devices
);
529 static void cache_load(int mtd
)
532 _cache_load("/dev/mtdblock*");
533 _cache_load("/dev/ubiblock*");
534 _cache_load("/dev/ubi[0-9]*");
536 _cache_load("/dev/loop*");
537 _cache_load("/dev/mmcblk*");
538 _cache_load("/dev/sd*");
539 _cache_load("/dev/hd*");
540 _cache_load("/dev/md*");
541 _cache_load("/dev/nvme*");
542 _cache_load("/dev/vd*");
543 _cache_load("/dev/xvd*");
544 _cache_load("/dev/dm-*");
548 static struct probe_info
* find_block_info(char *uuid
, char *label
, char *path
)
550 struct probe_info
*pr
= NULL
;
553 list_for_each_entry(pr
, &devices
, list
)
554 if (pr
->uuid
&& !strcasecmp(pr
->uuid
, uuid
))
558 list_for_each_entry(pr
, &devices
, list
)
559 if (pr
->label
&& !strcmp(pr
->label
, label
))
563 list_for_each_entry(pr
, &devices
, list
)
564 if (pr
->dev
&& !strcmp(basename(pr
->dev
), basename(path
)))
570 static char* find_mount_point(char *block
)
572 FILE *fp
= fopen("/proc/self/mountinfo", "r");
573 static char line
[256];
574 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
577 unsigned int minor
, major
;
582 rstat
= stat(block
, &s
);
584 while (fgets(line
, sizeof(line
), fp
)) {
585 pos
= strchr(line
, ' ');
589 pos
= strchr(pos
+ 1, ' ');
594 pos
= strchr(pos
, ':');
601 pos
= strchr(pos
, ' ');
607 pos
= strchr(pos
+ 1, ' ');
612 pos
= strchr(pos
, ' ');
618 pos
= strchr(pos
+ 1, ' ');
622 pos
= strchr(pos
+ 1, ' ');
626 pos
= strchr(pos
+ 1, ' ');
631 pos
= strchr(pos
, ' ');
637 if (!strcmp(block
, devname
)) {
638 point
= strdup(cpoint
);
645 if (!S_ISBLK(s
.st_mode
))
648 if (major
== major(s
.st_rdev
) &&
649 minor
== minor(s
.st_rdev
)) {
650 point
= strdup(cpoint
);
660 static int print_block_uci(struct probe_info
*pr
)
662 if (!strcmp(pr
->type
, "swap")) {
663 printf("config 'swap'\n");
665 char *mp
= find_mount_point(pr
->dev
);
667 printf("config 'mount'\n");
669 printf("\toption\ttarget\t'%s'\n", mp
);
672 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
676 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
678 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
679 printf("\toption\tenabled\t'0'\n\n");
684 static int print_block_info(struct probe_info
*pr
)
688 mp
= find_mount_point(pr
->dev
);
689 printf("%s:", pr
->dev
);
691 printf(" UUID=\"%s\"", pr
->uuid
);
694 printf(" LABEL=\"%s\"", pr
->label
);
697 printf(" VERSION=\"%s\"", pr
->version
);
700 printf(" MOUNT=\"%s\"", mp
);
704 printf(" TYPE=\"%s\"\n", pr
->type
);
709 static void check_filesystem(struct probe_info
*pr
)
713 const char *e2fsck
= "/usr/sbin/e2fsck";
714 const char *f2fsck
= "/usr/sbin/fsck.f2fs";
715 const char *fatfsck
= "/usr/sbin/fsck.fat";
716 const char *btrfsck
= "/usr/bin/btrfsck";
717 const char *ntfsck
= "/usr/bin/ntfsfix";
720 /* UBIFS does not need stuff like fsck */
721 if (!strncmp(pr
->type
, "ubifs", 5))
724 if (!strncmp(pr
->type
, "vfat", 4)) {
726 } else if (!strncmp(pr
->type
, "f2fs", 4)) {
728 } else if (!strncmp(pr
->type
, "ext", 3)) {
730 } else if (!strncmp(pr
->type
, "btrfs", 5)) {
732 } else if (!strncmp(pr
->type
, "ntfs", 4)) {
735 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
739 if (stat(ckfs
, &statbuf
) < 0) {
740 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
746 if(!strncmp(pr
->type
, "f2fs", 4)) {
747 execl(ckfs
, ckfs
, "-f", pr
->dev
, NULL
);
749 } else if(!strncmp(pr
->type
, "btrfs", 5)) {
750 execl(ckfs
, ckfs
, "--repair", pr
->dev
, NULL
);
752 } else if(!strncmp(pr
->type
, "ntfs", 4)) {
753 execl(ckfs
, ckfs
, "-b", pr
->dev
, NULL
);
756 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
759 } else if (pid
> 0) {
762 waitpid(pid
, &status
, 0);
763 if (WIFEXITED(status
) && WEXITSTATUS(status
))
764 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
765 if (WIFSIGNALED(status
))
766 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs
, strsignal(WTERMSIG(status
)));
770 static void handle_swapfiles(bool on
)
774 struct probe_info
*pr
;
776 vlist_for_each_element(&mounts
, m
, node
)
778 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
781 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
784 pr
= _probe_path(m
->target
);
789 if (!strcmp(pr
->type
, "swap")) {
791 swapon(pr
->dev
, m
->prio
);
800 static void to_devnull(int fd
)
802 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
807 if (devnull
> STDERR_FILENO
)
811 static int exec_mount(const char *source
, const char *target
,
812 const char *fstype
, const char *options
)
817 int err
, status
, pfds
[2];
818 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
820 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
822 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
823 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
830 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
831 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
843 to_devnull(STDIN_FILENO
);
844 to_devnull(STDOUT_FILENO
);
846 dup2(pfds
[1], STDERR_FILENO
);
850 if (options
&& *options
)
851 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
853 execl(cmd
, cmd
, source
, target
, NULL
);
860 mount_fd
= fdopen(pfds
[0], "r");
862 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
863 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
867 err
= waitpid(pid
, &status
, 0);
871 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
886 static int handle_mount(const char *source
, const char *target
,
887 const char *fstype
, struct mount
*m
)
890 size_t mount_opts_len
;
891 char *mount_opts
= NULL
, *ptr
;
893 err
= mount(source
, target
, fstype
, m
? m
->flags
: 0,
894 (m
&& m
->options
) ? m
->options
: "");
896 /* Requested file system type is not available in kernel,
897 attempt to call mount helper. */
898 if (err
== -1 && errno
== ENODEV
) {
900 /* Convert mount flags back into string representation,
901 first calculate needed length of string buffer... */
902 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
904 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
905 if ((mount_flags
[i
].flag
> 0) &&
906 (mount_flags
[i
].flag
< INT_MAX
) &&
907 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
908 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
910 /* ... then now allocate and fill it ... */
911 ptr
= mount_opts
= calloc(1, mount_opts_len
);
919 ptr
+= sprintf(ptr
, "%s,", m
->options
);
921 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
922 if ((mount_flags
[i
].flag
> 0) &&
923 (mount_flags
[i
].flag
< INT_MAX
) &&
924 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
925 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
927 mount_opts
[mount_opts_len
- 1] = 0;
930 /* ... and now finally invoke the external mount program */
931 err
= exec_mount(source
, target
, fstype
, mount_opts
);
937 static int blockd_notify(const char *method
, char *device
, struct mount
*m
,
938 struct probe_info
*pr
)
940 struct ubus_context
*ctx
= ubus_connect(NULL
);
947 if (!ubus_lookup_id(ctx
, "block", &id
)) {
948 struct blob_buf buf
= { 0 };
949 char *d
= strrchr(device
, '/');
956 blob_buf_init(&buf
, 0);
960 blobmsg_add_string(&buf
, "device", d
);
962 blobmsg_add_string(&buf
, "uuid", m
->uuid
);
964 blobmsg_add_string(&buf
, "label", m
->label
);
966 blobmsg_add_string(&buf
, "target", m
->target
);
968 blobmsg_add_string(&buf
, "options", m
->options
);
970 blobmsg_add_u32(&buf
, "autofs", m
->autofs
);
972 blobmsg_add_string(&buf
, "type", pr
->type
);
974 blobmsg_add_string(&buf
, "version", pr
->version
);
976 blobmsg_add_string(&buf
, "device", d
);
978 blobmsg_add_string(&buf
, "uuid", pr
->uuid
);
980 blobmsg_add_string(&buf
, "label", pr
->label
);
982 blobmsg_add_string(&buf
, "type", pr
->type
);
984 blobmsg_add_string(&buf
, "version", pr
->version
);
985 blobmsg_add_u32(&buf
, "anon", 1);
987 blobmsg_add_string(&buf
, "device", d
);
988 blobmsg_add_u32(&buf
, "remove", 1);
991 err
= ubus_invoke(ctx
, id
, method
, buf
.head
, NULL
, NULL
, 3000);
1001 static int mount_device(struct probe_info
*pr
, int type
)
1005 char *_target
= NULL
;
1014 device
= basename(pr
->dev
);
1016 if (!strcmp(pr
->type
, "swap")) {
1017 if ((type
== TYPE_HOTPLUG
) && !auto_swap
)
1019 m
= find_swap(pr
->uuid
, pr
->label
, device
);
1021 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
1026 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
1027 if (m
&& m
->extroot
)
1030 mp
= find_mount_point(pr
->dev
);
1032 if (m
&& m
->type
== TYPE_MOUNT
&& m
->target
&& strcmp(m
->target
, mp
)) {
1033 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
1041 if (type
== TYPE_HOTPLUG
)
1042 blockd_notify("hotplug", device
, m
, pr
);
1044 /* Check if device should be mounted & set the target directory */
1064 if (asprintf(&_target
, "/tmp/run/blockd/%s", device
) == -1)
1068 } else if (m
->target
) {
1071 if (asprintf(&_target
, "/mnt/%s", device
) == -1)
1076 } else if (anon_mount
) {
1077 if (asprintf(&_target
, "/mnt/%s", device
) == -1)
1082 /* No reason to mount this device */
1086 /* Mount the device */
1089 check_filesystem(pr
);
1091 mkdir_p(target
, 0755);
1092 if (!lstat(target
, &st
) && S_ISLNK(st
.st_mode
))
1095 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
1097 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1098 pr
->dev
, pr
->type
, target
, errno
);
1109 handle_swapfiles(true);
1111 if (type
!= TYPE_AUTOFS
)
1112 blockd_notify("mount", device
, NULL
, NULL
);
1117 static int umount_device(char *path
, int type
, bool all
)
1122 if (strlen(path
) > 5 && !strncmp("/dev/", path
, 5)) {
1123 mp
= find_mount_point(path
);
1125 devpath
= malloc(strlen(path
) + 6);
1126 strcpy(devpath
, "/dev/");
1127 strcat(devpath
, path
);
1128 mp
= find_mount_point(devpath
);
1134 if (!strcmp(mp
, "/") && !all
)
1137 if (type
!= TYPE_AUTOFS
)
1138 blockd_notify("umount", basename(path
), NULL
, NULL
);
1140 err
= umount2(mp
, MNT_DETACH
);
1142 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path
, mp
,
1145 ULOG_INFO("unmounted %s (%s)\n", path
, mp
);
1153 static int mount_action(char *action
, char *device
, int type
)
1156 struct probe_info
*pr
;
1158 if (!action
|| !device
)
1161 if (!strcmp(action
, "remove")) {
1162 if (type
== TYPE_HOTPLUG
)
1163 blockd_notify("hotplug", device
, NULL
, NULL
);
1165 umount_device(device
, type
, true);
1168 } else if (strcmp(action
, "add")) {
1169 ULOG_ERR("Unkown action %s\n", action
);
1174 if (config_load(NULL
))
1179 list_for_each_entry(pr
, &devices
, list
)
1180 if (!strcmp(basename(pr
->dev
), device
))
1186 return mount_device(find_block_info(NULL
, NULL
, path
), type
);
1189 static int main_hotplug(int argc
, char **argv
)
1191 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG
);
1194 static int main_autofs(int argc
, char **argv
)
1201 if (!strcmp(argv
[2], "start")) {
1202 struct probe_info
*pr
;
1204 if (config_load(NULL
))
1208 list_for_each_entry(pr
, &devices
, list
) {
1212 if (!strcmp(pr
->type
, "swap"))
1215 m
= find_block(pr
->uuid
, pr
->label
, NULL
, NULL
);
1216 if (m
&& m
->extroot
)
1219 blockd_notify("hotplug", pr
->dev
, m
, pr
);
1220 if ((!m
|| !m
->autofs
) && (mp
= find_mount_point(pr
->dev
))) {
1221 blockd_notify("mount", pr
->dev
, NULL
, NULL
);
1229 err
= mount_action(argv
[2], argv
[3], TYPE_AUTOFS
);
1233 ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv
[2], err
);
1239 static int find_block_mtd(char *name
, char *part
, int plen
)
1241 FILE *fp
= fopen("/proc/mtd", "r");
1242 static char line
[256];
1248 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1249 if (strstr(line
, name
)) {
1250 char *eol
= strstr(line
, ":");
1265 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1270 #ifdef UBIFS_EXTROOT
1271 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1275 while (ubi_dev_present(libubi
, dev
))
1277 struct ubi_dev_info dev_info
;
1278 struct ubi_vol_info vol_info
;
1280 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1282 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1285 *dev_num
= dev_info
.dev_num
;
1286 *vol_id
= vol_info
.vol_id
;
1294 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1300 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1302 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1307 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1313 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1315 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1321 static int find_root_dev(char *buf
, int len
)
1331 if (!(d
= opendir("/dev")))
1336 while ((e
= readdir(d
)) != NULL
) {
1337 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1339 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1350 static int test_fs_support(const char *name
)
1356 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1357 while (fgets(line
, sizeof(line
), f
)) {
1358 p
= strtok(line
, "\t\n");
1360 if (p
&& !strcmp(p
, "nodev"))
1361 p
= strtok(NULL
, "\t\n");
1363 if (p
&& !strcmp(p
, name
)) {
1375 * Check if mounted partition is a valid extroot
1377 * @path target mount point
1379 * Valid extroot partition has to contain /etc/.extroot-uuid with UUID of root
1380 * device. This function reads UUID and verifies it OR writes UUID to
1381 * .extroot-uuid if it doesn't exist yet (first extroot usage).
1383 static int check_extroot(char *path
)
1385 struct probe_info
*pr
= NULL
;
1386 struct probe_info
*tmp
;
1388 char uuid
[64] = { 0 };
1394 err
= find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
));
1395 #ifdef UBIFS_EXTROOT
1399 libubi
= libubi_open();
1400 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1401 libubi_close(libubi
);
1405 err
= find_root_dev(devpath
, sizeof(devpath
));
1408 ULOG_ERR("extroot: unable to determine root device\n");
1412 /* Find root device probe_info so we know its UUID */
1413 list_for_each_entry(tmp
, &devices
, list
) {
1414 if (!strcmp(tmp
->dev
, devpath
)) {
1420 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1424 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1428 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1429 if (stat(tag
, &s
)) {
1430 fp
= fopen(tag
, "w+");
1432 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1434 /* return 0 to continue boot regardless of error */
1437 fputs(pr
->uuid
, fp
);
1442 fp
= fopen(tag
, "r");
1444 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag
,
1449 if (!fgets(uuid
, sizeof(uuid
), fp
))
1450 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag
,
1454 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1457 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n", pr
->uuid
,
1458 basename(path
), uuid
);
1463 * Read info about extroot from UCI (using prefix) and mount it.
1465 static int mount_extroot(char *cfg
)
1467 char overlay
[] = "/tmp/extroot/overlay";
1468 char mnt
[] = "/tmp/extroot/mnt";
1470 struct probe_info
*pr
;
1474 /* Load @cfg/etc/config/fstab */
1475 if (config_load(cfg
))
1478 /* See if there is extroot-specific mount config */
1479 m
= find_block(NULL
, NULL
, NULL
, "/");
1481 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1483 if (!m
|| !m
->extroot
)
1485 ULOG_INFO("extroot: not configured\n");
1489 /* Find block device pointed by the mount config */
1490 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1492 if (!pr
&& delay_root
){
1493 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1497 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1500 if (strncmp(pr
->type
, "ext", 3) &&
1501 strncmp(pr
->type
, "f2fs", 4) &&
1502 strncmp(pr
->type
, "btrfs", 5) &&
1503 strncmp(pr
->type
, "ntfs", 4) &&
1504 strncmp(pr
->type
, "ubifs", 5)) {
1505 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr
->type
);
1509 if (test_fs_support(pr
->type
)) {
1510 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1516 mkdir_p(path
, 0755);
1519 check_filesystem(pr
);
1521 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1522 (m
->options
) ? (m
->options
) : (""));
1525 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1526 pr
->dev
, pr
->type
, path
, errno
);
1527 } else if (m
->overlay
) {
1528 err
= check_extroot(path
);
1533 ULOG_ERR("extroot: cannot find device %s%s\n",
1534 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1535 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1542 * Look for extroot config and mount it if present
1544 * Look for /etc/config/fstab on all supported partitions and use it for
1545 * mounting extroot if specified.
1547 static int main_extroot(int argc
, char **argv
)
1549 struct probe_info
*pr
;
1550 char blkdev_path
[32] = { 0 };
1552 #ifdef UBIFS_EXTROOT
1556 if (!getenv("PREINIT"))
1560 ULOG_ERR("Usage: block extroot\n");
1567 /* enable LOG_INFO messages */
1568 ulog_threshold(LOG_INFO
);
1571 * Look for "rootfs_data". We will want to mount it and check for
1572 * extroot configuration.
1575 /* Start with looking for MTD partition */
1576 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1577 if (blkdev_path
[0]) {
1578 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1579 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1580 char cfg
[] = "/tmp/jffs_cfg";
1583 * Mount MTD part and try extroot (using
1584 * /etc/config/fstab from that partition)
1587 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1588 err
= mount_extroot(cfg
);
1589 umount2(cfg
, MNT_DETACH
);
1592 rmdir("/tmp/overlay");
1598 #ifdef UBIFS_EXTROOT
1599 /* ... but it also could be an UBI volume */
1600 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1601 libubi
= libubi_open();
1602 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1603 libubi_close(libubi
);
1604 if (blkdev_path
[0]) {
1605 char cfg
[] = "/tmp/ubifs_cfg";
1607 /* Mount volume and try extroot (using fstab from that vol) */
1609 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1610 err
= mount_extroot(cfg
);
1611 umount2(cfg
, MNT_DETACH
);
1614 rmdir("/tmp/overlay");
1620 /* As a last resort look for /etc/config/fstab on "rootfs" partition */
1621 return mount_extroot(NULL
);
1624 static int main_mount(int argc
, char **argv
)
1626 struct probe_info
*pr
;
1628 if (config_load(NULL
))
1632 list_for_each_entry(pr
, &devices
, list
)
1633 mount_device(pr
, TYPE_DEV
);
1635 handle_swapfiles(true);
1640 static int main_umount(int argc
, char **argv
)
1642 struct probe_info
*pr
;
1645 if (config_load(NULL
))
1648 handle_swapfiles(false);
1653 all
= !strcmp(argv
[2], "-a");
1655 list_for_each_entry(pr
, &devices
, list
) {
1658 if (!strcmp(pr
->type
, "swap"))
1661 m
= find_block(pr
->uuid
, pr
->label
, basename(pr
->dev
), NULL
);
1662 if (m
&& m
->extroot
)
1665 umount_device(pr
->dev
, TYPE_DEV
, all
);
1671 static int main_detect(int argc
, char **argv
)
1673 struct probe_info
*pr
;
1676 printf("config 'global'\n");
1677 printf("\toption\tanon_swap\t'0'\n");
1678 printf("\toption\tanon_mount\t'0'\n");
1679 printf("\toption\tauto_swap\t'1'\n");
1680 printf("\toption\tauto_mount\t'1'\n");
1681 printf("\toption\tdelay_root\t'5'\n");
1682 printf("\toption\tcheck_fs\t'0'\n\n");
1683 list_for_each_entry(pr
, &devices
, list
)
1684 print_block_uci(pr
);
1689 static int main_info(int argc
, char **argv
)
1692 struct probe_info
*pr
;
1696 list_for_each_entry(pr
, &devices
, list
)
1697 print_block_info(pr
);
1702 for (i
= 2; i
< argc
; i
++) {
1705 if (stat(argv
[i
], &s
)) {
1706 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1709 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1710 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1713 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1715 print_block_info(pr
);
1721 static int swapon_usage(void)
1723 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1724 "\tStart swapping on [DEVICE]\n"
1725 " -a\tStart swapping on all swap devices\n"
1726 " -p pri\tSet priority of swap device\n"
1727 " -s\tShow summary\n");
1731 static int main_swapon(int argc
, char **argv
)
1737 struct probe_info
*pr
;
1743 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1746 fp
= fopen("/proc/swaps", "r");
1750 ULOG_ERR("failed to open /proc/swaps\n");
1753 while (getline(&lineptr
, &s
, fp
) > 0)
1754 printf("%s", lineptr
);
1761 list_for_each_entry(pr
, &devices
, list
) {
1762 if (strcmp(pr
->type
, "swap"))
1764 if (swapon(pr
->dev
, 0))
1765 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1771 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1774 return swapon_usage();
1778 if (optind
!= (argc
- 1))
1779 return swapon_usage();
1781 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1782 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1785 err
= swapon(argv
[optind
], flags
);
1787 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1794 static int main_swapoff(int argc
, char **argv
)
1797 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1798 "\tStop swapping on DEVICE\n"
1799 " -a\tStop swapping on all swap devices\n");
1803 if (!strcmp(argv
[1], "-a")) {
1804 FILE *fp
= fopen("/proc/swaps", "r");
1808 ULOG_ERR("failed to open /proc/swaps\n");
1811 if (fgets(line
, sizeof(line
), fp
))
1812 while (fgets(line
, sizeof(line
), fp
)) {
1813 char *end
= strchr(line
, ' ');
1819 err
= swapoff(line
);
1821 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1828 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1829 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1832 err
= swapoff(argv
[1]);
1834 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1842 int main(int argc
, char **argv
)
1844 char *base
= basename(*argv
);
1848 ulog_open(-1, -1, "block");
1849 ulog_threshold(LOG_NOTICE
);
1851 if (!strcmp(base
, "swapon"))
1852 return main_swapon(argc
, argv
);
1854 if (!strcmp(base
, "swapoff"))
1855 return main_swapoff(argc
, argv
);
1857 if ((argc
> 1) && !strcmp(base
, "block")) {
1858 if (!strcmp(argv
[1], "info"))
1859 return main_info(argc
, argv
);
1861 if (!strcmp(argv
[1], "detect"))
1862 return main_detect(argc
, argv
);
1864 if (!strcmp(argv
[1], "hotplug"))
1865 return main_hotplug(argc
, argv
);
1867 if (!strcmp(argv
[1], "autofs"))
1868 return main_autofs(argc
, argv
);
1870 if (!strcmp(argv
[1], "extroot"))
1871 return main_extroot(argc
, argv
);
1873 if (!strcmp(argv
[1], "mount"))
1874 return main_mount(argc
, argv
);
1876 if (!strcmp(argv
[1], "umount"))
1877 return main_umount(argc
, argv
);
1879 if (!strcmp(argv
[1], "remount")) {
1880 int ret
= main_umount(argc
, argv
);
1883 ret
= main_mount(argc
, argv
);
1888 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");