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];
53 while (fgets(line, sizeof(line), fp)) {
54 char *s, *t = strstr(line, " ");
81 * Match filesystem type against a bunch of valid types
83 * jffs2reset may ask if the filesystem type is actually ready for use
84 * with overlayfs before wiping it...
86 static int fs_rootfs_only(char *fstype)
88 if (strncmp(fstype, "ext4", 4) &&
89 strncmp(fstype, "f2fs", 4) &&
90 strncmp(fstype, "jffs2", 5) &&
91 strncmp(fstype, "ubifs", 5)) {
92 ULOG_ERR("block is mounted with wrong fs\n");
99 * Check if a given device is mounted and return its mountpoint
102 find_mount_point(char *block, int root_only)
104 FILE *fp = fopen("/proc/self/mountinfo", "r");
105 static char line[256];
106 int len = strlen(block);
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 (!strncmp(block, devname, len + 1)) {
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: %s\n", strerror(errno));
230 while (ret && fgets(line, sizeof(line), fp))
231 if (strstr(line, fs))