1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <dirent.h>
4 #include <errno.h>
5 #include <sys/mount.h>
6
7 #define TST_NO_DEFAULT_MAIN
8 #include "tst_test.h"
9 #include "tst_fs.h"
10
11 #define TST_FS_SETUP_OVERLAYFS_MSG "overlayfs is not configured in this kernel"
12 #define TST_FS_SETUP_OVERLAYFS_CONFIG "lowerdir="OVL_LOWER",upperdir="OVL_UPPER",workdir="OVL_WORK
13
create_overlay_dirs(void)14 void create_overlay_dirs(void)
15 {
16 DIR *dir = opendir(OVL_LOWER);
17 if (dir == NULL) {
18 SAFE_MKDIR(OVL_LOWER, 0755);
19 SAFE_MKDIR(OVL_UPPER, 0755);
20 SAFE_MKDIR(OVL_WORK, 0755);
21 SAFE_MKDIR(OVL_MNT, 0755);
22 return;
23 }
24 closedir(dir);
25 }
26
mount_overlay(const char * file,const int lineno,int skip)27 int mount_overlay(const char *file, const int lineno, int skip)
28 {
29 int ret;
30
31 create_overlay_dirs();
32 ret = mount("overlay", OVL_MNT, "overlay", 0,
33 TST_FS_SETUP_OVERLAYFS_CONFIG);
34 if (ret == 0)
35 return 0;
36
37 if (errno == ENODEV) {
38 if (skip) {
39 tst_brk(TCONF, "%s:%d: " TST_FS_SETUP_OVERLAYFS_MSG,
40 file, lineno);
41 } else {
42 tst_res(TINFO, "%s:%d: " TST_FS_SETUP_OVERLAYFS_MSG,
43 file, lineno);
44 }
45 } else {
46 tst_brk(TBROK | TERRNO, "overlayfs mount failed");
47 }
48 return ret;
49 }
50