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 size_t len
= strlen(overlay
);
27 static char line
[256];
33 while (ret
&& fgets(line
, sizeof(line
), fp
))
34 if (len
< sizeof(line
) && !strncmp(line
, overlay
, len
) && line
[len
] == ' ')
43 * Find path of a device mounted to the given point.
48 FILE *fp
= fopen("/proc/mounts", "r");
49 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 char *point
= NULL
, *pos
, *tmp
, *cpoint
, *devname
, *fstype
;
110 unsigned int minor
, major
;
115 rstat
= stat(block
, &s
);
117 while (fgets(line
, sizeof(line
), fp
)) {
118 /* skip first two columns */
119 pos
= strchr(line
, ' ');
123 pos
= strchr(pos
+ 1, ' ');
127 /* extract block device major:minor */
129 pos
= strchr(pos
, ':');
137 pos
= strchr(pos
, ' ');
144 /* skip another column */
145 pos
= strchr(pos
+ 1, ' ');
151 pos
= strchr(pos
, ' ');
158 /* skip another two columns */
159 pos
= strchr(pos
+ 1, ' ');
163 pos
= strchr(pos
+ 1, ' ');
169 pos
= strchr(pos
, ' ');
176 /* get device name */
178 pos
= strchr(pos
, ' ');
185 /* if device name matches */
186 if (!strcmp(block
, devname
)) {
187 if (root_only
&& fs_rootfs_only(fstype
))
190 /* found, return mountpoint */
191 point
= strdup(cpoint
);
195 /* last chance: check if major:minor of block device match */
199 if (!S_ISBLK(s
.st_mode
))
202 if (major
== major(s
.st_rdev
) &&
203 minor
== minor(s
.st_rdev
)) {
204 if (root_only
&& fs_rootfs_only(fstype
))
207 /* found, return mountpoint */
208 point
= strdup(cpoint
);
219 find_filesystem(char *fs
)
221 FILE *fp
= fopen("/proc/filesystems", "r");
222 static char line
[256];
226 ULOG_ERR("opening /proc/filesystems failed: %m\n");
230 while (ret
&& fgets(line
, sizeof(line
), fp
))
231 if (strstr(line
, fs
))