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/ulog.h>
42 #include <libubox/list.h>
43 #include <libubox/vlist.h>
44 #include <libubox/blobmsg_json.h>
45 #include <libubox/avl-cmp.h>
50 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
53 #include "libubi/libubi.h"
68 struct vlist_node node
;
86 struct vlist_node node
;
88 struct probe_info
*pr
;
92 static void vlist_nop_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
93 struct vlist_node
*node_old
);
94 static int devices_cmp(const void *k1
, const void *k2
, void *ptr
);
96 static struct blob_buf b
;
97 static VLIST_TREE(mounts
, avl_strcmp
, vlist_nop_update
, false, false);
98 static VLIST_TREE(devices
, devices_cmp
, vlist_nop_update
, false, false);
99 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
100 static unsigned int delay_root
;
112 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
113 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
114 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
115 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
116 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
117 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
118 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
132 static const struct uci_blob_param_list config_attr_list
= {
133 .n_params
= __CFG_MAX
,
134 .params
= config_policy
,
137 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
138 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
139 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
140 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
141 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
142 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
143 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
144 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
147 static const struct uci_blob_param_list mount_attr_list
= {
148 .n_params
= __MOUNT_MAX
,
149 .params
= mount_policy
,
161 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
162 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
163 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
164 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
165 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
166 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
169 static const struct uci_blob_param_list swap_attr_list
= {
170 .n_params
= __SWAP_MAX
,
171 .params
= swap_policy
,
179 static const struct mount_flag mount_flags
[] = {
180 { "sync", MS_SYNCHRONOUS
},
181 { "async", ~MS_SYNCHRONOUS
},
182 { "dirsync", MS_DIRSYNC
},
183 { "mand", MS_MANDLOCK
},
184 { "nomand", ~MS_MANDLOCK
},
185 { "atime", ~MS_NOATIME
},
186 { "noatime", MS_NOATIME
},
187 { "dev", ~MS_NODEV
},
188 { "nodev", MS_NODEV
},
189 { "diratime", ~MS_NODIRATIME
},
190 { "nodiratime", MS_NODIRATIME
},
191 { "exec", ~MS_NOEXEC
},
192 { "noexec", MS_NOEXEC
},
193 { "suid", ~MS_NOSUID
},
194 { "nosuid", MS_NOSUID
},
195 { "rw", ~MS_RDONLY
},
197 { "relatime", MS_RELATIME
},
198 { "norelatime", ~MS_RELATIME
},
199 { "strictatime", MS_STRICTATIME
},
200 { "acl", MS_POSIXACL
},
201 { "noacl", ~MS_POSIXACL
},
202 { "nouser_xattr", MS_NOUSER
},
203 { "user_xattr", ~MS_NOUSER
},
206 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
211 return strdup(blobmsg_get_string(attr
));
214 static char *blobmsg_get_basename(struct blob_attr
*attr
)
219 return strdup(basename(blobmsg_get_string(attr
)));
222 static void parse_mount_options(struct mount
*m
, char *optstr
)
226 char *p
, *opts
, *last
;
231 if (!optstr
|| !*optstr
)
234 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
247 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
248 if (!strcmp(last
, mount_flags
[i
].name
)) {
249 if (mount_flags
[i
].flag
< 0)
250 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
252 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
259 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
268 static int mount_add(struct uci_section
*s
)
270 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
273 blob_buf_init(&b
, 0);
274 uci_to_blob(&b
, s
, &mount_attr_list
);
275 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
277 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
280 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
283 m
= malloc(sizeof(struct mount
));
284 m
->type
= TYPE_MOUNT
;
285 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
286 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
287 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
288 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
289 if (tb
[MOUNT_AUTOFS
])
290 m
->autofs
= blobmsg_get_u32(tb
[MOUNT_AUTOFS
]);
293 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
295 m
->overlay
= m
->extroot
= 0;
296 if (m
->target
&& !strcmp(m
->target
, "/"))
298 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
299 m
->extroot
= m
->overlay
= 1;
301 if (m
->target
&& *m
->target
!= '/') {
302 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
303 s
->e
.name
, m
->target
);
309 vlist_add(&mounts
, &m
->node
, m
->uuid
);
311 vlist_add(&mounts
, &m
->node
, m
->label
);
313 vlist_add(&mounts
, &m
->node
, m
->device
);
318 static int swap_add(struct uci_section
*s
)
320 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
323 blob_buf_init(&b
, 0);
324 uci_to_blob(&b
, s
, &swap_attr_list
);
325 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
327 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
330 m
= malloc(sizeof(struct mount
));
331 memset(m
, 0, sizeof(struct mount
));
333 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
334 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
335 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
337 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
339 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
341 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
342 /* store complete swap path */
344 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
347 vlist_add(&mounts
, &m
->node
, m
->uuid
);
349 vlist_add(&mounts
, &m
->node
, m
->label
);
351 vlist_add(&mounts
, &m
->node
, m
->device
);
357 static int global_add(struct uci_section
*s
)
359 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
361 blob_buf_init(&b
, 0);
362 uci_to_blob(&b
, s
, &config_attr_list
);
363 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
365 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
367 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
370 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
372 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
375 if (tb
[CFG_DELAY_ROOT
])
376 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
378 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
384 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
388 vlist_for_each_element(&mounts
, m
, node
) {
389 if (m
->type
!= TYPE_SWAP
)
391 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
393 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
395 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
402 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
407 vlist_for_each_element(&mounts
, m
, node
) {
408 if (m
->type
!= TYPE_MOUNT
)
410 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
412 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
414 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
416 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
423 static void vlist_nop_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
424 struct vlist_node
*node_old
)
426 // NOTE: free on delete skipped
429 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
431 char *file
= basename(path
);
432 char *dir
= dirname(path
);
434 struct uci_package
*pkg
;
436 uci_set_confdir(ctx
, dir
);
437 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
439 if (uci_load(ctx
, file
, &pkg
)) {
440 uci_get_errorstr(ctx
, &err
, file
);
441 ULOG_ERR("unable to load configuration (%s)\n", err
);
450 static int config_load(char *cfg
)
452 struct uci_context
*ctx
= uci_alloc_context();
453 struct uci_package
*pkg
= NULL
;
454 struct uci_element
*e
;
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 ULOG_ERR("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 probe_info
* _probe_path(char *path
)
498 /* skip ubi device if ubiblock device is present */
499 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
500 path
[8] >= '0' && path
[8] <= '9' ) {
501 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
502 vlist_for_each_element(&devices
, dev
, node
)
503 if (!strcasecmp(dev
->pr
->dev
, tmppath
))
507 return probe_path(path
);
510 static int _cache_load(const char *path
)
512 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
517 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
520 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
521 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
523 dev
= malloc(sizeof(struct device
));
525 dev
->m
= find_block(pr
->uuid
, pr
->label
,
526 basename(pr
->dev
), NULL
);
527 vlist_add(&devices
, &dev
->node
, dev
);
536 static int devices_cmp(const void *k1
, const void *k2
, void *ptr
)
538 struct device
*dev1
= (struct device
*)k1
;
539 struct device
*dev2
= (struct device
*)k2
;
544 if (dev1
->m
->type
== TYPE_MOUNT
&& dev2
->m
->type
== TYPE_MOUNT
&&
545 dev1
->m
->target
&& dev2
->m
->target
) {
546 int len1
= strlen(dev1
->m
->target
);
547 int len2
= strlen(dev2
->m
->target
);
551 } else if (dev2
->m
) {
554 return strcmp(dev1
->pr
->dev
, dev2
->pr
->dev
);
557 static void cache_load(int mtd
)
559 vlist_update(&devices
);
561 _cache_load("/dev/mtdblock*");
562 _cache_load("/dev/ubiblock*");
563 _cache_load("/dev/ubi[0-9]*");
565 _cache_load("/dev/loop*");
566 _cache_load("/dev/mmcblk*");
567 _cache_load("/dev/sd*");
568 _cache_load("/dev/hd*");
569 _cache_load("/dev/md*");
570 _cache_load("/dev/nvme*");
571 _cache_load("/dev/vd*");
572 _cache_load("/dev/xvd*");
573 _cache_load("/dev/mapper/*");
574 vlist_flush(&devices
);
578 static struct device
* find_block_device(char *uuid
, char *label
, char *path
)
583 vlist_for_each_element(&devices
, dev
, node
)
584 if (dev
->pr
->uuid
&& !strcasecmp(dev
->pr
->uuid
, uuid
))
588 vlist_for_each_element(&devices
, dev
, node
)
589 if (dev
->pr
->label
&& !strcmp(dev
->pr
->label
, label
))
593 vlist_for_each_element(&devices
, dev
, node
)
594 if (dev
->pr
->dev
&& !strcmp(basename(dev
->pr
->dev
), basename(path
)))
600 static char* find_mount_point(char *block
)
602 FILE *fp
= fopen("/proc/self/mountinfo", "r");
603 static char line
[256];
604 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
607 unsigned int minor
, major
;
612 rstat
= stat(block
, &s
);
614 while (fgets(line
, sizeof(line
), fp
)) {
615 pos
= strchr(line
, ' ');
619 pos
= strchr(pos
+ 1, ' ');
624 pos
= strchr(pos
, ':');
631 pos
= strchr(pos
, ' ');
637 pos
= strchr(pos
+ 1, ' ');
642 pos
= strchr(pos
, ' ');
648 pos
= strchr(pos
+ 1, ' ');
652 pos
= strchr(pos
+ 1, ' ');
656 pos
= strchr(pos
+ 1, ' ');
661 pos
= strchr(pos
, ' ');
667 if (!strcmp(block
, devname
)) {
668 point
= strdup(cpoint
);
675 if (!S_ISBLK(s
.st_mode
))
678 if (major
== major(s
.st_rdev
) &&
679 minor
== minor(s
.st_rdev
)) {
680 point
= strdup(cpoint
);
690 static int print_block_uci(struct probe_info
*pr
)
692 if (!strcmp(pr
->type
, "swap")) {
693 printf("config 'swap'\n");
695 char *mp
= find_mount_point(pr
->dev
);
697 printf("config 'mount'\n");
699 printf("\toption\ttarget\t'%s'\n", mp
);
702 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
706 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
708 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
709 printf("\toption\tenabled\t'0'\n\n");
714 static int print_block_info(struct probe_info
*pr
)
718 mp
= find_mount_point(pr
->dev
);
719 printf("%s:", pr
->dev
);
721 printf(" UUID=\"%s\"", pr
->uuid
);
724 printf(" LABEL=\"%s\"", pr
->label
);
727 printf(" VERSION=\"%s\"", pr
->version
);
730 printf(" MOUNT=\"%s\"", mp
);
734 printf(" TYPE=\"%s\"\n", pr
->type
);
739 static void mkdir_p(char *dir
)
741 char *l
= strrchr(dir
, '/');
751 static void check_filesystem(struct probe_info
*pr
)
755 const char *e2fsck
= "/usr/sbin/e2fsck";
756 const char *f2fsck
= "/usr/sbin/fsck.f2fs";
757 const char *fatfsck
= "/usr/sbin/fsck.fat";
758 const char *btrfsck
= "/usr/bin/btrfsck";
759 const char *ntfsck
= "/usr/bin/ntfsfix";
762 /* UBIFS does not need stuff like fsck */
763 if (!strncmp(pr
->type
, "ubifs", 5))
766 if (!strncmp(pr
->type
, "vfat", 4)) {
768 } else if (!strncmp(pr
->type
, "f2fs", 4)) {
770 } else if (!strncmp(pr
->type
, "ext", 3)) {
772 } else if (!strncmp(pr
->type
, "btrfs", 5)) {
774 } else if (!strncmp(pr
->type
, "ntfs", 4)) {
777 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
781 if (stat(ckfs
, &statbuf
) < 0) {
782 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
788 if(!strncmp(pr
->type
, "f2fs", 4)) {
789 execl(ckfs
, ckfs
, "-f", pr
->dev
, NULL
);
791 } else if(!strncmp(pr
->type
, "btrfs", 5)) {
792 execl(ckfs
, ckfs
, "--repair", pr
->dev
, NULL
);
794 } else if(!strncmp(pr
->type
, "ntfs", 4)) {
795 execl(ckfs
, ckfs
, "-b", pr
->dev
, NULL
);
798 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
801 } else if (pid
> 0) {
804 waitpid(pid
, &status
, 0);
805 if (WIFEXITED(status
) && WEXITSTATUS(status
))
806 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
807 if (WIFSIGNALED(status
))
808 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs
, strsignal(WTERMSIG(status
)));
812 static void handle_swapfiles(bool on
)
816 struct probe_info
*pr
;
818 vlist_for_each_element(&mounts
, m
, node
)
820 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
823 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
826 pr
= _probe_path(m
->target
);
831 if (!strcmp(pr
->type
, "swap")) {
833 swapon(pr
->dev
, m
->prio
);
842 static void to_devnull(int fd
)
844 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
849 if (devnull
> STDERR_FILENO
)
853 static int exec_mount(const char *source
, const char *target
,
854 const char *fstype
, const char *options
)
859 int err
, status
, pfds
[2];
860 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
862 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
864 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
865 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
872 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
873 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
885 to_devnull(STDIN_FILENO
);
886 to_devnull(STDOUT_FILENO
);
888 dup2(pfds
[1], STDERR_FILENO
);
892 if (options
&& *options
)
893 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
895 execl(cmd
, cmd
, source
, target
, NULL
);
902 mount_fd
= fdopen(pfds
[0], "r");
904 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
905 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
909 err
= waitpid(pid
, &status
, 0);
913 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
928 static int hotplug_call_mount(const char *action
, const char *device
)
935 char * const argv
[] = { "hotplug-call", "mount", NULL
};
937 setenv("ACTION", action
, 1);
938 setenv("DEVICE", device
, 1);
940 execv("/sbin/hotplug-call", argv
);
942 } else if (pid
> 0) {
945 pid
= waitpid(pid
, &status
, 0);
946 if (pid
<= 0 || !WIFEXITED(status
) || WEXITSTATUS(status
)) {
948 ULOG_ERR("hotplug-call call failed\n");
957 static int handle_mount(const char *source
, const char *target
,
958 const char *fstype
, struct mount
*m
)
961 size_t mount_opts_len
;
962 char *mount_opts
= NULL
, *ptr
;
964 err
= mount(source
, target
, fstype
, m
? m
->flags
: 0,
965 (m
&& m
->options
) ? m
->options
: "");
967 /* Requested file system type is not available in kernel,
968 attempt to call mount helper. */
969 if (err
== -1 && errno
== ENODEV
) {
971 /* Convert mount flags back into string representation,
972 first calculate needed length of string buffer... */
973 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
975 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
976 if ((mount_flags
[i
].flag
> 0) &&
977 (mount_flags
[i
].flag
< INT_MAX
) &&
978 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
979 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
981 /* ... then now allocate and fill it ... */
982 ptr
= mount_opts
= calloc(1, mount_opts_len
);
990 ptr
+= sprintf(ptr
, "%s,", m
->options
);
992 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
993 if ((mount_flags
[i
].flag
> 0) &&
994 (mount_flags
[i
].flag
< INT_MAX
) &&
995 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
996 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
998 mount_opts
[mount_opts_len
- 1] = 0;
1001 /* ... and now finally invoke the external mount program */
1002 err
= exec_mount(source
, target
, fstype
, mount_opts
);
1008 static int blockd_notify(char *device
, struct mount
*m
, struct probe_info
*pr
)
1010 struct ubus_context
*ctx
= ubus_connect(NULL
);
1017 if (!ubus_lookup_id(ctx
, "block", &id
)) {
1018 struct blob_buf buf
= { 0 };
1019 char *d
= strrchr(device
, '/');
1026 blob_buf_init(&buf
, 0);
1030 blobmsg_add_string(&buf
, "device", d
);
1032 blobmsg_add_string(&buf
, "uuid", m
->uuid
);
1034 blobmsg_add_string(&buf
, "label", m
->label
);
1036 blobmsg_add_string(&buf
, "target", m
->target
);
1038 blobmsg_add_string(&buf
, "options", m
->options
);
1040 blobmsg_add_u32(&buf
, "autofs", m
->autofs
);
1042 blobmsg_add_string(&buf
, "type", pr
->type
);
1044 blobmsg_add_string(&buf
, "version", pr
->version
);
1046 blobmsg_add_string(&buf
, "device", d
);
1048 blobmsg_add_string(&buf
, "uuid", pr
->uuid
);
1050 blobmsg_add_string(&buf
, "label", pr
->label
);
1052 blobmsg_add_string(&buf
, "type", pr
->type
);
1054 blobmsg_add_string(&buf
, "version", pr
->version
);
1055 blobmsg_add_u32(&buf
, "anon", 1);
1057 blobmsg_add_string(&buf
, "device", d
);
1058 blobmsg_add_u32(&buf
, "remove", 1);
1061 err
= ubus_invoke(ctx
, id
, "hotplug", buf
.head
, NULL
, NULL
, 3000);
1071 static int mount_device(struct device
*dev
, int type
)
1074 struct probe_info
*pr
;
1086 device
= basename(pr
->dev
);
1088 if (!strcmp(pr
->type
, "swap")) {
1089 if ((type
== TYPE_HOTPLUG
) && !auto_swap
)
1091 m
= find_swap(pr
->uuid
, pr
->label
, device
);
1093 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
1099 if (m
&& m
->extroot
)
1102 mp
= find_mount_point(pr
->dev
);
1104 if (m
&& m
->type
== TYPE_MOUNT
&& strcmp(m
->target
, mp
)) {
1105 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
1113 if (type
== TYPE_HOTPLUG
)
1114 blockd_notify(device
, m
, pr
);
1116 /* Check if device should be mounted & set the target directory */
1136 snprintf(_target
, sizeof(_target
), "/tmp/run/blockd/%s", device
);
1138 } else if (m
->target
) {
1141 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
1144 } else if (anon_mount
) {
1145 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
1148 /* No reason to mount this device */
1152 /* Mount the device */
1155 check_filesystem(pr
);
1158 if (!lstat(target
, &st
) && S_ISLNK(st
.st_mode
))
1161 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
1163 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1164 pr
->dev
, pr
->type
, target
, errno
);
1168 handle_swapfiles(true);
1170 if (type
!= TYPE_AUTOFS
)
1171 hotplug_call_mount("add", device
);
1176 static int umount_device(char *path
, int type
, bool all
)
1181 mp
= find_mount_point(path
);
1184 if (!strcmp(mp
, "/") && !all
)
1187 if (type
!= TYPE_AUTOFS
)
1188 hotplug_call_mount("remove", basename(path
));
1190 err
= umount2(mp
, MNT_DETACH
);
1192 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path
, mp
,
1195 ULOG_INFO("unmounted %s (%s)\n", path
, mp
);
1203 static int mount_action(char *action
, char *device
, int type
)
1205 struct device
*the_dev
, *dev
;
1208 if (!action
|| !device
)
1211 if (config_load(NULL
))
1215 the_dev
= find_block_device(NULL
, NULL
, device
);
1217 if (!strcmp(action
, "remove")) {
1218 if (type
== TYPE_HOTPLUG
)
1219 blockd_notify(device
, NULL
, NULL
);
1221 if (!the_dev
|| !the_dev
->m
|| the_dev
->m
->type
!= TYPE_MOUNT
) {
1222 snprintf(path
, sizeof(path
), "/dev/%s", device
);
1223 umount_device(path
, type
, true);
1225 vlist_for_element_to_last_reverse(&devices
, the_dev
, dev
, node
)
1226 if (dev
->m
&& dev
->m
->type
== TYPE_MOUNT
)
1227 umount_device(dev
->pr
->dev
, type
, true);
1229 } else if (!strcmp(action
, "add")) {
1232 if (the_dev
->m
&& the_dev
->m
->type
== TYPE_MOUNT
) {
1233 vlist_for_first_to_element(&devices
, the_dev
, dev
, node
) {
1234 if (dev
->m
&& dev
->m
->type
== TYPE_MOUNT
) {
1235 int err
= mount_device(dev
, type
);
1242 return mount_device(the_dev
, type
);
1244 ULOG_ERR("Unkown action %s\n", action
);
1248 static int main_hotplug(int argc
, char **argv
)
1250 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG
);
1253 static int main_autofs(int argc
, char **argv
)
1260 if (!strcmp(argv
[2], "start")) {
1262 struct probe_info
*pr
;
1264 if (config_load(NULL
))
1268 vlist_for_each_element(&devices
, dev
, node
) {
1272 if (!strcmp(pr
->type
, "swap"))
1276 if (m
&& m
->extroot
)
1279 blockd_notify(pr
->dev
, m
, pr
);
1281 } else if (!strcmp(argv
[2], "available")) {
1282 err
= hotplug_call_mount("add", argv
[3]);
1283 } else if (!strcmp(argv
[2], "unavailable")) {
1284 err
= hotplug_call_mount("remove", argv
[3]);
1289 err
= mount_action(argv
[2], argv
[3], TYPE_AUTOFS
);
1293 ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv
[2], err
);
1299 static int find_block_mtd(char *name
, char *part
, int plen
)
1301 FILE *fp
= fopen("/proc/mtd", "r");
1302 static char line
[256];
1308 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1309 if (strstr(line
, name
)) {
1310 char *eol
= strstr(line
, ":");
1325 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1330 #ifdef UBIFS_EXTROOT
1331 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1335 while (ubi_dev_present(libubi
, dev
))
1337 struct ubi_dev_info dev_info
;
1338 struct ubi_vol_info vol_info
;
1340 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1342 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1345 *dev_num
= dev_info
.dev_num
;
1346 *vol_id
= vol_info
.vol_id
;
1354 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1360 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1362 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1367 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1373 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1375 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1382 static int find_root_dev(char *buf
, int len
)
1392 if (!(d
= opendir("/dev")))
1397 while ((e
= readdir(d
)) != NULL
) {
1398 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1400 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1413 static int test_fs_support(const char *name
)
1419 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1420 while (fgets(line
, sizeof(line
), f
)) {
1421 p
= strtok(line
, "\t\n");
1423 if (p
&& !strcmp(p
, "nodev"))
1424 p
= strtok(NULL
, "\t\n");
1426 if (p
&& !strcmp(p
, name
)) {
1437 static int check_extroot(char *path
)
1440 struct probe_info
*pr
;
1443 #ifdef UBIFS_EXTROOT
1444 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1448 libubi
= libubi_open();
1449 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1450 libubi_close(libubi
);
1455 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1456 if (find_root_dev(devpath
, sizeof(devpath
))) {
1457 ULOG_ERR("extroot: unable to determine root device\n");
1463 vlist_for_each_element(&devices
, dev
, node
) {
1465 if (!strcmp(pr
->dev
, devpath
)) {
1469 char uuid
[64] = { 0 };
1471 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1475 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1476 if (stat(tag
, &s
)) {
1477 fp
= fopen(tag
, "w+");
1479 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1481 /* return 0 to continue boot regardless of error */
1484 fputs(pr
->uuid
, fp
);
1489 fp
= fopen(tag
, "r");
1491 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1496 if (!fgets(uuid
, sizeof(uuid
), fp
))
1497 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1501 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1504 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1505 pr
->uuid
, basename(path
), uuid
);
1510 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1515 * Read info about extroot from UCI (using prefix) and mount it.
1517 static int mount_extroot(char *cfg
)
1519 char overlay
[] = "/tmp/extroot/overlay";
1520 char mnt
[] = "/tmp/extroot/mnt";
1523 struct probe_info
*pr
;
1527 /* Load @cfg/etc/config/fstab */
1528 if (config_load(cfg
))
1531 /* See if there is extroot-specific mount config */
1532 m
= find_block(NULL
, NULL
, NULL
, "/");
1534 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1536 if (!m
|| !m
->extroot
)
1538 ULOG_INFO("extroot: not configured\n");
1542 /* Find block device pointed by the mount config */
1543 dev
= find_block_device(m
->uuid
, m
->label
, m
->device
);
1545 if (!dev
&& delay_root
){
1546 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1550 dev
= find_block_device(m
->uuid
, m
->label
, m
->device
);
1554 if (strncmp(pr
->type
, "ext", 3) &&
1555 strncmp(pr
->type
, "f2fs", 4) &&
1556 strncmp(pr
->type
, "btrfs", 5) &&
1557 strncmp(pr
->type
, "ntfs", 4) &&
1558 strncmp(pr
->type
, "ubifs", 5)) {
1559 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr
->type
);
1563 if (test_fs_support(pr
->type
)) {
1564 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1573 check_filesystem(pr
);
1575 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1576 (m
->options
) ? (m
->options
) : (""));
1579 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1580 pr
->dev
, pr
->type
, path
, errno
);
1581 } else if (m
->overlay
) {
1582 err
= check_extroot(path
);
1587 ULOG_ERR("extroot: cannot find device %s%s\n",
1588 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1589 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1595 static int main_extroot(int argc
, char **argv
)
1597 char blkdev_path
[32] = { 0 };
1599 #ifdef UBIFS_EXTROOT
1603 if (!getenv("PREINIT"))
1607 ULOG_ERR("Usage: block extroot\n");
1614 /* enable LOG_INFO messages */
1615 ulog_threshold(LOG_INFO
);
1618 * Look for "rootfs_data". We will want to mount it and check for
1619 * extroot configuration.
1622 /* Start with looking for MTD partition */
1623 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1624 if (blkdev_path
[0]) {
1625 struct device
*dev
= find_block_device(NULL
, NULL
, blkdev_path
);
1626 if (dev
&& !strcmp(dev
->pr
->type
, "jffs2")) {
1627 char cfg
[] = "/tmp/jffs_cfg";
1630 * Mount MTD part and try extroot (using
1631 * /etc/config/fstab from that partition)
1634 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1635 err
= mount_extroot(cfg
);
1636 umount2(cfg
, MNT_DETACH
);
1639 rmdir("/tmp/overlay");
1645 #ifdef UBIFS_EXTROOT
1646 /* ... but it also could be an UBI volume */
1647 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1648 libubi
= libubi_open();
1649 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1650 libubi_close(libubi
);
1651 if (blkdev_path
[0]) {
1652 char cfg
[] = "/tmp/ubifs_cfg";
1654 /* Mount volume and try extroot (using fstab from that vol) */
1656 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1657 err
= mount_extroot(cfg
);
1658 umount2(cfg
, MNT_DETACH
);
1661 rmdir("/tmp/overlay");
1667 return mount_extroot(NULL
);
1670 static int main_mount(int argc
, char **argv
)
1674 if (config_load(NULL
))
1678 vlist_for_each_element(&devices
, dev
, node
)
1679 mount_device(dev
, TYPE_DEV
);
1681 handle_swapfiles(true);
1686 static int main_umount(int argc
, char **argv
)
1689 struct probe_info
*pr
;
1692 if (config_load(NULL
))
1695 handle_swapfiles(false);
1700 all
= !strcmp(argv
[2], "-a");
1702 vlist_for_each_element_reverse(&devices
, dev
, node
) {
1706 if (!strcmp(pr
->type
, "swap"))
1710 if (m
&& m
->extroot
)
1713 umount_device(pr
->dev
, TYPE_DEV
, all
);
1719 static int main_detect(int argc
, char **argv
)
1724 printf("config 'global'\n");
1725 printf("\toption\tanon_swap\t'0'\n");
1726 printf("\toption\tanon_mount\t'0'\n");
1727 printf("\toption\tauto_swap\t'1'\n");
1728 printf("\toption\tauto_mount\t'1'\n");
1729 printf("\toption\tdelay_root\t'5'\n");
1730 printf("\toption\tcheck_fs\t'0'\n\n");
1731 vlist_for_each_element(&devices
, dev
, node
)
1732 print_block_uci(dev
->pr
);
1737 static int main_info(int argc
, char **argv
)
1744 vlist_for_each_element(&devices
, dev
, node
)
1745 print_block_info(dev
->pr
);
1750 for (i
= 2; i
< argc
; i
++) {
1753 if (stat(argv
[i
], &s
)) {
1754 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1757 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1758 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1761 dev
= find_block_device(NULL
, NULL
, argv
[i
]);
1763 print_block_info(dev
->pr
);
1769 static int swapon_usage(void)
1771 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1772 "\tStart swapping on [DEVICE]\n"
1773 " -a\tStart swapping on all swap devices\n"
1774 " -p pri\tSet priority of swap device\n"
1775 " -s\tShow summary\n");
1779 static int main_swapon(int argc
, char **argv
)
1786 struct probe_info
*pr
;
1792 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1795 fp
= fopen("/proc/swaps", "r");
1799 ULOG_ERR("failed to open /proc/swaps\n");
1802 while (getline(&lineptr
, &s
, fp
) > 0)
1803 printf("%s", lineptr
);
1810 vlist_for_each_element(&devices
, dev
, node
) {
1812 if (strcmp(pr
->type
, "swap"))
1814 if (swapon(pr
->dev
, 0))
1815 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1821 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1824 return swapon_usage();
1828 if (optind
!= (argc
- 1))
1829 return swapon_usage();
1831 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1832 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1835 err
= swapon(argv
[optind
], flags
);
1837 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1844 static int main_swapoff(int argc
, char **argv
)
1847 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1848 "\tStop swapping on DEVICE\n"
1849 " -a\tStop swapping on all swap devices\n");
1853 if (!strcmp(argv
[1], "-a")) {
1854 FILE *fp
= fopen("/proc/swaps", "r");
1858 ULOG_ERR("failed to open /proc/swaps\n");
1861 if (fgets(line
, sizeof(line
), fp
))
1862 while (fgets(line
, sizeof(line
), fp
)) {
1863 char *end
= strchr(line
, ' ');
1869 err
= swapoff(line
);
1871 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1878 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1879 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1882 err
= swapoff(argv
[1]);
1884 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1892 int main(int argc
, char **argv
)
1894 char *base
= basename(*argv
);
1898 ulog_open(-1, -1, "block");
1899 ulog_threshold(LOG_NOTICE
);
1901 if (!strcmp(base
, "swapon"))
1902 return main_swapon(argc
, argv
);
1904 if (!strcmp(base
, "swapoff"))
1905 return main_swapoff(argc
, argv
);
1907 if ((argc
> 1) && !strcmp(base
, "block")) {
1908 if (!strcmp(argv
[1], "info"))
1909 return main_info(argc
, argv
);
1911 if (!strcmp(argv
[1], "detect"))
1912 return main_detect(argc
, argv
);
1914 if (!strcmp(argv
[1], "hotplug"))
1915 return main_hotplug(argc
, argv
);
1917 if (!strcmp(argv
[1], "autofs"))
1918 return main_autofs(argc
, argv
);
1920 if (!strcmp(argv
[1], "extroot"))
1921 return main_extroot(argc
, argv
);
1923 if (!strcmp(argv
[1], "mount"))
1924 return main_mount(argc
, argv
);
1926 if (!strcmp(argv
[1], "umount"))
1927 return main_umount(argc
, argv
);
1929 if (!strcmp(argv
[1], "remount")) {
1930 int ret
= main_umount(argc
, argv
);
1933 ret
= main_mount(argc
, argv
);
1938 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");