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>
39 #include <libubox/avl-cmp.h>
40 #include <libubox/blobmsg_json.h>
41 #include <libubox/list.h>
42 #include <libubox/ulog.h>
43 #include <libubox/utils.h>
44 #include <libubox/vlist.h>
49 #define AUTOFS_MOUNT_PATH "/tmp/run/blockd/"
52 #include "libubi/libubi.h"
67 struct vlist_node node
;
84 static struct vlist_tree mounts
;
85 static struct blob_buf b
;
86 static LIST_HEAD(devices
);
87 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
88 static unsigned int delay_root
;
100 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
101 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
102 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
103 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
104 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
105 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
106 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
120 static const struct uci_blob_param_list config_attr_list
= {
121 .n_params
= __CFG_MAX
,
122 .params
= config_policy
,
125 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
126 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
127 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
128 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
129 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
130 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
131 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
132 [MOUNT_AUTOFS
] = { .name
= "autofs", .type
= BLOBMSG_TYPE_INT32
},
135 static const struct uci_blob_param_list mount_attr_list
= {
136 .n_params
= __MOUNT_MAX
,
137 .params
= mount_policy
,
149 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
150 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
151 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
152 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
153 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
154 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
157 static const struct uci_blob_param_list swap_attr_list
= {
158 .n_params
= __SWAP_MAX
,
159 .params
= swap_policy
,
167 static const struct mount_flag mount_flags
[] = {
168 { "sync", MS_SYNCHRONOUS
},
169 { "async", ~MS_SYNCHRONOUS
},
170 { "dirsync", MS_DIRSYNC
},
171 { "mand", MS_MANDLOCK
},
172 { "nomand", ~MS_MANDLOCK
},
173 { "atime", ~MS_NOATIME
},
174 { "noatime", MS_NOATIME
},
175 { "dev", ~MS_NODEV
},
176 { "nodev", MS_NODEV
},
177 { "diratime", ~MS_NODIRATIME
},
178 { "nodiratime", MS_NODIRATIME
},
179 { "exec", ~MS_NOEXEC
},
180 { "noexec", MS_NOEXEC
},
181 { "suid", ~MS_NOSUID
},
182 { "nosuid", MS_NOSUID
},
183 { "rw", ~MS_RDONLY
},
185 { "relatime", MS_RELATIME
},
186 { "norelatime", ~MS_RELATIME
},
187 { "strictatime", MS_STRICTATIME
},
188 { "acl", MS_POSIXACL
},
189 { "noacl", ~MS_POSIXACL
},
190 { "nouser_xattr", MS_NOUSER
},
191 { "user_xattr", ~MS_NOUSER
},
194 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
199 return strdup(blobmsg_get_string(attr
));
202 static char *blobmsg_get_basename(struct blob_attr
*attr
)
207 return strdup(basename(blobmsg_get_string(attr
)));
210 static void parse_mount_options(struct mount
*m
, char *optstr
)
214 char *p
, *opts
, *last
;
219 if (!optstr
|| !*optstr
)
222 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
235 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
236 if (!strcmp(last
, mount_flags
[i
].name
)) {
237 if (mount_flags
[i
].flag
< 0)
238 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
240 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
247 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
256 static int mount_add(struct uci_section
*s
)
258 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
261 blob_buf_init(&b
, 0);
262 uci_to_blob(&b
, s
, &mount_attr_list
);
263 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
265 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
268 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
271 m
= malloc(sizeof(struct mount
));
272 m
->type
= TYPE_MOUNT
;
273 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
274 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
275 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
276 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
277 if (tb
[MOUNT_AUTOFS
])
278 m
->autofs
= blobmsg_get_u32(tb
[MOUNT_AUTOFS
]);
281 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
283 m
->overlay
= m
->extroot
= 0;
284 if (m
->target
&& !strcmp(m
->target
, "/"))
286 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
287 m
->extroot
= m
->overlay
= 1;
289 if (m
->target
&& *m
->target
!= '/') {
290 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
291 s
->e
.name
, m
->target
);
297 vlist_add(&mounts
, &m
->node
, m
->uuid
);
299 vlist_add(&mounts
, &m
->node
, m
->label
);
301 vlist_add(&mounts
, &m
->node
, m
->device
);
306 static int swap_add(struct uci_section
*s
)
308 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
311 blob_buf_init(&b
, 0);
312 uci_to_blob(&b
, s
, &swap_attr_list
);
313 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
315 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
318 m
= malloc(sizeof(struct mount
));
319 memset(m
, 0, sizeof(struct mount
));
321 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
322 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
323 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
325 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
327 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
329 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
330 /* store complete swap path */
332 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
335 vlist_add(&mounts
, &m
->node
, m
->uuid
);
337 vlist_add(&mounts
, &m
->node
, m
->label
);
339 vlist_add(&mounts
, &m
->node
, m
->device
);
345 static int global_add(struct uci_section
*s
)
347 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
349 blob_buf_init(&b
, 0);
350 uci_to_blob(&b
, s
, &config_attr_list
);
351 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
353 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
355 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
358 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
360 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
363 if (tb
[CFG_DELAY_ROOT
])
364 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
366 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
372 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
376 vlist_for_each_element(&mounts
, m
, node
) {
377 if (m
->type
!= TYPE_SWAP
)
379 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
381 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
383 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
390 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
395 vlist_for_each_element(&mounts
, m
, node
) {
396 if (m
->type
!= TYPE_MOUNT
)
398 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
400 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
402 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
404 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
411 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
412 struct vlist_node
*node_old
)
416 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
418 char *file
= basename(path
);
419 char *dir
= dirname(path
);
421 struct uci_package
*pkg
;
423 uci_set_confdir(ctx
, dir
);
424 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
426 if (uci_load(ctx
, file
, &pkg
)) {
427 uci_get_errorstr(ctx
, &err
, file
);
428 ULOG_ERR("unable to load configuration (%s)\n", err
);
437 static int config_load(char *cfg
)
439 struct uci_context
*ctx
= uci_alloc_context();
440 struct uci_package
*pkg
= NULL
;
441 struct uci_element
*e
;
444 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
447 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
448 pkg
= config_try_load(ctx
, path
);
451 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
452 pkg
= config_try_load(ctx
, path
);
457 snprintf(path
, sizeof(path
), "/etc/config/fstab");
458 pkg
= config_try_load(ctx
, path
);
462 ULOG_ERR("no usable configuration\n");
466 vlist_update(&mounts
);
467 uci_foreach_element(&pkg
->sections
, e
) {
468 struct uci_section
*s
= uci_to_section(e
);
470 if (!strcmp(s
->type
, "mount"))
472 if (!strcmp(s
->type
, "swap"))
474 if (!strcmp(s
->type
, "global"))
477 vlist_flush(&mounts
);
482 static bool mtdblock_is_nand(char *mtdnum
)
488 snprintf(tmppath
, sizeof(tmppath
) - 1, "/sys/class/mtd/mtd%s/type", mtdnum
);
489 fp
= fopen(tmppath
, "r");
493 if (!fgets(buf
, sizeof(buf
), fp
)) {
498 buf
[sizeof(buf
) - 1] = '\0'; /* make sure buf is 0-terminated */
499 buf
[strlen(buf
) - 1] = '\0'; /* strip final char (newline) */
501 if (strcmp(buf
, "nand"))
506 * Keep probing rootfs and rootfs_data in the meantime to not break
507 * devices using JFFS2 on NAND but only trigger the kernel warnings.
508 * Remove this once all devices using JFFS2 and squashfs directly on
509 * NAND have been converted to UBI.
511 snprintf(tmppath
, sizeof(tmppath
) - 1, "/sys/class/mtd/mtd%s/name", mtdnum
);
512 fp
= fopen(tmppath
, "r");
516 if (!fgets(buf
, sizeof(buf
), fp
)) {
521 buf
[sizeof(buf
) - 1] = '\0'; /* make sure buf is 0-terminated */
522 buf
[strlen(buf
) - 1] = '\0'; /* strip final char (newline) */
524 /* only return true if name differs from 'rootfs' and 'rootfs_data' */
525 if (strcmp(buf
, "rootfs") && strcmp(buf
, "rootfs_data"))
528 /* --- CUT HERE --- */
532 static struct probe_info
* _probe_path(char *path
)
534 struct probe_info
*pr
, *epr
;
537 if (!strncmp(path
, "/dev/mtdblock", 13) && mtdblock_is_nand(path
+ 13))
540 pr
= probe_path(path
);
544 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
545 path
[8] >= '0' && path
[8] <= '9' ) {
546 /* skip ubi device if not UBIFS (as it requires ubiblock) */
547 if (strcmp("ubifs", pr
->type
))
550 /* skip ubi device if ubiblock device is present */
551 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
552 list_for_each_entry(epr
, &devices
, list
)
553 if (!strcmp(epr
->dev
, tmppath
))
560 static int _cache_load(const char *path
)
562 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
566 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
569 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
570 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
572 list_add_tail(&pr
->list
, &devices
);
580 static void cache_load(int mtd
)
583 _cache_load("/dev/mtdblock*");
584 _cache_load("/dev/ubiblock*");
585 _cache_load("/dev/ubi[0-9]*");
587 _cache_load("/dev/loop*");
588 _cache_load("/dev/mmcblk*");
589 _cache_load("/dev/sd*");
590 _cache_load("/dev/hd*");
591 _cache_load("/dev/md*");
592 _cache_load("/dev/nvme*");
593 _cache_load("/dev/vd*");
594 _cache_load("/dev/xvd*");
595 _cache_load("/dev/dm-*");
599 static struct probe_info
* find_block_info(char *uuid
, char *label
, char *path
)
601 struct probe_info
*pr
= NULL
;
604 list_for_each_entry(pr
, &devices
, list
)
605 if (pr
->uuid
&& !strcasecmp(pr
->uuid
, uuid
))
609 list_for_each_entry(pr
, &devices
, list
)
610 if (pr
->label
&& !strcmp(pr
->label
, label
))
614 list_for_each_entry(pr
, &devices
, list
)
615 if (pr
->dev
&& !strcmp(basename(pr
->dev
), basename(path
)))
621 static char* find_mount_point(char *block
)
623 FILE *fp
= fopen("/proc/self/mountinfo", "r");
624 static char line
[256];
625 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
628 unsigned int minor
, major
;
633 rstat
= stat(block
, &s
);
635 while (fgets(line
, sizeof(line
), fp
)) {
636 pos
= strchr(line
, ' ');
640 pos
= strchr(pos
+ 1, ' ');
645 pos
= strchr(pos
, ':');
652 pos
= strchr(pos
, ' ');
658 pos
= strchr(pos
+ 1, ' ');
663 pos
= strchr(pos
, ' ');
669 pos
= strchr(pos
+ 1, ' ');
673 pos
= strchr(pos
+ 1, ' ');
677 pos
= strchr(pos
+ 1, ' ');
682 pos
= strchr(pos
, ' ');
688 if (!strcmp(block
, devname
)) {
689 point
= strdup(cpoint
);
696 if (!S_ISBLK(s
.st_mode
))
699 if (major
== major(s
.st_rdev
) &&
700 minor
== minor(s
.st_rdev
)) {
701 point
= strdup(cpoint
);
711 static int print_block_uci(struct probe_info
*pr
)
713 if (!strcmp(pr
->type
, "swap")) {
714 printf("config 'swap'\n");
716 char *mp
= find_mount_point(pr
->dev
);
718 printf("config 'mount'\n");
720 printf("\toption\ttarget\t'%s'\n", mp
);
723 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
727 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
729 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
730 printf("\toption\tenabled\t'0'\n\n");
735 static int print_block_info(struct probe_info
*pr
)
739 mp
= find_mount_point(pr
->dev
);
740 printf("%s:", pr
->dev
);
742 printf(" UUID=\"%s\"", pr
->uuid
);
745 printf(" LABEL=\"%s\"", pr
->label
);
748 printf(" VERSION=\"%s\"", pr
->version
);
751 printf(" MOUNT=\"%s\"", mp
);
755 printf(" TYPE=\"%s\"\n", pr
->type
);
760 static void check_filesystem(struct probe_info
*pr
)
764 const char *e2fsck
= "/usr/sbin/e2fsck";
765 const char *f2fsck
= "/usr/sbin/fsck.f2fs";
766 const char *fatfsck
= "/usr/sbin/fsck.fat";
767 const char *btrfsck
= "/usr/bin/btrfsck";
768 const char *ntfsck
= "/usr/bin/ntfsfix";
771 /* UBIFS does not need stuff like fsck */
772 if (!strncmp(pr
->type
, "ubifs", 5))
775 if (!strncmp(pr
->type
, "vfat", 4)) {
777 } else if (!strncmp(pr
->type
, "f2fs", 4)) {
779 } else if (!strncmp(pr
->type
, "ext", 3)) {
781 } else if (!strncmp(pr
->type
, "btrfs", 5)) {
783 } else if (!strncmp(pr
->type
, "ntfs", 4)) {
786 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
790 if (stat(ckfs
, &statbuf
) < 0) {
791 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
797 if(!strncmp(pr
->type
, "f2fs", 4)) {
798 execl(ckfs
, ckfs
, "-f", pr
->dev
, NULL
);
800 } else if(!strncmp(pr
->type
, "btrfs", 5)) {
801 execl(ckfs
, ckfs
, "--repair", pr
->dev
, NULL
);
803 } else if(!strncmp(pr
->type
, "ntfs", 4)) {
804 execl(ckfs
, ckfs
, "-b", pr
->dev
, NULL
);
807 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
810 } else if (pid
> 0) {
813 waitpid(pid
, &status
, 0);
814 if (WIFEXITED(status
) && WEXITSTATUS(status
))
815 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
816 if (WIFSIGNALED(status
))
817 ULOG_ERR("check_filesystem: %s terminated by %s\n", ckfs
, strsignal(WTERMSIG(status
)));
821 static void handle_swapfiles(bool on
)
825 struct probe_info
*pr
;
827 vlist_for_each_element(&mounts
, m
, node
)
829 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
832 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
835 pr
= _probe_path(m
->target
);
840 if (!strcmp(pr
->type
, "swap")) {
842 swapon(pr
->dev
, m
->prio
);
851 static void to_devnull(int fd
)
853 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
858 if (devnull
> STDERR_FILENO
)
862 static int exec_mount(const char *source
, const char *target
,
863 const char *fstype
, const char *options
)
868 int err
, status
, pfds
[2];
869 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
871 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
873 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
874 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
881 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
882 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
894 to_devnull(STDIN_FILENO
);
895 to_devnull(STDOUT_FILENO
);
897 dup2(pfds
[1], STDERR_FILENO
);
901 if (options
&& *options
)
902 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
904 execl(cmd
, cmd
, source
, target
, NULL
);
911 mount_fd
= fdopen(pfds
[0], "r");
913 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
914 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
918 err
= waitpid(pid
, &status
, 0);
922 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
937 static const char * const ntfs_fs
[] = { "ntfs3", "ntfs-3g", "antfs", "ntfs" };
939 static int handle_mount(const char *source
, const char *target
,
940 const char *fstype
, struct mount
*m
)
942 size_t mount_opts_len
;
943 char *mount_opts
= NULL
, *ptr
;
944 const char * const *filesystems
;
949 if (!strcmp(fstype
, "ntfs")) {
950 filesystems
= ntfs_fs
;
951 count
= ARRAY_SIZE(ntfs_fs
);
953 filesystems
= &fstype
;
957 for (i
= 0; i
< count
; i
++) {
958 const char *fs
= filesystems
[i
];
960 err
= mount(source
, target
, fs
, m
? m
->flags
: 0,
961 (m
&& m
->options
) ? m
->options
: "");
962 if (!err
|| errno
!= ENODEV
)
966 /* Requested file system type is not available in kernel,
967 attempt to call mount helper. */
968 if (err
== -1 && errno
== ENODEV
) {
970 /* Convert mount flags back into string representation,
971 first calculate needed length of string buffer... */
972 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
974 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
975 if ((mount_flags
[i
].flag
> 0) &&
976 (mount_flags
[i
].flag
< INT_MAX
) &&
977 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
978 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
980 /* ... then now allocate and fill it ... */
981 ptr
= mount_opts
= calloc(1, mount_opts_len
);
989 ptr
+= sprintf(ptr
, "%s,", m
->options
);
991 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
992 if ((mount_flags
[i
].flag
> 0) &&
993 (mount_flags
[i
].flag
< INT_MAX
) &&
994 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
995 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
997 mount_opts
[mount_opts_len
- 1] = 0;
1000 /* ... and now finally invoke the external mount program */
1001 for (i
= 0; i
< count
; i
++) {
1002 const char *fs
= filesystems
[i
];
1004 err
= exec_mount(source
, target
, fs
, mount_opts
);
1015 static int blockd_notify(const char *method
, char *device
, struct mount
*m
,
1016 struct probe_info
*pr
)
1018 struct ubus_context
*ctx
= ubus_connect(NULL
);
1025 if (!ubus_lookup_id(ctx
, "block", &id
)) {
1026 struct blob_buf buf
= { 0 };
1027 char *d
= strrchr(device
, '/');
1034 blob_buf_init(&buf
, 0);
1038 blobmsg_add_string(&buf
, "device", d
);
1040 blobmsg_add_string(&buf
, "uuid", m
->uuid
);
1042 blobmsg_add_string(&buf
, "label", m
->label
);
1044 blobmsg_add_string(&buf
, "target", m
->target
);
1046 blobmsg_add_string(&buf
, "options", m
->options
);
1048 blobmsg_add_u32(&buf
, "autofs", m
->autofs
);
1050 blobmsg_add_string(&buf
, "type", pr
->type
);
1052 blobmsg_add_string(&buf
, "version", pr
->version
);
1054 blobmsg_add_string(&buf
, "device", d
);
1056 blobmsg_add_string(&buf
, "uuid", pr
->uuid
);
1058 blobmsg_add_string(&buf
, "label", pr
->label
);
1060 blobmsg_add_string(&buf
, "type", pr
->type
);
1062 blobmsg_add_string(&buf
, "version", pr
->version
);
1063 blobmsg_add_u32(&buf
, "anon", 1);
1065 blobmsg_add_string(&buf
, "device", d
);
1066 blobmsg_add_u32(&buf
, "remove", 1);
1069 err
= ubus_invoke(ctx
, id
, method
, buf
.head
, NULL
, NULL
, 3000);
1079 static int mount_device(struct probe_info
*pr
, int type
)
1083 char *_target
= NULL
;
1092 device
= basename(pr
->dev
);
1094 if (!strcmp(pr
->type
, "swap")) {
1095 if ((type
== TYPE_HOTPLUG
) && !auto_swap
)
1097 m
= find_swap(pr
->uuid
, pr
->label
, device
);
1099 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
1104 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
1105 if (m
&& m
->extroot
)
1108 mp
= find_mount_point(pr
->dev
);
1110 if (m
&& m
->type
== TYPE_MOUNT
&& m
->target
&& strcmp(m
->target
, mp
)) {
1111 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
1119 if (type
== TYPE_HOTPLUG
)
1120 blockd_notify("hotplug", device
, m
, pr
);
1122 /* Check if device should be mounted & set the target directory */
1142 if (asprintf(&_target
, "/tmp/run/blockd/%s", device
) == -1)
1146 } else if (m
->target
) {
1149 if (asprintf(&_target
, "/mnt/%s", device
) == -1)
1154 } else if (anon_mount
) {
1155 if (asprintf(&_target
, "/mnt/%s", device
) == -1)
1160 /* No reason to mount this device */
1164 /* Mount the device */
1167 check_filesystem(pr
);
1169 mkdir_p(target
, 0755);
1170 if (!lstat(target
, &st
) && S_ISLNK(st
.st_mode
))
1173 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
1175 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %m\n",
1176 pr
->dev
, pr
->type
, target
, errno
);
1187 handle_swapfiles(true);
1189 if (type
!= TYPE_AUTOFS
)
1190 blockd_notify("mount", device
, NULL
, NULL
);
1195 static int umount_device(char *path
, int type
, bool all
)
1200 if (strlen(path
) > 5 && !strncmp("/dev/", path
, 5)) {
1201 mp
= find_mount_point(path
);
1203 devpath
= malloc(strlen(path
) + 6);
1204 strcpy(devpath
, "/dev/");
1205 strcat(devpath
, path
);
1206 mp
= find_mount_point(devpath
);
1212 if (!strcmp(mp
, "/") && !all
) {
1216 if (type
!= TYPE_AUTOFS
)
1217 blockd_notify("umount", basename(path
), NULL
, NULL
);
1219 err
= umount2(mp
, MNT_DETACH
);
1221 ULOG_ERR("unmounting %s (%s) failed (%d) - %m\n", path
, mp
,
1224 ULOG_INFO("unmounted %s (%s)\n", path
, mp
);
1232 static int mount_action(char *action
, char *device
, int type
)
1235 struct probe_info
*pr
;
1237 if (!action
|| !device
)
1240 if (!strcmp(action
, "remove")) {
1241 if (type
== TYPE_HOTPLUG
)
1242 blockd_notify("hotplug", device
, NULL
, NULL
);
1244 umount_device(device
, type
, true);
1247 } else if (strcmp(action
, "add")) {
1248 ULOG_ERR("Unkown action %s\n", action
);
1253 if (config_load(NULL
))
1258 list_for_each_entry(pr
, &devices
, list
)
1259 if (!strcmp(basename(pr
->dev
), device
))
1265 return mount_device(find_block_info(NULL
, NULL
, path
), type
);
1268 static int main_hotplug(int argc
, char **argv
)
1270 return mount_action(getenv("ACTION"), getenv("DEVNAME"), TYPE_HOTPLUG
);
1273 static int main_autofs(int argc
, char **argv
)
1280 if (!strcmp(argv
[2], "start")) {
1281 struct probe_info
*pr
;
1283 if (config_load(NULL
))
1287 list_for_each_entry(pr
, &devices
, list
) {
1291 if (!strcmp(pr
->type
, "swap"))
1294 m
= find_block(pr
->uuid
, pr
->label
, NULL
, NULL
);
1295 if (m
&& m
->extroot
)
1298 blockd_notify("hotplug", pr
->dev
, m
, pr
);
1299 if ((!m
|| !m
->autofs
) && (mp
= find_mount_point(pr
->dev
))) {
1300 blockd_notify("mount", pr
->dev
, NULL
, NULL
);
1308 err
= mount_action(argv
[2], argv
[3], TYPE_AUTOFS
);
1312 ULOG_ERR("autofs: \"%s\" action has failed: %d\n", argv
[2], err
);
1318 static int find_block_mtd(char *name
, char *part
, int plen
)
1320 FILE *fp
= fopen("/proc/mtd", "r");
1321 static char line
[256];
1327 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1328 if (strstr(line
, name
)) {
1329 char *eol
= strstr(line
, ":");
1344 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1349 #ifdef UBIFS_EXTROOT
1350 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1354 while (ubi_dev_present(libubi
, dev
))
1356 struct ubi_dev_info dev_info
;
1357 struct ubi_vol_info vol_info
;
1359 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1361 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1364 *dev_num
= dev_info
.dev_num
;
1365 *vol_id
= vol_info
.vol_id
;
1373 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1379 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1381 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1386 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1392 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1394 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1400 static int find_dev(const char *path
, char *buf
, int len
)
1410 if (!(d
= opendir("/dev")))
1415 while ((e
= readdir(d
)) != NULL
) {
1416 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1418 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1429 static int find_root_dev(char *buf
, int len
)
1431 int err
= find_dev("/", buf
, len
);
1433 err
= find_dev("/rom", buf
, len
);
1438 static int test_fs_support(const char *name
)
1444 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1445 while (fgets(line
, sizeof(line
), f
)) {
1446 p
= strtok(line
, "\t\n");
1448 if (p
&& !strcmp(p
, "nodev"))
1449 p
= strtok(NULL
, "\t\n");
1451 if (p
&& !strcmp(p
, name
)) {
1463 * Check if mounted partition is a valid extroot
1465 * @path target mount point
1467 * Valid extroot partition has to contain /etc/.extroot-uuid with UUID of root
1468 * device. This function reads UUID and verifies it OR writes UUID to
1469 * .extroot-uuid if it doesn't exist yet (first extroot usage).
1471 static int check_extroot(char *path
)
1473 struct probe_info
*pr
= NULL
;
1474 struct probe_info
*tmp
;
1476 char uuid
[64] = { 0 };
1482 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-default", path
);
1486 err
= find_root_dev(devpath
, sizeof(devpath
));
1488 err
= find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
));
1489 #ifdef UBIFS_EXTROOT
1493 libubi
= libubi_open();
1494 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1495 libubi_close(libubi
);
1499 ULOG_ERR("extroot: unable to determine root device\n");
1503 /* Find root device probe_info so we know its UUID */
1504 list_for_each_entry(tmp
, &devices
, list
) {
1505 if (!strcmp(tmp
->dev
, devpath
)) {
1511 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1515 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1519 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1520 if (stat(tag
, &s
)) {
1521 fp
= fopen(tag
, "w+");
1523 ULOG_ERR("extroot: failed to write UUID to %s: %d (%m)\n",
1525 /* return 0 to continue boot regardless of error */
1528 fputs(pr
->uuid
, fp
);
1533 fp
= fopen(tag
, "r");
1535 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag
,
1540 if (!fgets(uuid
, sizeof(uuid
), fp
))
1541 ULOG_ERR("extroot: failed to read UUID from %s: %d (%m)\n", tag
,
1545 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1548 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n", pr
->uuid
,
1549 basename(path
), uuid
);
1554 * Read info about extroot from UCI (using prefix) and mount it.
1556 static int mount_extroot(char *cfg
)
1558 char overlay
[] = "/tmp/extroot/overlay";
1559 char mnt
[] = "/tmp/extroot/mnt";
1561 struct probe_info
*pr
;
1565 /* Load @cfg/etc/config/fstab */
1566 if (config_load(cfg
))
1569 /* See if there is extroot-specific mount config */
1570 m
= find_block(NULL
, NULL
, NULL
, "/");
1572 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1574 if (!m
|| !m
->extroot
)
1576 ULOG_INFO("extroot: not configured\n");
1580 /* Find block device pointed by the mount config */
1581 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1583 if (!pr
&& delay_root
){
1584 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1588 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1591 if (strncmp(pr
->type
, "ext", 3) &&
1592 strncmp(pr
->type
, "f2fs", 4) &&
1593 strncmp(pr
->type
, "btrfs", 5) &&
1594 strncmp(pr
->type
, "ntfs", 4) &&
1595 strncmp(pr
->type
, "ubifs", 5)) {
1596 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs, btrfs, ntfs or ubifs\n", pr
->type
);
1600 if (test_fs_support(pr
->type
)) {
1601 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1607 mkdir_p(path
, 0755);
1610 check_filesystem(pr
);
1612 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1613 (m
->options
) ? (m
->options
) : (""));
1616 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%m)\n",
1617 pr
->dev
, pr
->type
, path
, errno
);
1618 } else if (m
->overlay
) {
1619 err
= check_extroot(path
);
1624 ULOG_ERR("extroot: cannot find device %s%s\n",
1625 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1626 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1633 * Look for extroot config and mount it if present
1635 * Look for /etc/config/fstab on all supported partitions and use it for
1636 * mounting extroot if specified.
1638 static int main_extroot(int argc
, char **argv
)
1640 struct probe_info
*pr
;
1641 char blkdev_path
[32] = { 0 };
1643 #ifdef UBIFS_EXTROOT
1647 if (!getenv("PREINIT"))
1651 ULOG_ERR("Usage: block extroot\n");
1658 /* enable LOG_INFO messages */
1659 ulog_threshold(LOG_INFO
);
1661 /* try the currently mounted overlay if exists */
1662 err
= mount_extroot("/tmp/overlay");
1667 * Look for "rootfs_data". We will want to mount it and check for
1668 * extroot configuration.
1671 /* Start with looking for MTD partition */
1672 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1673 if (blkdev_path
[0]) {
1674 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1675 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1676 char cfg
[] = "/tmp/jffs_cfg";
1679 * Mount MTD part and try extroot (using
1680 * /etc/config/fstab from that partition)
1683 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1684 err
= mount_extroot(cfg
);
1685 umount2(cfg
, MNT_DETACH
);
1688 rmdir("/tmp/overlay");
1694 #ifdef UBIFS_EXTROOT
1695 /* ... but it also could be an UBI volume */
1696 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1697 libubi
= libubi_open();
1698 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1699 libubi_close(libubi
);
1700 if (blkdev_path
[0]) {
1701 char cfg
[] = "/tmp/ubifs_cfg";
1703 /* Mount volume and try extroot (using fstab from that vol) */
1705 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1706 err
= mount_extroot(cfg
);
1707 umount2(cfg
, MNT_DETACH
);
1710 rmdir("/tmp/overlay");
1716 /* As a last resort look for /etc/config/fstab on "rootfs" partition */
1717 return mount_extroot(NULL
);
1720 static int main_mount(int argc
, char **argv
)
1722 struct probe_info
*pr
;
1724 if (config_load(NULL
))
1728 list_for_each_entry(pr
, &devices
, list
)
1729 mount_device(pr
, TYPE_DEV
);
1731 handle_swapfiles(true);
1736 static int main_umount(int argc
, char **argv
)
1738 struct probe_info
*pr
;
1741 if (config_load(NULL
))
1744 handle_swapfiles(false);
1749 all
= !strcmp(argv
[2], "-a");
1751 list_for_each_entry(pr
, &devices
, list
) {
1754 if (!strcmp(pr
->type
, "swap"))
1757 m
= find_block(pr
->uuid
, pr
->label
, basename(pr
->dev
), NULL
);
1758 if (m
&& m
->extroot
)
1761 umount_device(pr
->dev
, TYPE_DEV
, all
);
1767 static int main_detect(int argc
, char **argv
)
1769 struct probe_info
*pr
;
1772 printf("config 'global'\n");
1773 printf("\toption\tanon_swap\t'0'\n");
1774 printf("\toption\tanon_mount\t'0'\n");
1775 printf("\toption\tauto_swap\t'1'\n");
1776 printf("\toption\tauto_mount\t'1'\n");
1777 printf("\toption\tdelay_root\t'5'\n");
1778 printf("\toption\tcheck_fs\t'0'\n\n");
1779 list_for_each_entry(pr
, &devices
, list
)
1780 print_block_uci(pr
);
1785 static int main_info(int argc
, char **argv
)
1788 struct probe_info
*pr
;
1792 list_for_each_entry(pr
, &devices
, list
)
1793 print_block_info(pr
);
1798 for (i
= 2; i
< argc
; i
++) {
1801 if (stat(argv
[i
], &s
)) {
1802 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1805 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1806 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1809 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1811 print_block_info(pr
);
1817 static int swapon_usage(void)
1819 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1820 "\tStart swapping on [DEVICE]\n"
1821 " -a\tStart swapping on all swap devices\n"
1822 " -p pri\tSet priority of swap device\n"
1823 " -s\tShow summary\n");
1827 static int main_swapon(int argc
, char **argv
)
1833 struct probe_info
*pr
;
1839 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1842 fp
= fopen("/proc/swaps", "r");
1846 ULOG_ERR("failed to open /proc/swaps\n");
1849 while (getline(&lineptr
, &s
, fp
) > 0)
1850 printf("%s", lineptr
);
1857 list_for_each_entry(pr
, &devices
, list
) {
1858 if (strcmp(pr
->type
, "swap"))
1860 if (swapon(pr
->dev
, 0))
1861 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1867 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1870 return swapon_usage();
1874 if (optind
!= (argc
- 1))
1875 return swapon_usage();
1877 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1878 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1881 err
= swapon(argv
[optind
], flags
);
1883 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1890 static int main_swapoff(int argc
, char **argv
)
1893 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1894 "\tStop swapping on DEVICE\n"
1895 " -a\tStop swapping on all swap devices\n");
1899 if (!strcmp(argv
[1], "-a")) {
1900 FILE *fp
= fopen("/proc/swaps", "r");
1904 ULOG_ERR("failed to open /proc/swaps\n");
1907 if (fgets(line
, sizeof(line
), fp
))
1908 while (fgets(line
, sizeof(line
), fp
)) {
1909 char *end
= strchr(line
, ' ');
1915 err
= swapoff(line
);
1917 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1924 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1925 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1928 err
= swapoff(argv
[1]);
1930 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1938 int main(int argc
, char **argv
)
1940 char *base
= basename(*argv
);
1944 ulog_open(-1, -1, "block");
1945 ulog_threshold(LOG_NOTICE
);
1947 if (!strcmp(base
, "swapon"))
1948 return main_swapon(argc
, argv
);
1950 if (!strcmp(base
, "swapoff"))
1951 return main_swapoff(argc
, argv
);
1953 if ((argc
> 1) && !strcmp(base
, "block")) {
1954 if (!strcmp(argv
[1], "info"))
1955 return main_info(argc
, argv
);
1957 if (!strcmp(argv
[1], "detect"))
1958 return main_detect(argc
, argv
);
1960 if (!strcmp(argv
[1], "hotplug"))
1961 return main_hotplug(argc
, argv
);
1963 if (!strcmp(argv
[1], "autofs"))
1964 return main_autofs(argc
, argv
);
1966 if (!strcmp(argv
[1], "extroot"))
1967 return main_extroot(argc
, argv
);
1969 if (!strcmp(argv
[1], "mount"))
1970 return main_mount(argc
, argv
);
1972 if (!strcmp(argv
[1], "umount"))
1973 return main_umount(argc
, argv
);
1975 if (!strcmp(argv
[1], "remount")) {
1976 int ret
= main_umount(argc
, argv
);
1979 ret
= main_mount(argc
, argv
);
1984 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");