Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create directory for mount target provided in configuration file #1378

Merged
merged 1 commit into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion kernel/enter.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <myst/procfs.h>
#include <myst/pubkey.h>
#include <myst/ramfs.h>
#include <myst/realpath.h>
#include <myst/signal.h>
#include <myst/stack.h>
#include <myst/strings.h>
Expand Down Expand Up @@ -71,6 +72,67 @@ long myst_tcall(long n, long params[6])
return ret;
}

/**
* Create target directory if do not exist
* Then call mount syscall
*/
static int _create_and_mount(
const char* source,
const char* target,
const char* fs_type,
unsigned long mountflags,
const void* data,
bool is_auto)
{
int ret = 0;
struct locals
{
myst_path_t normalized_target;
myst_path_t suffix;
};
struct locals* locals = NULL;
myst_fs_t* parent_fs;

if (!(locals = malloc(sizeof(struct locals))))
ERAISE(-ENOMEM);

ECHECK(myst_realpath(target, &locals->normalized_target));

// target has to be an absolute path
if (locals->normalized_target.buf[0] != '/')
ERAISE(-EINVAL);

ECHECK(myst_mount_resolve(
locals->normalized_target.buf, locals->suffix.buf, &parent_fs));

// check if target exists and is directory
// if not, create target directory
{
struct stat buf;
int errornum = (*parent_fs->fs_stat)(
parent_fs, locals->normalized_target.buf, &buf);

if (errornum == -ENOENT)
// target do not exist, create directory
ECHECK(myst_syscall_mkdir(locals->normalized_target.buf, 0777));
else if (errornum == 0 && !S_ISDIR(buf.st_mode))
// target exists but is not directory
ERAISE(-ENOTDIR);
else
ECHECK(errornum);
}

ret =
myst_syscall_mount(source, target, fs_type, mountflags, data, is_auto);

done:

if (locals)
free(locals);

return ret;
}

static int _process_mount_configuration(myst_mounts_config_t* mounts)
{
size_t i;
Expand All @@ -82,13 +144,14 @@ static int _process_mount_configuration(myst_mounts_config_t* mounts)

for (i = 0; i < mounts->mounts_count; i++)
{
ret = myst_syscall_mount(
ret = _create_and_mount(
mounts->mounts[i].source,
mounts->mounts[i].target,
mounts->mounts[i].fs_type,
0,
NULL,
true);

if (ret != 0)
{
myst_eprintf(
Expand Down
13 changes: 5 additions & 8 deletions tests/mount/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,19 @@ build-extra:
$(MKROOTFS) $(SUBBINDIR)/mount rootfs
$(MYST) excpio rootfs appdir
# Can only mount ramfs that are located INSIDE enclave
$(MYST) mkcpio datafs appdir/ramfs
$(MYST) mkcpio data appdir/ramfs
# Can only mount ext2fs that are located OUTSIDE enclave
$(MYST) mkext2 datafs ext2fs
$(MYST) mkext2 data ext2fs

# Make mounting point in rootfs
mkdir -p appdir/mnt/datafs
mkdir -p appdir/mnt/hostfs
mkdir -p appdir/mnt/ramfs
mkdir -p appdir/mnt/ext2fs
mkdir -p appdir/mnt/

# Create rootfs
sudo chown -R $(USER).$(USER) appdir
$(MYST) mkcpio appdir rootfs

tests-hostfs:
$(RUNTEST) $(MYST_EXEC) $(OPTS) rootfs /bin/mount hostfs --app-config-path config-hostfs.json --mount $(CURDIR)/datafs=/mnt/hostfs
$(RUNTEST) $(MYST_EXEC) $(OPTS) rootfs /bin/mount hostfs --app-config-path config-hostfs.json --mount $(CURDIR)/data=/mnt/hostfs

tests-ramfs:
$(RUNTEST) $(MYST_EXEC) $(OPTS) rootfs /bin/mount ramfs --app-config-path config-ramfs.json --mount /ramfs=/mnt/ramfs
Expand All @@ -58,4 +55,4 @@ tests: build-extra
$(MAKE) tests-ext2fs

gdb:
$(MYST_GDB) --args $(MYST_EXEC) $(OPTS) rootfs /bin/mount ext2fs --app-config-path config-ext2fs.json --mount /ext2fs=/mnt/ext2fs
$(MYST_GDB) --args $(MYST_EXEC) $(OPTS) rootfs /bin/mount ramfs --app-config-path config-ramfs.json --mount /ramfs=/mnt/ramfs
File renamed without changes.
10 changes: 9 additions & 1 deletion tests/mount/mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ int validate_file(const char filename[])
return 0;
}

// test C mount API
int test_mount_and_access()
{
const char source[] = "/ramfs";
const char target[] = "/mnt/datafs";
const char filename[] = "/mnt/datafs/myfile";

if (mount("/ramfs", "/mnt/datafs", "ramfs", 0, NULL) != 0)
// create /mnt/datafs
assert(mkdir(target, 0777) == 0);

if (mount(source, target, "ramfs", 0, NULL) != 0)
{
printf("errno: %d\n", errno);
assert(false);
Expand Down Expand Up @@ -78,8 +84,10 @@ int main(int argc, const char* argv[])
return 1;
}

// Test C mount API
test_mount_and_access();

// Test mount configuration in config.json
if (0 == strcmp(argv[1], "hostfs"))
{
test_access_hostfs();
Expand Down