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.
25 #include <sys/types.h>
27 #include <sys/mount.h>
33 #include <libubox/list.h>
34 #include <libubox/vlist.h>
35 #include <libubox/blobmsg_json.h>
36 #include <libubox/avl-cmp.h>
38 #include "libblkid-tiny/libblkid-tiny.h"
41 #include "libubi/libubi.h"
44 #define ERROR(fmt, ...) do { \
45 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
46 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
55 struct vlist_node node
;
71 static struct vlist_tree mounts
;
72 static struct blob_buf b
;
73 static LIST_HEAD(devices
);
74 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
75 static unsigned int delay_root
;
87 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
88 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
89 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
90 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
91 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
92 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
93 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
106 static const struct uci_blob_param_list config_attr_list
= {
107 .n_params
= __CFG_MAX
,
108 .params
= config_policy
,
111 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
112 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
113 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
114 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
115 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
116 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
117 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
120 static const struct uci_blob_param_list mount_attr_list
= {
121 .n_params
= __MOUNT_MAX
,
122 .params
= mount_policy
,
134 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
135 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
136 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
137 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
138 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
139 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
142 static const struct uci_blob_param_list swap_attr_list
= {
143 .n_params
= __SWAP_MAX
,
144 .params
= swap_policy
,
153 # define MS_DIRSYNC (1 << 7)
157 # define MS_RELATIME (1 << 21)
160 #ifndef MS_STRICTATIME
161 # define MS_STRICTATIME (1 << 24)
164 static const struct mount_flag mount_flags
[] = {
165 { "sync", MS_SYNCHRONOUS
},
166 { "async", ~MS_SYNCHRONOUS
},
167 { "dirsync", MS_DIRSYNC
},
168 { "mand", MS_MANDLOCK
},
169 { "nomand", ~MS_MANDLOCK
},
170 { "atime", ~MS_NOATIME
},
171 { "noatime", MS_NOATIME
},
172 { "dev", ~MS_NODEV
},
173 { "nodev", MS_NODEV
},
174 { "diratime", ~MS_NODIRATIME
},
175 { "nodiratime", MS_NODIRATIME
},
176 { "exec", ~MS_NOEXEC
},
177 { "noexec", MS_NOEXEC
},
178 { "suid", ~MS_NOSUID
},
179 { "nosuid", MS_NOSUID
},
180 { "rw", ~MS_RDONLY
},
182 { "relatime", MS_RELATIME
},
183 { "norelatime", ~MS_RELATIME
},
184 { "strictatime", MS_STRICTATIME
},
187 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
192 return strdup(blobmsg_get_string(attr
));
195 static char *blobmsg_get_basename(struct blob_attr
*attr
)
200 return strdup(basename(blobmsg_get_string(attr
)));
203 static void parse_mount_options(struct mount
*m
, char *optstr
)
207 char *p
, *opts
, *last
;
212 if (!optstr
|| !*optstr
)
215 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
228 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
229 if (!strcmp(last
, mount_flags
[i
].name
)) {
230 if (mount_flags
[i
].flag
< 0)
231 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
233 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
240 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
249 static int mount_add(struct uci_section
*s
)
251 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
254 blob_buf_init(&b
, 0);
255 uci_to_blob(&b
, s
, &mount_attr_list
);
256 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
258 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
261 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
264 m
= malloc(sizeof(struct mount
));
265 m
->type
= TYPE_MOUNT
;
266 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
267 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
268 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
269 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
271 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
273 m
->overlay
= m
->extroot
= 0;
274 if (m
->target
&& !strcmp(m
->target
, "/"))
276 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
277 m
->extroot
= m
->overlay
= 1;
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 int config_load(char *cfg
)
401 struct uci_context
*ctx
;
402 struct uci_package
*pkg
;
403 struct uci_element
*e
;
405 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
407 ctx
= uci_alloc_context();
410 snprintf(path
, 32, "%s/etc/config", cfg
);
411 uci_set_confdir(ctx
, path
);
414 if (uci_load(ctx
, "fstab", &pkg
)) {
415 uci_set_confdir(ctx
, "/etc/config");
416 if (uci_load(ctx
, "fstab", &pkg
)) {
418 uci_get_errorstr(ctx
, &err
, "fstab");
419 ERROR("extroot: failed to load %s/etc/config/%s\n",
420 cfg
? cfg
: "", err
);
426 vlist_update(&mounts
);
427 uci_foreach_element(&pkg
->sections
, e
) {
428 struct uci_section
*s
= uci_to_section(e
);
430 if (!strcmp(s
->type
, "mount"))
432 if (!strcmp(s
->type
, "swap"))
434 if (!strcmp(s
->type
, "global"))
437 vlist_flush(&mounts
);
442 static struct blkid_struct_probe
* _probe_path(char *path
)
444 struct blkid_struct_probe
*pr
;
446 pr
= malloc(sizeof(*pr
));
451 memset(pr
, 0, sizeof(*pr
));
452 probe_block(path
, pr
);
454 if (pr
->err
|| !pr
->id
) {
462 static int _cache_load(const char *path
)
464 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
468 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
471 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
472 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
474 list_add_tail(&pr
->list
, &devices
);
482 static void cache_load(int mtd
)
485 _cache_load("/dev/mtdblock*");
486 _cache_load("/dev/ubiblock*");
487 _cache_load("/dev/ubi?*_?*");
489 _cache_load("/dev/mmcblk*");
490 _cache_load("/dev/sd*");
491 _cache_load("/dev/hd*");
492 _cache_load("/dev/md*");
493 _cache_load("/dev/vd*");
494 _cache_load("/dev/mapper/*");
497 static int print_block_info(struct blkid_struct_probe
*pr
)
499 printf("%s:", pr
->dev
);
501 printf(" UUID=\"%s\"", pr
->uuid
);
504 printf(" LABEL=\"%s\"", pr
->label
);
507 printf(" NAME=\"%s\"", pr
->name
);
510 printf(" VERSION=\"%s\"", pr
->version
);
512 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
517 static int print_block_uci(struct blkid_struct_probe
*pr
)
519 if (!strcmp(pr
->id
->name
, "swap")) {
520 printf("config 'swap'\n");
522 printf("config 'mount'\n");
523 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
526 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
528 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
529 printf("\toption\tenabled\t'0'\n\n");
534 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
536 struct blkid_struct_probe
*pr
= NULL
;
539 list_for_each_entry(pr
, &devices
, list
)
540 if (!strcasecmp(pr
->uuid
, uuid
))
544 list_for_each_entry(pr
, &devices
, list
)
545 if (!strcmp(pr
->label
, label
))
549 list_for_each_entry(pr
, &devices
, list
)
550 if (!strcmp(basename(pr
->dev
), basename(path
)))
556 static char* find_mount_point(char *block
)
558 FILE *fp
= fopen("/proc/mounts", "r");
559 static char line
[256];
560 int len
= strlen(block
);
566 while (fgets(line
, sizeof(line
), fp
)) {
567 if (!strncmp(line
, block
, len
)) {
568 char *p
= &line
[len
+ 1];
569 char *t
= strstr(p
, " ");
586 static void mkdir_p(char *dir
)
588 char *l
= strrchr(dir
, '/');
598 static void check_filesystem(struct blkid_struct_probe
*pr
)
602 char *e2fsck
= "/usr/sbin/e2fsck";
604 /* UBIFS does not need stuff like fsck */
605 if (!strncmp(pr
->id
->name
, "ubifs", 5))
608 if (strncmp(pr
->id
->name
, "ext", 3)) {
609 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
613 if (stat(e2fsck
, &statbuf
) < 0) {
614 ERROR("check_filesystem: %s not found\n", e2fsck
);
620 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
622 } else if (pid
> 0) {
625 waitpid(pid
, &status
, 0);
626 if (WEXITSTATUS(status
))
627 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
631 static void handle_swapfiles(bool on
)
635 struct blkid_struct_probe
*pr
;
637 vlist_for_each_element(&mounts
, m
, node
)
639 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
642 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
645 pr
= _probe_path(m
->target
);
650 if (!strcmp(pr
->id
->name
, "swap")) {
652 swapon(pr
->dev
, m
->prio
);
661 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
669 device
= basename(pr
->dev
);
671 if (!strcmp(pr
->id
->name
, "swap")) {
672 if (hotplug
&& !auto_swap
)
674 m
= find_swap(pr
->uuid
, pr
->label
, device
);
676 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
681 if (hotplug
&& !auto_mount
)
684 if (find_mount_point(pr
->dev
)) {
685 ERROR("%s is already mounted\n", pr
->dev
);
689 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
694 char *target
= m
->target
;
699 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
705 check_filesystem(pr
);
707 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
708 (m
->options
) ? (m
->options
) : (""));
710 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
711 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
713 handle_swapfiles(true);
718 char target
[] = "/mnt/mmcblk123";
721 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
725 check_filesystem(pr
);
727 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
729 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
730 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
732 handle_swapfiles(true);
739 static int umount_device(struct blkid_struct_probe
*pr
)
742 char *device
= basename(pr
->dev
);
749 if (!strcmp(pr
->id
->name
, "swap"))
752 mp
= find_mount_point(pr
->dev
);
756 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
760 err
= umount2(mp
, MNT_DETACH
);
762 ERROR("unmounting %s (%s) failed (%d) - %s\n",
763 pr
->dev
, mp
, err
, strerror(err
));
765 ERROR("unmounted %s (%s)\n",
771 static int main_hotplug(int argc
, char **argv
)
774 char *action
, *device
, *mount_point
;
776 action
= getenv("ACTION");
777 device
= getenv("DEVNAME");
779 if (!action
|| !device
)
781 snprintf(path
, sizeof(path
), "/dev/%s", device
);
783 if (!strcmp(action
, "remove")) {
785 mount_point
= find_mount_point(path
);
787 err
= umount2(mount_point
, MNT_DETACH
);
790 ERROR("umount of %s failed (%d) - %s\n",
791 mount_point
, err
, strerror(err
));
794 } else if (strcmp(action
, "add")) {
795 ERROR("Unkown action %s\n", action
);
800 if (config_load(NULL
))
804 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
807 static int find_block_mtd(char *name
, char *part
, int plen
)
809 FILE *fp
= fopen("/proc/mtd", "r");
810 static char line
[256];
816 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
817 if (strstr(line
, name
)) {
818 char *eol
= strstr(line
, ":");
833 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
839 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
843 while (ubi_dev_present(libubi
, dev
))
845 struct ubi_dev_info dev_info
;
846 struct ubi_vol_info vol_info
;
848 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
850 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
853 *dev_num
= dev_info
.dev_num
;
854 *vol_id
= vol_info
.vol_id
;
862 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
868 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
870 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
875 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
881 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
883 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
889 static int find_root_dev(char *buf
, int len
)
899 if (!(d
= opendir("/dev")))
904 while ((e
= readdir(d
)) != NULL
) {
905 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
907 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
918 static int check_extroot(char *path
)
920 struct blkid_struct_probe
*pr
= NULL
;
924 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
928 libubi
= libubi_open();
929 err
= find_block_ubi_RO(libubi
, "rootfs", fs
, sizeof(fs
));
930 libubi_close(libubi
);
935 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
936 if (find_root_dev(fs
, sizeof(fs
)))
941 list_for_each_entry(pr
, &devices
, list
) {
942 if (!strcmp(pr
->dev
, fs
)) {
946 char uuid
[64] = { 0 };
948 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
952 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
954 fp
= fopen(tag
, "w+");
956 ERROR("extroot: failed to write uuid tag file\n");
957 /* return 0 to continue boot regardless of error */
965 fp
= fopen(tag
, "r");
967 ERROR("extroot: failed to open uuid tag file\n");
971 fgets(uuid
, sizeof(uuid
), fp
);
973 if (!strcasecmp(uuid
, pr
->uuid
))
975 ERROR("extroot: uuid tag does not match rom uuid\n");
982 * Read info about extroot from UCI (using prefix) and mount it.
984 static int mount_extroot(char *cfg
)
986 char overlay
[] = "/tmp/extroot/overlay";
987 char mnt
[] = "/tmp/extroot/mnt";
989 struct blkid_struct_probe
*pr
;
993 /* Load @cfg/etc/config/fstab */
994 if (config_load(cfg
))
997 /* See if there is extroot-specific mount config */
998 m
= find_block(NULL
, NULL
, NULL
, "/");
1000 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1002 if (!m
|| !m
->extroot
)
1004 ERROR("extroot: no root or overlay mount defined\n");
1008 /* Find block device pointed by the mount config */
1009 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1011 if (!pr
&& delay_root
){
1012 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
1016 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1019 if (strncmp(pr
->id
->name
, "ext", 3) &&
1020 strncmp(pr
->id
->name
, "ubifs", 5)) {
1021 ERROR("extroot: %s is not supported, try ext4\n", pr
->id
->name
);
1029 check_filesystem(pr
);
1031 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
1034 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
1035 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
1036 } else if (m
->overlay
) {
1037 err
= check_extroot(path
);
1042 ERROR("extroot: cannot find block device\n");
1048 static int main_extroot(int argc
, char **argv
)
1050 struct blkid_struct_probe
*pr
;
1051 char blkdev_path
[32] = { 0 };
1053 #ifdef UBIFS_EXTROOT
1057 if (!getenv("PREINIT"))
1061 fprintf(stderr
, "Usage: block extroot mountpoint\n");
1069 * Look for "rootfs_data". We will want to mount it and check for
1070 * extroot configuration.
1073 /* Start with looking for MTD partition */
1074 find_block_mtd("rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1075 if (blkdev_path
[0]) {
1076 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1077 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
1078 char cfg
[] = "/tmp/jffs_cfg";
1081 * Mount MTD part and try extroot (using
1082 * /etc/config/fstab from that partition)
1085 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1086 err
= mount_extroot(cfg
);
1087 umount2(cfg
, MNT_DETACH
);
1090 rmdir("/tmp/overlay");
1096 #ifdef UBIFS_EXTROOT
1097 /* ... but it also could be an UBI volume */
1098 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1099 libubi
= libubi_open();
1100 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1101 libubi_close(libubi
);
1102 if (blkdev_path
[0]) {
1103 char cfg
[] = "/tmp/ubifs_cfg";
1105 /* Mount volume and try extroot (using fstab from that vol) */
1107 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1108 err
= mount_extroot(cfg
);
1109 umount2(cfg
, MNT_DETACH
);
1112 rmdir("/tmp/overlay");
1118 return mount_extroot(NULL
);
1121 static int main_mount(int argc
, char **argv
)
1123 struct blkid_struct_probe
*pr
;
1125 if (config_load(NULL
))
1129 list_for_each_entry(pr
, &devices
, list
)
1130 mount_device(pr
, 0);
1132 handle_swapfiles(true);
1137 static int main_umount(int argc
, char **argv
)
1139 struct blkid_struct_probe
*pr
;
1141 if (config_load(NULL
))
1144 handle_swapfiles(false);
1147 list_for_each_entry(pr
, &devices
, list
)
1153 static int main_detect(int argc
, char **argv
)
1155 struct blkid_struct_probe
*pr
;
1158 printf("config 'global'\n");
1159 printf("\toption\tanon_swap\t'0'\n");
1160 printf("\toption\tanon_mount\t'0'\n");
1161 printf("\toption\tauto_swap\t'1'\n");
1162 printf("\toption\tauto_mount\t'1'\n");
1163 printf("\toption\tdelay_root\t'5'\n");
1164 printf("\toption\tcheck_fs\t'0'\n\n");
1165 list_for_each_entry(pr
, &devices
, list
)
1166 print_block_uci(pr
);
1171 static int main_info(int argc
, char **argv
)
1174 struct blkid_struct_probe
*pr
;
1178 list_for_each_entry(pr
, &devices
, list
)
1179 print_block_info(pr
);
1184 for (i
= 2; i
< argc
; i
++) {
1187 if (stat(argv
[i
], &s
)) {
1188 ERROR("failed to stat %s\n", argv
[i
]);
1191 if (!S_ISBLK(s
.st_mode
)) {
1192 ERROR("%s is not a block device\n", argv
[i
]);
1195 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1197 print_block_info(pr
);
1203 static int swapon_usage(void)
1205 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1206 "\tStart swapping on [DEVICE]\n"
1207 " -a\tStart swapping on all swap devices\n"
1208 " -p pri\tSet priority of swap device\n"
1209 " -s\tShow summary\n");
1213 static int main_swapon(int argc
, char **argv
)
1219 struct blkid_struct_probe
*pr
;
1225 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1228 fp
= fopen("/proc/swaps", "r");
1232 ERROR("failed to open /proc/swaps\n");
1235 while (getline(&lineptr
, &s
, fp
) > 0)
1243 list_for_each_entry(pr
, &devices
, list
) {
1244 if (strcmp(pr
->id
->name
, "swap"))
1246 if (swapon(pr
->dev
, 0))
1247 ERROR("failed to swapon %s\n", pr
->dev
);
1253 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1256 return swapon_usage();
1261 if (optind
!= (argc
- 1))
1262 return swapon_usage();
1264 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1265 ERROR("%s is not a block device or file\n", argv
[optind
]);
1268 err
= swapon(argv
[optind
], flags
);
1270 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1277 static int main_swapoff(int argc
, char **argv
)
1280 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1281 "\tStop swapping on DEVICE\n"
1282 " -a\tStop swapping on all swap devices\n");
1286 if (!strcmp(argv
[1], "-a")) {
1287 FILE *fp
= fopen("/proc/swaps", "r");
1291 ERROR("failed to open /proc/swaps\n");
1294 fgets(line
, sizeof(line
), fp
);
1295 while (fgets(line
, sizeof(line
), fp
)) {
1296 char *end
= strchr(line
, ' ');
1302 err
= swapoff(line
);
1304 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1311 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1312 ERROR("%s is not a block device or file\n", argv
[1]);
1315 err
= swapoff(argv
[1]);
1317 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1325 int main(int argc
, char **argv
)
1327 char *base
= basename(*argv
);
1331 if (!strcmp(base
, "swapon"))
1332 return main_swapon(argc
, argv
);
1334 if (!strcmp(base
, "swapoff"))
1335 return main_swapoff(argc
, argv
);
1337 if ((argc
> 1) && !strcmp(base
, "block")) {
1338 if (!strcmp(argv
[1], "info"))
1339 return main_info(argc
, argv
);
1341 if (!strcmp(argv
[1], "detect"))
1342 return main_detect(argc
, argv
);
1344 if (!strcmp(argv
[1], "hotplug"))
1345 return main_hotplug(argc
, argv
);
1347 if (!strcmp(argv
[1], "extroot"))
1348 return main_extroot(argc
, argv
);
1350 if (!strcmp(argv
[1], "mount"))
1351 return main_mount(argc
, argv
);
1353 if (!strcmp(argv
[1], "umount"))
1354 return main_umount(argc
, argv
);
1357 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");