]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/storage/dir.c
storage/dir: cleanup mount code
[mirror_lxc.git] / src / lxc / storage / dir.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <stdint.h>
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "config.h"
11 #include "log.h"
12 #include "macro.h"
13 #include "memory_utils.h"
14 #include "mount_utils.h"
15 #include "storage.h"
16 #include "utils.h"
17
18 lxc_log_define(dir, lxc);
19
20 /*
21 * For a simple directory bind mount, we substitute the old container name and
22 * paths for the new.
23 */
24 int dir_clonepaths(struct lxc_storage *orig, struct lxc_storage *new,
25 const char *oldname, const char *cname, const char *oldpath,
26 const char *lxcpath, int snap, uint64_t newsize,
27 struct lxc_conf *conf)
28 {
29 const char *src_no_prefix;
30 int ret;
31 size_t len;
32
33 if (snap)
34 return log_error_errno(-EINVAL, EINVAL, "Directories cannot be snapshotted");
35
36 if (!orig->dest || !orig->src)
37 return ret_errno(EINVAL);
38
39 len = STRLITERALLEN("dir:") + strlen(lxcpath) + STRLITERALLEN("/") +
40 strlen(cname) + STRLITERALLEN("/rootfs") + 1;
41 new->src = malloc(len);
42 if (!new->src)
43 return ret_errno(ENOMEM);
44
45 ret = snprintf(new->src, len, "dir:%s/%s/rootfs", lxcpath, cname);
46 if (ret < 0 || (size_t)ret >= len)
47 return ret_errno(EIO);
48
49 src_no_prefix = lxc_storage_get_path(new->src, new->type);
50 new->dest = strdup(src_no_prefix);
51 if (!new->dest)
52 return log_error_errno(-ENOMEM, ENOMEM, "Failed to duplicate string \"%s\"", new->src);
53
54 TRACE("Created new path \"%s\" for dir storage driver", new->dest);
55 return 0;
56 }
57
58 int dir_create(struct lxc_storage *bdev, const char *dest, const char *n,
59 struct bdev_specs *specs, const struct lxc_conf *conf)
60 {
61 __do_free char *bdev_src = NULL, *bdev_dest = NULL;
62 int ret;
63 const char *src;
64 size_t len;
65
66 len = STRLITERALLEN("dir:");
67 if (specs && specs->dir)
68 src = specs->dir;
69 else
70 src = dest;
71
72 len += strlen(src) + 1;
73 bdev_src = malloc(len);
74 if (!bdev_src)
75 return ret_errno(ENOMEM);
76
77 ret = snprintf(bdev_src, len, "dir:%s", src);
78 if (ret < 0 || (size_t)ret >= len)
79 return ret_errno(EIO);
80
81 bdev_dest = strdup(dest);
82 if (!bdev_dest)
83 return ret_errno(ENOMEM);
84
85 ret = mkdir_p(dest, 0755);
86 if (ret < 0)
87 return log_error_errno(-errno, errno, "Failed to create directory \"%s\"", dest);
88
89 TRACE("Created directory \"%s\"", dest);
90 bdev->src = move_ptr(bdev_src);
91 bdev->dest = move_ptr(bdev_dest);
92
93 return 0;
94 }
95
96 int dir_destroy(struct lxc_storage *orig)
97 {
98 int ret;
99 const char *src;
100
101 src = lxc_storage_get_path(orig->src, orig->src);
102
103 ret = lxc_rmdir_onedev(src, NULL);
104 if (ret < 0)
105 return log_error_errno(ret, errno, "Failed to delete \"%s\"", src);
106
107 return 0;
108 }
109
110 bool dir_detect(const char *path)
111 {
112 struct stat statbuf;
113 int ret;
114
115 if (!strncmp(path, "dir:", 4))
116 return true;
117
118 ret = stat(path, &statbuf);
119 if (ret == -1 && errno == EPERM)
120 return log_error_errno(false, errno, "dir_detect: failed to look at \"%s\"", path);
121
122 if (ret == 0 && S_ISDIR(statbuf.st_mode))
123 return true;
124
125 return false;
126 }
127
128 int dir_mount(struct lxc_storage *bdev)
129 {
130 struct lxc_rootfs *rootfs = bdev->rootfs;
131 struct lxc_mount_options *mnt_opts = &rootfs->mnt_opts;
132 int ret;
133 const char *source, *target;
134
135 if (!strequal(bdev->type, "dir"))
136 return syserror_set(-EINVAL, "Invalid storage driver");
137
138 if (is_empty_string(bdev->src))
139 return syserror_set(-EINVAL, "Missing rootfs path");
140
141 if (is_empty_string(bdev->dest))
142 return syserror_set(-EINVAL, "Missing target mountpoint");
143
144 if (rootfs->dfd_idmapped >= 0 && !can_use_bind_mounts())
145 return syserror_set(-EOPNOTSUPP, "Idmapped mount requested but kernel doesn't support new mount API");
146
147 source = lxc_storage_get_path(bdev->src, bdev->type);
148 target = bdev->dest;
149
150 if (can_use_bind_mounts()) {
151 __do_close int fd_source = -EBADF, fd_target = -EBADF;
152
153 fd_target = open_at(-EBADF, target, PROTECT_OPATH_DIRECTORY, 0, 0);
154 if (fd_target < 0)
155 return syserror("Failed to open \"%s\"", target);
156
157 if (rootfs->dfd_idmapped >= 0) {
158 ret = move_detached_mount(rootfs->dfd_idmapped, fd_target, "",
159 PROTECT_OPATH_DIRECTORY,
160 PROTECT_LOOKUP_BENEATH);
161 } else {
162 fd_source = open_at(-EBADF, source, PROTECT_OPATH_DIRECTORY, 0, 0);
163 if (fd_source < 0)
164 return syserror("Failed to open \"%s\"", source);
165
166 ret = fd_bind_mount(fd_source, "",
167 PROTECT_OPATH_DIRECTORY,
168 PROTECT_LOOKUP_BENEATH, fd_target,
169 "", PROTECT_OPATH_DIRECTORY,
170 PROTECT_LOOKUP_BENEATH, 0, true);
171 }
172 } else {
173 ret = mount(source, target, "bind", MS_BIND | MS_REC | mnt_opts->mnt_flags | mnt_opts->prop_flags, mnt_opts->data);
174 if (!ret && (mnt_opts->mnt_flags & MS_RDONLY)) {
175 unsigned long mflags;
176
177 mflags = add_required_remount_flags(source, target,
178 MS_BIND |
179 MS_REC |
180 mnt_opts->mnt_flags |
181 MS_REMOUNT);
182
183 ret = mount(source, target, "bind", mflags, mnt_opts->data);
184 if (ret)
185 SYSERROR("Failed to remount \"%s\" on \"%s\" read-only", source, target);
186 else
187 TRACE("Remounted \"%s\" on \"%s\" read-only", source, target);
188 }
189 }
190 if (ret < 0)
191 return syserror_set(ret, "Failed to mount \"%s\" onto \"%s\"", source, target);
192
193 TRACE("Mounted \"%s\" onto \"%s\"", source, target);
194 return 0;
195 }
196
197 int dir_umount(struct lxc_storage *bdev)
198 {
199 if (strcmp(bdev->type, "dir"))
200 return ret_errno(EINVAL);
201
202 if (!bdev->src || !bdev->dest)
203 return ret_errno(EINVAL);
204
205 return umount2(bdev->dest, MNT_DETACH);
206 }