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.
26 #include <sys/types.h>
28 #include <sys/mount.h>
34 #include <libubox/list.h>
35 #include <libubox/vlist.h>
36 #include <libubox/blobmsg_json.h>
37 #include <libubox/avl-cmp.h>
39 #include "libblkid-tiny/libblkid-tiny.h"
42 #include "libubi/libubi.h"
47 static void klog(int prio
, const char *fmt
, ...)
50 FILE *f
= fopen("/dev/kmsg", "w");
53 fprintf(f
, "<%d>", prio
);
63 #define KINFO(fmt, ...) do { \
64 if (kmsg) klog(LOG_INFO, "block: " fmt, ## __VA_ARGS__); \
67 #define ERROR(fmt, ...) do { \
69 klog(LOG_ERR, "block: " fmt, ## __VA_ARGS__); \
71 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
72 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
82 struct vlist_node node
;
98 static struct vlist_tree mounts
;
99 static struct blob_buf b
;
100 static LIST_HEAD(devices
);
101 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
102 static unsigned int delay_root
;
114 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
115 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
116 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
117 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
118 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
119 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
120 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
133 static const struct uci_blob_param_list config_attr_list
= {
134 .n_params
= __CFG_MAX
,
135 .params
= config_policy
,
138 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
139 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
140 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
141 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
142 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
143 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
144 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
147 static const struct uci_blob_param_list mount_attr_list
= {
148 .n_params
= __MOUNT_MAX
,
149 .params
= mount_policy
,
161 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
162 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
163 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
164 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
165 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
166 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
169 static const struct uci_blob_param_list swap_attr_list
= {
170 .n_params
= __SWAP_MAX
,
171 .params
= swap_policy
,
180 # define MS_DIRSYNC (1 << 7)
184 # define MS_RELATIME (1 << 21)
187 #ifndef MS_STRICTATIME
188 # define MS_STRICTATIME (1 << 24)
191 static const struct mount_flag mount_flags
[] = {
192 { "sync", MS_SYNCHRONOUS
},
193 { "async", ~MS_SYNCHRONOUS
},
194 { "dirsync", MS_DIRSYNC
},
195 { "mand", MS_MANDLOCK
},
196 { "nomand", ~MS_MANDLOCK
},
197 { "atime", ~MS_NOATIME
},
198 { "noatime", MS_NOATIME
},
199 { "dev", ~MS_NODEV
},
200 { "nodev", MS_NODEV
},
201 { "diratime", ~MS_NODIRATIME
},
202 { "nodiratime", MS_NODIRATIME
},
203 { "exec", ~MS_NOEXEC
},
204 { "noexec", MS_NOEXEC
},
205 { "suid", ~MS_NOSUID
},
206 { "nosuid", MS_NOSUID
},
207 { "rw", ~MS_RDONLY
},
209 { "relatime", MS_RELATIME
},
210 { "norelatime", ~MS_RELATIME
},
211 { "strictatime", MS_STRICTATIME
},
214 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
219 return strdup(blobmsg_get_string(attr
));
222 static char *blobmsg_get_basename(struct blob_attr
*attr
)
227 return strdup(basename(blobmsg_get_string(attr
)));
230 static void parse_mount_options(struct mount
*m
, char *optstr
)
234 char *p
, *opts
, *last
;
239 if (!optstr
|| !*optstr
)
242 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
255 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
256 if (!strcmp(last
, mount_flags
[i
].name
)) {
257 if (mount_flags
[i
].flag
< 0)
258 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
260 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
267 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
276 static int mount_add(struct uci_section
*s
)
278 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
281 blob_buf_init(&b
, 0);
282 uci_to_blob(&b
, s
, &mount_attr_list
);
283 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
285 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
288 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
291 m
= malloc(sizeof(struct mount
));
292 m
->type
= TYPE_MOUNT
;
293 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
294 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
295 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
296 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
298 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
300 m
->overlay
= m
->extroot
= 0;
301 if (m
->target
&& !strcmp(m
->target
, "/"))
303 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
304 m
->extroot
= m
->overlay
= 1;
307 vlist_add(&mounts
, &m
->node
, m
->uuid
);
309 vlist_add(&mounts
, &m
->node
, m
->label
);
311 vlist_add(&mounts
, &m
->node
, m
->device
);
316 static int swap_add(struct uci_section
*s
)
318 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
321 blob_buf_init(&b
, 0);
322 uci_to_blob(&b
, s
, &swap_attr_list
);
323 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
325 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
328 m
= malloc(sizeof(struct mount
));
329 memset(m
, 0, sizeof(struct mount
));
331 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
332 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
333 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
335 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
337 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
339 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
340 /* store complete swap path */
342 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
345 vlist_add(&mounts
, &m
->node
, m
->uuid
);
347 vlist_add(&mounts
, &m
->node
, m
->label
);
349 vlist_add(&mounts
, &m
->node
, m
->device
);
355 static int global_add(struct uci_section
*s
)
357 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
359 blob_buf_init(&b
, 0);
360 uci_to_blob(&b
, s
, &config_attr_list
);
361 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
363 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
365 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
368 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
370 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
373 if (tb
[CFG_DELAY_ROOT
])
374 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
376 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
382 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
386 vlist_for_each_element(&mounts
, m
, node
) {
387 if (m
->type
!= TYPE_SWAP
)
389 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
391 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
393 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
400 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
405 vlist_for_each_element(&mounts
, m
, node
) {
406 if (m
->type
!= TYPE_MOUNT
)
408 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
410 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
412 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
414 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
421 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
422 struct vlist_node
*node_old
)
426 static struct uci_package
* config_try_load(struct uci_context
*ctx
, char *path
)
428 char *file
= basename(path
);
429 char *dir
= dirname(path
);
431 struct uci_package
*pkg
;
433 uci_set_confdir(ctx
, dir
);
434 KINFO("attempting to load %s/%s\n", dir
, file
);
436 if (uci_load(ctx
, file
, &pkg
)) {
437 uci_get_errorstr(ctx
, &err
, file
);
438 ERROR("unable to load configuration (%s)\n", err
);
447 static int config_load(char *cfg
)
449 struct uci_context
*ctx
= uci_alloc_context();
450 struct uci_package
*pkg
= NULL
;
451 struct uci_element
*e
;
454 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
457 snprintf(path
, sizeof(path
), "%s/upper/etc/config/fstab", cfg
);
458 pkg
= config_try_load(ctx
, path
);
461 snprintf(path
, sizeof(path
), "%s/etc/config/fstab", cfg
);
462 pkg
= config_try_load(ctx
, path
);
467 snprintf(path
, sizeof(path
), "/etc/config/fstab");
468 pkg
= config_try_load(ctx
, path
);
472 ERROR("extroot: no usable configuration\n");
476 vlist_update(&mounts
);
477 uci_foreach_element(&pkg
->sections
, e
) {
478 struct uci_section
*s
= uci_to_section(e
);
480 if (!strcmp(s
->type
, "mount"))
482 if (!strcmp(s
->type
, "swap"))
484 if (!strcmp(s
->type
, "global"))
487 vlist_flush(&mounts
);
492 static struct blkid_struct_probe
* _probe_path(char *path
)
494 struct blkid_struct_probe
*pr
;
496 pr
= malloc(sizeof(*pr
));
501 memset(pr
, 0, sizeof(*pr
));
502 probe_block(path
, pr
);
504 if (pr
->err
|| !pr
->id
) {
512 static int _cache_load(const char *path
)
514 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
518 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
521 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
522 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
524 list_add_tail(&pr
->list
, &devices
);
532 static void cache_load(int mtd
)
535 _cache_load("/dev/mtdblock*");
536 _cache_load("/dev/ubiblock*");
537 _cache_load("/dev/ubi?*_?*");
539 _cache_load("/dev/mmcblk*");
540 _cache_load("/dev/sd*");
541 _cache_load("/dev/hd*");
542 _cache_load("/dev/md*");
543 _cache_load("/dev/vd*");
544 _cache_load("/dev/mapper/*");
547 static int print_block_info(struct blkid_struct_probe
*pr
)
549 printf("%s:", pr
->dev
);
551 printf(" UUID=\"%s\"", pr
->uuid
);
554 printf(" LABEL=\"%s\"", pr
->label
);
557 printf(" NAME=\"%s\"", pr
->name
);
560 printf(" VERSION=\"%s\"", pr
->version
);
562 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
567 static int print_block_uci(struct blkid_struct_probe
*pr
)
569 if (!strcmp(pr
->id
->name
, "swap")) {
570 printf("config 'swap'\n");
572 printf("config 'mount'\n");
573 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
576 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
578 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
579 printf("\toption\tenabled\t'0'\n\n");
584 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
586 struct blkid_struct_probe
*pr
= NULL
;
589 list_for_each_entry(pr
, &devices
, list
)
590 if (!strcasecmp(pr
->uuid
, uuid
))
594 list_for_each_entry(pr
, &devices
, list
)
595 if (!strcmp(pr
->label
, label
))
599 list_for_each_entry(pr
, &devices
, list
)
600 if (!strcmp(basename(pr
->dev
), basename(path
)))
606 static char* find_mount_point(char *block
)
608 FILE *fp
= fopen("/proc/mounts", "r");
609 static char line
[256];
610 int len
= strlen(block
);
616 while (fgets(line
, sizeof(line
), fp
)) {
617 if (!strncmp(line
, block
, len
)) {
618 char *p
= &line
[len
+ 1];
619 char *t
= strstr(p
, " ");
636 static void mkdir_p(char *dir
)
638 char *l
= strrchr(dir
, '/');
648 static void check_filesystem(struct blkid_struct_probe
*pr
)
652 char *e2fsck
= "/usr/sbin/e2fsck";
654 /* UBIFS does not need stuff like fsck */
655 if (!strncmp(pr
->id
->name
, "ubifs", 5))
658 if (strncmp(pr
->id
->name
, "ext", 3)) {
659 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
663 if (stat(e2fsck
, &statbuf
) < 0) {
664 ERROR("check_filesystem: %s not found\n", e2fsck
);
670 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
672 } else if (pid
> 0) {
675 waitpid(pid
, &status
, 0);
676 if (WEXITSTATUS(status
))
677 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
681 static void handle_swapfiles(bool on
)
685 struct blkid_struct_probe
*pr
;
687 vlist_for_each_element(&mounts
, m
, node
)
689 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
692 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
695 pr
= _probe_path(m
->target
);
700 if (!strcmp(pr
->id
->name
, "swap")) {
702 swapon(pr
->dev
, m
->prio
);
711 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
719 device
= basename(pr
->dev
);
721 if (!strcmp(pr
->id
->name
, "swap")) {
722 if (hotplug
&& !auto_swap
)
724 m
= find_swap(pr
->uuid
, pr
->label
, device
);
726 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
731 if (hotplug
&& !auto_mount
)
734 if (find_mount_point(pr
->dev
)) {
735 ERROR("%s is already mounted\n", pr
->dev
);
739 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
744 char *target
= m
->target
;
749 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
755 check_filesystem(pr
);
757 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
758 (m
->options
) ? (m
->options
) : (""));
760 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
761 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
763 handle_swapfiles(true);
768 char target
[] = "/mnt/mmcblk123";
771 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
775 check_filesystem(pr
);
777 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
779 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
780 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
782 handle_swapfiles(true);
789 static int umount_device(struct blkid_struct_probe
*pr
)
792 char *device
= basename(pr
->dev
);
799 if (!strcmp(pr
->id
->name
, "swap"))
802 mp
= find_mount_point(pr
->dev
);
806 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
810 err
= umount2(mp
, MNT_DETACH
);
812 ERROR("unmounting %s (%s) failed (%d) - %s\n",
813 pr
->dev
, mp
, err
, strerror(err
));
815 ERROR("unmounted %s (%s)\n",
821 static int main_hotplug(int argc
, char **argv
)
824 char *action
, *device
, *mount_point
;
826 action
= getenv("ACTION");
827 device
= getenv("DEVNAME");
829 if (!action
|| !device
)
831 snprintf(path
, sizeof(path
), "/dev/%s", device
);
833 if (!strcmp(action
, "remove")) {
835 mount_point
= find_mount_point(path
);
837 err
= umount2(mount_point
, MNT_DETACH
);
840 ERROR("umount of %s failed (%d) - %s\n",
841 mount_point
, err
, strerror(err
));
844 } else if (strcmp(action
, "add")) {
845 ERROR("Unkown action %s\n", action
);
850 if (config_load(NULL
))
854 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
857 static int find_block_mtd(char *name
, char *part
, int plen
)
859 FILE *fp
= fopen("/proc/mtd", "r");
860 static char line
[256];
866 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
867 if (strstr(line
, name
)) {
868 char *eol
= strstr(line
, ":");
883 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
889 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
893 while (ubi_dev_present(libubi
, dev
))
895 struct ubi_dev_info dev_info
;
896 struct ubi_vol_info vol_info
;
898 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
900 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
903 *dev_num
= dev_info
.dev_num
;
904 *vol_id
= vol_info
.vol_id
;
912 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
918 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
920 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
925 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
931 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
933 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
939 static int find_root_dev(char *buf
, int len
)
949 if (!(d
= opendir("/dev")))
954 while ((e
= readdir(d
)) != NULL
) {
955 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
957 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
968 static int check_extroot(char *path
)
970 struct blkid_struct_probe
*pr
= NULL
;
974 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
978 libubi
= libubi_open();
979 err
= find_block_ubi_RO(libubi
, "rootfs", fs
, sizeof(fs
));
980 libubi_close(libubi
);
985 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
986 if (find_root_dev(fs
, sizeof(fs
)))
991 list_for_each_entry(pr
, &devices
, list
) {
992 if (!strcmp(pr
->dev
, fs
)) {
996 char uuid
[64] = { 0 };
998 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
1002 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
1003 if (stat(tag
, &s
)) {
1004 fp
= fopen(tag
, "w+");
1006 ERROR("extroot: failed to write uuid tag file\n");
1007 /* return 0 to continue boot regardless of error */
1010 fputs(pr
->uuid
, fp
);
1015 fp
= fopen(tag
, "r");
1017 ERROR("extroot: failed to open uuid tag file\n");
1021 fgets(uuid
, sizeof(uuid
), fp
);
1023 if (!strcasecmp(uuid
, pr
->uuid
))
1025 ERROR("extroot: uuid tag does not match rom uuid\n");
1032 * Read info about extroot from UCI (using prefix) and mount it.
1034 static int mount_extroot(char *cfg
)
1036 char overlay
[] = "/tmp/extroot/overlay";
1037 char mnt
[] = "/tmp/extroot/mnt";
1039 struct blkid_struct_probe
*pr
;
1043 /* Load @cfg/etc/config/fstab */
1044 if (config_load(cfg
))
1047 /* See if there is extroot-specific mount config */
1048 m
= find_block(NULL
, NULL
, NULL
, "/");
1050 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1052 if (!m
|| !m
->extroot
)
1054 ERROR("extroot: no root or overlay mount defined\n");
1058 /* Find block device pointed by the mount config */
1059 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1061 if (!pr
&& delay_root
){
1062 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
1066 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1069 if (strncmp(pr
->id
->name
, "ext", 3) &&
1070 strncmp(pr
->id
->name
, "ubifs", 5)) {
1071 ERROR("extroot: %s is not supported, try ext4\n", pr
->id
->name
);
1079 check_filesystem(pr
);
1081 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
1084 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
1085 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
1086 } else if (m
->overlay
) {
1087 err
= check_extroot(path
);
1092 ERROR("extroot: cannot find block device\n");
1098 static int main_extroot(int argc
, char **argv
)
1100 struct blkid_struct_probe
*pr
;
1101 char blkdev_path
[32] = { 0 };
1103 #ifdef UBIFS_EXTROOT
1107 if (!getenv("PREINIT"))
1111 fprintf(stderr
, "Usage: block extroot mountpoint\n");
1121 * Look for "rootfs_data". We will want to mount it and check for
1122 * extroot configuration.
1125 /* Start with looking for MTD partition */
1126 find_block_mtd("rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1127 if (blkdev_path
[0]) {
1128 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1129 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
1130 char cfg
[] = "/tmp/jffs_cfg";
1133 * Mount MTD part and try extroot (using
1134 * /etc/config/fstab from that partition)
1137 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1138 err
= mount_extroot(cfg
);
1139 umount2(cfg
, MNT_DETACH
);
1142 rmdir("/tmp/overlay");
1148 #ifdef UBIFS_EXTROOT
1149 /* ... but it also could be an UBI volume */
1150 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1151 libubi
= libubi_open();
1152 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1153 libubi_close(libubi
);
1154 if (blkdev_path
[0]) {
1155 char cfg
[] = "/tmp/ubifs_cfg";
1157 /* Mount volume and try extroot (using fstab from that vol) */
1159 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1160 err
= mount_extroot(cfg
);
1161 umount2(cfg
, MNT_DETACH
);
1164 rmdir("/tmp/overlay");
1170 return mount_extroot(NULL
);
1173 static int main_mount(int argc
, char **argv
)
1175 struct blkid_struct_probe
*pr
;
1177 if (config_load(NULL
))
1181 list_for_each_entry(pr
, &devices
, list
)
1182 mount_device(pr
, 0);
1184 handle_swapfiles(true);
1189 static int main_umount(int argc
, char **argv
)
1191 struct blkid_struct_probe
*pr
;
1193 if (config_load(NULL
))
1196 handle_swapfiles(false);
1199 list_for_each_entry(pr
, &devices
, list
)
1205 static int main_detect(int argc
, char **argv
)
1207 struct blkid_struct_probe
*pr
;
1210 printf("config 'global'\n");
1211 printf("\toption\tanon_swap\t'0'\n");
1212 printf("\toption\tanon_mount\t'0'\n");
1213 printf("\toption\tauto_swap\t'1'\n");
1214 printf("\toption\tauto_mount\t'1'\n");
1215 printf("\toption\tdelay_root\t'5'\n");
1216 printf("\toption\tcheck_fs\t'0'\n\n");
1217 list_for_each_entry(pr
, &devices
, list
)
1218 print_block_uci(pr
);
1223 static int main_info(int argc
, char **argv
)
1226 struct blkid_struct_probe
*pr
;
1230 list_for_each_entry(pr
, &devices
, list
)
1231 print_block_info(pr
);
1236 for (i
= 2; i
< argc
; i
++) {
1239 if (stat(argv
[i
], &s
)) {
1240 ERROR("failed to stat %s\n", argv
[i
]);
1243 if (!S_ISBLK(s
.st_mode
)) {
1244 ERROR("%s is not a block device\n", argv
[i
]);
1247 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1249 print_block_info(pr
);
1255 static int swapon_usage(void)
1257 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1258 "\tStart swapping on [DEVICE]\n"
1259 " -a\tStart swapping on all swap devices\n"
1260 " -p pri\tSet priority of swap device\n"
1261 " -s\tShow summary\n");
1265 static int main_swapon(int argc
, char **argv
)
1271 struct blkid_struct_probe
*pr
;
1277 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1280 fp
= fopen("/proc/swaps", "r");
1284 ERROR("failed to open /proc/swaps\n");
1287 while (getline(&lineptr
, &s
, fp
) > 0)
1295 list_for_each_entry(pr
, &devices
, list
) {
1296 if (strcmp(pr
->id
->name
, "swap"))
1298 if (swapon(pr
->dev
, 0))
1299 ERROR("failed to swapon %s\n", pr
->dev
);
1305 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1308 return swapon_usage();
1313 if (optind
!= (argc
- 1))
1314 return swapon_usage();
1316 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1317 ERROR("%s is not a block device or file\n", argv
[optind
]);
1320 err
= swapon(argv
[optind
], flags
);
1322 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1329 static int main_swapoff(int argc
, char **argv
)
1332 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1333 "\tStop swapping on DEVICE\n"
1334 " -a\tStop swapping on all swap devices\n");
1338 if (!strcmp(argv
[1], "-a")) {
1339 FILE *fp
= fopen("/proc/swaps", "r");
1343 ERROR("failed to open /proc/swaps\n");
1346 fgets(line
, sizeof(line
), fp
);
1347 while (fgets(line
, sizeof(line
), fp
)) {
1348 char *end
= strchr(line
, ' ');
1354 err
= swapoff(line
);
1356 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1363 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1364 ERROR("%s is not a block device or file\n", argv
[1]);
1367 err
= swapoff(argv
[1]);
1369 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1377 int main(int argc
, char **argv
)
1379 char *base
= basename(*argv
);
1383 if (!strcmp(base
, "swapon"))
1384 return main_swapon(argc
, argv
);
1386 if (!strcmp(base
, "swapoff"))
1387 return main_swapoff(argc
, argv
);
1389 if ((argc
> 1) && !strcmp(base
, "block")) {
1390 if (!strcmp(argv
[1], "info"))
1391 return main_info(argc
, argv
);
1393 if (!strcmp(argv
[1], "detect"))
1394 return main_detect(argc
, argv
);
1396 if (!strcmp(argv
[1], "hotplug"))
1397 return main_hotplug(argc
, argv
);
1399 if (!strcmp(argv
[1], "extroot"))
1400 return main_extroot(argc
, argv
);
1402 if (!strcmp(argv
[1], "mount"))
1403 return main_mount(argc
, argv
);
1405 if (!strcmp(argv
[1], "umount"))
1406 return main_umount(argc
, argv
);
1409 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");