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
;
85 static struct vlist_tree mounts
;
86 static struct blob_buf b
;
87 static LIST_HEAD(devices
);
88 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
89 static unsigned int delay_root
;
101 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
102 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
103 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
104 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
105 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
106 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
107 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
121 static const struct uci_blob_param_list config_attr_list
= {
122 .n_params
= __CFG_MAX
,
123 .params
= config_policy
,
126 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
127 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
128 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
129 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
130 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
131 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
132 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
133 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
136 static const struct uci_blob_param_list mount_attr_list
= {
137 .n_params
= __MOUNT_MAX
,
138 .params
= mount_policy
,
150 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
151 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
152 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
153 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
154 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
155 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
158 static const struct uci_blob_param_list swap_attr_list
= {
159 .n_params
= __SWAP_MAX
,
160 .params
= swap_policy
,
168 static const struct mount_flag mount_flags
[] = {
169 { "sync", MS_SYNCHRONOUS
},
170 { "async", ~MS_SYNCHRONOUS
},
171 { "dirsync", MS_DIRSYNC
},
172 { "mand", MS_MANDLOCK
},
173 { "nomand", ~MS_MANDLOCK
},
174 { "atime", ~MS_NOATIME
},
175 { "noatime", MS_NOATIME
},
176 { "dev", ~MS_NODEV
},
177 { "nodev", MS_NODEV
},
178 { "diratime", ~MS_NODIRATIME
},
179 { "nodiratime", MS_NODIRATIME
},
180 { "exec", ~MS_NOEXEC
},
181 { "noexec", MS_NOEXEC
},
182 { "suid", ~MS_NOSUID
},
183 { "nosuid", MS_NOSUID
},
184 { "rw", ~MS_RDONLY
},
186 { "relatime", MS_RELATIME
},
187 { "norelatime", ~MS_RELATIME
},
188 { "strictatime", MS_STRICTATIME
},
189 { "acl", MS_POSIXACL
},
190 { "noacl", ~MS_POSIXACL
},
191 { "nouser_xattr", MS_NOUSER
},
192 { "user_xattr", ~MS_NOUSER
},
195 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
200 return strdup(blobmsg_get_string(attr
));
203 static char *blobmsg_get_basename(struct blob_attr
*attr
)
208 return strdup(basename(blobmsg_get_string(attr
)));
211 static void parse_mount_options(struct mount
*m
, char *optstr
)
215 char *p
, *opts
, *last
;
220 if (!optstr
|| !*optstr
)
223 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
236 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
237 if (!strcmp(last
, mount_flags
[i
].name
)) {
238 if (mount_flags
[i
].flag
< 0)
239 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
241 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
248 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
257 static int mount_add(struct uci_section
*s
)
259 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
262 blob_buf_init(&b
, 0);
263 uci_to_blob(&b
, s
, &mount_attr_list
);
264 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
266 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
269 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
272 m
= malloc(sizeof(struct mount
));
273 m
->type
= TYPE_MOUNT
;
274 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
275 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
276 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
277 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
278 if (tb
[MOUNT_AUTOFS
])
279 m
->autofs
= blobmsg_get_u32(tb
[MOUNT_AUTOFS
]);
282 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
284 m
->overlay
= m
->extroot
= 0;
285 if (m
->target
&& !strcmp(m
->target
, "/"))
287 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
288 m
->extroot
= m
->overlay
= 1;
290 if (m
->target
&& *m
->target
!= '/') {
291 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
292 s
->e
.name
, m
->target
);
298 vlist_add(&mounts
, &m
->node
, m
->uuid
);
300 vlist_add(&mounts
, &m
->node
, m
->label
);
302 vlist_add(&mounts
, &m
->node
, m
->device
);
307 static int swap_add(struct uci_section
*s
)
309 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
312 blob_buf_init(&b
, 0);
313 uci_to_blob(&b
, s
, &swap_attr_list
);
314 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
316 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
319 m
= malloc(sizeof(struct mount
));
320 memset(m
, 0, sizeof(struct mount
));
322 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
323 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
324 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
326 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
328 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
330 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
331 /* store complete swap path */
333 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
336 vlist_add(&mounts
, &m
->node
, m
->uuid
);
338 vlist_add(&mounts
, &m
->node
, m
->label
);
340 vlist_add(&mounts
, &m
->node
, m
->device
);
346 static int global_add(struct uci_section
*s
)
348 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
350 blob_buf_init(&b
, 0);
351 uci_to_blob(&b
, s
, &config_attr_list
);
352 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
354 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
356 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
359 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
361 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
364 if (tb
[CFG_DELAY_ROOT
])
365 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
367 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
373 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
377 vlist_for_each_element(&mounts
, m
, node
) {
378 if (m
->type
!= TYPE_SWAP
)
380 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
382 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
384 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
391 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
396 vlist_for_each_element(&mounts
, m
, node
) {
397 if (m
->type
!= TYPE_MOUNT
)
399 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
401 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
403 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
405 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
412 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
413 struct vlist_node
*node_old
)
417 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
419 char *file
= basename(path
);
420 char *dir
= dirname(path
);
422 struct uci_package
*pkg
;
424 uci_set_confdir(ctx
, dir
);
425 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
427 if (uci_load(ctx
, file
, &pkg
)) {
428 uci_get_errorstr(ctx
, &err
, file
);
429 ULOG_ERR("unable to load configuration (%s)\n", err
);
438 static int config_load(char *cfg
)
440 struct uci_context
*ctx
= uci_alloc_context();
441 struct uci_package
*pkg
= NULL
;
442 struct uci_element
*e
;
445 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
448 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
449 pkg
= config_try_load(ctx
, path
);
452 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
453 pkg
= config_try_load(ctx
, path
);
458 snprintf(path
, sizeof(path
), "/etc/config/fstab");
459 pkg
= config_try_load(ctx
, path
);
463 ULOG_ERR("no usable configuration\n");
467 vlist_update(&mounts
);
468 uci_foreach_element(&pkg
->sections
, e
) {
469 struct uci_section
*s
= uci_to_section(e
);
471 if (!strcmp(s
->type
, "mount"))
473 if (!strcmp(s
->type
, "swap"))
475 if (!strcmp(s
->type
, "global"))
478 vlist_flush(&mounts
);
483 static struct probe_info
* _probe_path(char *path
)
485 struct probe_info
*pr
;
488 /* skip ubi device if ubiblock device is present */
489 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
490 path
[8] >= '0' && path
[8] <= '9' ) {
491 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
492 list_for_each_entry(pr
, &devices
, list
)
493 if (!strcasecmp(pr
->dev
, tmppath
))
497 return probe_path(path
);
500 static int _cache_load(const char *path
)
502 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
506 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
509 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
510 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
512 list_add_tail(&pr
->list
, &devices
);
520 static void cache_load(int mtd
)
523 _cache_load("/dev/mtdblock*");
524 _cache_load("/dev/ubiblock*");
525 _cache_load("/dev/ubi[0-9]*");
527 _cache_load("/dev/loop*");
528 _cache_load("/dev/mmcblk*");
529 _cache_load("/dev/sd*");
530 _cache_load("/dev/hd*");
531 _cache_load("/dev/md*");
532 _cache_load("/dev/nvme*");
533 _cache_load("/dev/vd*");
534 _cache_load("/dev/xvd*");
535 _cache_load("/dev/mapper/*");
539 static int print_block_uci(struct probe_info
*pr
)
541 if (!strcmp(pr
->type
, "swap")) {
542 printf("config 'swap'\n");
544 printf("config 'mount'\n");
545 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
548 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
550 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
551 printf("\toption\tenabled\t'0'\n\n");
556 static struct probe_info
* find_block_info(char *uuid
, char *label
, char *path
)
558 struct probe_info
*pr
= NULL
;
561 list_for_each_entry(pr
, &devices
, list
)
562 if (pr
->uuid
&& !strcasecmp(pr
->uuid
, uuid
))
566 list_for_each_entry(pr
, &devices
, list
)
567 if (pr
->label
&& !strcmp(pr
->label
, label
))
571 list_for_each_entry(pr
, &devices
, list
)
572 if (pr
->dev
&& !strcmp(basename(pr
->dev
), basename(path
)))
578 static char* find_mount_point(char *block
)
580 FILE *fp
= fopen("/proc/self/mountinfo", "r");
581 static char line
[256];
582 int len
= strlen(block
);
583 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
586 unsigned int minor
, major
;
591 rstat
= stat(block
, &s
);
593 while (fgets(line
, sizeof(line
), fp
)) {
594 pos
= strchr(line
, ' ');
598 pos
= strchr(pos
+ 1, ' ');
603 pos
= strchr(pos
, ':');
610 pos
= strchr(pos
, ' ');
616 pos
= strchr(pos
+ 1, ' ');
621 pos
= strchr(pos
, ' ');
627 pos
= strchr(pos
+ 1, ' ');
631 pos
= strchr(pos
+ 1, ' ');
635 pos
= strchr(pos
+ 1, ' ');
640 pos
= strchr(pos
, ' ');
646 if (!strncmp(block
, devname
, len
)) {
647 point
= strdup(cpoint
);
654 if (!S_ISBLK(s
.st_mode
))
657 if (major
== major(s
.st_rdev
) &&
658 minor
== minor(s
.st_rdev
)) {
659 point
= strdup(cpoint
);
669 static int print_block_info(struct probe_info
*pr
)
673 mp
= find_mount_point(pr
->dev
);
674 printf("%s:", pr
->dev
);
676 printf(" UUID=\"%s\"", pr
->uuid
);
679 printf(" LABEL=\"%s\"", pr
->label
);
682 printf(" VERSION=\"%s\"", pr
->version
);
685 printf(" MOUNT=\"%s\"", mp
);
689 printf(" TYPE=\"%s\"\n", pr
->type
);
694 static void mkdir_p(char *dir
)
696 char *l
= strrchr(dir
, '/');
706 static void check_filesystem(struct probe_info
*pr
)
710 const char *e2fsck
= "/usr/sbin/e2fsck";
711 const char *f2fsck
= "/usr/sbin/fsck.f2fs";
712 const char *dosfsck
= "/usr/sbin/dosfsck";
713 const char *btrfsck
= "/usr/bin/btrfsck";
714 const char *ntfsck
= "/usr/bin/ntfsfix";
717 /* UBIFS does not need stuff like fsck */
718 if (!strncmp(pr
->type
, "ubifs", 5))
721 if (!strncmp(pr
->type
, "vfat", 4)) {
723 } else if (!strncmp(pr
->type
, "f2fs", 4)) {
725 } else if (!strncmp(pr
->type
, "ext", 3)) {
727 } else if (!strncmp(pr
->type
, "btrfs", 5)) {
729 } else if (!strncmp(pr
->type
, "ntfs", 4)) {
732 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
736 if (stat(ckfs
, &statbuf
) < 0) {
737 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
743 if(!strncmp(pr
->type
, "f2fs", 4)) {
744 execl(ckfs
, ckfs
, "-f", pr
->dev
, NULL
);
746 } else if(!strncmp(pr
->type
, "btrfs", 5)) {
747 execl(ckfs
, ckfs
, "--repair", pr
->dev
, NULL
);
749 } else if(!strncmp(pr
->type
, "ntfs", 4)) {
750 execl(ckfs
, ckfs
, "-b", pr
->dev
, NULL
);
753 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
756 } else if (pid
> 0) {
759 waitpid(pid
, &status
, 0);
760 if (WIFEXITED(status
) && WEXITSTATUS(status
))
761 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
762 if (WIFSIGNALED(status
))
763 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs
, strsignal(WTERMSIG(status
)));
767 static void handle_swapfiles(bool on
)
771 struct probe_info
*pr
;
773 vlist_for_each_element(&mounts
, m
, node
)
775 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
778 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
781 pr
= _probe_path(m
->target
);
786 if (!strcmp(pr
->type
, "swap")) {
788 swapon(pr
->dev
, m
->prio
);
797 static void to_devnull(int fd
)
799 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
804 if (devnull
> STDERR_FILENO
)
808 static int exec_mount(const char *source
, const char *target
,
809 const char *fstype
, const char *options
)
814 int err
, status
, pfds
[2];
815 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
817 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
819 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
820 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
827 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
828 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
840 to_devnull(STDIN_FILENO
);
841 to_devnull(STDOUT_FILENO
);
843 dup2(pfds
[1], STDERR_FILENO
);
847 if (options
&& *options
)
848 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
850 execl(cmd
, cmd
, source
, target
, NULL
);
857 mount_fd
= fdopen(pfds
[0], "r");
859 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
860 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
864 err
= waitpid(pid
, &status
, 0);
868 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
883 static int hotplug_call_mount(const char *action
, const char *device
)
890 char * const argv
[] = { "hotplug-call", "mount", NULL
};
892 setenv("ACTION", action
, 1);
893 setenv("DEVICE", device
, 1);
895 execv("/sbin/hotplug-call", argv
);
897 } else if (pid
> 0) {
900 pid
= waitpid(pid
, &status
, 0);
901 if (pid
<= 0 || !WIFEXITED(status
) || WEXITSTATUS(status
)) {
903 ULOG_ERR("hotplug-call call failed\n");
912 static int handle_mount(const char *source
, const char *target
,
913 const char *fstype
, struct mount
*m
)
916 size_t mount_opts_len
;
917 char *mount_opts
= NULL
, *ptr
;
919 err
= mount(source
, target
, fstype
, m
? m
->flags
: 0,
920 (m
&& m
->options
) ? m
->options
: "");
922 /* Requested file system type is not available in kernel,
923 attempt to call mount helper. */
924 if (err
== -1 && errno
== ENODEV
) {
926 /* Convert mount flags back into string representation,
927 first calculate needed length of string buffer... */
928 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
930 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
931 if ((mount_flags
[i
].flag
> 0) &&
932 (mount_flags
[i
].flag
< INT_MAX
) &&
933 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
934 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
936 /* ... then now allocate and fill it ... */
937 ptr
= mount_opts
= calloc(1, mount_opts_len
);
945 ptr
+= sprintf(ptr
, "%s,", m
->options
);
947 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
948 if ((mount_flags
[i
].flag
> 0) &&
949 (mount_flags
[i
].flag
< INT_MAX
) &&
950 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
951 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
953 mount_opts
[mount_opts_len
- 1] = 0;
956 /* ... and now finally invoke the external mount program */
957 err
= exec_mount(source
, target
, fstype
, mount_opts
);
963 static int blockd_notify(char *device
, struct mount
*m
, struct probe_info
*pr
)
965 struct ubus_context
*ctx
= ubus_connect(NULL
);
972 if (!ubus_lookup_id(ctx
, "block", &id
)) {
973 struct blob_buf buf
= { 0 };
974 char *d
= strrchr(device
, '/');
981 blob_buf_init(&buf
, 0);
985 blobmsg_add_string(&buf
, "device", d
);
987 blobmsg_add_string(&buf
, "uuid", m
->uuid
);
989 blobmsg_add_string(&buf
, "label", m
->label
);
991 blobmsg_add_string(&buf
, "target", m
->target
);
993 blobmsg_add_string(&buf
, "options", m
->options
);
995 blobmsg_add_u32(&buf
, "autofs", m
->autofs
);
997 blobmsg_add_string(&buf
, "type", pr
->type
);
999 blobmsg_add_string(&buf
, "version", pr
->version
);
1001 blobmsg_add_string(&buf
, "device", d
);
1003 blobmsg_add_string(&buf
, "uuid", pr
->uuid
);
1005 blobmsg_add_string(&buf
, "label", pr
->label
);
1007 blobmsg_add_string(&buf
, "type", pr
->type
);
1009 blobmsg_add_string(&buf
, "version", pr
->version
);
1010 blobmsg_add_u32(&buf
, "anon", 1);
1012 blobmsg_add_string(&buf
, "device", d
);
1013 blobmsg_add_u32(&buf
, "remove", 1);
1016 err
= ubus_invoke(ctx
, id
, "hotplug", buf
.head
, NULL
, NULL
, 3000);
1026 static int mount_device(struct probe_info
*pr
, int type
)
1038 device
= basename(pr
->dev
);
1040 if (!strcmp(pr
->type
, "swap")) {
1041 if ((type
== TYPE_HOTPLUG
) && !auto_swap
)
1043 m
= find_swap(pr
->uuid
, pr
->label
, device
);
1045 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
1050 mp
= find_mount_point(pr
->dev
);
1051 if (mp
&& (type
!= TYPE_HOTPLUG
)) {
1052 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
1057 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
1058 if (m
&& m
->extroot
)
1061 if (type
== TYPE_HOTPLUG
)
1062 blockd_notify(device
, m
, pr
);
1064 /* Check if device should be mounted & set the target directory */
1084 snprintf(_target
, sizeof(_target
), "/tmp/run/blockd/%s", device
);
1086 } else if (m
->target
) {
1089 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
1092 } else if (anon_mount
) {
1093 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
1096 /* No reason to mount this device */
1100 /* Mount the device */
1103 check_filesystem(pr
);
1107 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
1109 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1110 pr
->dev
, pr
->type
, target
, errno
);
1114 handle_swapfiles(true);
1116 hotplug_call_mount("add", device
);
1121 static int umount_device(char *path
)
1126 mp
= find_mount_point(path
);
1130 hotplug_call_mount("remove", basename(path
));
1132 err
= umount2(mp
, MNT_DETACH
);
1134 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path
, mp
,
1137 ULOG_INFO("unmounted %s (%s)\n", path
, mp
);
1145 static int mount_action(char *action
, char *device
, int type
)
1149 if (!action
|| !device
)
1151 snprintf(path
, sizeof(path
), "/dev/%s", device
);
1153 if (!strcmp(action
, "remove")) {
1154 if (type
== TYPE_HOTPLUG
)
1155 blockd_notify(device
, NULL
, NULL
);
1157 umount_device(path
);
1160 } else if (strcmp(action
, "add")) {
1161 ULOG_ERR("Unkown action %s\n", action
);
1166 if (config_load(NULL
))
1170 return mount_device(find_block_info(NULL
, NULL
, path
), type
);
1173 static int main_hotplug(int argc
, char **argv
)
1175 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG
);
1178 static int main_autofs(int argc
, char **argv
)
1183 if (!strcmp(argv
[2], "start")) {
1184 struct probe_info
*pr
;
1186 if (config_load(NULL
))
1190 list_for_each_entry(pr
, &devices
, list
) {
1193 if (!strcmp(pr
->type
, "swap"))
1196 m
= find_block(pr
->uuid
, pr
->label
, NULL
, NULL
);
1197 if (m
&& m
->extroot
)
1200 blockd_notify(pr
->dev
, m
, pr
);
1208 return mount_action(argv
[2], argv
[3], TYPE_AUTOFS
);
1211 static int find_block_mtd(char *name
, char *part
, int plen
)
1213 FILE *fp
= fopen("/proc/mtd", "r");
1214 static char line
[256];
1220 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1221 if (strstr(line
, name
)) {
1222 char *eol
= strstr(line
, ":");
1237 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1242 #ifdef UBIFS_EXTROOT
1243 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1247 while (ubi_dev_present(libubi
, dev
))
1249 struct ubi_dev_info dev_info
;
1250 struct ubi_vol_info vol_info
;
1252 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1254 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1257 *dev_num
= dev_info
.dev_num
;
1258 *vol_id
= vol_info
.vol_id
;
1266 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1272 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1274 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1279 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1285 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1287 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1294 static int find_root_dev(char *buf
, int len
)
1304 if (!(d
= opendir("/dev")))
1309 while ((e
= readdir(d
)) != NULL
) {
1310 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1312 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1325 static int test_fs_support(const char *name
)
1331 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1332 while (fgets(line
, sizeof(line
), f
)) {
1333 p
= strtok(line
, "\t\n");
1335 if (p
&& !strcmp(p
, "nodev"))
1336 p
= strtok(NULL
, "\t\n");
1338 if (p
&& !strcmp(p
, name
)) {
1349 static int check_extroot(char *path
)
1351 struct probe_info
*pr
= NULL
;
1354 #ifdef UBIFS_EXTROOT
1355 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1359 libubi
= libubi_open();
1360 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1361 libubi_close(libubi
);
1366 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1367 if (find_root_dev(devpath
, sizeof(devpath
))) {
1368 ULOG_ERR("extroot: unable to determine root device\n");
1374 list_for_each_entry(pr
, &devices
, list
) {
1375 if (!strcmp(pr
->dev
, devpath
)) {
1379 char uuid
[64] = { 0 };
1381 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1385 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1386 if (stat(tag
, &s
)) {
1387 fp
= fopen(tag
, "w+");
1389 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1391 /* return 0 to continue boot regardless of error */
1394 fputs(pr
->uuid
, fp
);
1399 fp
= fopen(tag
, "r");
1401 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1406 if (!fgets(uuid
, sizeof(uuid
), fp
))
1407 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n",
1411 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1414 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1415 pr
->uuid
, basename(path
), uuid
);
1420 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1425 * Read info about extroot from UCI (using prefix) and mount it.
1427 static int mount_extroot(char *cfg
)
1429 char overlay
[] = "/tmp/extroot/overlay";
1430 char mnt
[] = "/tmp/extroot/mnt";
1432 struct probe_info
*pr
;
1436 /* Load @cfg/etc/config/fstab */
1437 if (config_load(cfg
))
1440 /* See if there is extroot-specific mount config */
1441 m
= find_block(NULL
, NULL
, NULL
, "/");
1443 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1445 if (!m
|| !m
->extroot
)
1447 ULOG_INFO("extroot: not configured\n");
1451 /* Find block device pointed by the mount config */
1452 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1454 if (!pr
&& delay_root
){
1455 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1459 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1462 if (strncmp(pr
->type
, "ext", 3) &&
1463 strncmp(pr
->type
, "f2fs", 4) &&
1464 strncmp(pr
->type
, "btrfs", 5) &&
1465 strncmp(pr
->type
, "ntfs", 4) &&
1466 strncmp(pr
->type
, "ubifs", 5)) {
1467 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr
->type
);
1471 if (test_fs_support(pr
->type
)) {
1472 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1481 check_filesystem(pr
);
1483 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1484 (m
->options
) ? (m
->options
) : (""));
1487 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1488 pr
->dev
, pr
->type
, path
, errno
);
1489 } else if (m
->overlay
) {
1490 err
= check_extroot(path
);
1495 ULOG_ERR("extroot: cannot find device %s%s\n",
1496 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1497 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1503 static int main_extroot(int argc
, char **argv
)
1505 struct probe_info
*pr
;
1506 char blkdev_path
[32] = { 0 };
1508 #ifdef UBIFS_EXTROOT
1512 if (!getenv("PREINIT"))
1516 ULOG_ERR("Usage: block extroot\n");
1523 /* enable LOG_INFO messages */
1524 ulog_threshold(LOG_INFO
);
1527 * Look for "rootfs_data". We will want to mount it and check for
1528 * extroot configuration.
1531 /* Start with looking for MTD partition */
1532 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1533 if (blkdev_path
[0]) {
1534 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1535 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1536 char cfg
[] = "/tmp/jffs_cfg";
1539 * Mount MTD part and try extroot (using
1540 * /etc/config/fstab from that partition)
1543 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1544 err
= mount_extroot(cfg
);
1545 umount2(cfg
, MNT_DETACH
);
1548 rmdir("/tmp/overlay");
1554 #ifdef UBIFS_EXTROOT
1555 /* ... but it also could be an UBI volume */
1556 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1557 libubi
= libubi_open();
1558 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1559 libubi_close(libubi
);
1560 if (blkdev_path
[0]) {
1561 char cfg
[] = "/tmp/ubifs_cfg";
1563 /* Mount volume and try extroot (using fstab from that vol) */
1565 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1566 err
= mount_extroot(cfg
);
1567 umount2(cfg
, MNT_DETACH
);
1570 rmdir("/tmp/overlay");
1576 return mount_extroot(NULL
);
1579 static int main_mount(int argc
, char **argv
)
1581 struct probe_info
*pr
;
1583 if (config_load(NULL
))
1587 list_for_each_entry(pr
, &devices
, list
)
1588 mount_device(pr
, TYPE_DEV
);
1590 handle_swapfiles(true);
1595 static int main_umount(int argc
, char **argv
)
1597 struct probe_info
*pr
;
1599 if (config_load(NULL
))
1602 handle_swapfiles(false);
1605 list_for_each_entry(pr
, &devices
, list
) {
1608 if (!strcmp(pr
->type
, "swap"))
1611 m
= find_block(pr
->uuid
, pr
->label
, basename(pr
->dev
), NULL
);
1612 if (m
&& m
->extroot
)
1615 umount_device(pr
->dev
);
1621 static int main_detect(int argc
, char **argv
)
1623 struct probe_info
*pr
;
1626 printf("config 'global'\n");
1627 printf("\toption\tanon_swap\t'0'\n");
1628 printf("\toption\tanon_mount\t'0'\n");
1629 printf("\toption\tauto_swap\t'1'\n");
1630 printf("\toption\tauto_mount\t'1'\n");
1631 printf("\toption\tdelay_root\t'5'\n");
1632 printf("\toption\tcheck_fs\t'0'\n\n");
1633 list_for_each_entry(pr
, &devices
, list
)
1634 print_block_uci(pr
);
1639 static int main_info(int argc
, char **argv
)
1642 struct probe_info
*pr
;
1646 list_for_each_entry(pr
, &devices
, list
)
1647 print_block_info(pr
);
1652 for (i
= 2; i
< argc
; i
++) {
1655 if (stat(argv
[i
], &s
)) {
1656 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1659 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1660 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1663 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1665 print_block_info(pr
);
1671 static int swapon_usage(void)
1673 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1674 "\tStart swapping on [DEVICE]\n"
1675 " -a\tStart swapping on all swap devices\n"
1676 " -p pri\tSet priority of swap device\n"
1677 " -s\tShow summary\n");
1681 static int main_swapon(int argc
, char **argv
)
1687 struct probe_info
*pr
;
1693 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1696 fp
= fopen("/proc/swaps", "r");
1700 ULOG_ERR("failed to open /proc/swaps\n");
1703 while (getline(&lineptr
, &s
, fp
) > 0)
1704 printf("%s", lineptr
);
1711 list_for_each_entry(pr
, &devices
, list
) {
1712 if (strcmp(pr
->type
, "swap"))
1714 if (swapon(pr
->dev
, 0))
1715 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1721 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1724 return swapon_usage();
1729 if (optind
!= (argc
- 1))
1730 return swapon_usage();
1732 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1733 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1736 err
= swapon(argv
[optind
], flags
);
1738 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1745 static int main_swapoff(int argc
, char **argv
)
1748 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1749 "\tStop swapping on DEVICE\n"
1750 " -a\tStop swapping on all swap devices\n");
1754 if (!strcmp(argv
[1], "-a")) {
1755 FILE *fp
= fopen("/proc/swaps", "r");
1759 ULOG_ERR("failed to open /proc/swaps\n");
1762 if (fgets(line
, sizeof(line
), fp
))
1763 while (fgets(line
, sizeof(line
), fp
)) {
1764 char *end
= strchr(line
, ' ');
1770 err
= swapoff(line
);
1772 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1779 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1780 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1783 err
= swapoff(argv
[1]);
1785 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1793 int main(int argc
, char **argv
)
1795 char *base
= basename(*argv
);
1799 ulog_open(-1, -1, "block");
1800 ulog_threshold(LOG_NOTICE
);
1802 if (!strcmp(base
, "swapon"))
1803 return main_swapon(argc
, argv
);
1805 if (!strcmp(base
, "swapoff"))
1806 return main_swapoff(argc
, argv
);
1808 if ((argc
> 1) && !strcmp(base
, "block")) {
1809 if (!strcmp(argv
[1], "info"))
1810 return main_info(argc
, argv
);
1812 if (!strcmp(argv
[1], "detect"))
1813 return main_detect(argc
, argv
);
1815 if (!strcmp(argv
[1], "hotplug"))
1816 return main_hotplug(argc
, argv
);
1818 if (!strcmp(argv
[1], "autofs"))
1819 return main_autofs(argc
, argv
);
1821 if (!strcmp(argv
[1], "extroot"))
1822 return main_extroot(argc
, argv
);
1824 if (!strcmp(argv
[1], "mount"))
1825 return main_mount(argc
, argv
);
1827 if (!strcmp(argv
[1], "umount"))
1828 return main_umount(argc
, argv
);
1830 if (!strcmp(argv
[1], "remount")) {
1831 int ret
= main_umount(argc
, argv
);
1834 ret
= main_mount(argc
, argv
);
1839 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");