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 *dosfsck
= "/usr/sbin/dosfsck";
696 /* UBIFS does not need stuff like fsck */
697 if (!strncmp(pr
->type
, "ubifs", 5))
700 if (!strncmp(pr
->type
, "vfat", 4)) {
702 } else if (!strncmp(pr
->type
, "ext", 3)) {
705 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
709 if (stat(ckfs
, &statbuf
) < 0) {
710 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
716 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
718 } else if (pid
> 0) {
721 waitpid(pid
, &status
, 0);
722 if (WEXITSTATUS(status
))
723 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
727 static void handle_swapfiles(bool on
)
731 struct probe_info
*pr
;
733 vlist_for_each_element(&mounts
, m
, node
)
735 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
738 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
741 pr
= _probe_path(m
->target
);
746 if (!strcmp(pr
->type
, "swap")) {
748 swapon(pr
->dev
, m
->prio
);
757 static void to_devnull(int fd
)
759 int devnull
= open("/dev/null", fd
? O_WRONLY
: O_RDONLY
);
764 if (devnull
> STDERR_FILENO
)
768 static int exec_mount(const char *source
, const char *target
,
769 const char *fstype
, const char *options
)
774 int err
, status
, pfds
[2];
775 char errmsg
[128], cmd
[sizeof("/sbin/mount.XXXXXXXXXXXXXXXX\0")];
777 snprintf(cmd
, sizeof(cmd
), "/sbin/mount.%s", fstype
);
779 if (stat(cmd
, &s
) < 0 || !S_ISREG(s
.st_mode
) || !(s
.st_mode
& S_IXUSR
)) {
780 ULOG_ERR("No \"mount.%s\" utility available\n", fstype
);
787 fcntl(pfds
[0], F_SETFD
, fcntl(pfds
[0], F_GETFD
) | FD_CLOEXEC
);
788 fcntl(pfds
[1], F_SETFD
, fcntl(pfds
[1], F_GETFD
) | FD_CLOEXEC
);
800 to_devnull(STDIN_FILENO
);
801 to_devnull(STDOUT_FILENO
);
803 dup2(pfds
[1], STDERR_FILENO
);
807 if (options
&& *options
)
808 execl(cmd
, cmd
, "-o", options
, source
, target
, NULL
);
810 execl(cmd
, cmd
, source
, target
, NULL
);
817 mount_fd
= fdopen(pfds
[0], "r");
819 while (fgets(errmsg
, sizeof(errmsg
), mount_fd
))
820 ULOG_ERR("mount.%s: %s", fstype
, errmsg
);
824 err
= waitpid(pid
, &status
, 0);
828 ULOG_ERR("mount.%s: failed with status %d\n", fstype
, status
);
843 static int handle_mount(const char *source
, const char *target
,
844 const char *fstype
, struct mount
*m
)
847 size_t mount_opts_len
;
848 char *mount_opts
= NULL
, *ptr
;
850 err
= mount(source
, target
, fstype
, m
? m
->flags
: 0,
851 (m
&& m
->options
) ? m
->options
: "");
853 /* Requested file system type is not available in kernel,
854 attempt to call mount helper. */
855 if (err
== -1 && errno
== ENODEV
) {
857 /* Convert mount flags back into string representation,
858 first calculate needed length of string buffer... */
859 mount_opts_len
= 1 + (m
->options
? strlen(m
->options
) : 0);
861 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
862 if ((mount_flags
[i
].flag
> 0) &&
863 (mount_flags
[i
].flag
< INT_MAX
) &&
864 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
865 mount_opts_len
+= strlen(mount_flags
[i
].name
) + 1;
867 /* ... then now allocate and fill it ... */
868 ptr
= mount_opts
= calloc(1, mount_opts_len
);
876 ptr
+= sprintf(ptr
, "%s,", m
->options
);
878 for (i
= 0; i
< ARRAY_SIZE(mount_flags
); i
++)
879 if ((mount_flags
[i
].flag
> 0) &&
880 (mount_flags
[i
].flag
< INT_MAX
) &&
881 (m
->flags
& (uint32_t)mount_flags
[i
].flag
))
882 ptr
+= sprintf(ptr
, "%s,", mount_flags
[i
].name
);
884 mount_opts
[mount_opts_len
- 1] = 0;
887 /* ... and now finally invoke the external mount program */
888 err
= exec_mount(source
, target
, fstype
, mount_opts
);
894 static int mount_device(struct probe_info
*pr
, int hotplug
)
903 device
= basename(pr
->dev
);
905 if (!strcmp(pr
->type
, "swap")) {
906 if (hotplug
&& !auto_swap
)
908 m
= find_swap(pr
->uuid
, pr
->label
, device
);
910 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
915 if (hotplug
&& !auto_mount
)
918 mp
= find_mount_point(pr
->dev
);
920 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
925 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
930 char *target
= m
->target
;
935 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
941 check_filesystem(pr
);
943 err
= handle_mount(pr
->dev
, target
, pr
->type
, m
);
945 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
946 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
948 handle_swapfiles(true);
956 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
960 check_filesystem(pr
);
962 err
= handle_mount(pr
->dev
, target
, pr
->type
, NULL
);
964 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
965 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
967 handle_swapfiles(true);
974 static int umount_device(struct probe_info
*pr
)
977 char *device
= basename(pr
->dev
);
984 if (!strcmp(pr
->type
, "swap"))
987 mp
= find_mount_point(pr
->dev
);
991 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
995 err
= umount2(mp
, MNT_DETACH
);
997 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
998 pr
->dev
, mp
, errno
, strerror(errno
));
1000 ULOG_INFO("unmounted %s (%s)\n",
1007 static int main_hotplug(int argc
, char **argv
)
1010 char *action
, *device
, *mount_point
;
1012 action
= getenv("ACTION");
1013 device
= getenv("DEVNAME");
1015 if (!action
|| !device
)
1017 snprintf(path
, sizeof(path
), "/dev/%s", device
);
1019 if (!strcmp(action
, "remove")) {
1021 mount_point
= find_mount_point(path
);
1023 err
= umount2(mount_point
, MNT_DETACH
);
1026 ULOG_ERR("umount of %s failed (%d) - %s\n",
1027 mount_point
, errno
, strerror(errno
));
1031 } else if (strcmp(action
, "add")) {
1032 ULOG_ERR("Unkown action %s\n", action
);
1037 if (config_load(NULL
))
1041 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
1044 static int find_block_mtd(char *name
, char *part
, int plen
)
1046 FILE *fp
= fopen("/proc/mtd", "r");
1047 static char line
[256];
1053 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
1054 if (strstr(line
, name
)) {
1055 char *eol
= strstr(line
, ":");
1070 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
1075 #ifdef UBIFS_EXTROOT
1076 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
1080 while (ubi_dev_present(libubi
, dev
))
1082 struct ubi_dev_info dev_info
;
1083 struct ubi_vol_info vol_info
;
1085 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
1087 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
1090 *dev_num
= dev_info
.dev_num
;
1091 *vol_id
= vol_info
.vol_id
;
1099 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
1105 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1107 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
1112 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
1118 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
1120 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
1127 static int find_root_dev(char *buf
, int len
)
1137 if (!(d
= opendir("/dev")))
1142 while ((e
= readdir(d
)) != NULL
) {
1143 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1145 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1158 static int test_fs_support(const char *name
)
1164 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1165 while (fgets(line
, sizeof(line
), f
)) {
1166 p
= strtok(line
, "\t\n");
1168 if (p
&& !strcmp(p
, "nodev"))
1169 p
= strtok(NULL
, "\t\n");
1171 if (p
&& !strcmp(p
, name
)) {
1182 static int check_extroot(char *path
)
1184 struct probe_info
*pr
= NULL
;
1187 #ifdef UBIFS_EXTROOT
1188 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1192 libubi
= libubi_open();
1193 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1194 libubi_close(libubi
);
1199 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1200 if (find_root_dev(devpath
, sizeof(devpath
))) {
1201 ULOG_ERR("extroot: unable to determine root device\n");
1207 list_for_each_entry(pr
, &devices
, list
) {
1208 if (!strcmp(pr
->dev
, devpath
)) {
1212 char uuid
[64] = { 0 };
1214 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1218 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1219 if (stat(tag
, &s
)) {
1220 fp
= fopen(tag
, "w+");
1222 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1223 tag
, errno
, strerror(errno
));
1224 /* return 0 to continue boot regardless of error */
1227 fputs(pr
->uuid
, fp
);
1232 fp
= fopen(tag
, "r");
1234 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1235 tag
, errno
, strerror(errno
));
1239 if (!fgets(uuid
, sizeof(uuid
), fp
))
1240 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1241 tag
, errno
, strerror(errno
));
1244 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1247 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1248 pr
->uuid
, basename(path
), uuid
);
1253 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1258 * Read info about extroot from UCI (using prefix) and mount it.
1260 static int mount_extroot(char *cfg
)
1262 char overlay
[] = "/tmp/extroot/overlay";
1263 char mnt
[] = "/tmp/extroot/mnt";
1265 struct probe_info
*pr
;
1269 /* Load @cfg/etc/config/fstab */
1270 if (config_load(cfg
))
1273 /* See if there is extroot-specific mount config */
1274 m
= find_block(NULL
, NULL
, NULL
, "/");
1276 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1278 if (!m
|| !m
->extroot
)
1280 ULOG_INFO("extroot: not configured\n");
1284 /* Find block device pointed by the mount config */
1285 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1287 if (!pr
&& delay_root
){
1288 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1292 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1295 if (strncmp(pr
->type
, "ext", 3) &&
1296 strncmp(pr
->type
, "ubifs", 5)) {
1297 ULOG_ERR("extroot: unsupported filesystem %s, try ext4\n", pr
->type
);
1301 if (test_fs_support(pr
->type
)) {
1302 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1311 check_filesystem(pr
);
1313 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1314 (m
->options
) ? (m
->options
) : (""));
1317 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1318 pr
->dev
, pr
->type
, path
, errno
, strerror(errno
));
1319 } else if (m
->overlay
) {
1320 err
= check_extroot(path
);
1325 ULOG_ERR("extroot: cannot find device %s%s\n",
1326 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1327 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1333 static int main_extroot(int argc
, char **argv
)
1335 struct probe_info
*pr
;
1336 char blkdev_path
[32] = { 0 };
1338 #ifdef UBIFS_EXTROOT
1342 if (!getenv("PREINIT"))
1346 ULOG_ERR("Usage: block extroot\n");
1353 /* enable LOG_INFO messages */
1354 ulog_threshold(LOG_INFO
);
1357 * Look for "rootfs_data". We will want to mount it and check for
1358 * extroot configuration.
1361 /* Start with looking for MTD partition */
1362 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1363 if (blkdev_path
[0]) {
1364 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1365 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1366 char cfg
[] = "/tmp/jffs_cfg";
1369 * Mount MTD part and try extroot (using
1370 * /etc/config/fstab from that partition)
1373 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1374 err
= mount_extroot(cfg
);
1375 umount2(cfg
, MNT_DETACH
);
1378 rmdir("/tmp/overlay");
1384 #ifdef UBIFS_EXTROOT
1385 /* ... but it also could be an UBI volume */
1386 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1387 libubi
= libubi_open();
1388 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1389 libubi_close(libubi
);
1390 if (blkdev_path
[0]) {
1391 char cfg
[] = "/tmp/ubifs_cfg";
1393 /* Mount volume and try extroot (using fstab from that vol) */
1395 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1396 err
= mount_extroot(cfg
);
1397 umount2(cfg
, MNT_DETACH
);
1400 rmdir("/tmp/overlay");
1406 return mount_extroot(NULL
);
1409 static int main_mount(int argc
, char **argv
)
1411 struct probe_info
*pr
;
1413 if (config_load(NULL
))
1417 list_for_each_entry(pr
, &devices
, list
)
1418 mount_device(pr
, 0);
1420 handle_swapfiles(true);
1425 static int main_umount(int argc
, char **argv
)
1427 struct probe_info
*pr
;
1429 if (config_load(NULL
))
1432 handle_swapfiles(false);
1435 list_for_each_entry(pr
, &devices
, list
)
1441 static int main_detect(int argc
, char **argv
)
1443 struct probe_info
*pr
;
1446 printf("config 'global'\n");
1447 printf("\toption\tanon_swap\t'0'\n");
1448 printf("\toption\tanon_mount\t'0'\n");
1449 printf("\toption\tauto_swap\t'1'\n");
1450 printf("\toption\tauto_mount\t'1'\n");
1451 printf("\toption\tdelay_root\t'5'\n");
1452 printf("\toption\tcheck_fs\t'0'\n\n");
1453 list_for_each_entry(pr
, &devices
, list
)
1454 print_block_uci(pr
);
1459 static int main_info(int argc
, char **argv
)
1462 struct probe_info
*pr
;
1466 list_for_each_entry(pr
, &devices
, list
)
1467 print_block_info(pr
);
1472 for (i
= 2; i
< argc
; i
++) {
1475 if (stat(argv
[i
], &s
)) {
1476 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1479 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1480 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1483 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1485 print_block_info(pr
);
1491 static int swapon_usage(void)
1493 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1494 "\tStart swapping on [DEVICE]\n"
1495 " -a\tStart swapping on all swap devices\n"
1496 " -p pri\tSet priority of swap device\n"
1497 " -s\tShow summary\n");
1501 static int main_swapon(int argc
, char **argv
)
1507 struct probe_info
*pr
;
1513 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1516 fp
= fopen("/proc/swaps", "r");
1520 ULOG_ERR("failed to open /proc/swaps\n");
1523 while (getline(&lineptr
, &s
, fp
) > 0)
1524 printf("%s", lineptr
);
1531 list_for_each_entry(pr
, &devices
, list
) {
1532 if (strcmp(pr
->type
, "swap"))
1534 if (swapon(pr
->dev
, 0))
1535 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1541 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1544 return swapon_usage();
1549 if (optind
!= (argc
- 1))
1550 return swapon_usage();
1552 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1553 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1556 err
= swapon(argv
[optind
], flags
);
1558 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1565 static int main_swapoff(int argc
, char **argv
)
1568 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1569 "\tStop swapping on DEVICE\n"
1570 " -a\tStop swapping on all swap devices\n");
1574 if (!strcmp(argv
[1], "-a")) {
1575 FILE *fp
= fopen("/proc/swaps", "r");
1579 ULOG_ERR("failed to open /proc/swaps\n");
1582 if (fgets(line
, sizeof(line
), fp
))
1583 while (fgets(line
, sizeof(line
), fp
)) {
1584 char *end
= strchr(line
, ' ');
1590 err
= swapoff(line
);
1592 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1599 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1600 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1603 err
= swapoff(argv
[1]);
1605 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1613 int main(int argc
, char **argv
)
1615 char *base
= basename(*argv
);
1619 ulog_open(-1, -1, "block");
1620 ulog_threshold(LOG_NOTICE
);
1622 if (!strcmp(base
, "swapon"))
1623 return main_swapon(argc
, argv
);
1625 if (!strcmp(base
, "swapoff"))
1626 return main_swapoff(argc
, argv
);
1628 if ((argc
> 1) && !strcmp(base
, "block")) {
1629 if (!strcmp(argv
[1], "info"))
1630 return main_info(argc
, argv
);
1632 if (!strcmp(argv
[1], "detect"))
1633 return main_detect(argc
, argv
);
1635 if (!strcmp(argv
[1], "hotplug"))
1636 return main_hotplug(argc
, argv
);
1638 if (!strcmp(argv
[1], "extroot"))
1639 return main_extroot(argc
, argv
);
1641 if (!strcmp(argv
[1], "mount"))
1642 return main_mount(argc
, argv
);
1644 if (!strcmp(argv
[1], "umount"))
1645 return main_umount(argc
, argv
);
1647 if (!strcmp(argv
[1], "remount")) {
1648 int ret
= main_umount(argc
, argv
);
1651 ret
= main_mount(argc
, argv
);
1656 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");