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 int config_load(char *cfg
)
428 struct uci_context
*ctx
;
429 struct uci_package
*pkg
;
430 struct uci_element
*e
;
432 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
434 ctx
= uci_alloc_context();
437 snprintf(path
, 32, "%s/etc/config", cfg
);
438 uci_set_confdir(ctx
, path
);
441 if (uci_load(ctx
, "fstab", &pkg
)) {
442 uci_set_confdir(ctx
, "/etc/config");
443 if (uci_load(ctx
, "fstab", &pkg
)) {
445 uci_get_errorstr(ctx
, &err
, "fstab");
446 ERROR("extroot: failed to load %s/etc/config/%s\n",
447 cfg
? cfg
: "", err
);
453 vlist_update(&mounts
);
454 uci_foreach_element(&pkg
->sections
, e
) {
455 struct uci_section
*s
= uci_to_section(e
);
457 if (!strcmp(s
->type
, "mount"))
459 if (!strcmp(s
->type
, "swap"))
461 if (!strcmp(s
->type
, "global"))
464 vlist_flush(&mounts
);
469 static struct blkid_struct_probe
* _probe_path(char *path
)
471 struct blkid_struct_probe
*pr
;
473 pr
= malloc(sizeof(*pr
));
478 memset(pr
, 0, sizeof(*pr
));
479 probe_block(path
, pr
);
481 if (pr
->err
|| !pr
->id
) {
489 static int _cache_load(const char *path
)
491 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
495 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
498 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
499 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
501 list_add_tail(&pr
->list
, &devices
);
509 static void cache_load(int mtd
)
512 _cache_load("/dev/mtdblock*");
513 _cache_load("/dev/ubiblock*");
514 _cache_load("/dev/ubi?*_?*");
516 _cache_load("/dev/mmcblk*");
517 _cache_load("/dev/sd*");
518 _cache_load("/dev/hd*");
519 _cache_load("/dev/md*");
520 _cache_load("/dev/vd*");
521 _cache_load("/dev/mapper/*");
524 static int print_block_info(struct blkid_struct_probe
*pr
)
526 printf("%s:", pr
->dev
);
528 printf(" UUID=\"%s\"", pr
->uuid
);
531 printf(" LABEL=\"%s\"", pr
->label
);
534 printf(" NAME=\"%s\"", pr
->name
);
537 printf(" VERSION=\"%s\"", pr
->version
);
539 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
544 static int print_block_uci(struct blkid_struct_probe
*pr
)
546 if (!strcmp(pr
->id
->name
, "swap")) {
547 printf("config 'swap'\n");
549 printf("config 'mount'\n");
550 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
553 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
555 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
556 printf("\toption\tenabled\t'0'\n\n");
561 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
563 struct blkid_struct_probe
*pr
= NULL
;
566 list_for_each_entry(pr
, &devices
, list
)
567 if (!strcasecmp(pr
->uuid
, uuid
))
571 list_for_each_entry(pr
, &devices
, list
)
572 if (!strcmp(pr
->label
, label
))
576 list_for_each_entry(pr
, &devices
, list
)
577 if (!strcmp(basename(pr
->dev
), basename(path
)))
583 static char* find_mount_point(char *block
)
585 FILE *fp
= fopen("/proc/mounts", "r");
586 static char line
[256];
587 int len
= strlen(block
);
593 while (fgets(line
, sizeof(line
), fp
)) {
594 if (!strncmp(line
, block
, len
)) {
595 char *p
= &line
[len
+ 1];
596 char *t
= strstr(p
, " ");
613 static void mkdir_p(char *dir
)
615 char *l
= strrchr(dir
, '/');
625 static void check_filesystem(struct blkid_struct_probe
*pr
)
629 char *e2fsck
= "/usr/sbin/e2fsck";
631 /* UBIFS does not need stuff like fsck */
632 if (!strncmp(pr
->id
->name
, "ubifs", 5))
635 if (strncmp(pr
->id
->name
, "ext", 3)) {
636 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
640 if (stat(e2fsck
, &statbuf
) < 0) {
641 ERROR("check_filesystem: %s not found\n", e2fsck
);
647 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
649 } else if (pid
> 0) {
652 waitpid(pid
, &status
, 0);
653 if (WEXITSTATUS(status
))
654 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
658 static void handle_swapfiles(bool on
)
662 struct blkid_struct_probe
*pr
;
664 vlist_for_each_element(&mounts
, m
, node
)
666 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
669 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
672 pr
= _probe_path(m
->target
);
677 if (!strcmp(pr
->id
->name
, "swap")) {
679 swapon(pr
->dev
, m
->prio
);
688 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
696 device
= basename(pr
->dev
);
698 if (!strcmp(pr
->id
->name
, "swap")) {
699 if (hotplug
&& !auto_swap
)
701 m
= find_swap(pr
->uuid
, pr
->label
, device
);
703 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
708 if (hotplug
&& !auto_mount
)
711 if (find_mount_point(pr
->dev
)) {
712 ERROR("%s is already mounted\n", pr
->dev
);
716 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
721 char *target
= m
->target
;
726 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
732 check_filesystem(pr
);
734 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
735 (m
->options
) ? (m
->options
) : (""));
737 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
738 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
740 handle_swapfiles(true);
745 char target
[] = "/mnt/mmcblk123";
748 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
752 check_filesystem(pr
);
754 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
756 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
757 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
759 handle_swapfiles(true);
766 static int umount_device(struct blkid_struct_probe
*pr
)
769 char *device
= basename(pr
->dev
);
776 if (!strcmp(pr
->id
->name
, "swap"))
779 mp
= find_mount_point(pr
->dev
);
783 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
787 err
= umount2(mp
, MNT_DETACH
);
789 ERROR("unmounting %s (%s) failed (%d) - %s\n",
790 pr
->dev
, mp
, err
, strerror(err
));
792 ERROR("unmounted %s (%s)\n",
798 static int main_hotplug(int argc
, char **argv
)
801 char *action
, *device
, *mount_point
;
803 action
= getenv("ACTION");
804 device
= getenv("DEVNAME");
806 if (!action
|| !device
)
808 snprintf(path
, sizeof(path
), "/dev/%s", device
);
810 if (!strcmp(action
, "remove")) {
812 mount_point
= find_mount_point(path
);
814 err
= umount2(mount_point
, MNT_DETACH
);
817 ERROR("umount of %s failed (%d) - %s\n",
818 mount_point
, err
, strerror(err
));
821 } else if (strcmp(action
, "add")) {
822 ERROR("Unkown action %s\n", action
);
827 if (config_load(NULL
))
831 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
834 static int find_block_mtd(char *name
, char *part
, int plen
)
836 FILE *fp
= fopen("/proc/mtd", "r");
837 static char line
[256];
843 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
844 if (strstr(line
, name
)) {
845 char *eol
= strstr(line
, ":");
860 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
866 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
870 while (ubi_dev_present(libubi
, dev
))
872 struct ubi_dev_info dev_info
;
873 struct ubi_vol_info vol_info
;
875 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
877 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
880 *dev_num
= dev_info
.dev_num
;
881 *vol_id
= vol_info
.vol_id
;
889 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
895 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
897 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
902 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
908 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
910 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
916 static int find_root_dev(char *buf
, int len
)
926 if (!(d
= opendir("/dev")))
931 while ((e
= readdir(d
)) != NULL
) {
932 snprintf(buf
, len
, "/dev/%s", e
->d_name
);
934 if (stat(buf
, &s
) || s
.st_rdev
!= root
)
945 static int check_extroot(char *path
)
947 struct blkid_struct_probe
*pr
= NULL
;
951 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
955 libubi
= libubi_open();
956 err
= find_block_ubi_RO(libubi
, "rootfs", fs
, sizeof(fs
));
957 libubi_close(libubi
);
962 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
963 if (find_root_dev(fs
, sizeof(fs
)))
968 list_for_each_entry(pr
, &devices
, list
) {
969 if (!strcmp(pr
->dev
, fs
)) {
973 char uuid
[64] = { 0 };
975 snprintf(tag
, sizeof(tag
), "%s/etc", path
);
979 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
981 fp
= fopen(tag
, "w+");
983 ERROR("extroot: failed to write uuid tag file\n");
984 /* return 0 to continue boot regardless of error */
992 fp
= fopen(tag
, "r");
994 ERROR("extroot: failed to open uuid tag file\n");
998 fgets(uuid
, sizeof(uuid
), fp
);
1000 if (!strcasecmp(uuid
, pr
->uuid
))
1002 ERROR("extroot: uuid tag does not match rom uuid\n");
1009 * Read info about extroot from UCI (using prefix) and mount it.
1011 static int mount_extroot(char *cfg
)
1013 char overlay
[] = "/tmp/extroot/overlay";
1014 char mnt
[] = "/tmp/extroot/mnt";
1016 struct blkid_struct_probe
*pr
;
1020 /* Load @cfg/etc/config/fstab */
1021 if (config_load(cfg
))
1024 /* See if there is extroot-specific mount config */
1025 m
= find_block(NULL
, NULL
, NULL
, "/");
1027 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
1029 if (!m
|| !m
->extroot
)
1031 ERROR("extroot: no root or overlay mount defined\n");
1035 /* Find block device pointed by the mount config */
1036 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1038 if (!pr
&& delay_root
){
1039 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
1043 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
1046 if (strncmp(pr
->id
->name
, "ext", 3) &&
1047 strncmp(pr
->id
->name
, "ubifs", 5)) {
1048 ERROR("extroot: %s is not supported, try ext4\n", pr
->id
->name
);
1056 check_filesystem(pr
);
1058 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
1061 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
1062 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
1063 } else if (m
->overlay
) {
1064 err
= check_extroot(path
);
1069 ERROR("extroot: cannot find block device\n");
1075 static int main_extroot(int argc
, char **argv
)
1077 struct blkid_struct_probe
*pr
;
1078 char blkdev_path
[32] = { 0 };
1080 #ifdef UBIFS_EXTROOT
1084 if (!getenv("PREINIT"))
1088 fprintf(stderr
, "Usage: block extroot mountpoint\n");
1098 * Look for "rootfs_data". We will want to mount it and check for
1099 * extroot configuration.
1102 /* Start with looking for MTD partition */
1103 find_block_mtd("rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1104 if (blkdev_path
[0]) {
1105 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1106 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
1107 char cfg
[] = "/tmp/jffs_cfg";
1110 * Mount MTD part and try extroot (using
1111 * /etc/config/fstab from that partition)
1114 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1115 err
= mount_extroot(cfg
);
1116 umount2(cfg
, MNT_DETACH
);
1119 rmdir("/tmp/overlay");
1125 #ifdef UBIFS_EXTROOT
1126 /* ... but it also could be an UBI volume */
1127 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1128 libubi
= libubi_open();
1129 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1130 libubi_close(libubi
);
1131 if (blkdev_path
[0]) {
1132 char cfg
[] = "/tmp/ubifs_cfg";
1134 /* Mount volume and try extroot (using fstab from that vol) */
1136 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1137 err
= mount_extroot(cfg
);
1138 umount2(cfg
, MNT_DETACH
);
1141 rmdir("/tmp/overlay");
1147 return mount_extroot(NULL
);
1150 static int main_mount(int argc
, char **argv
)
1152 struct blkid_struct_probe
*pr
;
1154 if (config_load(NULL
))
1158 list_for_each_entry(pr
, &devices
, list
)
1159 mount_device(pr
, 0);
1161 handle_swapfiles(true);
1166 static int main_umount(int argc
, char **argv
)
1168 struct blkid_struct_probe
*pr
;
1170 if (config_load(NULL
))
1173 handle_swapfiles(false);
1176 list_for_each_entry(pr
, &devices
, list
)
1182 static int main_detect(int argc
, char **argv
)
1184 struct blkid_struct_probe
*pr
;
1187 printf("config 'global'\n");
1188 printf("\toption\tanon_swap\t'0'\n");
1189 printf("\toption\tanon_mount\t'0'\n");
1190 printf("\toption\tauto_swap\t'1'\n");
1191 printf("\toption\tauto_mount\t'1'\n");
1192 printf("\toption\tdelay_root\t'5'\n");
1193 printf("\toption\tcheck_fs\t'0'\n\n");
1194 list_for_each_entry(pr
, &devices
, list
)
1195 print_block_uci(pr
);
1200 static int main_info(int argc
, char **argv
)
1203 struct blkid_struct_probe
*pr
;
1207 list_for_each_entry(pr
, &devices
, list
)
1208 print_block_info(pr
);
1213 for (i
= 2; i
< argc
; i
++) {
1216 if (stat(argv
[i
], &s
)) {
1217 ERROR("failed to stat %s\n", argv
[i
]);
1220 if (!S_ISBLK(s
.st_mode
)) {
1221 ERROR("%s is not a block device\n", argv
[i
]);
1224 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1226 print_block_info(pr
);
1232 static int swapon_usage(void)
1234 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1235 "\tStart swapping on [DEVICE]\n"
1236 " -a\tStart swapping on all swap devices\n"
1237 " -p pri\tSet priority of swap device\n"
1238 " -s\tShow summary\n");
1242 static int main_swapon(int argc
, char **argv
)
1248 struct blkid_struct_probe
*pr
;
1254 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1257 fp
= fopen("/proc/swaps", "r");
1261 ERROR("failed to open /proc/swaps\n");
1264 while (getline(&lineptr
, &s
, fp
) > 0)
1272 list_for_each_entry(pr
, &devices
, list
) {
1273 if (strcmp(pr
->id
->name
, "swap"))
1275 if (swapon(pr
->dev
, 0))
1276 ERROR("failed to swapon %s\n", pr
->dev
);
1282 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1285 return swapon_usage();
1290 if (optind
!= (argc
- 1))
1291 return swapon_usage();
1293 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1294 ERROR("%s is not a block device or file\n", argv
[optind
]);
1297 err
= swapon(argv
[optind
], flags
);
1299 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1306 static int main_swapoff(int argc
, char **argv
)
1309 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1310 "\tStop swapping on DEVICE\n"
1311 " -a\tStop swapping on all swap devices\n");
1315 if (!strcmp(argv
[1], "-a")) {
1316 FILE *fp
= fopen("/proc/swaps", "r");
1320 ERROR("failed to open /proc/swaps\n");
1323 fgets(line
, sizeof(line
), fp
);
1324 while (fgets(line
, sizeof(line
), fp
)) {
1325 char *end
= strchr(line
, ' ');
1331 err
= swapoff(line
);
1333 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1340 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1341 ERROR("%s is not a block device or file\n", argv
[1]);
1344 err
= swapoff(argv
[1]);
1346 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1354 int main(int argc
, char **argv
)
1356 char *base
= basename(*argv
);
1360 if (!strcmp(base
, "swapon"))
1361 return main_swapon(argc
, argv
);
1363 if (!strcmp(base
, "swapoff"))
1364 return main_swapoff(argc
, argv
);
1366 if ((argc
> 1) && !strcmp(base
, "block")) {
1367 if (!strcmp(argv
[1], "info"))
1368 return main_info(argc
, argv
);
1370 if (!strcmp(argv
[1], "detect"))
1371 return main_detect(argc
, argv
);
1373 if (!strcmp(argv
[1], "hotplug"))
1374 return main_hotplug(argc
, argv
);
1376 if (!strcmp(argv
[1], "extroot"))
1377 return main_extroot(argc
, argv
);
1379 if (!strcmp(argv
[1], "mount"))
1380 return main_mount(argc
, argv
);
1382 if (!strcmp(argv
[1], "umount"))
1383 return main_umount(argc
, argv
);
1386 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");