mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-18 12:16:31 +00:00
ospp project (feature) add namespace overlayfs cgroup (#949)
## 开发进展: ## namespace - pid_namespace 基本实现,基于pid_struct等数据结构实现隔离 - mnt_namespace 基本实现,挂载点的隔离通过不同的挂载树来实现 - usernamespace 作为支持性的namespace,目前受限实现全局静态 ## overlayfs - 实现若干个文件系统的叠加,在mount中传入多个路径作为多个fs的mount路径以及最后merge层的fs路径 - copy-up机制的,除最上层外其他层为只读层,满足写时拷贝,需要修改的时候copy到上层修改 - whiteout特殊文件,用于标记在下层需要被删除的文件用来掩盖需要删除的文件 ## cgroups - 目前cgroups还处于框架阶段,之后具体实现具体的内存、CPU等子系统
This commit is contained in:
92
user/apps/test_overlayfs/main.c
Normal file
92
user/apps/test_overlayfs/main.c
Normal file
@ -0,0 +1,92 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mount.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
// #define LOWERDIR "/tmp/overlayfs/lower"
|
||||
// #define UPPERDIR "/tmp/overlayfs/upper"
|
||||
// #define WORKDIR "/tmp/overlayfs/work"
|
||||
// #define MERGEDDIR "/tmp/overlayfs/merged"
|
||||
|
||||
// void create_directories()
|
||||
// {
|
||||
// mkdir(LOWERDIR, 0755);
|
||||
// mkdir(UPPERDIR, 0755);
|
||||
// mkdir(WORKDIR, 0755);
|
||||
// mkdir(MERGEDDIR, 0755);
|
||||
// }
|
||||
#define TMPDIR "/tmp"
|
||||
#define OVERLAYFSDIR "/tmp/overlayfs"
|
||||
#define LOWERDIR "/tmp/overlayfs/lower"
|
||||
#define UPPERDIR "/tmp/overlayfs/upper"
|
||||
#define WORKDIR "/tmp/overlayfs/work"
|
||||
#define MERGEDDIR "/tmp/overlayfs/merged"
|
||||
|
||||
void create_directories()
|
||||
{
|
||||
mkdir(TMPDIR, 0755);
|
||||
mkdir(OVERLAYFSDIR, 0755);
|
||||
mkdir(LOWERDIR, 0755);
|
||||
mkdir(UPPERDIR, 0755);
|
||||
mkdir(WORKDIR, 0755);
|
||||
mkdir(MERGEDDIR, 0755);
|
||||
printf("step1 : success\n");
|
||||
}
|
||||
|
||||
void create_lower_file()
|
||||
{
|
||||
char filepath[256];
|
||||
snprintf(filepath, sizeof(filepath), "%s/lowerfile.txt", LOWERDIR);
|
||||
|
||||
int fd = open(filepath, O_CREAT | O_WRONLY, 0644);
|
||||
if (fd < 0)
|
||||
{
|
||||
perror("Failed to create file in lowerdir");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
write(fd, "This is a lower layer file.\n", 28);
|
||||
close(fd);
|
||||
printf("step2 : success\n");
|
||||
}
|
||||
|
||||
void mount_overlayfs()
|
||||
{
|
||||
char options[1024];
|
||||
snprintf(options, sizeof(options),
|
||||
"lowerdir=%s,upperdir=%s,workdir=%s",
|
||||
LOWERDIR, UPPERDIR, WORKDIR);
|
||||
|
||||
if (mount("overlay", MERGEDDIR, "overlay", 0, options) != 0)
|
||||
{
|
||||
perror("Mount failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("OverlayFS mounted successfully.\n");
|
||||
printf("step3 : success\n");
|
||||
}
|
||||
|
||||
void create_directory_in_merged()
|
||||
{
|
||||
char dirpath[256];
|
||||
snprintf(dirpath, sizeof(dirpath), "%s/newdir", UPPERDIR);
|
||||
|
||||
if (mkdir(dirpath, 0755) != 0)
|
||||
{
|
||||
perror("Failed to create directory in merged dir");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Directory created in merged: %s\n", dirpath);
|
||||
printf("step4 : success\n");
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
create_directories();
|
||||
mount_overlayfs();
|
||||
create_directory_in_merged();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user