Skip to content

Commit

Permalink
conf: move all mount options into struct lxc_mount_options
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner committed Apr 25, 2021
1 parent 4e86cad commit 423374e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
1 change: 0 additions & 1 deletion src/lxc/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4065,7 +4065,6 @@ void lxc_conf_free(struct lxc_conf *conf)
free(conf->rootfs.bdev_type);
free(conf->rootfs.options);
free(conf->rootfs.path);
free(conf->rootfs.data);
put_lxc_rootfs(&conf->rootfs, true);
free(conf->logfile);
if (conf->logfd != -1)
Expand Down
21 changes: 18 additions & 3 deletions src/lxc/conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,9 @@ struct lxc_mount_options {
int relative : 1;
char userns_path[PATH_MAX];
int userns_fd;
unsigned long mnt_flags;
unsigned long prop_flags;
char *data;
};

/* Defines a structure to store the rootfs location, the
Expand All @@ -209,8 +212,6 @@ struct lxc_mount_options {
* @buf : static buffer to construct paths
* @bev_type : optional backing store type
* @options : mount options
* @mountflags : the portion of @options that are flags
* @data : the portion of @options that are not flags
* @managed : whether it is managed by LXC
* @dfd_mnt : fd for @mount
* @dfd_dev : fd for /dev of the container
Expand All @@ -230,7 +231,6 @@ struct lxc_rootfs {
char *bdev_type;
char *options;
unsigned long mountflags;
char *data;
bool managed;
struct lxc_mount_options mnt_opts;
struct lxc_storage *storage;
Expand Down Expand Up @@ -585,6 +585,20 @@ static inline bool idmapped_rootfs_mnt(const struct lxc_rootfs *rootfs)
return rootfs->mnt_opts.userns_fd >= 0;
}

static inline void put_lxc_mount_options(struct lxc_mount_options *mnt_opts)
{
mnt_opts->create_dir = 0;
mnt_opts->create_file = 0;
mnt_opts->optional = 0;
mnt_opts->relative = 0;
mnt_opts->userns_path[0] = '\0';
mnt_opts->mnt_flags = 0;
mnt_opts->prop_flags = 0;

close_prot_errno_disarm(mnt_opts->userns_fd);
free_disarm(mnt_opts->data);
}

static inline void put_lxc_rootfs(struct lxc_rootfs *rootfs, bool unpin)
{
if (rootfs) {
Expand All @@ -594,6 +608,7 @@ static inline void put_lxc_rootfs(struct lxc_rootfs *rootfs, bool unpin)
close_prot_errno_disarm(rootfs->mnt_opts.userns_fd);
if (unpin)
close_prot_errno_disarm(rootfs->fd_path_pin);
put_lxc_mount_options(&rootfs->mnt_opts);
storage_put(rootfs->storage);
rootfs->storage = NULL;
}
Expand Down
17 changes: 8 additions & 9 deletions src/lxc/confile.c
Original file line number Diff line number Diff line change
Expand Up @@ -2791,8 +2791,8 @@ static int set_config_rootfs_options(const char *key, const char *value,
struct lxc_conf *lxc_conf, void *data)
{
__do_free char *dup = NULL, *mdata = NULL, *opts = NULL;
unsigned long mflags = 0, pflags = 0;
struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
struct lxc_mount_options *mnt_opts = &rootfs->mnt_opts;
int ret;

clr_config_rootfs_options(key, lxc_conf, data);
Expand All @@ -2803,29 +2803,28 @@ static int set_config_rootfs_options(const char *key, const char *value,
if (!dup)
return -ENOMEM;

ret = parse_lxc_mntopts(&rootfs->mnt_opts, dup);
ret = parse_lxc_mntopts(mnt_opts, dup);
if (ret < 0)
return ret;

ret = parse_mntopts(dup, &mflags, &mdata);
ret = parse_mntopts(dup, &mnt_opts->mnt_flags, &mdata);
if (ret < 0)
return ret_errno(EINVAL);

ret = parse_propagationopts(dup, &pflags);
ret = parse_propagationopts(dup, &mnt_opts->prop_flags);
if (ret < 0)
return ret_errno(EINVAL);

ret = set_config_string_item(&opts, dup);
if (ret < 0)
return ret_errno(ENOMEM);

if (rootfs->mnt_opts.create_dir || rootfs->mnt_opts.create_file ||
rootfs->mnt_opts.optional || rootfs->mnt_opts.relative)
if (mnt_opts->create_dir || mnt_opts->create_file ||
mnt_opts->optional || mnt_opts->relative)
return syserror_set(-EINVAL, "Invalid LXC specifc mount option for rootfs mount");

rootfs->mountflags = mflags | pflags;
mnt_opts->data = move_ptr(mdata);
rootfs->options = move_ptr(opts);
rootfs->data = move_ptr(mdata);

return 0;
}
Expand Down Expand Up @@ -5017,7 +5016,7 @@ static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
void *data)
{
free_disarm(c->rootfs.options);
free_disarm(c->rootfs.data);
put_lxc_mount_options(&c->rootfs.mnt_opts);

return 0;
}
Expand Down
25 changes: 9 additions & 16 deletions src/lxc/storage/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ bool dir_detect(const char *path)

int dir_mount(struct lxc_storage *bdev)
{
struct lxc_mount_options *mnt_opts = &bdev->rootfs->mnt_opts;
__do_free char *mntdata = NULL;
unsigned long mflags = 0, mntflags = 0, pflags = 0;
unsigned long mflags = 0;
int ret;
const char *src;

Expand All @@ -138,14 +139,6 @@ int dir_mount(struct lxc_storage *bdev)
if (!bdev->src || !bdev->dest)
return -22;

ret = parse_mntopts(bdev->mntopts, &mntflags, &mntdata);
if (ret < 0)
return log_error_errno(ret, errno, "Failed to parse mount options \"%s\"", bdev->mntopts);

ret = parse_propagationopts(bdev->mntopts, &pflags);
if (ret < 0)
return log_error_errno(-EINVAL, EINVAL, "Failed to parse mount propagation options \"%s\"", bdev->mntopts);

src = lxc_storage_get_path(bdev->src, bdev->type);

if (can_use_bind_mounts()) {
Expand All @@ -163,28 +156,28 @@ int dir_mount(struct lxc_storage *bdev)
PROTECT_LOOKUP_BENEATH, fd_target, "",
PROTECT_OPATH_DIRECTORY,
PROTECT_LOOKUP_BENEATH, 0,
bdev->rootfs->mnt_opts.userns_fd, true);
mnt_opts->userns_fd, true);
if (ret < 0)
return syserror("Failed to mount \"%s\" onto \"%s\"", src, bdev->dest);
} else {
ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mntflags | pflags, mntdata);
ret = mount(src, bdev->dest, "bind", MS_BIND | MS_REC | mnt_opts->mnt_flags | mnt_opts->prop_flags, mntdata);
if (ret < 0)
return log_error_errno(-errno, errno, "Failed to mount \"%s\" on \"%s\"", src, bdev->dest);

if (ret == 0 && (mntflags & MS_RDONLY)) {
mflags = add_required_remount_flags(src, bdev->dest, MS_BIND | MS_REC | mntflags | pflags | MS_REMOUNT);
if (ret == 0 && (mnt_opts->mnt_flags & MS_RDONLY)) {
mflags = add_required_remount_flags(src, bdev->dest, MS_BIND | MS_REC | mnt_opts->mnt_flags | mnt_opts->mnt_flags | MS_REMOUNT);

ret = mount(src, bdev->dest, "bind", mflags, mntdata);
if (ret < 0)
return log_error_errno(-errno, errno, "Failed to remount \"%s\" on \"%s\" read-only with options \"%s\", mount flags \"%lu\", and propagation flags \"%lu\"",
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, pflags);
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, mnt_opts->mnt_flags);
else
DEBUG("Remounted \"%s\" on \"%s\" read-only with options \"%s\", mount flags \"%lu\", and propagation flags \"%lu\"",
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, pflags);
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, mnt_opts->mnt_flags);
}

TRACE("Mounted \"%s\" on \"%s\" with options \"%s\", mount flags \"%lu\", and propagation flags \"%lu\"",
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, pflags);
src ? src : "(none)", bdev->dest ? bdev->dest : "(none)", mntdata, mflags, mnt_opts->mnt_flags);
}

TRACE("Mounted \"%s\" onto \"%s\"", src, bdev->dest);
Expand Down

0 comments on commit 423374e

Please sign in to comment.