2 * Copyright (C) 2014 John Crispin <blogic@openwrt.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2.1
6 * as published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
18 #include <sys/sysmacros.h>
20 #include "libfstools.h"
23 find_overlay_mount(char *overlay
)
25 FILE *fp
= fopen("/proc/mounts", "r");
26 static char line
[256];
32 while (ret
&& fgets(line
, sizeof(line
), fp
))
33 if (!strncmp(line
, overlay
, strlen(overlay
)))
42 * Find path of a device mounted to the given point.
47 FILE *fp
= fopen("/proc/mounts", "r");
48 static char line
[256];
54 while (fgets(line
, sizeof(line
), fp
)) {
55 char *s
, *t
= strstr(line
, " ");
82 * Match filesystem type against a bunch of valid types
84 * jffs2reset may ask if the filesystem type is actually ready for use
85 * with overlayfs before wiping it...
87 static int fs_rootfs_only(char *fstype
)
89 if (strncmp(fstype
, "ext4", 4) &&
90 strncmp(fstype
, "f2fs", 4) &&
91 strncmp(fstype
, "jffs2", 5) &&
92 strncmp(fstype
, "ubifs", 5)) {
93 ULOG_ERR("block is mounted with wrong fs\n");
100 * Check if a given device is mounted and return its mountpoint
103 find_mount_point(char *block
, int root_only
)
105 FILE *fp
= fopen("/proc/self/mountinfo", "r");
106 static char line
[256];
107 int len
= strlen(block
);
108 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
, *fstype
;
111 unsigned int minor
, major
;
116 rstat
= stat(block
, &s
);
118 while (fgets(line
, sizeof(line
), fp
)) {
119 /* skip first two columns */
120 pos
= strchr(line
, ' ');
124 pos
= strchr(pos
+ 1, ' ');
128 /* extract block device major:minor */
130 pos
= strchr(pos
, ':');
138 pos
= strchr(pos
, ' ');
145 /* skip another column */
146 pos
= strchr(pos
+ 1, ' ');
152 pos
= strchr(pos
, ' ');
159 /* skip another two columns */
160 pos
= strchr(pos
+ 1, ' ');
164 pos
= strchr(pos
+ 1, ' ');
170 pos
= strchr(pos
, ' ');
177 /* get device name */
179 pos
= strchr(pos
, ' ');
186 /* if device name matches */
187 if (!strncmp(block
, devname
, len
)) {
188 if (root_only
&& fs_rootfs_only(fstype
))
191 /* found, return mountpoint */
192 point
= strdup(cpoint
);
196 /* last chance: check if major:minor of block device match */
200 if (!S_ISBLK(s
.st_mode
))
203 if (major
== major(s
.st_rdev
) &&
204 minor
== minor(s
.st_rdev
)) {
205 if (root_only
&& fs_rootfs_only(fstype
))
208 /* found, return mountpoint */
209 point
= strdup(cpoint
);
220 find_filesystem(char *fs
)
222 FILE *fp
= fopen("/proc/filesystems", "r");
223 static char line
[256];
227 ULOG_ERR("opening /proc/filesystems failed: %s\n", strerror(errno
));
231 while (ret
&& fgets(line
, sizeof(line
), fp
))
232 if (strstr(line
, fs
))