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.
22 #include <sys/types.h>
24 #include <sys/mount.h>
30 #include <libubox/list.h>
31 #include <libubox/vlist.h>
32 #include <libubox/blobmsg_json.h>
33 #include <libubox/avl-cmp.h>
35 #include "libblkid-tiny/libblkid-tiny.h"
43 struct vlist_node node
;
58 static struct vlist_tree mounts
;
59 static struct blob_buf b
;
60 static LIST_HEAD(devices
);
61 static int anon_mount
, anon_swap
, auto_mount
, auto_swap
, check_fs
;
62 static unsigned int delay_root
;
74 static const struct blobmsg_policy config_policy
[__CFG_MAX
] = {
75 [CFG_ANON_SWAP
] = { .name
= "anon_swap", .type
= BLOBMSG_TYPE_INT32
},
76 [CFG_ANON_MOUNT
] = { .name
= "anon_mount", .type
= BLOBMSG_TYPE_INT32
},
77 [CFG_AUTO_SWAP
] = { .name
= "auto_swap", .type
= BLOBMSG_TYPE_INT32
},
78 [CFG_AUTO_MOUNT
] = { .name
= "auto_mount", .type
= BLOBMSG_TYPE_INT32
},
79 [CFG_DELAY_ROOT
] = { .name
= "delay_root", .type
= BLOBMSG_TYPE_INT32
},
80 [CFG_CHECK_FS
] = { .name
= "check_fs", .type
= BLOBMSG_TYPE_INT32
},
93 static const struct uci_blob_param_list config_attr_list
= {
94 .n_params
= __CFG_MAX
,
95 .params
= config_policy
,
98 static const struct blobmsg_policy mount_policy
[__MOUNT_MAX
] = {
99 [MOUNT_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
100 [MOUNT_LABEL
] = { .name
= "label", .type
= BLOBMSG_TYPE_STRING
},
101 [MOUNT_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
102 [MOUNT_TARGET
] = { .name
= "target", .type
= BLOBMSG_TYPE_STRING
},
103 [MOUNT_OPTIONS
] = { .name
= "options", .type
= BLOBMSG_TYPE_STRING
},
104 [MOUNT_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
107 static const struct uci_blob_param_list mount_attr_list
= {
108 .n_params
= __MOUNT_MAX
,
109 .params
= mount_policy
,
120 static const struct blobmsg_policy swap_policy
[__SWAP_MAX
] = {
121 [SWAP_ENABLE
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_INT32
},
122 [SWAP_UUID
] = { .name
= "uuid", .type
= BLOBMSG_TYPE_STRING
},
123 [SWAP_DEVICE
] = { .name
= "device", .type
= BLOBMSG_TYPE_STRING
},
124 [SWAP_PRIO
] = { .name
= "priority", .type
= BLOBMSG_TYPE_INT32
},
127 static const struct uci_blob_param_list swap_attr_list
= {
128 .n_params
= __SWAP_MAX
,
129 .params
= swap_policy
,
132 static char *blobmsg_get_strdup(struct blob_attr
*attr
)
137 return strdup(blobmsg_get_string(attr
));
140 static char *blobmsg_get_basename(struct blob_attr
*attr
)
145 return strdup(basename(blobmsg_get_string(attr
)));
148 static int mount_add(struct uci_section
*s
)
150 struct blob_attr
*tb
[__MOUNT_MAX
] = { 0 };
153 blob_buf_init(&b
, 0);
154 uci_to_blob(&b
, s
, &mount_attr_list
);
155 blobmsg_parse(mount_policy
, __MOUNT_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
157 if (!tb
[MOUNT_LABEL
] && !tb
[MOUNT_UUID
] && !tb
[MOUNT_DEVICE
])
160 if (tb
[MOUNT_ENABLE
] && !blobmsg_get_u32(tb
[MOUNT_ENABLE
]))
163 m
= malloc(sizeof(struct mount
));
164 m
->type
= TYPE_MOUNT
;
165 m
->uuid
= blobmsg_get_strdup(tb
[MOUNT_UUID
]);
166 m
->label
= blobmsg_get_strdup(tb
[MOUNT_LABEL
]);
167 m
->target
= blobmsg_get_strdup(tb
[MOUNT_TARGET
]);
168 m
->options
= blobmsg_get_strdup(tb
[MOUNT_OPTIONS
]);
169 m
->device
= blobmsg_get_basename(tb
[MOUNT_DEVICE
]);
170 m
->overlay
= m
->extroot
= 0;
171 if (m
->target
&& !strcmp(m
->target
, "/"))
173 if (m
->target
&& !strcmp(m
->target
, "/overlay"))
174 m
->extroot
= m
->overlay
= 1;
177 vlist_add(&mounts
, &m
->node
, m
->uuid
);
179 vlist_add(&mounts
, &m
->node
, m
->label
);
181 vlist_add(&mounts
, &m
->node
, m
->device
);
186 static int swap_add(struct uci_section
*s
)
188 struct blob_attr
*tb
[__SWAP_MAX
] = { 0 };
191 blob_buf_init(&b
, 0);
192 uci_to_blob(&b
, s
, &swap_attr_list
);
193 blobmsg_parse(swap_policy
, __SWAP_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
195 if (!tb
[SWAP_UUID
] && !tb
[SWAP_DEVICE
])
198 m
= malloc(sizeof(struct mount
));
199 memset(m
, 0, sizeof(struct mount
));
201 m
->uuid
= blobmsg_get_strdup(tb
[SWAP_UUID
]);
202 m
->device
= blobmsg_get_basename(tb
[SWAP_DEVICE
]);
204 m
->prio
= blobmsg_get_u32(tb
[SWAP_PRIO
]);
206 m
->prio
= ((m
->prio
<< SWAP_FLAG_PRIO_SHIFT
) & SWAP_FLAG_PRIO_MASK
) | SWAP_FLAG_PREFER
;
208 if ((!tb
[SWAP_ENABLE
]) || blobmsg_get_u32(tb
[SWAP_ENABLE
]))
209 vlist_add(&mounts
, &m
->node
, (m
->uuid
) ? (m
->uuid
) : (m
->device
));
214 static int global_add(struct uci_section
*s
)
216 struct blob_attr
*tb
[__CFG_MAX
] = { 0 };
218 blob_buf_init(&b
, 0);
219 uci_to_blob(&b
, s
, &config_attr_list
);
220 blobmsg_parse(config_policy
, __CFG_MAX
, tb
, blob_data(b
.head
), blob_len(b
.head
));
222 if ((tb
[CFG_ANON_MOUNT
]) && blobmsg_get_u32(tb
[CFG_ANON_MOUNT
]))
224 if ((tb
[CFG_ANON_SWAP
]) && blobmsg_get_u32(tb
[CFG_ANON_SWAP
]))
227 if ((tb
[CFG_AUTO_MOUNT
]) && blobmsg_get_u32(tb
[CFG_AUTO_MOUNT
]))
229 if ((tb
[CFG_AUTO_SWAP
]) && blobmsg_get_u32(tb
[CFG_AUTO_SWAP
]))
232 if (tb
[CFG_DELAY_ROOT
])
233 delay_root
= blobmsg_get_u32(tb
[CFG_DELAY_ROOT
]);
235 if ((tb
[CFG_CHECK_FS
]) && blobmsg_get_u32(tb
[CFG_CHECK_FS
]))
241 static struct mount
* find_swap(const char *uuid
, const char *device
)
245 vlist_for_each_element(&mounts
, m
, node
) {
246 if (m
->type
!= TYPE_SWAP
)
248 if (uuid
&& m
->uuid
&& !strcmp(m
->uuid
, uuid
))
250 if (device
&& m
->device
&& !strcmp(m
->device
, device
))
257 static struct mount
* find_block(const char *uuid
, const char *label
, const char *device
,
262 vlist_for_each_element(&mounts
, m
, node
) {
263 if (m
->type
!= TYPE_MOUNT
)
265 if (m
->uuid
&& uuid
&& !strcmp(m
->uuid
, uuid
))
267 if (m
->label
&& label
&& !strcmp(m
->label
, label
))
269 if (m
->target
&& target
&& !strcmp(m
->target
, target
))
271 if (m
->device
&& device
&& !strcmp(m
->device
, device
))
278 static void mounts_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
279 struct vlist_node
*node_old
)
283 static int config_load(char *cfg
)
285 struct uci_context
*ctx
;
286 struct uci_package
*pkg
;
287 struct uci_element
*e
;
289 vlist_init(&mounts
, avl_strcmp
, mounts_update
);
291 ctx
= uci_alloc_context();
294 snprintf(path
, 32, "%s/etc/config", cfg
);
295 uci_set_confdir(ctx
, path
);
298 if (uci_load(ctx
, "fstab", &pkg
))
301 vlist_update(&mounts
);
302 uci_foreach_element(&pkg
->sections
, e
) {
303 struct uci_section
*s
= uci_to_section(e
);
305 if (!strcmp(s
->type
, "mount"))
307 if (!strcmp(s
->type
, "swap"))
309 if (!strcmp(s
->type
, "global"))
312 vlist_flush(&mounts
);
317 static int _cache_load(const char *path
)
319 int gl_flags
= GLOB_NOESCAPE
| GLOB_MARK
;
323 if (glob(path
, gl_flags
, NULL
, &gl
) < 0)
326 for (j
= 0; j
< gl
.gl_pathc
; j
++) {
327 struct blkid_struct_probe
*pr
= malloc(sizeof(struct blkid_struct_probe
));
328 memset(pr
, 0, sizeof(struct blkid_struct_probe
));
329 probe_block(gl
.gl_pathv
[j
], pr
);
330 if (pr
->err
|| !pr
->id
)
333 list_add_tail(&pr
->list
, &devices
);
341 static void cache_load(int mtd
)
344 _cache_load("/dev/mtdblock*");
345 _cache_load("/dev/mmcblk*");
346 _cache_load("/dev/sd*");
347 _cache_load("/dev/sdc*");
348 _cache_load("/dev/hd*");
351 static int print_block_info(struct blkid_struct_probe
*pr
)
353 printf("%s:", pr
->dev
);
355 printf(" UUID=\"%s\"", pr
->uuid
);
358 printf(" LABEL=\"%s\"", pr
->label
);
361 printf(" NAME=\"%s\"", pr
->name
);
364 printf(" VERSION=\"%s\"", pr
->version
);
366 printf(" TYPE=\"%s\"\n", pr
->id
->name
);
371 static int print_block_uci(struct blkid_struct_probe
*pr
)
373 if (!strcmp(pr
->id
->name
, "swap")) {
374 printf("config 'swap'\n");
376 printf("config 'mount'\n");
377 printf("\toption\ttarget\t'/mnt/%s'\n", basename(pr
->dev
));
380 printf("\toption\tuuid\t'%s'\n", pr
->uuid
);
382 printf("\toption\tdevice\t'%s'\n", pr
->dev
);
383 printf("\toption\tenabled\t'0'\n\n");
388 static struct blkid_struct_probe
* find_block_info(char *uuid
, char *label
, char *path
)
390 struct blkid_struct_probe
*pr
= NULL
;
393 list_for_each_entry(pr
, &devices
, list
)
394 if (!strcmp(pr
->uuid
, uuid
))
398 list_for_each_entry(pr
, &devices
, list
)
399 if (strcmp(pr
->label
, label
))
403 list_for_each_entry(pr
, &devices
, list
)
404 if (!strcmp(pr
->dev
, path
))
410 static char* find_mount_point(char *block
)
412 FILE *fp
= fopen("/proc/mounts", "r");
413 static char line
[256];
414 int len
= strlen(block
);
420 while (fgets(line
, sizeof(line
), fp
)) {
421 if (!strncmp(line
, block
, len
)) {
422 char *p
= &line
[len
+ 1];
423 char *t
= strstr(p
, " ");
438 static void mkdir_p(char *dir
)
440 char *l
= strrchr(dir
, '/');
450 static void check_filesystem(struct blkid_struct_probe
*pr
)
454 char *e2fsck
= "/usr/sbin/e2fsck";
456 if (strncmp(pr
->id
->name
, "ext", 3)) {
457 fprintf(stderr
, "check_filesystem: %s is not supported\n", pr
->id
->name
);
461 if (stat(e2fsck
, &statbuf
) < 0) {
462 fprintf(stderr
, "check_filesystem: %s not found\n", e2fsck
);
468 execl(e2fsck
, e2fsck
, "-p", pr
->dev
, NULL
);
470 } else if (pid
> 0) {
473 waitpid(pid
, &status
, 0);
474 if (WEXITSTATUS(status
))
475 fprintf(stderr
, "check_filesystem: %s returned %d\n", e2fsck
, WEXITSTATUS(status
));
479 static int mount_device(struct blkid_struct_probe
*pr
, int hotplug
)
482 char *device
= basename(pr
->dev
);
487 if (!strcmp(pr
->id
->name
, "swap")) {
488 if (hotplug
&& !auto_swap
)
490 m
= find_swap(pr
->uuid
, device
);
492 swapon(pr
->dev
, (m
) ? (m
->prio
) : (0));
497 if (hotplug
&& !auto_mount
)
500 if (find_mount_point(pr
->dev
)) {
501 fprintf(stderr
, "%s is already mounted\n", pr
->dev
);
505 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
510 char *target
= m
->target
;
511 char _target
[] = "/mnt/mmcblk123";
515 snprintf(_target
, sizeof(_target
), "/mnt/%s", device
);
521 check_filesystem(pr
);
523 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
525 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
526 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
531 char target
[] = "/mnt/mmcblk123";
534 snprintf(target
, sizeof(target
), "/mnt/%s", device
);
538 check_filesystem(pr
);
540 err
= mount(pr
->dev
, target
, pr
->id
->name
, 0, "");
542 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
543 pr
->dev
, pr
->id
->name
, target
, err
, strerror(err
));
550 static int umount_device(struct blkid_struct_probe
*pr
)
553 char *device
= basename(pr
->dev
);
560 if (!strcmp(pr
->id
->name
, "swap"))
563 mp
= find_mount_point(pr
->dev
);
567 m
= find_block(pr
->uuid
, pr
->label
, device
, NULL
);
571 err
= umount2(mp
, MNT_DETACH
);
573 fprintf(stderr
, "unmounting %s (%s) failed (%d) - %s\n",
574 pr
->dev
, mp
, err
, strerror(err
));
576 fprintf(stderr
, "unmounted %s (%s)\n",
582 static int main_hotplug(int argc
, char **argv
)
585 char *action
, *device
, *mount_point
;
587 action
= getenv("ACTION");
588 device
= getenv("DEVNAME");
590 if (!action
|| !device
)
592 snprintf(path
, sizeof(path
), "/dev/%s", device
);
594 if (!strcmp(action
, "remove")) {
596 mount_point
= find_mount_point(path
);
598 err
= umount2(mount_point
, MNT_DETACH
);
601 fprintf(stderr
, "umount of %s failed (%d) - %s\n",
602 mount_point
, err
, strerror(err
));
605 } else if (strcmp(action
, "add")) {
606 fprintf(stderr
, "Unkown action %s\n", action
);
611 if (config_load(NULL
))
615 return mount_device(find_block_info(NULL
, NULL
, path
), 1);
618 static int find_block_mtd(char *name
, char *part
, int plen
)
620 FILE *fp
= fopen("/proc/mtd", "r");
621 static char line
[256];
627 while (!index
&& fgets(line
, sizeof(line
), fp
)) {
628 if (strstr(line
, name
)) {
629 char *eol
= strstr(line
, ":");
644 snprintf(part
, plen
, "/dev/mtdblock%s", index
);
649 static int check_extroot(char *path
)
651 struct blkid_struct_probe
*pr
= NULL
;
654 if (find_block_mtd("rootfs", fs
, sizeof(fs
)))
657 list_for_each_entry(pr
, &devices
, list
) {
658 if (!strcmp(pr
->dev
, fs
)) {
662 char uuid
[32] = { 0 };
664 snprintf(tag
, sizeof(tag
), "%s/etc/.extroot-uuid", path
);
666 fp
= fopen(tag
, "w+");
668 fprintf(stderr
, "extroot: failed to write uuid tag file\n");
669 /* return 0 to continue boot regardless of error */
677 fp
= fopen(tag
, "r");
679 fprintf(stderr
, "extroot: failed to open uuid tag file\n");
683 fgets(uuid
, sizeof(uuid
), fp
);
685 if (!strcmp(uuid
, pr
->uuid
))
687 fprintf(stderr
, "extroot: uuid tag does not match rom uuid\n");
693 static int mount_extroot(char *cfg
)
695 char overlay
[] = "/tmp/overlay";
696 char mnt
[] = "/tmp/mnt";
698 struct blkid_struct_probe
*pr
;
702 if (config_load(cfg
))
705 m
= find_block(NULL
, NULL
, NULL
, "/");
707 m
= find_block(NULL
, NULL
, NULL
, "/overlay");
709 if (!m
|| !m
->extroot
)
712 pr
= find_block_info(m
->uuid
, m
->label
, NULL
);
714 if (!pr
&& delay_root
){
715 fprintf(stderr
, "extroot: is not ready yet, retrying in %ui seconds\n", delay_root
);
719 pr
= find_block_info(m
->uuid
, m
->label
, NULL
);
722 if (strncmp(pr
->id
->name
, "ext", 3)) {
723 fprintf(stderr
, "extroot: %s is not supported, try ext4\n", pr
->id
->name
);
731 check_filesystem(pr
);
733 err
= mount(pr
->dev
, path
, pr
->id
->name
, 0, (m
->options
) ? (m
->options
) : (""));
736 fprintf(stderr
, "mounting %s (%s) as %s failed (%d) - %s\n",
737 pr
->dev
, pr
->id
->name
, path
, err
, strerror(err
));
738 } else if (m
->overlay
) {
739 err
= check_extroot(path
);
748 static int main_extroot(int argc
, char **argv
)
750 struct blkid_struct_probe
*pr
;
752 char fs_data
[32] = { 0 };
755 if (!getenv("PREINIT"))
759 fprintf(stderr
, "Usage: block extroot mountpoint\n");
766 find_block_mtd("rootfs", fs
, sizeof(fs
));
770 pr
= find_block_info(NULL
, NULL
, fs
);
774 find_block_mtd("rootfs_data", fs_data
, sizeof(fs_data
));
776 pr
= find_block_info(NULL
, NULL
, fs_data
);
777 if (pr
&& !strcmp(pr
->id
->name
, "jffs2")) {
778 char cfg
[] = "/tmp/jffs_cfg";
781 if (!mount(fs_data
, cfg
, "jffs2", MS_NOATIME
, NULL
)) {
782 err
= mount_extroot(cfg
);
783 umount2(cfg
, MNT_DETACH
);
786 rmdir("/tmp/overlay");
792 return mount_extroot(NULL
);
795 static int main_mount(int argc
, char **argv
)
797 struct blkid_struct_probe
*pr
;
799 if (config_load(NULL
))
803 list_for_each_entry(pr
, &devices
, list
)
809 static int main_umount(int argc
, char **argv
)
811 struct blkid_struct_probe
*pr
;
813 if (config_load(NULL
))
817 list_for_each_entry(pr
, &devices
, list
)
823 static int main_detect(int argc
, char **argv
)
825 struct blkid_struct_probe
*pr
;
828 printf("config 'global'\n");
829 printf("\toption\tanon_swap\t'0'\n");
830 printf("\toption\tanon_mount\t'0'\n");
831 printf("\toption\tauto_swap\t'1'\n");
832 printf("\toption\tauto_mount\t'1'\n");
833 printf("\toption\tdelay_root\t'0'\n");
834 printf("\toption\tcheck_fs\t'0'\n\n");
835 list_for_each_entry(pr
, &devices
, list
)
841 static int main_info(int argc
, char **argv
)
844 struct blkid_struct_probe
*pr
;
848 list_for_each_entry(pr
, &devices
, list
)
849 print_block_info(pr
);
854 for (i
= 2; i
< argc
; i
++) {
857 if (stat(argv
[i
], &s
)) {
858 fprintf(stderr
, "failed to stat %s\n", argv
[i
]);
861 if (!S_ISBLK(s
.st_mode
)) {
862 fprintf(stderr
, "%s is not a block device\n", argv
[i
]);
865 pr
= find_block_info(NULL
, NULL
, argv
[i
]);
867 print_block_info(pr
);
873 static int main_swapon(int argc
, char **argv
)
876 fprintf(stderr
, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
880 if (!strcmp(argv
[1], "-a")) {
881 struct blkid_struct_probe
*pr
;
884 list_for_each_entry(pr
, &devices
, list
) {
885 if (strcmp(pr
->id
->name
, "swap"))
887 if (swapon(pr
->dev
, 0))
888 fprintf(stderr
, "failed to swapon %s\n", pr
->dev
);
894 if (stat(argv
[1], &s
) || !S_ISBLK(s
.st_mode
)) {
895 fprintf(stderr
, "%s is not a block device\n", argv
[1]);
898 err
= swapon(argv
[1], 0);
900 fprintf(stderr
, "failed to swapon %s (%d)\n", argv
[1], err
);
908 static int main_swapoff(int argc
, char **argv
)
911 fprintf(stderr
, "Usage: swapoff [-a] [DEVICE]\n\nStop swapping on DEVICE\n\n\t-a Stop swapping on all swap devices\n");
915 if (!strcmp(argv
[1], "-a")) {
916 FILE *fp
= fopen("/proc/swaps", "r");
920 fprintf(stderr
, "failed to open /proc/swaps\n");
923 fgets(line
, sizeof(line
), fp
);
924 while (fgets(line
, sizeof(line
), fp
)) {
925 char *end
= strchr(line
, ' ');
933 fprintf(stderr
, "failed to swapoff %s (%d)\n", line
, err
);
940 if (stat(argv
[1], &s
) || !S_ISBLK(s
.st_mode
)) {
941 fprintf(stderr
, "%s is not a block device\n", argv
[1]);
944 err
= swapoff(argv
[1]);
946 fprintf(stderr
, "fsiled to swapoff %s (%d)\n", argv
[1], err
);
954 int main(int argc
, char **argv
)
956 char *base
= basename(*argv
);
960 if (!strcmp(base
, "swapon"))
961 return main_swapon(argc
, argv
);
963 if (!strcmp(base
, "swapoff"))
964 return main_swapoff(argc
, argv
);
966 if ((argc
> 1) && !strcmp(base
, "block")) {
967 if (!strcmp(argv
[1], "info"))
968 return main_info(argc
, argv
);
970 if (!strcmp(argv
[1], "detect"))
971 return main_detect(argc
, argv
);
973 if (!strcmp(argv
[1], "hotplug"))
974 return main_hotplug(argc
, argv
);
976 if (!strcmp(argv
[1], "extroot"))
977 return main_extroot(argc
, argv
);
979 if (!strcmp(argv
[1], "mount"))
980 return main_mount(argc
, argv
);
982 if (!strcmp(argv
[1], "umount"))
983 return main_umount(argc
, argv
);
986 fprintf(stderr
, "Usage: block <info|mount|umount|detect>\n");