Skip to content

Commit

Permalink
Merge pull request #123 from alexlarsson/mount-option-tmpdir
Browse files Browse the repository at this point in the history
lib: Add option for specifying the mountpoint for the erofs image
  • Loading branch information
alexlarsson committed Apr 28, 2023
2 parents b61e1a6 + 3e07198 commit a13a89a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
40 changes: 27 additions & 13 deletions libcomposefs/lcfs-mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ static int lcfs_mount_erofs_ovl(struct lcfs_mount_state_s *state,
uint32_t image_flags;
char imagemountbuf[] = "/tmp/.composefs.XXXXXX";
char *imagemount;
bool created_tmpdir = false;
char loopname[PATH_MAX];
int res, errsv;
int lowerdir_alt = 0;
Expand All @@ -506,11 +507,16 @@ static int lcfs_mount_erofs_ovl(struct lcfs_mount_state_s *state,
if (loopfd < 0)
return loopfd;

imagemount = mkdtemp(imagemountbuf);
if (imagemount == NULL) {
errsv = errno;
close(loopfd);
return -errsv;
if (options->image_mountdir) {
imagemount = (char *)options->image_mountdir;
} else {
imagemount = mkdtemp(imagemountbuf);
if (imagemount == NULL) {
errsv = errno;
close(loopfd);
return -errsv;
}
created_tmpdir = true;
}

res = lcfs_mount_erofs(loopname, imagemount, image_flags, state);
Expand Down Expand Up @@ -582,7 +588,9 @@ static int lcfs_mount_erofs_ovl(struct lcfs_mount_state_s *state,
free(overlay_options);

umount2(imagemount, MNT_DETACH);
rmdir(imagemount);
if (created_tmpdir) {
rmdir(imagemount);
}

return res;
}
Expand Down Expand Up @@ -621,12 +629,16 @@ static int lcfs_mount_cfs(struct lcfs_mount_state_s *state,
needs_overlay = options->upperdir != NULL;

if (needs_overlay) {
imagemount = mkdtemp(imagemountbuf);
if (imagemount == NULL) {
res = -errno;
goto fail;
if (options->image_mountdir) {
imagemount = options->image_mountdir;
} else {
imagemount = mkdtemp(imagemountbuf);
if (imagemount == NULL) {
res = -errno;
goto fail;
}
created_tmpdir = true;
}
created_tmpdir = true;
} else {
imagemount = state->mountpoint;
}
Expand Down Expand Up @@ -684,11 +696,13 @@ static int lcfs_mount_cfs(struct lcfs_mount_state_s *state,

fail:

if (created_tmpdir) {
if (needs_overlay) {
if (mounted_cfs) {
umount2(imagemount, MNT_DETACH);
}
rmdir(imagemount);
if (created_tmpdir) {
rmdir(imagemount);
}
}
free(cfsimg);
free(basedir);
Expand Down
1 change: 1 addition & 0 deletions libcomposefs/lcfs-mount.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct lcfs_mount_options_s {
const char *expected_digest;
uint32_t flags;
int idmap_fd; /* userns fd */
const char *image_mountdir; /* Temporary location to mount images if needed */

uint32_t reserved[4];
void *reserved2[4];
Expand Down

0 comments on commit a13a89a

Please sign in to comment.