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>
40 #include <libubox/ulog.h>
41 #include <libubox/list.h>
42 #include <libubox/vlist.h>
43 #include <libubox/blobmsg_json.h>
44 #include <libubox/avl-cmp.h>
49 #include "libubi/libubi.h"
58 struct vlist_node node
;
74 static struct vlist_tree mounts
;
75 static struct blob_buf b
;
76 static LIST_HEAD(devices
);
77 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
78 static unsigned int delay_root
;
90 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
91 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
92 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
93 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
94 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
95 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
96 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
109 static const struct uci_blob_param_list config_attr_list
= {
110 .n_params
= __CFG_MAX
,
111 .params
= config_policy
,
114 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
115 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
116 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
117 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
118 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
119 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
120 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
123 static const struct uci_blob_param_list mount_attr_list
= {
124 .n_params
= __MOUNT_MAX
,
125 .params
= mount_policy
,
137 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
138 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
139 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
140 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
141 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
142 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
145 static const struct uci_blob_param_list swap_attr_list
= {
146 .n_params
= __SWAP_MAX
,
147 .params
= swap_policy
,
155 static const struct mount_flag mount_flags
[] = {
156 { "sync", MS_SYNCHRONOUS
},
157 { "async", ~MS_SYNCHRONOUS
},
158 { "dirsync", MS_DIRSYNC
},
159 { "mand", MS_MANDLOCK
},
160 { "nomand", ~MS_MANDLOCK
},
161 { "atime", ~MS_NOATIME
},
162 { "noatime", MS_NOATIME
},
163 { "dev", ~MS_NODEV
},
164 { "nodev", MS_NODEV
},
165 { "diratime", ~MS_NODIRATIME
},
166 { "nodiratime", MS_NODIRATIME
},
167 { "exec", ~MS_NOEXEC
},
168 { "noexec", MS_NOEXEC
},
169 { "suid", ~MS_NOSUID
},
170 { "nosuid", MS_NOSUID
},
171 { "rw", ~MS_RDONLY
},
173 { "relatime", MS_RELATIME
},
174 { "norelatime", ~MS_RELATIME
},
175 { "strictatime", MS_STRICTATIME
},
176 { "acl", MS_POSIXACL
},
177 { "noacl", ~MS_POSIXACL
},
178 { "nouser_xattr", MS_NOUSER
},
179 { "user_xattr", ~MS_NOUSER
},
182 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
187 return strdup(blobmsg_get_string(attr
));
190 static char *blobmsg_get_basename(struct blob_attr
*attr
)
195 return strdup(basename(blobmsg_get_string(attr
)));
198 static void parse_mount_options(struct mount
*m
, char *optstr
)
202 char *p
, *opts
, *last
;
207 if (!optstr
|| !*optstr
)
210 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
223 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
224 if (!strcmp(last
, mount_flags
[i
].name
)) {
225 if (mount_flags
[i
].flag
< 0)
226 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
228 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
235 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
244 static int mount_add(struct uci_section
*s
)
246 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
249 blob_buf_init(&b
, 0);
250 uci_to_blob(&b
, s
, &mount_attr_list
);
251 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
253 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
256 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
259 m
= malloc(sizeof(struct mount
));
260 m
->type
= TYPE_MOUNT
;
261 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
262 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
263 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
264 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
266 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
268 m
->overlay
= m
->extroot
= 0;
269 if (m
->target
&& !strcmp(m
->target
, "/"))
271 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
272 m
->extroot
= m
->overlay
= 1;
274 if (m
->target
&& *m
->target
!= '/') {
275 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
276 s
->e
.name
, m
->target
);
282 vlist_add(&mounts
, &m
->node
, m
->uuid
);
284 vlist_add(&mounts
, &m
->node
, m
->label
);
286 vlist_add(&mounts
, &m
->node
, m
->device
);
291 static int swap_add(struct uci_section
*s
)
293 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
296 blob_buf_init(&b
, 0);
297 uci_to_blob(&b
, s
, &swap_attr_list
);
298 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
300 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
303 m
= malloc(sizeof(struct mount
));
304 memset(m
, 0, sizeof(struct mount
));
306 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
307 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
308 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
310 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
312 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
314 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
315 /* store complete swap path */
317 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
320 vlist_add(&mounts
, &m
->node
, m
->uuid
);
322 vlist_add(&mounts
, &m
->node
, m
->label
);
324 vlist_add(&mounts
, &m
->node
, m
->device
);
330 static int global_add(struct uci_section
*s
)
332 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
334 blob_buf_init(&b
, 0);
335 uci_to_blob(&b
, s
, &config_attr_list
);
336 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
338 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
340 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
343 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
345 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
348 if (tb
[CFG_DELAY_ROOT
])
349 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
351 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
357 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
361 vlist_for_each_element(&mounts
, m
, node
) {
362 if (m
->type
!= TYPE_SWAP
)
364 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
366 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
368 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
375 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
380 vlist_for_each_element(&mounts
, m
, node
) {
381 if (m
->type
!= TYPE_MOUNT
)
383 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
385 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
387 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
389 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
396 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
397 struct vlist_node
*node_old
)
401 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
403 char *file
= basename(path
);
404 char *dir
= dirname(path
);
406 struct uci_package
*pkg
;
408 uci_set_confdir(ctx
, dir
);
409 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
411 if (uci_load(ctx
, file
, &pkg
)) {
412 uci_get_errorstr(ctx
, &err
, file
);
413 ULOG_ERR("unable to load configuration (%s)\n", err
);
422 static int config_load(char *cfg
)
424 struct uci_context
*ctx
= uci_alloc_context();
425 struct uci_package
*pkg
= NULL
;
426 struct uci_element
*e
;
429 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
432 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
433 pkg
= config_try_load(ctx
, path
);
436 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
437 pkg
= config_try_load(ctx
, path
);
442 snprintf(path
, sizeof(path
), "/etc/config/fstab");
443 pkg
= config_try_load(ctx
, path
);
447 ULOG_ERR("no usable configuration\n");
451 vlist_update(&mounts
);
452 uci_foreach_element(&pkg
->sections
, e
) {
453 struct uci_section
*s
= uci_to_section(e
);
455 if (!strcmp(s
->type
, "mount"))
457 if (!strcmp(s
->type
, "swap"))
459 if (!strcmp(s
->type
, "global"))
462 vlist_flush(&mounts
);
467 static struct probe_info
* _probe_path(char *path
)
469 struct probe_info
*pr
;
472 /* skip ubi device if ubiblock device is present */
473 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
474 path
[8] >= '0' && path
[8] <= '9' ) {
475 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
476 list_for_each_entry(pr
, &devices
, list
)
477 if (!strcasecmp(pr
->dev
, tmppath
))
481 return probe_path(path
);
484 static int _cache_load(const char *path
)
486 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
490 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
493 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
494 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
496 list_add_tail(&pr
->list
, &devices
);
504 static void cache_load(int mtd
)
507 _cache_load("/dev/mtdblock*");
508 _cache_load("/dev/ubiblock*");
509 _cache_load("/dev/ubi[0-9]*");
511 _cache_load("/dev/loop*");
512 _cache_load("/dev/mmcblk*");
513 _cache_load("/dev/sd*");
514 _cache_load("/dev/hd*");
515 _cache_load("/dev/md*");
516 _cache_load("/dev/vd*");
517 _cache_load("/dev/mapper/*");
521 static int print_block_uci(struct probe_info
*pr
)
523 if (!strcmp(pr
->type
, "swap")) {
524 printf("config 'swap'\n");
526 printf("config 'mount'\n");
527 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
530 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
532 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
533 printf("\toption\tenabled\t'0'\n\n");
538 static struct probe_info
* find_block_info(char *uuid
, char *label
, char *path
)
540 struct probe_info
*pr
= NULL
;
543 list_for_each_entry(pr
, &devices
, list
)
544 if (pr
->uuid
&& !strcasecmp(pr
->uuid
, uuid
))
548 list_for_each_entry(pr
, &devices
, list
)
549 if (pr
->label
&& !strcmp(pr
->label
, label
))
553 list_for_each_entry(pr
, &devices
, list
)
554 if (pr
->dev
&& !strcmp(basename(pr
->dev
), basename(path
)))
560 static char* find_mount_point(char *block
)
562 FILE *fp
= fopen("/proc/self/mountinfo", "r");
563 static char line
[256];
564 int len
= strlen(block
);
565 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
568 unsigned int minor
, major
;
573 rstat
= stat(block
, &s
);
575 while (fgets(line
, sizeof(line
), fp
)) {
576 pos
= strchr(line
, ' ');
580 pos
= strchr(pos
+ 1, ' ');
585 pos
= strchr(pos
, ':');
592 pos
= strchr(pos
, ' ');
598 pos
= strchr(pos
+ 1, ' ');
603 pos
= strchr(pos
, ' ');
609 pos
= strchr(pos
+ 1, ' ');
613 pos
= strchr(pos
+ 1, ' ');
617 pos
= strchr(pos
+ 1, ' ');
622 pos
= strchr(pos
, ' ');
628 if (!strncmp(block
, devname
, len
)) {
629 point
= strdup(cpoint
);
636 if (!S_ISBLK(s
.st_mode
))
639 if (major
== major(s
.st_rdev
) &&
640 minor
== minor(s
.st_rdev
)) {
641 point
= strdup(cpoint
);
651 static int print_block_info(struct probe_info
*pr
)
655 mp
= find_mount_point(pr
->dev
);
656 printf("%s:", pr
->dev
);
658 printf(" UUID=\"%s\"", pr
->uuid
);
661 printf(" LABEL=\"%s\"", pr
->label
);
664 printf(" VERSION=\"%s\"", pr
->version
);
667 printf(" MOUNT=\"%s\"", mp
);
671 printf(" TYPE=\"%s\"\n", pr
->type
);
676 static void mkdir_p(char *dir
)
678 char *l
= strrchr(dir
, '/');
688 static void check_filesystem(struct probe_info
*pr
)
692 const char *e2fsck
= "/usr/sbin/e2fsck";
693 const char *f2fsck
= "/usr/sbin/fsck.f2fs";
694 const char *dosfsck
= "/usr/sbin/dosfsck";
697 /* UBIFS does not need stuff like fsck */
698 if (!strncmp(pr
->type
, "ubifs", 5))
701 if (!strncmp(pr
->type
, "vfat", 4)) {
703 } else if (!strncmp(pr
->type
, "f2fs", 4)) {
705 } else if (!strncmp(pr
->type
, "ext", 3)) {
708 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
712 if (stat(ckfs
, &statbuf
) < 0) {
713 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
719 if(!strncmp(pr
->type
, "f2fs", 4)) {
720 execl(ckfs
, ckfs
, "-f", pr
->dev
, NULL
);
723 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
726 } else if (pid
> 0) {
729 waitpid(pid
, &status
, 0);
730 if (WEXITSTATUS(status
))
731 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
735 static void handle_swapfiles(bool on
)
739 struct probe_info
*pr
;
741 vlist_for_each_element(&mounts
, m
, node
)
743 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
746 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
749 pr
= _probe_path(m
->target
);
754 if (!strcmp(pr
->type
, "swap")) {
756 swapon(pr
->dev
, m
->prio
);
765 static void to_devnull(int fd
)
767 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
772 if (devnull
> STDERR_FILENO
)
776 static int exec_mount(const char *source
, const char *target
,
777 const char *fstype
, const char *options
)
782 int err
, status
, pfds
[2];
783 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
785 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
787 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
788 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
795 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
796 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
808 to_devnull(STDIN_FILENO
);
809 to_devnull(STDOUT_FILENO
);
811 dup2(pfds
[1], STDERR_FILENO
);
815 if (options
&& *options
)
816 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
818 execl(cmd
, cmd
, source
, target
, NULL
);
825 mount_fd
= fdopen(pfds
[0], "r");
827 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
828 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
832 err
= waitpid(pid
, &status
, 0);
836 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
851 static int handle_mount(const char *source
, const char *target
,
852 const char *fstype
, struct mount
*m
)
855 size_t mount_opts_len
;
856 char *mount_opts
= NULL
, *ptr
;
858 err
= mount(source
, target
, fstype
, m
? m
->flags
: 0,
859 (m
&& m
->options
) ? m
->options
: "");
861 /* Requested file system type is not available in kernel,
862 attempt to call mount helper. */
863 if (err
== -1 && errno
== ENODEV
) {
865 /* Convert mount flags back into string representation,
866 first calculate needed length of string buffer... */
867 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
869 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
870 if ((mount_flags
[i
].flag
> 0) &&
871 (mount_flags
[i
].flag
< INT_MAX
) &&
872 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
873 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
875 /* ... then now allocate and fill it ... */
876 ptr
= mount_opts
= calloc(1, mount_opts_len
);
884 ptr
+= sprintf(ptr
, "%s,", m
->options
);
886 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
887 if ((mount_flags
[i
].flag
> 0) &&
888 (mount_flags
[i
].flag
< INT_MAX
) &&
889 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
890 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
892 mount_opts
[mount_opts_len
- 1] = 0;
895 /* ... and now finally invoke the external mount program */
896 err
= exec_mount(source
, target
, fstype
, mount_opts
);
902 static int mount_device(struct probe_info
*pr
, int hotplug
)
911 device
= basename(pr
->dev
);
913 if (!strcmp(pr
->type
, "swap")) {
914 if (hotplug
&& !auto_swap
)
916 m
= find_swap(pr
->uuid
, pr
->label
, device
);
918 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
923 if (hotplug
&& !auto_mount
)
926 mp
= find_mount_point(pr
->dev
);
928 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
933 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
938 char *target
= m
->target
;
943 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
949 check_filesystem(pr
);
951 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
953 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
954 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
956 handle_swapfiles(true);
964 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
968 check_filesystem(pr
);
970 err
= handle_mount(pr
->dev
, target
, pr
->type
, NULL
);
972 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
973 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
975 handle_swapfiles(true);
982 static int umount_device(struct probe_info
*pr
)
985 char *device
= basename(pr
->dev
);
992 if (!strcmp(pr
->type
, "swap"))
995 mp
= find_mount_point(pr
->dev
);
999 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
1000 if (m
&& m
->extroot
)
1003 err
= umount2(mp
, MNT_DETACH
);
1005 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
1006 pr
->dev
, mp
, errno
, strerror(errno
));
1008 ULOG_INFO("unmounted %s (%s)\n",
1015 static int main_hotplug(int argc
, char **argv
)
1018 char *action
, *device
, *mount_point
;
1020 action
= getenv("ACTION");
1021 device
= getenv("DEVNAME");
1023 if (!action
|| !device
)
1025 snprintf(path
, sizeof(path
), "/dev/%s", device
);
1027 if (!strcmp(action
, "remove")) {
1029 mount_point
= find_mount_point(path
);
1031 err
= umount2(mount_point
, MNT_DETACH
);
1034 ULOG_ERR("umount of %s failed (%d) - %s\n",
1035 mount_point
, errno
, strerror(errno
));
1039 } else if (strcmp(action
, "add")) {
1040 ULOG_ERR("Unkown action %s\n", action
);
1045 if (config_load(NULL
))
1049 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
1052 static int find_block_mtd(char *name
, char *part
, int plen
)
1054 FILE *fp
= fopen("/proc/mtd", "r");
1055 static char line
[256];
1061 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1062 if (strstr(line
, name
)) {
1063 char *eol
= strstr(line
, ":");
1078 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1083 #ifdef UBIFS_EXTROOT
1084 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1088 while (ubi_dev_present(libubi
, dev
))
1090 struct ubi_dev_info dev_info
;
1091 struct ubi_vol_info vol_info
;
1093 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1095 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1098 *dev_num
= dev_info
.dev_num
;
1099 *vol_id
= vol_info
.vol_id
;
1107 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1113 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1115 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1120 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1126 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1128 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1135 static int find_root_dev(char *buf
, int len
)
1145 if (!(d
= opendir("/dev")))
1150 while ((e
= readdir(d
)) != NULL
) {
1151 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1153 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1166 static int test_fs_support(const char *name
)
1172 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1173 while (fgets(line
, sizeof(line
), f
)) {
1174 p
= strtok(line
, "\t\n");
1176 if (p
&& !strcmp(p
, "nodev"))
1177 p
= strtok(NULL
, "\t\n");
1179 if (p
&& !strcmp(p
, name
)) {
1190 static int check_extroot(char *path
)
1192 struct probe_info
*pr
= NULL
;
1195 #ifdef UBIFS_EXTROOT
1196 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1200 libubi
= libubi_open();
1201 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1202 libubi_close(libubi
);
1207 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1208 if (find_root_dev(devpath
, sizeof(devpath
))) {
1209 ULOG_ERR("extroot: unable to determine root device\n");
1215 list_for_each_entry(pr
, &devices
, list
) {
1216 if (!strcmp(pr
->dev
, devpath
)) {
1220 char uuid
[64] = { 0 };
1222 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1226 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1227 if (stat(tag
, &s
)) {
1228 fp
= fopen(tag
, "w+");
1230 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1231 tag
, errno
, strerror(errno
));
1232 /* return 0 to continue boot regardless of error */
1235 fputs(pr
->uuid
, fp
);
1240 fp
= fopen(tag
, "r");
1242 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1243 tag
, errno
, strerror(errno
));
1247 if (!fgets(uuid
, sizeof(uuid
), fp
))
1248 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1249 tag
, errno
, strerror(errno
));
1252 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1255 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1256 pr
->uuid
, basename(path
), uuid
);
1261 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1266 * Read info about extroot from UCI (using prefix) and mount it.
1268 static int mount_extroot(char *cfg
)
1270 char overlay
[] = "/tmp/extroot/overlay";
1271 char mnt
[] = "/tmp/extroot/mnt";
1273 struct probe_info
*pr
;
1277 /* Load @cfg/etc/config/fstab */
1278 if (config_load(cfg
))
1281 /* See if there is extroot-specific mount config */
1282 m
= find_block(NULL
, NULL
, NULL
, "/");
1284 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1286 if (!m
|| !m
->extroot
)
1288 ULOG_INFO("extroot: not configured\n");
1292 /* Find block device pointed by the mount config */
1293 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1295 if (!pr
&& delay_root
){
1296 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1300 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1303 if (strncmp(pr
->type
, "ext", 3) &&
1304 strncmp(pr
->type
, "f2fs", 4) &&
1305 strncmp(pr
->type
, "ubifs", 5)) {
1306 ULOG_ERR("extroot: unsupported filesystem %s, try ext4, f2fs or ubifs\n", pr
->type
);
1310 if (test_fs_support(pr
->type
)) {
1311 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1320 check_filesystem(pr
);
1322 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1323 (m
->options
) ? (m
->options
) : (""));
1326 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1327 pr
->dev
, pr
->type
, path
, errno
, strerror(errno
));
1328 } else if (m
->overlay
) {
1329 err
= check_extroot(path
);
1334 ULOG_ERR("extroot: cannot find device %s%s\n",
1335 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1336 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1342 static int main_extroot(int argc
, char **argv
)
1344 struct probe_info
*pr
;
1345 char blkdev_path
[32] = { 0 };
1347 #ifdef UBIFS_EXTROOT
1351 if (!getenv("PREINIT"))
1355 ULOG_ERR("Usage: block extroot\n");
1362 /* enable LOG_INFO messages */
1363 ulog_threshold(LOG_INFO
);
1366 * Look for "rootfs_data". We will want to mount it and check for
1367 * extroot configuration.
1370 /* Start with looking for MTD partition */
1371 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1372 if (blkdev_path
[0]) {
1373 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1374 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1375 char cfg
[] = "/tmp/jffs_cfg";
1378 * Mount MTD part and try extroot (using
1379 * /etc/config/fstab from that partition)
1382 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1383 err
= mount_extroot(cfg
);
1384 umount2(cfg
, MNT_DETACH
);
1387 rmdir("/tmp/overlay");
1393 #ifdef UBIFS_EXTROOT
1394 /* ... but it also could be an UBI volume */
1395 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1396 libubi
= libubi_open();
1397 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1398 libubi_close(libubi
);
1399 if (blkdev_path
[0]) {
1400 char cfg
[] = "/tmp/ubifs_cfg";
1402 /* Mount volume and try extroot (using fstab from that vol) */
1404 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1405 err
= mount_extroot(cfg
);
1406 umount2(cfg
, MNT_DETACH
);
1409 rmdir("/tmp/overlay");
1415 return mount_extroot(NULL
);
1418 static int main_mount(int argc
, char **argv
)
1420 struct probe_info
*pr
;
1422 if (config_load(NULL
))
1426 list_for_each_entry(pr
, &devices
, list
)
1427 mount_device(pr
, 0);
1429 handle_swapfiles(true);
1434 static int main_umount(int argc
, char **argv
)
1436 struct probe_info
*pr
;
1438 if (config_load(NULL
))
1441 handle_swapfiles(false);
1444 list_for_each_entry(pr
, &devices
, list
)
1450 static int main_detect(int argc
, char **argv
)
1452 struct probe_info
*pr
;
1455 printf("config 'global'\n");
1456 printf("\toption\tanon_swap\t'0'\n");
1457 printf("\toption\tanon_mount\t'0'\n");
1458 printf("\toption\tauto_swap\t'1'\n");
1459 printf("\toption\tauto_mount\t'1'\n");
1460 printf("\toption\tdelay_root\t'5'\n");
1461 printf("\toption\tcheck_fs\t'0'\n\n");
1462 list_for_each_entry(pr
, &devices
, list
)
1463 print_block_uci(pr
);
1468 static int main_info(int argc
, char **argv
)
1471 struct probe_info
*pr
;
1475 list_for_each_entry(pr
, &devices
, list
)
1476 print_block_info(pr
);
1481 for (i
= 2; i
< argc
; i
++) {
1484 if (stat(argv
[i
], &s
)) {
1485 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1488 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1489 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1492 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1494 print_block_info(pr
);
1500 static int swapon_usage(void)
1502 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1503 "\tStart swapping on [DEVICE]\n"
1504 " -a\tStart swapping on all swap devices\n"
1505 " -p pri\tSet priority of swap device\n"
1506 " -s\tShow summary\n");
1510 static int main_swapon(int argc
, char **argv
)
1516 struct probe_info
*pr
;
1522 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1525 fp
= fopen("/proc/swaps", "r");
1529 ULOG_ERR("failed to open /proc/swaps\n");
1532 while (getline(&lineptr
, &s
, fp
) > 0)
1533 printf("%s", lineptr
);
1540 list_for_each_entry(pr
, &devices
, list
) {
1541 if (strcmp(pr
->type
, "swap"))
1543 if (swapon(pr
->dev
, 0))
1544 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1550 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1553 return swapon_usage();
1558 if (optind
!= (argc
- 1))
1559 return swapon_usage();
1561 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1562 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1565 err
= swapon(argv
[optind
], flags
);
1567 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1574 static int main_swapoff(int argc
, char **argv
)
1577 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1578 "\tStop swapping on DEVICE\n"
1579 " -a\tStop swapping on all swap devices\n");
1583 if (!strcmp(argv
[1], "-a")) {
1584 FILE *fp
= fopen("/proc/swaps", "r");
1588 ULOG_ERR("failed to open /proc/swaps\n");
1591 if (fgets(line
, sizeof(line
), fp
))
1592 while (fgets(line
, sizeof(line
), fp
)) {
1593 char *end
= strchr(line
, ' ');
1599 err
= swapoff(line
);
1601 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1608 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1609 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1612 err
= swapoff(argv
[1]);
1614 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1622 int main(int argc
, char **argv
)
1624 char *base
= basename(*argv
);
1628 ulog_open(-1, -1, "block");
1629 ulog_threshold(LOG_NOTICE
);
1631 if (!strcmp(base
, "swapon"))
1632 return main_swapon(argc
, argv
);
1634 if (!strcmp(base
, "swapoff"))
1635 return main_swapoff(argc
, argv
);
1637 if ((argc
> 1) && !strcmp(base
, "block")) {
1638 if (!strcmp(argv
[1], "info"))
1639 return main_info(argc
, argv
);
1641 if (!strcmp(argv
[1], "detect"))
1642 return main_detect(argc
, argv
);
1644 if (!strcmp(argv
[1], "hotplug"))
1645 return main_hotplug(argc
, argv
);
1647 if (!strcmp(argv
[1], "extroot"))
1648 return main_extroot(argc
, argv
);
1650 if (!strcmp(argv
[1], "mount"))
1651 return main_mount(argc
, argv
);
1653 if (!strcmp(argv
[1], "umount"))
1654 return main_umount(argc
, argv
);
1656 if (!strcmp(argv
[1], "remount")) {
1657 int ret
= main_umount(argc
, argv
);
1660 ret
= main_mount(argc
, argv
);
1665 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");