1*f5b20388Scodeironman #include <stdio.h>
2*f5b20388Scodeironman #include <stdlib.h>
3*f5b20388Scodeironman #include <sys/mount.h>
4*f5b20388Scodeironman #include <sys/stat.h>
5*f5b20388Scodeironman #include <unistd.h>
6*f5b20388Scodeironman #include <fcntl.h>
7*f5b20388Scodeironman #include <string.h>
8*f5b20388Scodeironman #include <errno.h>
9*f5b20388Scodeironman
10*f5b20388Scodeironman // #define LOWERDIR "/tmp/overlayfs/lower"
11*f5b20388Scodeironman // #define UPPERDIR "/tmp/overlayfs/upper"
12*f5b20388Scodeironman // #define WORKDIR "/tmp/overlayfs/work"
13*f5b20388Scodeironman // #define MERGEDDIR "/tmp/overlayfs/merged"
14*f5b20388Scodeironman
15*f5b20388Scodeironman // void create_directories()
16*f5b20388Scodeironman // {
17*f5b20388Scodeironman // mkdir(LOWERDIR, 0755);
18*f5b20388Scodeironman // mkdir(UPPERDIR, 0755);
19*f5b20388Scodeironman // mkdir(WORKDIR, 0755);
20*f5b20388Scodeironman // mkdir(MERGEDDIR, 0755);
21*f5b20388Scodeironman // }
22*f5b20388Scodeironman #define TMPDIR "/tmp"
23*f5b20388Scodeironman #define OVERLAYFSDIR "/tmp/overlayfs"
24*f5b20388Scodeironman #define LOWERDIR "/tmp/overlayfs/lower"
25*f5b20388Scodeironman #define UPPERDIR "/tmp/overlayfs/upper"
26*f5b20388Scodeironman #define WORKDIR "/tmp/overlayfs/work"
27*f5b20388Scodeironman #define MERGEDDIR "/tmp/overlayfs/merged"
28*f5b20388Scodeironman
create_directories()29*f5b20388Scodeironman void create_directories()
30*f5b20388Scodeironman {
31*f5b20388Scodeironman mkdir(TMPDIR, 0755);
32*f5b20388Scodeironman mkdir(OVERLAYFSDIR, 0755);
33*f5b20388Scodeironman mkdir(LOWERDIR, 0755);
34*f5b20388Scodeironman mkdir(UPPERDIR, 0755);
35*f5b20388Scodeironman mkdir(WORKDIR, 0755);
36*f5b20388Scodeironman mkdir(MERGEDDIR, 0755);
37*f5b20388Scodeironman printf("step1 : success\n");
38*f5b20388Scodeironman }
39*f5b20388Scodeironman
create_lower_file()40*f5b20388Scodeironman void create_lower_file()
41*f5b20388Scodeironman {
42*f5b20388Scodeironman char filepath[256];
43*f5b20388Scodeironman snprintf(filepath, sizeof(filepath), "%s/lowerfile.txt", LOWERDIR);
44*f5b20388Scodeironman
45*f5b20388Scodeironman int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
46*f5b20388Scodeironman if (fd < 0)
47*f5b20388Scodeironman {
48*f5b20388Scodeironman perror("Failed to create file in lowerdir");
49*f5b20388Scodeironman exit(EXIT_FAILURE);
50*f5b20388Scodeironman }
51*f5b20388Scodeironman write(fd, "This is a lower layer file.\n", 28);
52*f5b20388Scodeironman close(fd);
53*f5b20388Scodeironman printf("step2 : success\n");
54*f5b20388Scodeironman }
55*f5b20388Scodeironman
mount_overlayfs()56*f5b20388Scodeironman void mount_overlayfs()
57*f5b20388Scodeironman {
58*f5b20388Scodeironman char options[1024];
59*f5b20388Scodeironman snprintf(options, sizeof(options),
60*f5b20388Scodeironman "lowerdir=%s,upperdir=%s,workdir=%s",
61*f5b20388Scodeironman LOWERDIR, UPPERDIR, WORKDIR);
62*f5b20388Scodeironman
63*f5b20388Scodeironman if (mount("overlay", MERGEDDIR, "overlay", 0, options) != 0)
64*f5b20388Scodeironman {
65*f5b20388Scodeironman perror("Mount failed");
66*f5b20388Scodeironman exit(EXIT_FAILURE);
67*f5b20388Scodeironman }
68*f5b20388Scodeironman printf("OverlayFS mounted successfully.\n");
69*f5b20388Scodeironman printf("step3 : success\n");
70*f5b20388Scodeironman }
71*f5b20388Scodeironman
create_directory_in_merged()72*f5b20388Scodeironman void create_directory_in_merged()
73*f5b20388Scodeironman {
74*f5b20388Scodeironman char dirpath[256];
75*f5b20388Scodeironman snprintf(dirpath, sizeof(dirpath), "%s/newdir", UPPERDIR);
76*f5b20388Scodeironman
77*f5b20388Scodeironman if (mkdir(dirpath, 0755) != 0)
78*f5b20388Scodeironman {
79*f5b20388Scodeironman perror("Failed to create directory in merged dir");
80*f5b20388Scodeironman exit(EXIT_FAILURE);
81*f5b20388Scodeironman }
82*f5b20388Scodeironman printf("Directory created in merged: %s\n", dirpath);
83*f5b20388Scodeironman printf("step4 : success\n");
84*f5b20388Scodeironman }
85*f5b20388Scodeironman
main()86*f5b20388Scodeironman int main()
87*f5b20388Scodeironman {
88*f5b20388Scodeironman create_directories();
89*f5b20388Scodeironman mount_overlayfs();
90*f5b20388Scodeironman create_directory_in_merged();
91*f5b20388Scodeironman return 0;
92*f5b20388Scodeironman }