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.
24 #include <sys/types.h>
26 #include <sys/mount.h>
32 #include <libubox/list.h>
33 #include <libubox/vlist.h>
34 #include <libubox/blobmsg_json.h>
35 #include <libubox/avl-cmp.h>
37 #include "libblkid-tiny/libblkid-tiny.h"
40 #include "libubi/libubi.h"
43 #define ERROR(fmt, ...) do { \
44 syslog(LOG_ERR, fmt, ## __VA_ARGS__); \
45 fprintf(stderr, "block: "fmt, ## __VA_ARGS__); \
54 struct vlist_node node
;
70 static struct vlist_tree mounts
;
71 static struct blob_buf b
;
72 static LIST_HEAD(devices
);
73 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
74 static unsigned int delay_root
;
86 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
87 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
88 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
89 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
90 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
91 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
92 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
105 static const struct uci_blob_param_list config_attr_list
= {
106 .n_params
= __CFG_MAX
,
107 .params
= config_policy
,
110 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
111 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
112 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
113 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
114 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
115 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
116 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
119 static const struct uci_blob_param_list mount_attr_list
= {
120 .n_params
= __MOUNT_MAX
,
121 .params
= mount_policy
,
133 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
134 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
135 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
136 [SWAP_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
137 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
138 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
141 static const struct uci_blob_param_list swap_attr_list
= {
142 .n_params
= __SWAP_MAX
,
143 .params
= swap_policy
,
152 # define MS_DIRSYNC (1 << 7)
156 # define MS_RELATIME (1 << 21)
159 #ifndef MS_STRICTATIME
160 # define MS_STRICTATIME (1 << 24)
163 static const struct mount_flag mount_flags
[] = {
164 { "sync", MS_SYNCHRONOUS
},
165 { "async", ~MS_SYNCHRONOUS
},
166 { "dirsync", MS_DIRSYNC
},
167 { "mand", MS_MANDLOCK
},
168 { "nomand", ~MS_MANDLOCK
},
169 { "atime", ~MS_NOATIME
},
170 { "noatime", MS_NOATIME
},
171 { "dev", ~MS_NODEV
},
172 { "nodev", MS_NODEV
},
173 { "diratime", ~MS_NODIRATIME
},
174 { "nodiratime", MS_NODIRATIME
},
175 { "exec", ~MS_NOEXEC
},
176 { "noexec", MS_NOEXEC
},
177 { "suid", ~MS_NOSUID
},
178 { "nosuid", MS_NOSUID
},
179 { "rw", ~MS_RDONLY
},
181 { "relatime", MS_RELATIME
},
182 { "norelatime", ~MS_RELATIME
},
183 { "strictatime", MS_STRICTATIME
},
186 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
191 return strdup(blobmsg_get_string(attr
));
194 static char *blobmsg_get_basename(struct blob_attr
*attr
)
199 return strdup(basename(blobmsg_get_string(attr
)));
202 static void parse_mount_options(struct mount
*m
, char *optstr
)
206 char *p
, *opts
, *last
;
211 if (!optstr
|| !*optstr
)
214 m
->options
= opts
= calloc(1, strlen(optstr
) + 1);
227 for (i
= 0, is_flag
= false; i
< ARRAY_SIZE(mount_flags
); i
++) {
228 if (!strcmp(last
, mount_flags
[i
].name
)) {
229 if (mount_flags
[i
].flag
< 0)
230 m
->flags
&= (uint32_t)mount_flags
[i
].flag
;
232 m
->flags
|= (uint32_t)mount_flags
[i
].flag
;
239 opts
+= sprintf(opts
, "%s%s", (opts
> m
->options
) ? "," : "", last
);
248 static int mount_add(struct uci_section
*s
)
250 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
253 blob_buf_init(&b
, 0);
254 uci_to_blob(&b
, s
, &mount_attr_list
);
255 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
257 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
260 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
263 m
= malloc(sizeof(struct mount
));
264 m
->type
= TYPE_MOUNT
;
265 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
266 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
267 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
268 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
270 parse_mount_options(m
, blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]));
272 m
->overlay
= m
->extroot
= 0;
273 if (m
->target
&& !strcmp(m
->target
, "/"))
275 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
276 m
->extroot
= m
->overlay
= 1;
279 vlist_add(&mounts
, &m
->node
, m
->uuid
);
281 vlist_add(&mounts
, &m
->node
, m
->label
);
283 vlist_add(&mounts
, &m
->node
, m
->device
);
288 static int swap_add(struct uci_section
*s
)
290 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
293 blob_buf_init(&b
, 0);
294 uci_to_blob(&b
, s
, &swap_attr_list
);
295 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
297 if (!tb
[SWAP_UUID
] && !tb
[SWAP_LABEL
] && !tb
[SWAP_DEVICE
])
300 m
= malloc(sizeof(struct mount
));
301 memset(m
, 0, sizeof(struct mount
));
303 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
304 m
->label
= blobmsg_get_strdup(tb
[SWAP_LABEL
]);
305 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
307 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
309 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
311 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
])) {
312 /* store complete swap path */
314 m
->target
= blobmsg_get_strdup(tb
[SWAP_DEVICE
]);
317 vlist_add(&mounts
, &m
->node
, m
->uuid
);
319 vlist_add(&mounts
, &m
->node
, m
->label
);
321 vlist_add(&mounts
, &m
->node
, m
->device
);
327 static int global_add(struct uci_section
*s
)
329 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
331 blob_buf_init(&b
, 0);
332 uci_to_blob(&b
, s
, &config_attr_list
);
333 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
335 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
337 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
340 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
342 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
345 if (tb
[CFG_DELAY_ROOT
])
346 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
348 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
354 static struct mount
* find_swap(const char *uuid
, const char *label
, const char *device
)
358 vlist_for_each_element(&mounts
, m
, node
) {
359 if (m
->type
!= TYPE_SWAP
)
361 if (uuid
&& m
->uuid
&& !strcasecmp(m
->uuid
, uuid
))
363 if (label
&& m
->label
&& !strcmp(m
->label
, label
))
365 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
372 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
377 vlist_for_each_element(&mounts
, m
, node
) {
378 if (m
->type
!= TYPE_MOUNT
)
380 if (m
->uuid
&& uuid
&& !strcasecmp(m
->uuid
, uuid
))
382 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
384 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
386 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
393 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
394 struct vlist_node
*node_old
)
398 static int config_load(char *cfg
)
400 struct uci_context
*ctx
;
401 struct uci_package
*pkg
;
402 struct uci_element
*e
;
404 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
406 ctx
= uci_alloc_context();
409 snprintf(path
, 32, "%s/etc/config", cfg
);
410 uci_set_confdir(ctx
, path
);
413 if (uci_load(ctx
, "fstab", &pkg
))
416 uci_get_errorstr(ctx
, &err
, "fstab");
417 ERROR("extroot: failed to load %s/etc/config/%s\n",
418 cfg
? cfg
: "", err
);
423 vlist_update(&mounts
);
424 uci_foreach_element(&pkg
->sections
, e
) {
425 struct uci_section
*s
= uci_to_section(e
);
427 if (!strcmp(s
->type
, "mount"))
429 if (!strcmp(s
->type
, "swap"))
431 if (!strcmp(s
->type
, "global"))
434 vlist_flush(&mounts
);
439 static struct blkid_struct_probe
* _probe_path(char *path
)
441 struct blkid_struct_probe
*pr
;
443 pr
= malloc(sizeof(*pr
));
448 memset(pr
, 0, sizeof(*pr
));
449 probe_block(path
, pr
);
451 if (pr
->err
|| !pr
->id
) {
459 static int _cache_load(const char *path
)
461 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
465 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
468 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
469 struct blkid_struct_probe
*pr
= _probe_path(gl
.gl_pathv
[j
]);
471 list_add_tail(&pr
->list
, &devices
);
479 static void cache_load(int mtd
)
482 _cache_load("/dev/mtdblock*");
483 _cache_load("/dev/ubiblock*");
485 _cache_load("/dev/mmcblk*");
486 _cache_load("/dev/sd*");
487 _cache_load("/dev/hd*");
488 _cache_load("/dev/md*");
489 _cache_load("/dev/vd*");
490 _cache_load("/dev/mapper/*");
493 static int print_block_info(struct blkid_struct_probe
*pr
)
495 printf("%s:", pr
->dev
);
497 printf(" UUID=\"%s\"", pr
->uuid
);
500 printf(" LABEL=\"%s\"", pr
->label
);
503 printf(" NAME=\"%s\"", pr
->name
);
506 printf(" VERSION=\"%s\"", pr
->version
);
508 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
513 static int print_block_uci(struct blkid_struct_probe
*pr
)
515 if (!strcmp(pr
->id
->name
, "swap")) {
516 printf("config 'swap'\n");
518 printf("config 'mount'\n");
519 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
522 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
524 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
525 printf("\toption\tenabled\t'0'\n\n");
530 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
532 struct blkid_struct_probe
*pr
= NULL
;
535 list_for_each_entry(pr
, &devices
, list
)
536 if (!strcasecmp(pr
->uuid
, uuid
))
540 list_for_each_entry(pr
, &devices
, list
)
541 if (!strcmp(pr
->label
, label
))
545 list_for_each_entry(pr
, &devices
, list
)
546 if (!strcmp(basename(pr
->dev
), basename(path
)))
552 static char* find_mount_point(char *block
)
554 FILE *fp
= fopen("/proc/mounts", "r");
555 static char line
[256];
556 int len
= strlen(block
);
562 while (fgets(line
, sizeof(line
), fp
)) {
563 if (!strncmp(line
, block
, len
)) {
564 char *p
= &line
[len
+ 1];
565 char *t
= strstr(p
, " ");
582 static void mkdir_p(char *dir
)
584 char *l
= strrchr(dir
, '/');
594 static void check_filesystem(struct blkid_struct_probe
*pr
)
598 char *e2fsck
= "/usr/sbin/e2fsck";
600 if (strncmp(pr
->id
->name
, "ext", 3)) {
601 ERROR("check_filesystem: %s is not supported\n", pr
->id
->name
);
605 if (stat(e2fsck
, &statbuf
) < 0) {
606 ERROR("check_filesystem: %s not found\n", e2fsck
);
612 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
614 } else if (pid
> 0) {
617 waitpid(pid
, &status
, 0);
618 if (WEXITSTATUS(status
))
619 ERROR("check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
623 static void handle_swapfiles(bool on
)
627 struct blkid_struct_probe
*pr
;
629 vlist_for_each_element(&mounts
, m
, node
)
631 if (m
->type
!= TYPE_SWAP
|| !m
->target
)
634 if (stat(m
->target
, &s
) || !S_ISREG(s
.st_mode
))
637 pr
= _probe_path(m
->target
);
642 if (!strcmp(pr
->id
->name
, "swap")) {
644 swapon(pr
->dev
, m
->prio
);
653 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
661 device
= basename(pr
->dev
);
663 if (!strcmp(pr
->id
->name
, "swap")) {
664 if (hotplug
&& !auto_swap
)
666 m
= find_swap(pr
->uuid
, pr
->label
, device
);
668 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
673 if (hotplug
&& !auto_mount
)
676 if (find_mount_point(pr
->dev
)) {
677 ERROR("%s is already mounted\n", pr
->dev
);
681 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
686 char *target
= m
->target
;
691 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
697 check_filesystem(pr
);
699 err
= mount(pr
->dev
, target
, pr
->id
->name
, m
->flags
,
700 (m
->options
) ? (m
->options
) : (""));
702 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
703 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
705 handle_swapfiles(true);
710 char target
[] = "/mnt/mmcblk123";
713 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
717 check_filesystem(pr
);
719 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
721 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
722 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
724 handle_swapfiles(true);
731 static int umount_device(struct blkid_struct_probe
*pr
)
734 char *device
= basename(pr
->dev
);
741 if (!strcmp(pr
->id
->name
, "swap"))
744 mp
= find_mount_point(pr
->dev
);
748 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
752 err
= umount2(mp
, MNT_DETACH
);
754 ERROR("unmounting %s (%s) failed (%d) - %s\n",
755 pr
->dev
, mp
, err
, strerror(err
));
757 ERROR("unmounted %s (%s)\n",
763 static int main_hotplug(int argc
, char **argv
)
766 char *action
, *device
, *mount_point
;
768 action
= getenv("ACTION");
769 device
= getenv("DEVNAME");
771 if (!action
|| !device
)
773 snprintf(path
, sizeof(path
), "/dev/%s", device
);
775 if (!strcmp(action
, "remove")) {
777 mount_point
= find_mount_point(path
);
779 err
= umount2(mount_point
, MNT_DETACH
);
782 ERROR("umount of %s failed (%d) - %s\n",
783 mount_point
, err
, strerror(err
));
786 } else if (strcmp(action
, "add")) {
787 ERROR("Unkown action %s\n", action
);
792 if (config_load(NULL
))
796 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
799 static int find_block_mtd(char *name
, char *part
, int plen
)
801 FILE *fp
= fopen("/proc/mtd", "r");
802 static char line
[256];
808 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
809 if (strstr(line
, name
)) {
810 char *eol
= strstr(line
, ":");
825 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
831 static int find_ubi_vol(libubi_t libubi
, char *name
, int *dev_num
, int *vol_id
)
835 while (ubi_dev_present(libubi
, dev
))
837 struct ubi_dev_info dev_info
;
838 struct ubi_vol_info vol_info
;
840 if (ubi_get_dev_info1(libubi
, dev
++, &dev_info
))
842 if (ubi_get_vol_info1_nm(libubi
, dev_info
.dev_num
, name
, &vol_info
))
845 *dev_num
= dev_info
.dev_num
;
846 *vol_id
= vol_info
.vol_id
;
854 static int find_block_ubi(libubi_t libubi
, char *name
, char *part
, int plen
)
860 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
862 snprintf(part
, plen
, "/dev/ubi%d_%d", dev_num
, vol_id
);
867 static int find_block_ubi_RO(libubi_t libubi
, char *name
, char *part
, int plen
)
873 err
= find_ubi_vol(libubi
, name
, &dev_num
, &vol_id
);
875 snprintf(part
, plen
, "/dev/ubiblock%d_%d", dev_num
, vol_id
);
881 static int check_extroot(char *path
)
883 struct blkid_struct_probe
*pr
= NULL
;
887 if (find_block_mtd("rootfs", fs
, sizeof(fs
))) {
891 libubi
= libubi_open();
892 err
= find_block_ubi_RO(libubi
, "rootfs", fs
, sizeof(fs
));
893 libubi_close(libubi
);
898 if (find_block_mtd("rootfs", fs
, sizeof(fs
)))
902 list_for_each_entry(pr
, &devices
, list
) {
903 if (!strcmp(pr
->dev
, fs
)) {
907 char uuid
[64] = { 0 };
909 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
911 fp
= fopen(tag
, "w+");
913 ERROR("extroot: failed to write uuid tag file\n");
914 /* return 0 to continue boot regardless of error */
922 fp
= fopen(tag
, "r");
924 ERROR("extroot: failed to open uuid tag file\n");
928 fgets(uuid
, sizeof(uuid
), fp
);
930 if (!strcasecmp(uuid
, pr
->uuid
))
932 ERROR("extroot: uuid tag does not match rom uuid\n");
939 * Read info about extroot from UCI (using prefix) and mount it.
941 static int mount_extroot(char *cfg
)
943 char overlay
[] = "/tmp/extroot/overlay";
944 char mnt
[] = "/tmp/extroot/mnt";
946 struct blkid_struct_probe
*pr
;
950 /* Load @cfg/etc/config/fstab */
951 if (config_load(cfg
))
954 /* See if there is extroot-specific mount config */
955 m
= find_block(NULL
, NULL
, NULL
, "/");
957 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
959 if (!m
|| !m
->extroot
)
961 ERROR("extroot: no root or overlay mount defined\n");
965 /* Find block device pointed by the mount config */
966 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
968 if (!pr
&& delay_root
){
969 ERROR("extroot: is not ready yet, retrying in %u seconds\n", delay_root
);
973 pr
= find_block_info(m
->uuid
, m
->label
, m
->device
);
976 if (strncmp(pr
->id
->name
, "ext", 3)) {
977 ERROR("extroot: %s is not supported, try ext4\n", pr
->id
->name
);
985 check_filesystem(pr
);
987 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
990 ERROR("mounting %s (%s) as %s failed (%d) - %s\n",
991 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
992 } else if (m
->overlay
) {
993 err
= check_extroot(path
);
998 ERROR("extroot: cannot find block device\n");
1004 static int main_extroot(int argc
, char **argv
)
1006 struct blkid_struct_probe
*pr
;
1007 char fs
[32] = { 0 };
1008 char blkdev_path
[32] = { 0 };
1010 #ifdef UBIFS_EXTROOT
1014 if (!getenv("PREINIT"))
1018 fprintf(stderr
, "Usage: block extroot mountpoint\n");
1026 * Make sure there is "rootfs" MTD partition or UBI volume.
1029 find_block_mtd("rootfs", fs
, sizeof(fs
));
1031 #ifdef UBIFS_EXTROOT
1032 libubi
= libubi_open();
1033 find_block_ubi_RO(libubi
, "rootfs", fs
, sizeof(fs
));
1034 libubi_close(libubi
);
1036 ERROR("extroot: unable to locate rootfs mtdblock / ubiblock\n");
1040 ERROR("extroot: unable to locate rootfs mtdblock\n");
1045 pr
= find_block_info(NULL
, NULL
, fs
);
1047 ERROR("extroot: unable to retrieve rootfs information\n");
1052 * Look for "rootfs_data". We will want to mount it and check for
1053 * extroot configuration.
1056 /* Start with looking for MTD partition */
1057 find_block_mtd("rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1058 if (blkdev_path
[0]) {
1059 pr
= find_block_info(NULL
, NULL
, blkdev_path
);
1060 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
1061 char cfg
[] = "/tmp/jffs_cfg";
1064 * Mount MTD part and try extroot (using
1065 * /etc/config/fstab from that partition)
1068 if (!mount(blkdev_path
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
1069 err
= mount_extroot(cfg
);
1070 umount2(cfg
, MNT_DETACH
);
1073 rmdir("/tmp/overlay");
1079 #ifdef UBIFS_EXTROOT
1080 /* ... but it also could be an UBI volume */
1081 memset(blkdev_path
, 0, sizeof(blkdev_path
));
1082 libubi
= libubi_open();
1083 find_block_ubi(libubi
, "rootfs_data", blkdev_path
, sizeof(blkdev_path
));
1084 libubi_close(libubi
);
1085 if (blkdev_path
[0]) {
1086 char cfg
[] = "/tmp/ubifs_cfg";
1088 /* Mount volume and try extroot (using fstab from that vol) */
1090 if (!mount(blkdev_path
, cfg
, "ubifs", MS_NOATIME
, NULL
)) {
1091 err
= mount_extroot(cfg
);
1092 umount2(cfg
, MNT_DETACH
);
1095 rmdir("/tmp/overlay");
1101 return mount_extroot(NULL
);
1104 static int main_mount(int argc
, char **argv
)
1106 struct blkid_struct_probe
*pr
;
1108 if (config_load(NULL
))
1112 list_for_each_entry(pr
, &devices
, list
)
1113 mount_device(pr
, 0);
1115 handle_swapfiles(true);
1120 static int main_umount(int argc
, char **argv
)
1122 struct blkid_struct_probe
*pr
;
1124 if (config_load(NULL
))
1127 handle_swapfiles(false);
1130 list_for_each_entry(pr
, &devices
, list
)
1136 static int main_detect(int argc
, char **argv
)
1138 struct blkid_struct_probe
*pr
;
1141 printf("config 'global'\n");
1142 printf("\toption\tanon_swap\t'0'\n");
1143 printf("\toption\tanon_mount\t'0'\n");
1144 printf("\toption\tauto_swap\t'1'\n");
1145 printf("\toption\tauto_mount\t'1'\n");
1146 printf("\toption\tdelay_root\t'5'\n");
1147 printf("\toption\tcheck_fs\t'0'\n\n");
1148 list_for_each_entry(pr
, &devices
, list
)
1149 print_block_uci(pr
);
1154 static int main_info(int argc
, char **argv
)
1157 struct blkid_struct_probe
*pr
;
1161 list_for_each_entry(pr
, &devices
, list
)
1162 print_block_info(pr
);
1167 for (i
= 2; i
< argc
; i
++) {
1170 if (stat(argv
[i
], &s
)) {
1171 ERROR("failed to stat %s\n", argv
[i
]);
1174 if (!S_ISBLK(s
.st_mode
)) {
1175 ERROR("%s is not a block device\n", argv
[i
]);
1178 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
1180 print_block_info(pr
);
1186 static int swapon_usage(void)
1188 fprintf(stderr
, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
1189 "\tStart swapping on [DEVICE]\n"
1190 " -a\tStart swapping on all swap devices\n"
1191 " -p pri\tSet priority of swap device\n"
1192 " -s\tShow summary\n");
1196 static int main_swapon(int argc
, char **argv
)
1202 struct blkid_struct_probe
*pr
;
1208 while ((ch
= getopt(argc
, argv
, "ap:s")) != -1) {
1211 fp
= fopen("/proc/swaps", "r");
1215 ERROR("failed to open /proc/swaps\n");
1218 while (getline(&lineptr
, &s
, fp
) > 0)
1226 list_for_each_entry(pr
, &devices
, list
) {
1227 if (strcmp(pr
->id
->name
, "swap"))
1229 if (swapon(pr
->dev
, 0))
1230 ERROR("failed to swapon %s\n", pr
->dev
);
1236 flags
= ((pri
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
1239 return swapon_usage();
1244 if (optind
!= (argc
- 1))
1245 return swapon_usage();
1247 if (stat(argv
[optind
], &st
) || (!S_ISBLK(st
.st_mode
) && !S_ISREG(st
.st_mode
))) {
1248 ERROR("%s is not a block device or file\n", argv
[optind
]);
1251 err
= swapon(argv
[optind
], flags
);
1253 ERROR("failed to swapon %s (%d)\n", argv
[optind
], err
);
1260 static int main_swapoff(int argc
, char **argv
)
1263 ERROR("Usage: swapoff [-a] [DEVICE]\n\n"
1264 "\tStop swapping on DEVICE\n"
1265 " -a\tStop swapping on all swap devices\n");
1269 if (!strcmp(argv
[1], "-a")) {
1270 FILE *fp
= fopen("/proc/swaps", "r");
1274 ERROR("failed to open /proc/swaps\n");
1277 fgets(line
, sizeof(line
), fp
);
1278 while (fgets(line
, sizeof(line
), fp
)) {
1279 char *end
= strchr(line
, ' ');
1285 err
= swapoff(line
);
1287 ERROR("failed to swapoff %s (%d)\n", line
, err
);
1294 if (stat(argv
[1], &s
) || (!S_ISBLK(s
.st_mode
) && !S_ISREG(s
.st_mode
))) {
1295 ERROR("%s is not a block device or file\n", argv
[1]);
1298 err
= swapoff(argv
[1]);
1300 ERROR("fsiled to swapoff %s (%d)\n", argv
[1], err
);
1308 int main(int argc
, char **argv
)
1310 char *base
= basename(*argv
);
1314 if (!strcmp(base
, "swapon"))
1315 return main_swapon(argc
, argv
);
1317 if (!strcmp(base
, "swapoff"))
1318 return main_swapoff(argc
, argv
);
1320 if ((argc
> 1) && !strcmp(base
, "block")) {
1321 if (!strcmp(argv
[1], "info"))
1322 return main_info(argc
, argv
);
1324 if (!strcmp(argv
[1], "detect"))
1325 return main_detect(argc
, argv
);
1327 if (!strcmp(argv
[1], "hotplug"))
1328 return main_hotplug(argc
, argv
);
1330 if (!strcmp(argv
[1], "extroot"))
1331 return main_extroot(argc
, argv
);
1333 if (!strcmp(argv
[1], "mount"))
1334 return main_mount(argc
, argv
);
1336 if (!strcmp(argv
[1], "umount"))
1337 return main_umount(argc
, argv
);
1340 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");