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.
27 #include <sys/types.h>
29 #include <sys/mount.h>
35 #include <libubox/list.h>
36 #include <libubox/vlist.h>
37 #include <libubox/blobmsg_json.h>
38 #include <libubox/avl-cmp.h>
40 #include "libblkid-tiny/libblkid-tiny.h"
43 #include "libubi/libubi.h"
48 static void klog(int prio
, const char *fmt
, ...)
51 FILE *f
= fopen("/dev/kmsg", "w");
54 fprintf(f
, "<%d>", prio
);
64 #define KINFO(fmt, ...) do { \
65 if (kmsg) klog(LOG_INFO, "block: " fmt, ## __VA_ARGS__); \
68 #define ERROR(fmt, ...) do { \
70 klog(LOG_ERR, "block: " fmt, ## __VA_ARGS__); \
72 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
73 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
83 struct vlist_node node
;
99 static struct vlist_tree mounts
;
100 static struct blob_buf b
;
101 static LIST_HEAD(devices
);
102 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
103 static unsigned int delay_root
;
115 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
116 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
117 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
118 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
119 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
120 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
121 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
134 static const struct uci_blob_param_list config_attr_list
= {
135 .n_params
= __CFG_MAX
,
136 .params
= config_policy
,
139 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
140 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
141 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
142 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
143 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
144 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
145 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
148 static const struct uci_blob_param_list mount_attr_list
= {
149 .n_params
= __MOUNT_MAX
,
150 .params
= mount_policy
,
162 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
163 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
164 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
165 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
166 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
167 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
170 static const struct uci_blob_param_list swap_attr_list
= {
171 .n_params
= __SWAP_MAX
,
172 .params
= swap_policy
,
181 # define MS_DIRSYNC (1 << 7)
185 # define MS_RELATIME (1 << 21)
188 #ifndef MS_STRICTATIME
189 # define MS_STRICTATIME (1 << 24)
192 static const struct mount_flag mount_flags
[] = {
193 { "sync", MS_SYNCHRONOUS
},
194 { "async", ~MS_SYNCHRONOUS
},
195 { "dirsync", MS_DIRSYNC
},
196 { "mand", MS_MANDLOCK
},
197 { "nomand", ~MS_MANDLOCK
},
198 { "atime", ~MS_NOATIME
},
199 { "noatime", MS_NOATIME
},
200 { "dev", ~MS_NODEV
},
201 { "nodev", MS_NODEV
},
202 { "diratime", ~MS_NODIRATIME
},
203 { "nodiratime", MS_NODIRATIME
},
204 { "exec", ~MS_NOEXEC
},
205 { "noexec", MS_NOEXEC
},
206 { "suid", ~MS_NOSUID
},
207 { "nosuid", MS_NOSUID
},
208 { "rw", ~MS_RDONLY
},
210 { "relatime", MS_RELATIME
},
211 { "norelatime", ~MS_RELATIME
},
212 { "strictatime", MS_STRICTATIME
},
215 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
220 return strdup(blobmsg_get_string(attr
));
223 static char *blobmsg_get_basename(struct blob_attr
*attr
)
228 return strdup(basename(blobmsg_get_string(attr
)));
231 static void parse_mount_options(struct mount
*m
, char *optstr
)
235 char *p
, *opts
, *last
;
240 if (!optstr
|| !*optstr
)
243 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
256 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
257 if (!strcmp(last
, mount_flags
[i
].name
)) {
258 if (mount_flags
[i
].flag
< 0)
259 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
261 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
268 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
277 static int mount_add(struct uci_section
*s
)
279 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
282 blob_buf_init(&b
, 0);
283 uci_to_blob(&b
, s
, &mount_attr_list
);
284 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
286 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
289 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
292 m
= malloc(sizeof(struct mount
));
293 m
->type
= TYPE_MOUNT
;
294 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
295 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
296 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
297 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
299 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
301 m
->overlay
= m
->extroot
= 0;
302 if (m
->target
&& !strcmp(m
->target
, "/"))
304 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
305 m
->extroot
= m
->overlay
= 1;
308 vlist_add(&mounts
, &m
->node
, m
->uuid
);
310 vlist_add(&mounts
, &m
->node
, m
->label
);
312 vlist_add(&mounts
, &m
->node
, m
->device
);
317 static int swap_add(struct uci_section
*s
)
319 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
322 blob_buf_init(&b
, 0);
323 uci_to_blob(&b
, s
, &swap_attr_list
);
324 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
326 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
329 m
= malloc(sizeof(struct mount
));
330 memset(m
, 0, sizeof(struct mount
));
332 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
333 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
334 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
336 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
338 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
340 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
341 /* store complete swap path */
343 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
346 vlist_add(&mounts
, &m
->node
, m
->uuid
);
348 vlist_add(&mounts
, &m
->node
, m
->label
);
350 vlist_add(&mounts
, &m
->node
, m
->device
);
356 static int global_add(struct uci_section
*s
)
358 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
360 blob_buf_init(&b
, 0);
361 uci_to_blob(&b
, s
, &config_attr_list
);
362 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
364 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
366 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
369 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
371 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
374 if (tb
[CFG_DELAY_ROOT
])
375 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
377 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
383 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
387 vlist_for_each_element(&mounts
, m
, node
) {
388 if (m
->type
!= TYPE_SWAP
)
390 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
392 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
394 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
401 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
406 vlist_for_each_element(&mounts
, m
, node
) {
407 if (m
->type
!= TYPE_MOUNT
)
409 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
411 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
413 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
415 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
422 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
423 struct vlist_node
*node_old
)
427 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
429 char *file
= basename(path
);
430 char *dir
= dirname(path
);
432 struct uci_package
*pkg
;
434 uci_set_confdir(ctx
, dir
);
435 KINFO("attempting to load %s/%s\n", dir
, file
);
437 if (uci_load(ctx
, file
, &pkg
)) {
438 uci_get_errorstr(ctx
, &err
, file
);
439 ERROR("unable to load configuration (%s)\n", err
);
448 static int config_load(char *cfg
)
450 struct uci_context
*ctx
= uci_alloc_context();
451 struct uci_package
*pkg
= NULL
;
452 struct uci_element
*e
;
455 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
458 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
459 pkg
= config_try_load(ctx
, path
);
462 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
463 pkg
= config_try_load(ctx
, path
);
468 snprintf(path
, sizeof(path
), "/etc/config/fstab");
469 pkg
= config_try_load(ctx
, path
);
473 ERROR("no usable configuration\n");
477 vlist_update(&mounts
);
478 uci_foreach_element(&pkg
->sections
, e
) {
479 struct uci_section
*s
= uci_to_section(e
);
481 if (!strcmp(s
->type
, "mount"))
483 if (!strcmp(s
->type
, "swap"))
485 if (!strcmp(s
->type
, "global"))
488 vlist_flush(&mounts
);
493 static struct blkid_struct_probe
* _probe_path(char *path
)
495 struct blkid_struct_probe
*pr
;
497 pr
= malloc(sizeof(*pr
));
502 memset(pr
, 0, sizeof(*pr
));
503 probe_block(path
, pr
);
505 if (pr
->err
|| !pr
->id
) {
513 static int _cache_load(const char *path
)
515 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
519 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
522 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
523 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
525 list_add_tail(&pr
->list
, &devices
);
533 static void cache_load(int mtd
)
536 _cache_load("/dev/mtdblock*");
537 _cache_load("/dev/ubiblock*");
538 _cache_load("/dev/ubi?*_?*");
540 _cache_load("/dev/mmcblk*");
541 _cache_load("/dev/sd*");
542 _cache_load("/dev/hd*");
543 _cache_load("/dev/md*");
544 _cache_load("/dev/vd*");
545 _cache_load("/dev/mapper/*");
548 static int print_block_info(struct blkid_struct_probe
*pr
)
550 printf("%s:", pr
->dev
);
552 printf(" UUID=\"%s\"", pr
->uuid
);
555 printf(" LABEL=\"%s\"", pr
->label
);
558 printf(" NAME=\"%s\"", pr
->name
);
561 printf(" VERSION=\"%s\"", pr
->version
);
563 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
568 static int print_block_uci(struct blkid_struct_probe
*pr
)
570 if (!strcmp(pr
->id
->name
, "swap")) {
571 printf("config 'swap'\n");
573 printf("config 'mount'\n");
574 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
577 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
579 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
580 printf("\toption\tenabled\t'0'\n\n");
585 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
587 struct blkid_struct_probe
*pr
= NULL
;
590 list_for_each_entry(pr
, &devices
, list
)
591 if (!strcasecmp(pr
->uuid
, uuid
))
595 list_for_each_entry(pr
, &devices
, list
)
596 if (!strcmp(pr
->label
, label
))
600 list_for_each_entry(pr
, &devices
, list
)
601 if (!strcmp(basename(pr
->dev
), basename(path
)))
607 static char* find_mount_point(char *block
)
609 FILE *fp
= fopen("/proc/mounts", "r");
610 static char line
[256];
611 int len
= strlen(block
);
617 while (fgets(line
, sizeof(line
), fp
)) {
618 if (!strncmp(line
, block
, len
)) {
619 char *p
= &line
[len
+ 1];
620 char *t
= strstr(p
, " ");
637 static void mkdir_p(char *dir
)
639 char *l
= strrchr(dir
, '/');
649 static void check_filesystem(struct blkid_struct_probe
*pr
)
653 char *e2fsck
= "/usr/sbin/e2fsck";
655 /* UBIFS does not need stuff like fsck */
656 if (!strncmp(pr
->id
->name
, "ubifs", 5))
659 if (strncmp(pr
->id
->name
, "ext", 3)) {
660 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
664 if (stat(e2fsck
, &statbuf
) < 0) {
665 ERROR("check_filesystem: %s not found\n", e2fsck
);
671 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
673 } else if (pid
> 0) {
676 waitpid(pid
, &status
, 0);
677 if (WEXITSTATUS(status
))
678 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
682 static void handle_swapfiles(bool on
)
686 struct blkid_struct_probe
*pr
;
688 vlist_for_each_element(&mounts
, m
, node
)
690 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
693 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
696 pr
= _probe_path(m
->target
);
701 if (!strcmp(pr
->id
->name
, "swap")) {
703 swapon(pr
->dev
, m
->prio
);
712 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
720 device
= basename(pr
->dev
);
722 if (!strcmp(pr
->id
->name
, "swap")) {
723 if (hotplug
&& !auto_swap
)
725 m
= find_swap(pr
->uuid
, pr
->label
, device
);
727 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
732 if (hotplug
&& !auto_mount
)
735 if (find_mount_point(pr
->dev
)) {
736 ERROR("%s is already mounted\n", pr
->dev
);
740 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
745 char *target
= m
->target
;
750 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
756 check_filesystem(pr
);
758 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
759 (m
->options
) ? (m
->options
) : (""));
761 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
762 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
764 handle_swapfiles(true);
769 char target
[] = "/mnt/mmcblk123";
772 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
776 check_filesystem(pr
);
778 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
780 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
781 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
783 handle_swapfiles(true);
790 static int umount_device(struct blkid_struct_probe
*pr
)
793 char *device
= basename(pr
->dev
);
800 if (!strcmp(pr
->id
->name
, "swap"))
803 mp
= find_mount_point(pr
->dev
);
807 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
811 err
= umount2(mp
, MNT_DETACH
);
813 ERROR("unmounting %s (%s) failed (%d) - %s\n",
814 pr
->dev
, mp
, err
, strerror(err
));
816 ERROR("unmounted %s (%s)\n",
822 static int main_hotplug(int argc
, char **argv
)
825 char *action
, *device
, *mount_point
;
827 action
= getenv("ACTION");
828 device
= getenv("DEVNAME");
830 if (!action
|| !device
)
832 snprintf(path
, sizeof(path
), "/dev/%s", device
);
834 if (!strcmp(action
, "remove")) {
836 mount_point
= find_mount_point(path
);
838 err
= umount2(mount_point
, MNT_DETACH
);
841 ERROR("umount of %s failed (%d) - %s\n",
842 mount_point
, err
, strerror(err
));
845 } else if (strcmp(action
, "add")) {
846 ERROR("Unkown action %s\n", action
);
851 if (config_load(NULL
))
855 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
858 static int find_block_mtd(char *name
, char *part
, int plen
)
860 FILE *fp
= fopen("/proc/mtd", "r");
861 static char line
[256];
867 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
868 if (strstr(line
, name
)) {
869 char *eol
= strstr(line
, ":");
884 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
890 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
894 while (ubi_dev_present(libubi
, dev
))
896 struct ubi_dev_info dev_info
;
897 struct ubi_vol_info vol_info
;
899 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
901 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
904 *dev_num
= dev_info
.dev_num
;
905 *vol_id
= vol_info
.vol_id
;
913 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
919 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
921 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
926 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
932 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
934 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
940 static int find_root_dev(char *buf
, int len
)
950 if (!(d
= opendir("/dev")))
955 while ((e
= readdir(d
)) != NULL
) {
956 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
958 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
969 static int test_fs_support(const char *name
)
975 if ((f
= fopen("/proc/filesystems", "r")) != NULL
) {
976 while (fgets(line
, sizeof(line
), f
)) {
977 p
= strtok(line
, "\t\n");
979 if (p
&& !strcmp(p
, "nodev"))
980 p
= strtok(NULL
, "\t\n");
982 if (p
&& !strcmp(p
, name
)) {
993 static int check_extroot(char *path
)
995 struct blkid_struct_probe
*pr
= NULL
;
999 if (find_block_mtd("rootfs", devpath
, sizeof(devpath
))) {
1003 libubi
= libubi_open();
1004 err
= find_block_ubi_RO(libubi
, "rootfs", devpath
, sizeof(devpath
));
1005 libubi_close(libubi
);
1010 if (find_block_mtd("rootfs", devpath
, sizeof(devpath
))) {
1011 if (find_root_dev(devpath
, sizeof(devpath
))) {
1012 ERROR("extroot: unable to determine root device\n");
1018 list_for_each_entry(pr
, &devices
, list
) {
1019 if (!strcmp(pr
->dev
, devpath
)) {
1023 char uuid
[64] = { 0 };
1025 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1029 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1030 if (stat(tag
, &s
)) {
1031 fp
= fopen(tag
, "w+");
1033 ERROR("extroot: failed to write UUID to %s: %d (%s)\n",
1034 tag
, errno
, strerror(errno
));
1035 /* return 0 to continue boot regardless of error */
1038 fputs(pr
->uuid
, fp
);
1043 fp
= fopen(tag
, "r");
1045 ERROR("extroot: failed to read UUID from %s: %d (%s)\n",
1046 tag
, errno
, strerror(errno
));
1050 fgets(uuid
, sizeof(uuid
), fp
);
1053 if (!strcasecmp(uuid
, pr
->uuid
))
1056 ERROR("extroot: UUID mismatch (root: %s, %s: %s)\n",
1057 pr
->uuid
, basename(path
), uuid
);
1062 ERROR("extroot: unable to lookup root device %s\n", devpath
);
1067 * Read info about extroot from UCI (using prefix) and mount it.
1069 static int mount_extroot(char *cfg
)
1071 char overlay
[] = "/tmp/extroot/overlay";
1072 char mnt
[] = "/tmp/extroot/mnt";
1074 struct blkid_struct_probe
*pr
;
1078 /* Load @cfg/etc/config/fstab */
1079 if (config_load(cfg
))
1082 /* See if there is extroot-specific mount config */
1083 m
= find_block(NULL
, NULL
, NULL
, "/");
1085 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1087 if (!m
|| !m
->extroot
)
1089 KINFO("extroot: not configured\n");
1093 /* Find block device pointed by the mount config */
1094 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1096 if (!pr
&& delay_root
){
1097 KINFO("extroot: device not present, retrying in %u seconds\n", delay_root
);
1101 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1104 if (strncmp(pr
->id
->name
, "ext", 3) &&
1105 strncmp(pr
->id
->name
, "ubifs", 5)) {
1106 ERROR("extroot: unsupported filesystem %s, try ext4\n", pr
->id
->name
);
1110 if (test_fs_support(pr
->id
->name
)) {
1111 ERROR("extroot: filesystem %s not supported by kernel\n", pr
->id
->name
);
1120 check_filesystem(pr
);
1122 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
1125 ERROR("extroot: mounting %s (%s) on %s failed: %d (%s)\n",
1126 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
1127 } else if (m
->overlay
) {
1128 err
= check_extroot(path
);
1133 ERROR("extroot: cannot find device %s%s\n",
1134 (m
->uuid
? "with UUID " : (m
->label
? "with label " : "")),
1135 (m
->uuid
? m
->uuid
: (m
->label
? m
->label
: m
->device
)));
1141 static int main_extroot(int argc
, char **argv
)
1143 struct blkid_struct_probe
*pr
;
1144 char blkdev_path
[32] = { 0 };
1146 #ifdef UBIFS_EXTROOT
1150 if (!getenv("PREINIT"))
1154 fprintf(stderr
, "Usage: block extroot\n");
1164 * Look for "rootfs_data". We will want to mount it and check for
1165 * extroot configuration.
1168 /* Start with looking for MTD partition */
1169 find_block_mtd("rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1170 if (blkdev_path
[0]) {
1171 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1172 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
1173 char cfg
[] = "/tmp/jffs_cfg";
1176 * Mount MTD part and try extroot (using
1177 * /etc/config/fstab from that partition)
1180 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1181 err
= mount_extroot(cfg
);
1182 umount2(cfg
, MNT_DETACH
);
1185 rmdir("/tmp/overlay");
1191 #ifdef UBIFS_EXTROOT
1192 /* ... but it also could be an UBI volume */
1193 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1194 libubi
= libubi_open();
1195 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1196 libubi_close(libubi
);
1197 if (blkdev_path
[0]) {
1198 char cfg
[] = "/tmp/ubifs_cfg";
1200 /* Mount volume and try extroot (using fstab from that vol) */
1202 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1203 err
= mount_extroot(cfg
);
1204 umount2(cfg
, MNT_DETACH
);
1207 rmdir("/tmp/overlay");
1213 return mount_extroot(NULL
);
1216 static int main_mount(int argc
, char **argv
)
1218 struct blkid_struct_probe
*pr
;
1220 if (config_load(NULL
))
1224 list_for_each_entry(pr
, &devices
, list
)
1225 mount_device(pr
, 0);
1227 handle_swapfiles(true);
1232 static int main_umount(int argc
, char **argv
)
1234 struct blkid_struct_probe
*pr
;
1236 if (config_load(NULL
))
1239 handle_swapfiles(false);
1242 list_for_each_entry(pr
, &devices
, list
)
1248 static int main_detect(int argc
, char **argv
)
1250 struct blkid_struct_probe
*pr
;
1253 printf("config 'global'\n");
1254 printf("\toption\tanon_swap\t'0'\n");
1255 printf("\toption\tanon_mount\t'0'\n");
1256 printf("\toption\tauto_swap\t'1'\n");
1257 printf("\toption\tauto_mount\t'1'\n");
1258 printf("\toption\tdelay_root\t'5'\n");
1259 printf("\toption\tcheck_fs\t'0'\n\n");
1260 list_for_each_entry(pr
, &devices
, list
)
1261 print_block_uci(pr
);
1266 static int main_info(int argc
, char **argv
)
1269 struct blkid_struct_probe
*pr
;
1273 list_for_each_entry(pr
, &devices
, list
)
1274 print_block_info(pr
);
1279 for (i
= 2; i
< argc
; i
++) {
1282 if (stat(argv
[i
], &s
)) {
1283 ERROR("failed to stat %s\n", argv
[i
]);
1286 if (!S_ISBLK(s
.st_mode
)) {
1287 ERROR("%s is not a block device\n", argv
[i
]);
1290 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1292 print_block_info(pr
);
1298 static int swapon_usage(void)
1300 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1301 "\tStart swapping on [DEVICE]\n"
1302 " -a\tStart swapping on all swap devices\n"
1303 " -p pri\tSet priority of swap device\n"
1304 " -s\tShow summary\n");
1308 static int main_swapon(int argc
, char **argv
)
1314 struct blkid_struct_probe
*pr
;
1320 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1323 fp
= fopen("/proc/swaps", "r");
1327 ERROR("failed to open /proc/swaps\n");
1330 while (getline(&lineptr
, &s
, fp
) > 0)
1338 list_for_each_entry(pr
, &devices
, list
) {
1339 if (strcmp(pr
->id
->name
, "swap"))
1341 if (swapon(pr
->dev
, 0))
1342 ERROR("failed to swapon %s\n", pr
->dev
);
1348 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1351 return swapon_usage();
1356 if (optind
!= (argc
- 1))
1357 return swapon_usage();
1359 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1360 ERROR("%s is not a block device or file\n", argv
[optind
]);
1363 err
= swapon(argv
[optind
], flags
);
1365 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1372 static int main_swapoff(int argc
, char **argv
)
1375 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1376 "\tStop swapping on DEVICE\n"
1377 " -a\tStop swapping on all swap devices\n");
1381 if (!strcmp(argv
[1], "-a")) {
1382 FILE *fp
= fopen("/proc/swaps", "r");
1386 ERROR("failed to open /proc/swaps\n");
1389 fgets(line
, sizeof(line
), fp
);
1390 while (fgets(line
, sizeof(line
), fp
)) {
1391 char *end
= strchr(line
, ' ');
1397 err
= swapoff(line
);
1399 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1406 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1407 ERROR("%s is not a block device or file\n", argv
[1]);
1410 err
= swapoff(argv
[1]);
1412 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1420 int main(int argc
, char **argv
)
1422 char *base
= basename(*argv
);
1426 if (!strcmp(base
, "swapon"))
1427 return main_swapon(argc
, argv
);
1429 if (!strcmp(base
, "swapoff"))
1430 return main_swapoff(argc
, argv
);
1432 if ((argc
> 1) && !strcmp(base
, "block")) {
1433 if (!strcmp(argv
[1], "info"))
1434 return main_info(argc
, argv
);
1436 if (!strcmp(argv
[1], "detect"))
1437 return main_detect(argc
, argv
);
1439 if (!strcmp(argv
[1], "hotplug"))
1440 return main_hotplug(argc
, argv
);
1442 if (!strcmp(argv
[1], "extroot"))
1443 return main_extroot(argc
, argv
);
1445 if (!strcmp(argv
[1], "mount"))
1446 return main_mount(argc
, argv
);
1448 if (!strcmp(argv
[1], "umount"))
1449 return main_umount(argc
, argv
);
1452 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");