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.
28 #include <sys/types.h>
30 #include <sys/mount.h>
38 #include <libubox/ulog.h>
39 #include <libubox/list.h>
40 #include <libubox/vlist.h>
41 #include <libubox/blobmsg_json.h>
42 #include <libubox/avl-cmp.h>
47 #include "libubi/libubi.h"
56 struct vlist_node node
;
72 static struct vlist_tree mounts
;
73 static struct blob_buf b
;
74 static LIST_HEAD(devices
);
75 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
76 static unsigned int delay_root
;
88 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
89 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
90 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
91 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
92 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
93 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
94 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
107 static const struct uci_blob_param_list config_attr_list
= {
108 .n_params
= __CFG_MAX
,
109 .params
= config_policy
,
112 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
113 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
114 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
115 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
116 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
117 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
118 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
121 static const struct uci_blob_param_list mount_attr_list
= {
122 .n_params
= __MOUNT_MAX
,
123 .params
= mount_policy
,
135 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
136 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
137 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
138 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
139 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
140 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
143 static const struct uci_blob_param_list swap_attr_list
= {
144 .n_params
= __SWAP_MAX
,
145 .params
= swap_policy
,
153 static const struct mount_flag mount_flags
[] = {
154 { "sync", MS_SYNCHRONOUS
},
155 { "async", ~MS_SYNCHRONOUS
},
156 { "dirsync", MS_DIRSYNC
},
157 { "mand", MS_MANDLOCK
},
158 { "nomand", ~MS_MANDLOCK
},
159 { "atime", ~MS_NOATIME
},
160 { "noatime", MS_NOATIME
},
161 { "dev", ~MS_NODEV
},
162 { "nodev", MS_NODEV
},
163 { "diratime", ~MS_NODIRATIME
},
164 { "nodiratime", MS_NODIRATIME
},
165 { "exec", ~MS_NOEXEC
},
166 { "noexec", MS_NOEXEC
},
167 { "suid", ~MS_NOSUID
},
168 { "nosuid", MS_NOSUID
},
169 { "rw", ~MS_RDONLY
},
171 { "relatime", MS_RELATIME
},
172 { "norelatime", ~MS_RELATIME
},
173 { "strictatime", MS_STRICTATIME
},
174 { "acl", MS_POSIXACL
},
175 { "noacl", ~MS_POSIXACL
},
176 { "nouser_xattr", MS_NOUSER
},
177 { "user_xattr", ~MS_NOUSER
},
180 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
185 return strdup(blobmsg_get_string(attr
));
188 static char *blobmsg_get_basename(struct blob_attr
*attr
)
193 return strdup(basename(blobmsg_get_string(attr
)));
196 static void parse_mount_options(struct mount
*m
, char *optstr
)
200 char *p
, *opts
, *last
;
205 if (!optstr
|| !*optstr
)
208 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
221 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
222 if (!strcmp(last
, mount_flags
[i
].name
)) {
223 if (mount_flags
[i
].flag
< 0)
224 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
226 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
233 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
242 static int mount_add(struct uci_section
*s
)
244 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
247 blob_buf_init(&b
, 0);
248 uci_to_blob(&b
, s
, &mount_attr_list
);
249 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
251 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
254 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
257 m
= malloc(sizeof(struct mount
));
258 m
->type
= TYPE_MOUNT
;
259 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
260 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
261 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
262 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
264 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
266 m
->overlay
= m
->extroot
= 0;
267 if (m
->target
&& !strcmp(m
->target
, "/"))
269 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
270 m
->extroot
= m
->overlay
= 1;
272 if (m
->target
&& *m
->target
!= '/') {
273 ULOG_WARN("ignoring mount section %s due to invalid target '%s'\n",
274 s
->e
.name
, m
->target
);
280 vlist_add(&mounts
, &m
->node
, m
->uuid
);
282 vlist_add(&mounts
, &m
->node
, m
->label
);
284 vlist_add(&mounts
, &m
->node
, m
->device
);
289 static int swap_add(struct uci_section
*s
)
291 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
294 blob_buf_init(&b
, 0);
295 uci_to_blob(&b
, s
, &swap_attr_list
);
296 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
298 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
301 m
= malloc(sizeof(struct mount
));
302 memset(m
, 0, sizeof(struct mount
));
304 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
305 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
306 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
308 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
310 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
312 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
313 /* store complete swap path */
315 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
318 vlist_add(&mounts
, &m
->node
, m
->uuid
);
320 vlist_add(&mounts
, &m
->node
, m
->label
);
322 vlist_add(&mounts
, &m
->node
, m
->device
);
328 static int global_add(struct uci_section
*s
)
330 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
332 blob_buf_init(&b
, 0);
333 uci_to_blob(&b
, s
, &config_attr_list
);
334 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
336 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
338 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
341 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
343 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
346 if (tb
[CFG_DELAY_ROOT
])
347 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
349 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
355 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
359 vlist_for_each_element(&mounts
, m
, node
) {
360 if (m
->type
!= TYPE_SWAP
)
362 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
364 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
366 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
373 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
378 vlist_for_each_element(&mounts
, m
, node
) {
379 if (m
->type
!= TYPE_MOUNT
)
381 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
383 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
385 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
387 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
394 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
395 struct vlist_node
*node_old
)
399 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
401 char *file
= basename(path
);
402 char *dir
= dirname(path
);
404 struct uci_package
*pkg
;
406 uci_set_confdir(ctx
, dir
);
407 ULOG_INFO("attempting to load %s/%s\n", dir
, file
);
409 if (uci_load(ctx
, file
, &pkg
)) {
410 uci_get_errorstr(ctx
, &err
, file
);
411 ULOG_ERR("unable to load configuration (%s)\n", err
);
420 static int config_load(char *cfg
)
422 struct uci_context
*ctx
= uci_alloc_context();
423 struct uci_package
*pkg
= NULL
;
424 struct uci_element
*e
;
427 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
430 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
431 pkg
= config_try_load(ctx
, path
);
434 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
435 pkg
= config_try_load(ctx
, path
);
440 snprintf(path
, sizeof(path
), "/etc/config/fstab");
441 pkg
= config_try_load(ctx
, path
);
445 ULOG_ERR("no usable configuration\n");
449 vlist_update(&mounts
);
450 uci_foreach_element(&pkg
->sections
, e
) {
451 struct uci_section
*s
= uci_to_section(e
);
453 if (!strcmp(s
->type
, "mount"))
455 if (!strcmp(s
->type
, "swap"))
457 if (!strcmp(s
->type
, "global"))
460 vlist_flush(&mounts
);
465 static struct probe_info
* _probe_path(char *path
)
467 struct probe_info
*pr
;
470 /* skip ubi device if ubiblock device is present */
471 if (path
[5] == 'u' && path
[6] == 'b' && path
[7] == 'i' &&
472 path
[8] >= '0' && path
[8] <= '9' ) {
473 snprintf(tmppath
, sizeof(tmppath
), "/dev/ubiblock%s", path
+ 8);
474 list_for_each_entry(pr
, &devices
, list
)
475 if (!strcasecmp(pr
->dev
, tmppath
))
479 return probe_path(path
);
482 static int _cache_load(const char *path
)
484 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
488 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
491 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
492 struct probe_info
*pr
= _probe_path(gl
.gl_pathv
[j
]);
494 list_add_tail(&pr
->list
, &devices
);
502 static void cache_load(int mtd
)
505 _cache_load("/dev/mtdblock*");
506 _cache_load("/dev/ubiblock*");
507 _cache_load("/dev/ubi[0-9]*");
509 _cache_load("/dev/loop*");
510 _cache_load("/dev/mmcblk*");
511 _cache_load("/dev/sd*");
512 _cache_load("/dev/hd*");
513 _cache_load("/dev/md*");
514 _cache_load("/dev/vd*");
515 _cache_load("/dev/mapper/*");
519 static int print_block_uci(struct probe_info
*pr
)
521 if (!strcmp(pr
->type
, "swap")) {
522 printf("config 'swap'\n");
524 printf("config 'mount'\n");
525 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
528 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
530 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
531 printf("\toption\tenabled\t'0'\n\n");
536 static struct probe_info
* find_block_info(char *uuid
, char *label
, char *path
)
538 struct probe_info
*pr
= NULL
;
541 list_for_each_entry(pr
, &devices
, list
)
542 if (pr
->uuid
&& !strcasecmp(pr
->uuid
, uuid
))
546 list_for_each_entry(pr
, &devices
, list
)
547 if (pr
->label
&& !strcmp(pr
->label
, label
))
551 list_for_each_entry(pr
, &devices
, list
)
552 if (pr
->dev
&& !strcmp(basename(pr
->dev
), basename(path
)))
558 static char* find_mount_point(char *block
)
560 FILE *fp
= fopen("/proc/self/mountinfo", "r");
561 static char line
[256];
562 int len
= strlen(block
);
563 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
;
566 unsigned int minor
, major
;
571 rstat
= stat(block
, &s
);
573 while (fgets(line
, sizeof(line
), fp
)) {
574 pos
= strchr(line
, ' ');
578 pos
= strchr(pos
+ 1, ' ');
583 pos
= strchr(pos
, ':');
590 pos
= strchr(pos
, ' ');
596 pos
= strchr(pos
+ 1, ' ');
601 pos
= strchr(pos
, ' ');
607 pos
= strchr(pos
+ 1, ' ');
611 pos
= strchr(pos
+ 1, ' ');
615 pos
= strchr(pos
+ 1, ' ');
620 pos
= strchr(pos
, ' ');
626 if (!strncmp(block
, devname
, len
)) {
627 point
= strdup(cpoint
);
634 if (!S_ISBLK(s
.st_mode
))
637 if (major
== major(s
.st_rdev
) &&
638 minor
== minor(s
.st_rdev
)) {
639 point
= strdup(cpoint
);
649 static int print_block_info(struct probe_info
*pr
)
653 mp
= find_mount_point(pr
->dev
);
654 printf("%s:", pr
->dev
);
656 printf(" UUID=\"%s\"", pr
->uuid
);
659 printf(" LABEL=\"%s\"", pr
->label
);
662 printf(" VERSION=\"%s\"", pr
->version
);
665 printf(" MOUNT=\"%s\"", mp
);
669 printf(" TYPE=\"%s\"\n", pr
->type
);
674 static void mkdir_p(char *dir
)
676 char *l
= strrchr(dir
, '/');
686 static void check_filesystem(struct probe_info
*pr
)
690 const char *e2fsck
= "/usr/sbin/e2fsck";
691 const char *dosfsck
= "/usr/sbin/dosfsck";
694 /* UBIFS does not need stuff like fsck */
695 if (!strncmp(pr
->type
, "ubifs", 5))
698 if (!strncmp(pr
->type
, "vfat", 4)) {
700 } else if (!strncmp(pr
->type
, "ext", 3)) {
703 ULOG_ERR("check_filesystem: %s is not supported\n", pr
->type
);
707 if (stat(ckfs
, &statbuf
) < 0) {
708 ULOG_ERR("check_filesystem: %s not found\n", ckfs
);
714 execl(ckfs
, ckfs
, "-p", pr
->dev
, NULL
);
716 } else if (pid
> 0) {
719 waitpid(pid
, &status
, 0);
720 if (WEXITSTATUS(status
))
721 ULOG_ERR("check_filesystem: %s returned %d\n", ckfs
, WEXITSTATUS(status
));
725 static void handle_swapfiles(bool on
)
729 struct probe_info
*pr
;
731 vlist_for_each_element(&mounts
, m
, node
)
733 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
736 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
739 pr
= _probe_path(m
->target
);
744 if (!strcmp(pr
->type
, "swap")) {
746 swapon(pr
->dev
, m
->prio
);
755 static int mount_device(struct probe_info
*pr
, int hotplug
)
764 device
= basename(pr
->dev
);
766 if (!strcmp(pr
->type
, "swap")) {
767 if (hotplug
&& !auto_swap
)
769 m
= find_swap(pr
->uuid
, pr
->label
, device
);
771 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
776 if (hotplug
&& !auto_mount
)
779 mp
= find_mount_point(pr
->dev
);
781 ULOG_ERR("%s is already mounted on %s\n", pr
->dev
, mp
);
786 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
791 char *target
= m
->target
;
796 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
802 check_filesystem(pr
);
804 err
= mount(pr
->dev
, target
, pr
->type
, m
->flags
,
805 (m
->options
) ? (m
->options
) : (""));
807 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
808 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
810 handle_swapfiles(true);
818 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
822 check_filesystem(pr
);
824 err
= mount(pr
->dev
, target
, pr
->type
, 0, "");
826 ULOG_ERR("mounting %s (%s) as %s failed (%d) - %s\n",
827 pr
->dev
, pr
->type
, target
, errno
, strerror(errno
));
829 handle_swapfiles(true);
836 static int umount_device(struct probe_info
*pr
)
839 char *device
= basename(pr
->dev
);
846 if (!strcmp(pr
->type
, "swap"))
849 mp
= find_mount_point(pr
->dev
);
853 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
857 err
= umount2(mp
, MNT_DETACH
);
859 ULOG_ERR("unmounting %s (%s) failed (%d) - %s\n",
860 pr
->dev
, mp
, errno
, strerror(errno
));
862 ULOG_INFO("unmounted %s (%s)\n",
869 static int main_hotplug(int argc
, char **argv
)
872 char *action
, *device
, *mount_point
;
874 action
= getenv("ACTION");
875 device
= getenv("DEVNAME");
877 if (!action
|| !device
)
879 snprintf(path
, sizeof(path
), "/dev/%s", device
);
881 if (!strcmp(action
, "remove")) {
883 mount_point
= find_mount_point(path
);
885 err
= umount2(mount_point
, MNT_DETACH
);
888 ULOG_ERR("umount of %s failed (%d) - %s\n",
889 mount_point
, errno
, strerror(errno
));
893 } else if (strcmp(action
, "add")) {
894 ULOG_ERR("Unkown action %s\n", action
);
899 if (config_load(NULL
))
903 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
906 static int find_block_mtd(char *name
, char *part
, int plen
)
908 FILE *fp
= fopen("/proc/mtd", "r");
909 static char line
[256];
915 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
916 if (strstr(line
, name
)) {
917 char *eol
= strstr(line
, ":");
932 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
938 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
942 while (ubi_dev_present(libubi
, dev
))
944 struct ubi_dev_info dev_info
;
945 struct ubi_vol_info vol_info
;
947 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
949 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
952 *dev_num
= dev_info
.dev_num
;
953 *vol_id
= vol_info
.vol_id
;
961 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
967 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
969 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
974 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
980 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
982 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
989 static int find_root_dev(char *buf
, int len
)
999 if (!(d
= opendir("/dev")))
1004 while ((e
= readdir(d
)) != NULL
) {
1005 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
1007 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
1020 static int test_fs_support(const char *name
)
1026 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
1027 while (fgets(line
, sizeof(line
), f
)) {
1028 p
= strtok(line
, "\t\n");
1030 if (p
&& !strcmp(p
, "nodev"))
1031 p
= strtok(NULL
, "\t\n");
1033 if (p
&& !strcmp(p
, name
)) {
1044 static int check_extroot(char *path
)
1046 struct probe_info
*pr
= NULL
;
1049 #ifdef UBIFS_EXTROOT
1050 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1054 libubi
= libubi_open();
1055 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1056 libubi_close(libubi
);
1061 if (find_block_mtd("\"rootfs\"", devpath
, sizeof(devpath
))) {
1062 if (find_root_dev(devpath
, sizeof(devpath
))) {
1063 ULOG_ERR("extroot: unable to determine root device\n");
1069 list_for_each_entry(pr
, &devices
, list
) {
1070 if (!strcmp(pr
->dev
, devpath
)) {
1074 char uuid
[64] = { 0 };
1076 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1080 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1081 if (stat(tag
, &s
)) {
1082 fp
= fopen(tag
, "w+");
1084 ULOG_ERR("extroot: failed to write UUID to %s: %d (%s)\n",
1085 tag
, errno
, strerror(errno
));
1086 /* return 0 to continue boot regardless of error */
1089 fputs(pr
->uuid
, fp
);
1094 fp
= fopen(tag
, "r");
1096 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1097 tag
, errno
, strerror(errno
));
1101 if (!fgets(uuid
, sizeof(uuid
), fp
))
1102 ULOG_ERR("extroot: failed to read UUID from %s: %d (%s)\n",
1103 tag
, errno
, strerror(errno
));
1106 if (*uuid
&& !strcasecmp(uuid
, pr
->uuid
))
1109 ULOG_ERR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1110 pr
->uuid
, basename(path
), uuid
);
1115 ULOG_ERR("extroot: unable to lookup root device %s\n", devpath
);
1120 * Read info about extroot from UCI (using prefix) and mount it.
1122 static int mount_extroot(char *cfg
)
1124 char overlay
[] = "/tmp/extroot/overlay";
1125 char mnt
[] = "/tmp/extroot/mnt";
1127 struct probe_info
*pr
;
1131 /* Load @cfg/etc/config/fstab */
1132 if (config_load(cfg
))
1135 /* See if there is extroot-specific mount config */
1136 m
= find_block(NULL
, NULL
, NULL
, "/");
1138 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1140 if (!m
|| !m
->extroot
)
1142 ULOG_INFO("extroot: not configured\n");
1146 /* Find block device pointed by the mount config */
1147 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1149 if (!pr
&& delay_root
){
1150 ULOG_INFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1154 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1157 if (strncmp(pr
->type
, "ext", 3) &&
1158 strncmp(pr
->type
, "ubifs", 5)) {
1159 ULOG_ERR("extroot: unsupported filesystem %s, try ext4\n", pr
->type
);
1163 if (test_fs_support(pr
->type
)) {
1164 ULOG_ERR("extroot: filesystem %s not supported by kernel\n", pr
->type
);
1173 check_filesystem(pr
);
1175 err
= mount(pr
->dev
, path
, pr
->type
, m
->flags
,
1176 (m
->options
) ? (m
->options
) : (""));
1179 ULOG_ERR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1180 pr
->dev
, pr
->type
, path
, errno
, strerror(errno
));
1181 } else if (m
->overlay
) {
1182 err
= check_extroot(path
);
1187 ULOG_ERR("extroot: cannot find device %s%s\n",
1188 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1189 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1195 static int main_extroot(int argc
, char **argv
)
1197 struct probe_info
*pr
;
1198 char blkdev_path
[32] = { 0 };
1200 #ifdef UBIFS_EXTROOT
1204 if (!getenv("PREINIT"))
1208 ULOG_ERR("Usage: block extroot\n");
1215 /* enable LOG_INFO messages */
1216 ulog_threshold(LOG_INFO
);
1219 * Look for "rootfs_data". We will want to mount it and check for
1220 * extroot configuration.
1223 /* Start with looking for MTD partition */
1224 find_block_mtd("\"rootfs_data\"", blkdev_path
, sizeof(blkdev_path
));
1225 if (blkdev_path
[0]) {
1226 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1227 if (pr
&& !strcmp(pr
->type
, "jffs2")) {
1228 char cfg
[] = "/tmp/jffs_cfg";
1231 * Mount MTD part and try extroot (using
1232 * /etc/config/fstab from that partition)
1235 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1236 err
= mount_extroot(cfg
);
1237 umount2(cfg
, MNT_DETACH
);
1240 rmdir("/tmp/overlay");
1246 #ifdef UBIFS_EXTROOT
1247 /* ... but it also could be an UBI volume */
1248 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1249 libubi
= libubi_open();
1250 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1251 libubi_close(libubi
);
1252 if (blkdev_path
[0]) {
1253 char cfg
[] = "/tmp/ubifs_cfg";
1255 /* Mount volume and try extroot (using fstab from that vol) */
1257 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1258 err
= mount_extroot(cfg
);
1259 umount2(cfg
, MNT_DETACH
);
1262 rmdir("/tmp/overlay");
1268 return mount_extroot(NULL
);
1271 static int main_mount(int argc
, char **argv
)
1273 struct probe_info
*pr
;
1275 if (config_load(NULL
))
1279 list_for_each_entry(pr
, &devices
, list
)
1280 mount_device(pr
, 0);
1282 handle_swapfiles(true);
1287 static int main_umount(int argc
, char **argv
)
1289 struct probe_info
*pr
;
1291 if (config_load(NULL
))
1294 handle_swapfiles(false);
1297 list_for_each_entry(pr
, &devices
, list
)
1303 static int main_detect(int argc
, char **argv
)
1305 struct probe_info
*pr
;
1308 printf("config 'global'\n");
1309 printf("\toption\tanon_swap\t'0'\n");
1310 printf("\toption\tanon_mount\t'0'\n");
1311 printf("\toption\tauto_swap\t'1'\n");
1312 printf("\toption\tauto_mount\t'1'\n");
1313 printf("\toption\tdelay_root\t'5'\n");
1314 printf("\toption\tcheck_fs\t'0'\n\n");
1315 list_for_each_entry(pr
, &devices
, list
)
1316 print_block_uci(pr
);
1321 static int main_info(int argc
, char **argv
)
1324 struct probe_info
*pr
;
1328 list_for_each_entry(pr
, &devices
, list
)
1329 print_block_info(pr
);
1334 for (i
= 2; i
< argc
; i
++) {
1337 if (stat(argv
[i
], &s
)) {
1338 ULOG_ERR("failed to stat %s\n", argv
[i
]);
1341 if (!S_ISBLK(s
.st_mode
) && !(S_ISCHR(s
.st_mode
) && major(s
.st_rdev
) == 250)) {
1342 ULOG_ERR("%s is not a block device\n", argv
[i
]);
1345 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1347 print_block_info(pr
);
1353 static int swapon_usage(void)
1355 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1356 "\tStart swapping on [DEVICE]\n"
1357 " -a\tStart swapping on all swap devices\n"
1358 " -p pri\tSet priority of swap device\n"
1359 " -s\tShow summary\n");
1363 static int main_swapon(int argc
, char **argv
)
1369 struct probe_info
*pr
;
1375 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1378 fp
= fopen("/proc/swaps", "r");
1382 ULOG_ERR("failed to open /proc/swaps\n");
1385 while (getline(&lineptr
, &s
, fp
) > 0)
1386 printf("%s", lineptr
);
1393 list_for_each_entry(pr
, &devices
, list
) {
1394 if (strcmp(pr
->type
, "swap"))
1396 if (swapon(pr
->dev
, 0))
1397 ULOG_ERR("failed to swapon %s\n", pr
->dev
);
1403 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1406 return swapon_usage();
1411 if (optind
!= (argc
- 1))
1412 return swapon_usage();
1414 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1415 ULOG_ERR("%s is not a block device or file\n", argv
[optind
]);
1418 err
= swapon(argv
[optind
], flags
);
1420 ULOG_ERR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1427 static int main_swapoff(int argc
, char **argv
)
1430 ULOG_ERR("Usage: swapoff [-a] [DEVICE]\n\n"
1431 "\tStop swapping on DEVICE\n"
1432 " -a\tStop swapping on all swap devices\n");
1436 if (!strcmp(argv
[1], "-a")) {
1437 FILE *fp
= fopen("/proc/swaps", "r");
1441 ULOG_ERR("failed to open /proc/swaps\n");
1444 if (fgets(line
, sizeof(line
), fp
))
1445 while (fgets(line
, sizeof(line
), fp
)) {
1446 char *end
= strchr(line
, ' ');
1452 err
= swapoff(line
);
1454 ULOG_ERR("failed to swapoff %s (%d)\n", line
, err
);
1461 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1462 ULOG_ERR("%s is not a block device or file\n", argv
[1]);
1465 err
= swapoff(argv
[1]);
1467 ULOG_ERR("failed to swapoff %s (%d)\n", argv
[1], err
);
1475 int main(int argc
, char **argv
)
1477 char *base
= basename(*argv
);
1481 ulog_open(-1, -1, "block");
1482 ulog_threshold(LOG_NOTICE
);
1484 if (!strcmp(base
, "swapon"))
1485 return main_swapon(argc
, argv
);
1487 if (!strcmp(base
, "swapoff"))
1488 return main_swapoff(argc
, argv
);
1490 if ((argc
> 1) && !strcmp(base
, "block")) {
1491 if (!strcmp(argv
[1], "info"))
1492 return main_info(argc
, argv
);
1494 if (!strcmp(argv
[1], "detect"))
1495 return main_detect(argc
, argv
);
1497 if (!strcmp(argv
[1], "hotplug"))
1498 return main_hotplug(argc
, argv
);
1500 if (!strcmp(argv
[1], "extroot"))
1501 return main_extroot(argc
, argv
);
1503 if (!strcmp(argv
[1], "mount"))
1504 return main_mount(argc
, argv
);
1506 if (!strcmp(argv
[1], "umount"))
1507 return main_umount(argc
, argv
);
1509 if (!strcmp(argv
[1], "remount")) {
1510 int ret
= main_umount(argc
, argv
);
1513 ret
= main_mount(argc
, argv
);
1518 ULOG_ERR("Usage: block <info|mount|umount|detect>\n");