Skip to content

Commit

Permalink
Create directory for mount target provided in configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
Zijie Wu authored and asvrada committed Jul 7, 2022
1 parent 7f6b600 commit d6fbd7e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
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

0 comments on commit d6fbd7e

Please sign in to comment.