]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
cd61c3bf | 2 | |
a8fbdf54 | 3 | #include <errno.h> |
ebd93cb6 | 4 | #include <fcntl.h> |
e08f94ac LP |
5 | #include <linux/fs.h> |
6 | #include <linux/loop.h> | |
a8fbdf54 TA |
7 | #include <stdio.h> |
8 | #include <stdlib.h> | |
e306723e | 9 | #include <sys/file.h> |
204f52e3 | 10 | #include <sys/ioctl.h> |
a8fbdf54 TA |
11 | #include <sys/stat.h> |
12 | #include <unistd.h> | |
546dbec5 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
cd61c3bf | 15 | #include "btrfs-util.h" |
c8b3094d | 16 | #include "chattr-util.h" |
ebd93cb6 | 17 | #include "copy.h" |
a0956174 | 18 | #include "dirent-util.h" |
57f1b61b | 19 | #include "discover-image.h" |
c7664c07 | 20 | #include "dissect-image.h" |
686d13b9 | 21 | #include "env-file.h" |
b6e953f2 | 22 | #include "env-util.h" |
3ffd4af2 | 23 | #include "fd-util.h" |
f4f15635 | 24 | #include "fs-util.h" |
a8fbdf54 | 25 | #include "hashmap.h" |
e2054217 | 26 | #include "hostname-setup.h" |
c7664c07 | 27 | #include "id128-util.h" |
a8fbdf54 TA |
28 | #include "lockfile-util.h" |
29 | #include "log.h" | |
c7664c07 | 30 | #include "loop-util.h" |
546dbec5 | 31 | #include "macro.h" |
30535c16 | 32 | #include "mkdir.h" |
d8b4d14d | 33 | #include "nulstr-util.h" |
d58ad743 | 34 | #include "os-util.h" |
8e0b6570 | 35 | #include "path-util.h" |
c6878637 | 36 | #include "rm-rf.h" |
8b43440b | 37 | #include "string-table.h" |
07630cea | 38 | #include "string-util.h" |
8e0b6570 | 39 | #include "strv.h" |
a8fbdf54 | 40 | #include "time-util.h" |
8e0b6570 | 41 | #include "utf8.h" |
89a5a90c | 42 | #include "xattr-util.h" |
cd61c3bf | 43 | |
5ef46e5f | 44 | static const char* const image_search_path[_IMAGE_CLASS_MAX] = { |
9bca4ae4 LP |
45 | [IMAGE_MACHINE] = "/etc/machines\0" /* only place symlinks here */ |
46 | "/run/machines\0" /* and here too */ | |
47 | "/var/lib/machines\0" /* the main place for images */ | |
48 | "/var/lib/container\0" /* legacy */ | |
49 | "/usr/local/lib/machines\0" | |
50 | "/usr/lib/machines\0", | |
5ef46e5f | 51 | |
9bca4ae4 LP |
52 | [IMAGE_PORTABLE] = "/etc/portables\0" /* only place symlinks here */ |
53 | "/run/portables\0" /* and here too */ | |
54 | "/var/lib/portables\0" /* the main place for images */ | |
55 | "/usr/local/lib/portables\0" | |
56 | "/usr/lib/portables\0", | |
57 | ||
58 | [IMAGE_EXTENSION] = "/etc/extensions\0" /* only place symlinks here */ | |
59 | "/run/extensions\0" /* and here too */ | |
60 | "/var/lib/extensions\0" /* the main place for images */ | |
61 | "/usr/local/lib/extensions\0" | |
62 | "/usr/lib/extensions\0", | |
5ef46e5f | 63 | }; |
c2ce6a3d | 64 | |
8301aa0b YW |
65 | static Image *image_free(Image *i) { |
66 | assert(i); | |
9614bb06 | 67 | |
cd61c3bf LP |
68 | free(i->name); |
69 | free(i->path); | |
c7664c07 LP |
70 | |
71 | free(i->hostname); | |
72 | strv_free(i->machine_info); | |
73 | strv_free(i->os_release); | |
bcf94222 | 74 | strv_free(i->extension_release); |
c7664c07 | 75 | |
6b430fdb | 76 | return mfree(i); |
cd61c3bf LP |
77 | } |
78 | ||
8301aa0b | 79 | DEFINE_TRIVIAL_REF_UNREF_FUNC(Image, image, image_free); |
b07ec5a1 YW |
80 | DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(image_hash_ops, char, string_hash_func, string_compare_func, |
81 | Image, image_unref); | |
9614bb06 | 82 | |
8e0b6570 LP |
83 | static char **image_settings_path(Image *image) { |
84 | _cleanup_strv_free_ char **l = NULL; | |
8e0b6570 LP |
85 | const char *fn, *s; |
86 | unsigned i = 0; | |
87 | ||
88 | assert(image); | |
89 | ||
90 | l = new0(char*, 4); | |
91 | if (!l) | |
92 | return NULL; | |
93 | ||
94 | fn = strjoina(image->name, ".nspawn"); | |
95 | ||
b910cc72 LP |
96 | FOREACH_STRING(s, "/etc/systemd/nspawn", "/run/systemd/nspawn") { |
97 | l[i] = path_join(s, fn); | |
8e0b6570 LP |
98 | if (!l[i]) |
99 | return NULL; | |
100 | ||
101 | i++; | |
102 | } | |
103 | ||
104 | l[i] = file_in_same_dir(image->path, fn); | |
105 | if (!l[i]) | |
106 | return NULL; | |
107 | ||
ae2a15bc | 108 | return TAKE_PTR(l); |
8e0b6570 LP |
109 | } |
110 | ||
bafbac4e LP |
111 | static char *image_roothash_path(Image *image) { |
112 | const char *fn; | |
113 | ||
114 | assert(image); | |
115 | ||
116 | fn = strjoina(image->name, ".roothash"); | |
117 | ||
118 | return file_in_same_dir(image->path, fn); | |
119 | } | |
120 | ||
c2ce6a3d | 121 | static int image_new( |
cd61c3bf | 122 | ImageType t, |
5fc7f358 | 123 | const char *pretty, |
cd61c3bf | 124 | const char *path, |
5fc7f358 | 125 | const char *filename, |
cd61c3bf | 126 | bool read_only, |
10f9c755 | 127 | usec_t crtime, |
cd61c3bf | 128 | usec_t mtime, |
c2ce6a3d | 129 | Image **ret) { |
cd61c3bf LP |
130 | |
131 | _cleanup_(image_unrefp) Image *i = NULL; | |
cd61c3bf | 132 | |
cd61c3bf LP |
133 | assert(t >= 0); |
134 | assert(t < _IMAGE_TYPE_MAX); | |
5fc7f358 LP |
135 | assert(pretty); |
136 | assert(filename); | |
c2ce6a3d | 137 | assert(ret); |
cd61c3bf | 138 | |
c2108701 | 139 | i = new(Image, 1); |
cd61c3bf LP |
140 | if (!i) |
141 | return -ENOMEM; | |
142 | ||
c2108701 LP |
143 | *i = (Image) { |
144 | .n_ref = 1, | |
145 | .type = t, | |
146 | .read_only = read_only, | |
147 | .crtime = crtime, | |
148 | .mtime = mtime, | |
149 | .usage = UINT64_MAX, | |
150 | .usage_exclusive = UINT64_MAX, | |
151 | .limit = UINT64_MAX, | |
152 | .limit_exclusive = UINT64_MAX, | |
153 | }; | |
cd61c3bf | 154 | |
5fc7f358 | 155 | i->name = strdup(pretty); |
cd61c3bf LP |
156 | if (!i->name) |
157 | return -ENOMEM; | |
158 | ||
657ee2d8 | 159 | i->path = path_join(path, filename); |
5fc7f358 LP |
160 | if (!i->path) |
161 | return -ENOMEM; | |
162 | ||
858d36c1 | 163 | path_simplify(i->path, false); |
cd61c3bf | 164 | |
1cc6c93a | 165 | *ret = TAKE_PTR(i); |
c2ce6a3d | 166 | |
cd61c3bf LP |
167 | return 0; |
168 | } | |
169 | ||
4756c94e LP |
170 | static int extract_pretty(const char *path, const char *suffix, char **ret) { |
171 | _cleanup_free_ char *name = NULL; | |
172 | const char *p; | |
173 | size_t n; | |
174 | ||
175 | assert(path); | |
176 | assert(ret); | |
177 | ||
178 | p = last_path_component(path); | |
179 | n = strcspn(p, "/"); | |
180 | ||
181 | name = strndup(p, n); | |
182 | if (!name) | |
183 | return -ENOMEM; | |
184 | ||
185 | if (suffix) { | |
186 | char *e; | |
187 | ||
188 | e = endswith(name, suffix); | |
189 | if (!e) | |
190 | return -EINVAL; | |
191 | ||
192 | *e = 0; | |
193 | } | |
194 | ||
195 | if (!image_name_is_valid(name)) | |
196 | return -EINVAL; | |
197 | ||
198 | *ret = TAKE_PTR(name); | |
199 | return 0; | |
200 | } | |
201 | ||
5fc7f358 LP |
202 | static int image_make( |
203 | const char *pretty, | |
204 | int dfd, | |
205 | const char *path, | |
206 | const char *filename, | |
3a6ce860 | 207 | const struct stat *st, |
5fc7f358 LP |
208 | Image **ret) { |
209 | ||
2570578d | 210 | _cleanup_free_ char *pretty_buffer = NULL, *parent = NULL; |
3a6ce860 | 211 | struct stat stbuf; |
5fc7f358 | 212 | bool read_only; |
cd61c3bf LP |
213 | int r; |
214 | ||
3a6ce860 | 215 | assert(dfd >= 0 || dfd == AT_FDCWD); |
bcb846f3 | 216 | assert(path || dfd == AT_FDCWD); |
5fc7f358 | 217 | assert(filename); |
cd61c3bf | 218 | |
eb38edce | 219 | /* We explicitly *do* follow symlinks here, since we want to allow symlinking trees, raw files and block |
3a6ce860 LP |
220 | * devices into /var/lib/machines/, and treat them normally. |
221 | * | |
222 | * This function returns -ENOENT if we can't find the image after all, and -EMEDIUMTYPE if it's not a file we | |
223 | * recognize. */ | |
cd61c3bf | 224 | |
3a6ce860 LP |
225 | if (!st) { |
226 | if (fstatat(dfd, filename, &stbuf, 0) < 0) | |
227 | return -errno; | |
228 | ||
229 | st = &stbuf; | |
230 | } | |
cd61c3bf | 231 | |
2570578d LP |
232 | if (!path) { |
233 | if (dfd == AT_FDCWD) | |
234 | (void) safe_getcwd(&parent); | |
235 | else | |
236 | (void) fd_get_path(dfd, &parent); | |
237 | } | |
bcb846f3 | 238 | |
5fc7f358 LP |
239 | read_only = |
240 | (path && path_startswith(path, "/usr")) || | |
08ff5529 | 241 | (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS); |
86e339c8 | 242 | |
3a6ce860 | 243 | if (S_ISDIR(st->st_mode)) { |
01b72568 LP |
244 | _cleanup_close_ int fd = -1; |
245 | unsigned file_attr = 0; | |
02dcf215 | 246 | usec_t crtime = 0; |
cd61c3bf | 247 | |
c2ce6a3d | 248 | if (!ret) |
3a6ce860 | 249 | return 0; |
cd61c3bf | 250 | |
4756c94e LP |
251 | if (!pretty) { |
252 | r = extract_pretty(filename, NULL, &pretty_buffer); | |
253 | if (r < 0) | |
254 | return r; | |
255 | ||
256 | pretty = pretty_buffer; | |
257 | } | |
5fc7f358 | 258 | |
01b72568 LP |
259 | fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY); |
260 | if (fd < 0) | |
261 | return -errno; | |
262 | ||
674b04ff | 263 | if (btrfs_might_be_subvol(st)) { |
cd61c3bf | 264 | |
21222ea5 LP |
265 | r = btrfs_is_filesystem(fd); |
266 | if (r < 0) | |
267 | return r; | |
268 | if (r) { | |
10f9c755 | 269 | BtrfsSubvolInfo info; |
cd61c3bf | 270 | |
c2ce6a3d | 271 | /* It's a btrfs subvolume */ |
cd61c3bf | 272 | |
5bcd08db | 273 | r = btrfs_subvol_get_info_fd(fd, 0, &info); |
10f9c755 LP |
274 | if (r < 0) |
275 | return r; | |
c2ce6a3d LP |
276 | |
277 | r = image_new(IMAGE_SUBVOLUME, | |
5fc7f358 | 278 | pretty, |
c2ce6a3d | 279 | path, |
5fc7f358 LP |
280 | filename, |
281 | info.read_only || read_only, | |
10f9c755 | 282 | info.otime, |
c2ce6a3d | 283 | 0, |
c2ce6a3d LP |
284 | ret); |
285 | if (r < 0) | |
286 | return r; | |
287 | ||
5bcd08db LP |
288 | if (btrfs_quota_scan_ongoing(fd) == 0) { |
289 | BtrfsQuotaInfo quota; | |
b6b18498 | 290 | |
5bcd08db LP |
291 | r = btrfs_subvol_get_subtree_quota_fd(fd, 0, "a); |
292 | if (r >= 0) { | |
293 | (*ret)->usage = quota.referenced; | |
294 | (*ret)->usage_exclusive = quota.exclusive; | |
295 | ||
296 | (*ret)->limit = quota.referenced_max; | |
297 | (*ret)->limit_exclusive = quota.exclusive_max; | |
298 | } | |
b6b18498 LP |
299 | } |
300 | ||
3a6ce860 | 301 | return 0; |
cd61c3bf | 302 | } |
c2ce6a3d | 303 | } |
cd61c3bf | 304 | |
02dcf215 LP |
305 | /* Get directory creation time (not available everywhere, but that's OK */ |
306 | (void) fd_getcrtime(dfd, &crtime); | |
307 | ||
308 | /* If the IMMUTABLE bit is set, we consider the directory read-only. Since the ioctl is not | |
01b72568 LP |
309 | * supported everywhere we ignore failures. */ |
310 | (void) read_attr_fd(fd, &file_attr); | |
cd61c3bf | 311 | |
01b72568 | 312 | /* It's just a normal directory. */ |
c2ce6a3d | 313 | r = image_new(IMAGE_DIRECTORY, |
5fc7f358 | 314 | pretty, |
c2ce6a3d | 315 | path, |
5fc7f358 | 316 | filename, |
01b72568 | 317 | read_only || (file_attr & FS_IMMUTABLE_FL), |
02dcf215 LP |
318 | crtime, |
319 | 0, /* we don't use mtime of stat() here, since it's not the time of last change of the tree, but only of the top-level dir */ | |
c2ce6a3d LP |
320 | ret); |
321 | if (r < 0) | |
322 | return r; | |
cd61c3bf | 323 | |
3a6ce860 | 324 | return 0; |
cd61c3bf | 325 | |
3a6ce860 | 326 | } else if (S_ISREG(st->st_mode) && endswith(filename, ".raw")) { |
10f9c755 | 327 | usec_t crtime = 0; |
cd61c3bf | 328 | |
aceac2f0 | 329 | /* It's a RAW disk image */ |
cd61c3bf | 330 | |
c2ce6a3d | 331 | if (!ret) |
3a6ce860 | 332 | return 0; |
cd61c3bf | 333 | |
4756c94e | 334 | (void) fd_getcrtime_at(dfd, filename, &crtime, 0); |
10f9c755 | 335 | |
4756c94e LP |
336 | if (!pretty) { |
337 | r = extract_pretty(filename, ".raw", &pretty_buffer); | |
338 | if (r < 0) | |
339 | return r; | |
340 | ||
341 | pretty = pretty_buffer; | |
342 | } | |
10f9c755 | 343 | |
aceac2f0 | 344 | r = image_new(IMAGE_RAW, |
5fc7f358 | 345 | pretty, |
c2ce6a3d | 346 | path, |
5fc7f358 | 347 | filename, |
3a6ce860 | 348 | !(st->st_mode & 0222) || read_only, |
10f9c755 | 349 | crtime, |
3a6ce860 | 350 | timespec_load(&st->st_mtim), |
c2ce6a3d LP |
351 | ret); |
352 | if (r < 0) | |
353 | return r; | |
cd61c3bf | 354 | |
3a6ce860 LP |
355 | (*ret)->usage = (*ret)->usage_exclusive = st->st_blocks * 512; |
356 | (*ret)->limit = (*ret)->limit_exclusive = st->st_size; | |
b6b18498 | 357 | |
3a6ce860 | 358 | return 0; |
eb38edce | 359 | |
3a6ce860 | 360 | } else if (S_ISBLK(st->st_mode)) { |
eb38edce LP |
361 | _cleanup_close_ int block_fd = -1; |
362 | uint64_t size = UINT64_MAX; | |
363 | ||
364 | /* A block device */ | |
365 | ||
366 | if (!ret) | |
3a6ce860 | 367 | return 0; |
eb38edce | 368 | |
4756c94e LP |
369 | if (!pretty) { |
370 | r = extract_pretty(filename, NULL, &pretty_buffer); | |
371 | if (r < 0) | |
372 | return r; | |
373 | ||
374 | pretty = pretty_buffer; | |
375 | } | |
eb38edce LP |
376 | |
377 | block_fd = openat(dfd, filename, O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY); | |
378 | if (block_fd < 0) | |
2570578d | 379 | log_debug_errno(errno, "Failed to open block device %s/%s, ignoring: %m", path ?: strnull(parent), filename); |
eb38edce | 380 | else { |
3a6ce860 LP |
381 | /* Refresh stat data after opening the node */ |
382 | if (fstat(block_fd, &stbuf) < 0) | |
eb38edce | 383 | return -errno; |
3a6ce860 LP |
384 | st = &stbuf; |
385 | ||
386 | if (!S_ISBLK(st->st_mode)) /* Verify that what we opened is actually what we think it is */ | |
eb38edce LP |
387 | return -ENOTTY; |
388 | ||
389 | if (!read_only) { | |
390 | int state = 0; | |
391 | ||
392 | if (ioctl(block_fd, BLKROGET, &state) < 0) | |
2570578d | 393 | log_debug_errno(errno, "Failed to issue BLKROGET on device %s/%s, ignoring: %m", path ?: strnull(parent), filename); |
eb38edce LP |
394 | else if (state) |
395 | read_only = true; | |
396 | } | |
397 | ||
398 | if (ioctl(block_fd, BLKGETSIZE64, &size) < 0) | |
2570578d | 399 | log_debug_errno(errno, "Failed to issue BLKGETSIZE64 on device %s/%s, ignoring: %m", path ?: strnull(parent), filename); |
eb38edce LP |
400 | |
401 | block_fd = safe_close(block_fd); | |
402 | } | |
403 | ||
404 | r = image_new(IMAGE_BLOCK, | |
405 | pretty, | |
406 | path, | |
407 | filename, | |
3a6ce860 | 408 | !(st->st_mode & 0222) || read_only, |
eb38edce LP |
409 | 0, |
410 | 0, | |
411 | ret); | |
412 | if (r < 0) | |
413 | return r; | |
414 | ||
ed0cb346 | 415 | if (!IN_SET(size, 0, UINT64_MAX)) |
eb38edce LP |
416 | (*ret)->usage = (*ret)->usage_exclusive = (*ret)->limit = (*ret)->limit_exclusive = size; |
417 | ||
3a6ce860 | 418 | return 0; |
c2ce6a3d | 419 | } |
cd61c3bf | 420 | |
3a6ce860 | 421 | return -EMEDIUMTYPE; |
c2ce6a3d | 422 | } |
cd61c3bf | 423 | |
d577d4a4 LP |
424 | int image_find(ImageClass class, |
425 | const char *name, | |
426 | const char *root, | |
427 | Image **ret) { | |
428 | ||
c2ce6a3d LP |
429 | const char *path; |
430 | int r; | |
cd61c3bf | 431 | |
5ef46e5f LP |
432 | assert(class >= 0); |
433 | assert(class < _IMAGE_CLASS_MAX); | |
c2ce6a3d | 434 | assert(name); |
cd61c3bf | 435 | |
c2ce6a3d LP |
436 | /* There are no images with invalid names */ |
437 | if (!image_name_is_valid(name)) | |
3a6ce860 | 438 | return -ENOENT; |
cd61c3bf | 439 | |
5ef46e5f | 440 | NULSTR_FOREACH(path, image_search_path[class]) { |
d577d4a4 | 441 | _cleanup_free_ char *resolved = NULL; |
c2ce6a3d | 442 | _cleanup_closedir_ DIR *d = NULL; |
3a6ce860 | 443 | struct stat st; |
d577d4a4 | 444 | int flags; |
cd61c3bf | 445 | |
d577d4a4 LP |
446 | r = chase_symlinks_and_opendir(path, root, CHASE_PREFIX_ROOT, &resolved, &d); |
447 | if (r == -ENOENT) | |
448 | continue; | |
449 | if (r < 0) | |
450 | return r; | |
cd61c3bf | 451 | |
d577d4a4 LP |
452 | /* As mentioned above, we follow symlinks on this fstatat(), because we want to permit people |
453 | * to symlink block devices into the search path. (For now, we disable that when operating | |
454 | * relative to some root directory.) */ | |
455 | flags = root ? AT_SYMLINK_NOFOLLOW : 0; | |
456 | if (fstatat(dirfd(d), name, &st, flags) < 0) { | |
aceac2f0 | 457 | _cleanup_free_ char *raw = NULL; |
5fc7f358 | 458 | |
3a6ce860 LP |
459 | if (errno != ENOENT) |
460 | return -errno; | |
461 | ||
b910cc72 | 462 | raw = strjoin(name, ".raw"); |
aceac2f0 | 463 | if (!raw) |
5fc7f358 LP |
464 | return -ENOMEM; |
465 | ||
d577d4a4 | 466 | if (fstatat(dirfd(d), raw, &st, flags) < 0) { |
3a6ce860 LP |
467 | if (errno == ENOENT) |
468 | continue; | |
469 | ||
470 | return -errno; | |
471 | } | |
472 | ||
473 | if (!S_ISREG(st.st_mode)) | |
5fc7f358 | 474 | continue; |
3a6ce860 | 475 | |
d577d4a4 | 476 | r = image_make(name, dirfd(d), resolved, raw, &st, ret); |
3a6ce860 LP |
477 | |
478 | } else { | |
479 | if (!S_ISDIR(st.st_mode) && !S_ISBLK(st.st_mode)) | |
480 | continue; | |
481 | ||
d577d4a4 | 482 | r = image_make(name, dirfd(d), resolved, name, &st, ret); |
5fc7f358 | 483 | } |
3a6ce860 LP |
484 | if (IN_SET(r, -ENOENT, -EMEDIUMTYPE)) |
485 | continue; | |
c2ce6a3d LP |
486 | if (r < 0) |
487 | return r; | |
cd61c3bf | 488 | |
cf604fd4 LP |
489 | if (ret) |
490 | (*ret)->discoverable = true; | |
491 | ||
c2ce6a3d LP |
492 | return 1; |
493 | } | |
494 | ||
cf604fd4 | 495 | if (class == IMAGE_MACHINE && streq(name, ".host")) { |
d577d4a4 | 496 | r = image_make(".host", AT_FDCWD, NULL, empty_to_root(root), NULL, ret); |
cf604fd4 LP |
497 | if (r < 0) |
498 | return r; | |
499 | ||
500 | if (ret) | |
501 | (*ret)->discoverable = true; | |
502 | ||
503 | return r; | |
504 | } | |
5fc7f358 | 505 | |
3a6ce860 | 506 | return -ENOENT; |
c2ce6a3d LP |
507 | }; |
508 | ||
2ddf182b | 509 | int image_from_path(const char *path, Image **ret) { |
cf604fd4 LP |
510 | |
511 | /* Note that we don't set the 'discoverable' field of the returned object, because we don't check here whether | |
512 | * the image is in the image search path. And if it is we don't know if the path we used is actually not | |
3fe91079 | 513 | * overridden by another, different image earlier in the search path */ |
cf604fd4 | 514 | |
2ddf182b LP |
515 | if (path_equal(path, "/")) |
516 | return image_make(".host", AT_FDCWD, NULL, "/", NULL, ret); | |
517 | ||
518 | return image_make(NULL, AT_FDCWD, NULL, path, NULL, ret); | |
519 | } | |
520 | ||
d577d4a4 | 521 | int image_find_harder(ImageClass class, const char *name_or_path, const char *root, Image **ret) { |
2ddf182b | 522 | if (image_name_is_valid(name_or_path)) |
d577d4a4 | 523 | return image_find(class, name_or_path, root, ret); |
2ddf182b LP |
524 | |
525 | return image_from_path(name_or_path, ret); | |
526 | } | |
527 | ||
d577d4a4 LP |
528 | int image_discover( |
529 | ImageClass class, | |
530 | const char *root, | |
531 | Hashmap *h) { | |
532 | ||
c2ce6a3d LP |
533 | const char *path; |
534 | int r; | |
535 | ||
5ef46e5f LP |
536 | assert(class >= 0); |
537 | assert(class < _IMAGE_CLASS_MAX); | |
c2ce6a3d LP |
538 | assert(h); |
539 | ||
5ef46e5f | 540 | NULSTR_FOREACH(path, image_search_path[class]) { |
d577d4a4 | 541 | _cleanup_free_ char *resolved = NULL; |
c2ce6a3d LP |
542 | _cleanup_closedir_ DIR *d = NULL; |
543 | struct dirent *de; | |
544 | ||
d577d4a4 LP |
545 | r = chase_symlinks_and_opendir(path, root, CHASE_PREFIX_ROOT, &resolved, &d); |
546 | if (r == -ENOENT) | |
547 | continue; | |
548 | if (r < 0) | |
549 | return r; | |
c2ce6a3d LP |
550 | |
551 | FOREACH_DIRENT_ALL(de, d, return -errno) { | |
552 | _cleanup_(image_unrefp) Image *image = NULL; | |
1bf36bf9 | 553 | _cleanup_free_ char *truncated = NULL; |
3a6ce860 LP |
554 | const char *pretty; |
555 | struct stat st; | |
d577d4a4 | 556 | int flags; |
c2ce6a3d | 557 | |
1bf36bf9 | 558 | if (dot_or_dot_dot(de->d_name)) |
c2ce6a3d LP |
559 | continue; |
560 | ||
d577d4a4 LP |
561 | /* As mentioned above, we follow symlinks on this fstatat(), because we want to |
562 | * permit people to symlink block devices into the search path. */ | |
563 | flags = root ? AT_SYMLINK_NOFOLLOW : 0; | |
564 | if (fstatat(dirfd(d), de->d_name, &st, flags) < 0) { | |
3a6ce860 LP |
565 | if (errno == ENOENT) |
566 | continue; | |
567 | ||
568 | return -errno; | |
569 | } | |
570 | ||
571 | if (S_ISREG(st.st_mode)) { | |
572 | const char *e; | |
573 | ||
574 | e = endswith(de->d_name, ".raw"); | |
575 | if (!e) | |
576 | continue; | |
577 | ||
1bf36bf9 LP |
578 | truncated = strndup(de->d_name, e - de->d_name); |
579 | if (!truncated) | |
580 | return -ENOMEM; | |
581 | ||
582 | pretty = truncated; | |
3a6ce860 | 583 | } else if (S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) |
1bf36bf9 | 584 | pretty = de->d_name; |
3a6ce860 LP |
585 | else |
586 | continue; | |
1bf36bf9 LP |
587 | |
588 | if (!image_name_is_valid(pretty)) | |
589 | continue; | |
590 | ||
591 | if (hashmap_contains(h, pretty)) | |
c2ce6a3d LP |
592 | continue; |
593 | ||
d577d4a4 | 594 | r = image_make(pretty, dirfd(d), resolved, de->d_name, &st, &image); |
3a6ce860 | 595 | if (IN_SET(r, -ENOENT, -EMEDIUMTYPE)) |
c2ce6a3d LP |
596 | continue; |
597 | if (r < 0) | |
598 | return r; | |
599 | ||
cf604fd4 LP |
600 | image->discoverable = true; |
601 | ||
c2ce6a3d LP |
602 | r = hashmap_put(h, image->name, image); |
603 | if (r < 0) | |
604 | return r; | |
605 | ||
606 | image = NULL; | |
cd61c3bf LP |
607 | } |
608 | } | |
609 | ||
5ef46e5f | 610 | if (class == IMAGE_MACHINE && !hashmap_contains(h, ".host")) { |
5fc7f358 LP |
611 | _cleanup_(image_unrefp) Image *image = NULL; |
612 | ||
d577d4a4 | 613 | r = image_make(".host", AT_FDCWD, NULL, empty_to_root("/"), NULL, &image); |
5fc7f358 LP |
614 | if (r < 0) |
615 | return r; | |
616 | ||
cf604fd4 LP |
617 | image->discoverable = true; |
618 | ||
5fc7f358 LP |
619 | r = hashmap_put(h, image->name, image); |
620 | if (r < 0) | |
621 | return r; | |
622 | ||
623 | image = NULL; | |
5fc7f358 LP |
624 | } |
625 | ||
cd61c3bf LP |
626 | return 0; |
627 | } | |
628 | ||
08682124 | 629 | int image_remove(Image *i) { |
8e766630 | 630 | _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; |
8e0b6570 | 631 | _cleanup_strv_free_ char **settings = NULL; |
bafbac4e | 632 | _cleanup_free_ char *roothash = NULL; |
8e0b6570 | 633 | char **j; |
30535c16 LP |
634 | int r; |
635 | ||
08682124 LP |
636 | assert(i); |
637 | ||
d94c2b06 | 638 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
08682124 LP |
639 | return -EROFS; |
640 | ||
8e0b6570 LP |
641 | settings = image_settings_path(i); |
642 | if (!settings) | |
643 | return -ENOMEM; | |
644 | ||
bafbac4e LP |
645 | roothash = image_roothash_path(i); |
646 | if (!roothash) | |
647 | return -ENOMEM; | |
648 | ||
30535c16 LP |
649 | /* Make sure we don't interfere with a running nspawn */ |
650 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
651 | if (r < 0) | |
652 | return r; | |
653 | ||
ebd93cb6 LP |
654 | switch (i->type) { |
655 | ||
656 | case IMAGE_SUBVOLUME: | |
9fb0b9c7 LP |
657 | |
658 | /* Let's unlink first, maybe it is a symlink? If that works we are happy. Otherwise, let's get out the | |
659 | * big guns */ | |
660 | if (unlink(i->path) < 0) { | |
661 | r = btrfs_subvol_remove(i->path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); | |
662 | if (r < 0) | |
663 | return r; | |
664 | } | |
665 | ||
8e0b6570 | 666 | break; |
ebd93cb6 LP |
667 | |
668 | case IMAGE_DIRECTORY: | |
01b72568 | 669 | /* Allow deletion of read-only directories */ |
db9a4254 | 670 | (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL, NULL); |
8e0b6570 LP |
671 | r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
672 | if (r < 0) | |
673 | return r; | |
674 | ||
675 | break; | |
01b72568 | 676 | |
eb38edce LP |
677 | case IMAGE_BLOCK: |
678 | ||
679 | /* If this is inside of /dev, then it's a real block device, hence let's not touch the device node | |
680 | * itself (but let's remove the stuff stored alongside it). If it's anywhere else, let's try to unlink | |
681 | * the thing (it's most likely a symlink after all). */ | |
682 | ||
683 | if (path_startswith(i->path, "/dev")) | |
684 | break; | |
685 | ||
4831981d | 686 | _fallthrough_; |
aceac2f0 | 687 | case IMAGE_RAW: |
41d1ed05 LP |
688 | if (unlink(i->path) < 0) |
689 | return -errno; | |
8e0b6570 | 690 | break; |
ebd93cb6 LP |
691 | |
692 | default: | |
15411c0c | 693 | return -EOPNOTSUPP; |
ebd93cb6 | 694 | } |
8e0b6570 LP |
695 | |
696 | STRV_FOREACH(j, settings) { | |
697 | if (unlink(*j) < 0 && errno != ENOENT) | |
698 | log_debug_errno(errno, "Failed to unlink %s, ignoring: %m", *j); | |
699 | } | |
700 | ||
bafbac4e LP |
701 | if (unlink(roothash) < 0 && errno != ENOENT) |
702 | log_debug_errno(errno, "Failed to unlink %s, ignoring: %m", roothash); | |
703 | ||
8e0b6570 LP |
704 | return 0; |
705 | } | |
706 | ||
bafbac4e | 707 | static int rename_auxiliary_file(const char *path, const char *new_name, const char *suffix) { |
8e0b6570 LP |
708 | _cleanup_free_ char *rs = NULL; |
709 | const char *fn; | |
710 | ||
bafbac4e | 711 | fn = strjoina(new_name, suffix); |
8e0b6570 LP |
712 | |
713 | rs = file_in_same_dir(path, fn); | |
714 | if (!rs) | |
715 | return -ENOMEM; | |
716 | ||
717 | return rename_noreplace(AT_FDCWD, path, AT_FDCWD, rs); | |
ebd93cb6 LP |
718 | } |
719 | ||
720 | int image_rename(Image *i, const char *new_name) { | |
8e766630 | 721 | _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT; |
bafbac4e | 722 | _cleanup_free_ char *new_path = NULL, *nn = NULL, *roothash = NULL; |
8e0b6570 | 723 | _cleanup_strv_free_ char **settings = NULL; |
01b72568 | 724 | unsigned file_attr = 0; |
8e0b6570 | 725 | char **j; |
ebd93cb6 LP |
726 | int r; |
727 | ||
728 | assert(i); | |
729 | ||
730 | if (!image_name_is_valid(new_name)) | |
731 | return -EINVAL; | |
732 | ||
d94c2b06 | 733 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
ebd93cb6 LP |
734 | return -EROFS; |
735 | ||
8e0b6570 LP |
736 | settings = image_settings_path(i); |
737 | if (!settings) | |
738 | return -ENOMEM; | |
739 | ||
bafbac4e LP |
740 | roothash = image_roothash_path(i); |
741 | if (!roothash) | |
742 | return -ENOMEM; | |
743 | ||
30535c16 LP |
744 | /* Make sure we don't interfere with a running nspawn */ |
745 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
746 | if (r < 0) | |
747 | return r; | |
748 | ||
749 | /* Make sure nobody takes the new name, between the time we | |
750 | * checked it is currently unused in all search paths, and the | |
f8e2f4d6 | 751 | * time we take possession of it */ |
30535c16 LP |
752 | r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock); |
753 | if (r < 0) | |
754 | return r; | |
755 | ||
d577d4a4 | 756 | r = image_find(IMAGE_MACHINE, new_name, NULL, NULL); |
3a6ce860 | 757 | if (r >= 0) |
ebd93cb6 | 758 | return -EEXIST; |
3a6ce860 LP |
759 | if (r != -ENOENT) |
760 | return r; | |
ebd93cb6 LP |
761 | |
762 | switch (i->type) { | |
763 | ||
ebd93cb6 | 764 | case IMAGE_DIRECTORY: |
01b72568 LP |
765 | /* Turn of the immutable bit while we rename the image, so that we can rename it */ |
766 | (void) read_attr_path(i->path, &file_attr); | |
767 | ||
768 | if (file_attr & FS_IMMUTABLE_FL) | |
db9a4254 | 769 | (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL, NULL); |
01b72568 | 770 | |
4831981d | 771 | _fallthrough_; |
01b72568 | 772 | case IMAGE_SUBVOLUME: |
ebd93cb6 LP |
773 | new_path = file_in_same_dir(i->path, new_name); |
774 | break; | |
775 | ||
eb38edce LP |
776 | case IMAGE_BLOCK: |
777 | ||
778 | /* Refuse renaming raw block devices in /dev, the names are picked by udev after all. */ | |
779 | if (path_startswith(i->path, "/dev")) | |
780 | return -EROFS; | |
781 | ||
782 | new_path = file_in_same_dir(i->path, new_name); | |
783 | break; | |
784 | ||
aceac2f0 | 785 | case IMAGE_RAW: { |
ebd93cb6 LP |
786 | const char *fn; |
787 | ||
63c372cb | 788 | fn = strjoina(new_name, ".raw"); |
ebd93cb6 LP |
789 | new_path = file_in_same_dir(i->path, fn); |
790 | break; | |
791 | } | |
792 | ||
793 | default: | |
15411c0c | 794 | return -EOPNOTSUPP; |
ebd93cb6 LP |
795 | } |
796 | ||
797 | if (!new_path) | |
798 | return -ENOMEM; | |
799 | ||
800 | nn = strdup(new_name); | |
801 | if (!nn) | |
802 | return -ENOMEM; | |
803 | ||
f85ef957 AC |
804 | r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path); |
805 | if (r < 0) | |
806 | return r; | |
ebd93cb6 | 807 | |
01b72568 LP |
808 | /* Restore the immutable bit, if it was set before */ |
809 | if (file_attr & FS_IMMUTABLE_FL) | |
db9a4254 | 810 | (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL, NULL); |
01b72568 | 811 | |
f9ecfd3b DL |
812 | free_and_replace(i->path, new_path); |
813 | free_and_replace(i->name, nn); | |
ebd93cb6 | 814 | |
8e0b6570 | 815 | STRV_FOREACH(j, settings) { |
bafbac4e | 816 | r = rename_auxiliary_file(*j, new_name, ".nspawn"); |
8e0b6570 LP |
817 | if (r < 0 && r != -ENOENT) |
818 | log_debug_errno(r, "Failed to rename settings file %s, ignoring: %m", *j); | |
819 | } | |
820 | ||
bafbac4e LP |
821 | r = rename_auxiliary_file(roothash, new_name, ".roothash"); |
822 | if (r < 0 && r != -ENOENT) | |
823 | log_debug_errno(r, "Failed to rename roothash file %s, ignoring: %m", roothash); | |
824 | ||
ebd93cb6 LP |
825 | return 0; |
826 | } | |
827 | ||
bafbac4e | 828 | static int clone_auxiliary_file(const char *path, const char *new_name, const char *suffix) { |
8e0b6570 LP |
829 | _cleanup_free_ char *rs = NULL; |
830 | const char *fn; | |
831 | ||
bafbac4e | 832 | fn = strjoina(new_name, suffix); |
8e0b6570 LP |
833 | |
834 | rs = file_in_same_dir(path, fn); | |
835 | if (!rs) | |
836 | return -ENOMEM; | |
837 | ||
8a016c74 | 838 | return copy_file_atomic(path, rs, 0664, 0, 0, COPY_REFLINK); |
8e0b6570 LP |
839 | } |
840 | ||
ebd93cb6 | 841 | int image_clone(Image *i, const char *new_name, bool read_only) { |
8e766630 | 842 | _cleanup_(release_lock_file) LockFile name_lock = LOCK_FILE_INIT; |
8e0b6570 | 843 | _cleanup_strv_free_ char **settings = NULL; |
bafbac4e | 844 | _cleanup_free_ char *roothash = NULL; |
ebd93cb6 | 845 | const char *new_path; |
8e0b6570 | 846 | char **j; |
ebd93cb6 LP |
847 | int r; |
848 | ||
849 | assert(i); | |
850 | ||
851 | if (!image_name_is_valid(new_name)) | |
852 | return -EINVAL; | |
853 | ||
8e0b6570 LP |
854 | settings = image_settings_path(i); |
855 | if (!settings) | |
856 | return -ENOMEM; | |
857 | ||
bafbac4e LP |
858 | roothash = image_roothash_path(i); |
859 | if (!roothash) | |
860 | return -ENOMEM; | |
861 | ||
30535c16 LP |
862 | /* Make sure nobody takes the new name, between the time we |
863 | * checked it is currently unused in all search paths, and the | |
f8e2f4d6 | 864 | * time we take possession of it */ |
30535c16 LP |
865 | r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock); |
866 | if (r < 0) | |
867 | return r; | |
868 | ||
d577d4a4 | 869 | r = image_find(IMAGE_MACHINE, new_name, NULL, NULL); |
3a6ce860 | 870 | if (r >= 0) |
ebd93cb6 | 871 | return -EEXIST; |
3a6ce860 LP |
872 | if (r != -ENOENT) |
873 | return r; | |
ebd93cb6 LP |
874 | |
875 | switch (i->type) { | |
876 | ||
877 | case IMAGE_SUBVOLUME: | |
878 | case IMAGE_DIRECTORY: | |
9a50e3ca | 879 | /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain |
13e785f7 | 880 | * directory. */ |
9a50e3ca | 881 | |
63c372cb | 882 | new_path = strjoina("/var/lib/machines/", new_name); |
ebd93cb6 | 883 | |
17cbb288 LP |
884 | r = btrfs_subvol_snapshot(i->path, new_path, |
885 | (read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) | | |
886 | BTRFS_SNAPSHOT_FALLBACK_COPY | | |
887 | BTRFS_SNAPSHOT_FALLBACK_DIRECTORY | | |
888 | BTRFS_SNAPSHOT_FALLBACK_IMMUTABLE | | |
889 | BTRFS_SNAPSHOT_RECURSIVE | | |
890 | BTRFS_SNAPSHOT_QUOTA); | |
891 | if (r >= 0) | |
9a50e3ca | 892 | /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */ |
8120ee28 | 893 | (void) btrfs_subvol_auto_qgroup(new_path, 0, true); |
5bcd08db | 894 | |
ebd93cb6 LP |
895 | break; |
896 | ||
aceac2f0 | 897 | case IMAGE_RAW: |
63c372cb | 898 | new_path = strjoina("/var/lib/machines/", new_name, ".raw"); |
ebd93cb6 | 899 | |
8a016c74 | 900 | r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, FS_NOCOW_FL, FS_NOCOW_FL, COPY_REFLINK|COPY_CRTIME); |
ebd93cb6 LP |
901 | break; |
902 | ||
eb38edce | 903 | case IMAGE_BLOCK: |
ebd93cb6 | 904 | default: |
15411c0c | 905 | return -EOPNOTSUPP; |
ebd93cb6 LP |
906 | } |
907 | ||
908 | if (r < 0) | |
909 | return r; | |
910 | ||
8e0b6570 | 911 | STRV_FOREACH(j, settings) { |
bafbac4e | 912 | r = clone_auxiliary_file(*j, new_name, ".nspawn"); |
8e0b6570 LP |
913 | if (r < 0 && r != -ENOENT) |
914 | log_debug_errno(r, "Failed to clone settings %s, ignoring: %m", *j); | |
915 | } | |
916 | ||
bafbac4e LP |
917 | r = clone_auxiliary_file(roothash, new_name, ".roothash"); |
918 | if (r < 0 && r != -ENOENT) | |
919 | log_debug_errno(r, "Failed to clone root hash file %s, ignoring: %m", roothash); | |
920 | ||
ebd93cb6 LP |
921 | return 0; |
922 | } | |
923 | ||
924 | int image_read_only(Image *i, bool b) { | |
8e766630 | 925 | _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; |
ebd93cb6 | 926 | int r; |
c7664c07 | 927 | |
ebd93cb6 LP |
928 | assert(i); |
929 | ||
d94c2b06 | 930 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
ebd93cb6 LP |
931 | return -EROFS; |
932 | ||
30535c16 LP |
933 | /* Make sure we don't interfere with a running nspawn */ |
934 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
935 | if (r < 0) | |
936 | return r; | |
937 | ||
ebd93cb6 LP |
938 | switch (i->type) { |
939 | ||
940 | case IMAGE_SUBVOLUME: | |
5bcd08db LP |
941 | |
942 | /* Note that we set the flag only on the top-level | |
943 | * subvolume of the image. */ | |
944 | ||
ebd93cb6 LP |
945 | r = btrfs_subvol_set_read_only(i->path, b); |
946 | if (r < 0) | |
947 | return r; | |
01b72568 LP |
948 | |
949 | break; | |
950 | ||
951 | case IMAGE_DIRECTORY: | |
952 | /* For simple directory trees we cannot use the access | |
953 | mode of the top-level directory, since it has an | |
954 | effect on the container itself. However, we can | |
955 | use the "immutable" flag, to at least make the | |
956 | top-level directory read-only. It's not as good as | |
957 | a read-only subvolume, but at least something, and | |
13e785f7 | 958 | we can read the value back. */ |
01b72568 | 959 | |
db9a4254 | 960 | r = chattr_path(i->path, b ? FS_IMMUTABLE_FL : 0, FS_IMMUTABLE_FL, NULL); |
01b72568 LP |
961 | if (r < 0) |
962 | return r; | |
963 | ||
ebd93cb6 LP |
964 | break; |
965 | ||
aceac2f0 | 966 | case IMAGE_RAW: { |
ebd93cb6 LP |
967 | struct stat st; |
968 | ||
969 | if (stat(i->path, &st) < 0) | |
970 | return -errno; | |
971 | ||
972 | if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0) | |
973 | return -errno; | |
f2068bcc LP |
974 | |
975 | /* If the images is now read-only, it's a good time to | |
976 | * defrag it, given that no write patterns will | |
977 | * fragment it again. */ | |
978 | if (b) | |
979 | (void) btrfs_defrag(i->path); | |
ebd93cb6 LP |
980 | break; |
981 | } | |
982 | ||
eb38edce LP |
983 | case IMAGE_BLOCK: { |
984 | _cleanup_close_ int fd = -1; | |
985 | struct stat st; | |
986 | int state = b; | |
987 | ||
988 | fd = open(i->path, O_CLOEXEC|O_RDONLY|O_NONBLOCK|O_NOCTTY); | |
989 | if (fd < 0) | |
990 | return -errno; | |
991 | ||
992 | if (fstat(fd, &st) < 0) | |
993 | return -errno; | |
994 | if (!S_ISBLK(st.st_mode)) | |
995 | return -ENOTTY; | |
996 | ||
997 | if (ioctl(fd, BLKROSET, &state) < 0) | |
998 | return -errno; | |
999 | ||
1000 | break; | |
1001 | } | |
1002 | ||
ebd93cb6 | 1003 | default: |
15411c0c | 1004 | return -EOPNOTSUPP; |
ebd93cb6 LP |
1005 | } |
1006 | ||
1007 | return 0; | |
08682124 LP |
1008 | } |
1009 | ||
30535c16 LP |
1010 | int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) { |
1011 | _cleanup_free_ char *p = NULL; | |
1012 | LockFile t = LOCK_FILE_INIT; | |
1013 | struct stat st; | |
f25bed67 | 1014 | bool exclusive; |
30535c16 LP |
1015 | int r; |
1016 | ||
1017 | assert(path); | |
1018 | assert(global); | |
1019 | assert(local); | |
1020 | ||
f25bed67 LP |
1021 | /* Locks an image path. This actually creates two locks: one "local" one, next to the image path |
1022 | * itself, which might be shared via NFS. And another "global" one, in /run, that uses the | |
1023 | * device/inode number. This has the benefit that we can even lock a tree that is a mount point, | |
1024 | * correctly. */ | |
30535c16 | 1025 | |
30535c16 LP |
1026 | if (!path_is_absolute(path)) |
1027 | return -EINVAL; | |
1028 | ||
f25bed67 LP |
1029 | switch (operation & (LOCK_SH|LOCK_EX)) { |
1030 | case LOCK_SH: | |
1031 | exclusive = false; | |
1032 | break; | |
1033 | case LOCK_EX: | |
1034 | exclusive = true; | |
1035 | break; | |
1036 | default: | |
1037 | return -EINVAL; | |
1038 | } | |
1039 | ||
b6e953f2 LP |
1040 | if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) { |
1041 | *local = *global = (LockFile) LOCK_FILE_INIT; | |
1042 | return 0; | |
1043 | } | |
1044 | ||
f25bed67 LP |
1045 | /* Prohibit taking exclusive locks on the host image. We can't allow this, since we ourselves are |
1046 | * running off it after all, and we don't want any images to manipulate the host image. We make an | |
1047 | * exception for shared locks however: we allow those (and make them NOPs since there's no point in | |
1048 | * taking them if there can't be exclusive locks). Strictly speaking these are questionable as well, | |
1049 | * since it means changes made to the host might propagate to the container as they happen (and a | |
1050 | * shared lock kinda suggests that no changes happen at all while it is in place), but it's too | |
1051 | * useful not to allow read-only containers off the host root, hence let's support this, and trust | |
1052 | * the user to do the right thing with this. */ | |
1053 | if (path_equal(path, "/")) { | |
1054 | if (exclusive) | |
1055 | return -EBUSY; | |
1056 | ||
1057 | *local = *global = (LockFile) LOCK_FILE_INIT; | |
1058 | return 0; | |
1059 | } | |
b6e953f2 | 1060 | |
30535c16 | 1061 | if (stat(path, &st) >= 0) { |
eb38edce LP |
1062 | if (S_ISBLK(st.st_mode)) |
1063 | r = asprintf(&p, "/run/systemd/nspawn/locks/block-%u:%u", major(st.st_rdev), minor(st.st_rdev)); | |
1064 | else if (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode)) | |
1065 | r = asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino); | |
1066 | else | |
1067 | return -ENOTTY; | |
eb38edce | 1068 | if (r < 0) |
30535c16 LP |
1069 | return -ENOMEM; |
1070 | } | |
1071 | ||
f25bed67 LP |
1072 | /* For block devices we don't need the "local" lock, as the major/minor lock above should be |
1073 | * sufficient, since block devices are host local anyway. */ | |
1074 | if (!path_startswith(path, "/dev/")) { | |
eb38edce | 1075 | r = make_lock_file_for(path, operation, &t); |
8be17c9b | 1076 | if (r < 0) { |
f25bed67 | 1077 | if (!exclusive && r == -EROFS) |
771b7ead | 1078 | log_debug_errno(r, "Failed to create shared lock for '%s', ignoring: %m", path); |
8be17c9b LT |
1079 | else |
1080 | return r; | |
1081 | } | |
eb38edce | 1082 | } |
30535c16 LP |
1083 | |
1084 | if (p) { | |
6e5dcce4 | 1085 | (void) mkdir_p("/run/systemd/nspawn/locks", 0700); |
30535c16 LP |
1086 | |
1087 | r = make_lock_file(p, operation, global); | |
1088 | if (r < 0) { | |
1089 | release_lock_file(&t); | |
1090 | return r; | |
1091 | } | |
546dbec5 LP |
1092 | } else |
1093 | *global = (LockFile) LOCK_FILE_INIT; | |
30535c16 LP |
1094 | |
1095 | *local = t; | |
1096 | return 0; | |
1097 | } | |
1098 | ||
cb81cd80 | 1099 | int image_set_limit(Image *i, uint64_t referenced_max) { |
d6ce17c7 LP |
1100 | assert(i); |
1101 | ||
d94c2b06 | 1102 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
d6ce17c7 LP |
1103 | return -EROFS; |
1104 | ||
1105 | if (i->type != IMAGE_SUBVOLUME) | |
15411c0c | 1106 | return -EOPNOTSUPP; |
d6ce17c7 | 1107 | |
5bcd08db LP |
1108 | /* We set the quota both for the subvolume as well as for the |
1109 | * subtree. The latter is mostly for historical reasons, since | |
1110 | * we didn't use to have a concept of subtree quota, and hence | |
1111 | * only modified the subvolume quota. */ | |
1112 | ||
1113 | (void) btrfs_qgroup_set_limit(i->path, 0, referenced_max); | |
1114 | (void) btrfs_subvol_auto_qgroup(i->path, 0, true); | |
1115 | return btrfs_subvol_set_subtree_quota_limit(i->path, 0, referenced_max); | |
d6ce17c7 LP |
1116 | } |
1117 | ||
c7664c07 | 1118 | int image_read_metadata(Image *i) { |
8e766630 | 1119 | _cleanup_(release_lock_file) LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; |
c7664c07 LP |
1120 | int r; |
1121 | ||
1122 | assert(i); | |
1123 | ||
1124 | r = image_path_lock(i->path, LOCK_SH|LOCK_NB, &global_lock, &local_lock); | |
1125 | if (r < 0) | |
1126 | return r; | |
1127 | ||
1128 | switch (i->type) { | |
1129 | ||
1130 | case IMAGE_SUBVOLUME: | |
1131 | case IMAGE_DIRECTORY: { | |
bcf94222 | 1132 | _cleanup_strv_free_ char **machine_info = NULL, **os_release = NULL, **extension_release = NULL; |
c7664c07 LP |
1133 | sd_id128_t machine_id = SD_ID128_NULL; |
1134 | _cleanup_free_ char *hostname = NULL; | |
1135 | _cleanup_free_ char *path = NULL; | |
1136 | ||
a5648b80 | 1137 | r = chase_symlinks("/etc/hostname", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL); |
c7664c07 LP |
1138 | if (r < 0 && r != -ENOENT) |
1139 | log_debug_errno(r, "Failed to chase /etc/hostname in image %s: %m", i->name); | |
1140 | else if (r >= 0) { | |
1141 | r = read_etc_hostname(path, &hostname); | |
1142 | if (r < 0) | |
1143 | log_debug_errno(errno, "Failed to read /etc/hostname of image %s: %m", i->name); | |
1144 | } | |
1145 | ||
1146 | path = mfree(path); | |
1147 | ||
a5648b80 | 1148 | r = chase_symlinks("/etc/machine-id", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL); |
c7664c07 LP |
1149 | if (r < 0 && r != -ENOENT) |
1150 | log_debug_errno(r, "Failed to chase /etc/machine-id in image %s: %m", i->name); | |
1151 | else if (r >= 0) { | |
1152 | _cleanup_close_ int fd = -1; | |
1153 | ||
1154 | fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY); | |
1155 | if (fd < 0) | |
1156 | log_debug_errno(errno, "Failed to open %s: %m", path); | |
1157 | else { | |
1158 | r = id128_read_fd(fd, ID128_PLAIN, &machine_id); | |
1159 | if (r < 0) | |
1160 | log_debug_errno(r, "Image %s contains invalid machine ID.", i->name); | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | path = mfree(path); | |
1165 | ||
a5648b80 | 1166 | r = chase_symlinks("/etc/machine-info", i->path, CHASE_PREFIX_ROOT|CHASE_TRAIL_SLASH, &path, NULL); |
c7664c07 LP |
1167 | if (r < 0 && r != -ENOENT) |
1168 | log_debug_errno(r, "Failed to chase /etc/machine-info in image %s: %m", i->name); | |
1169 | else if (r >= 0) { | |
aa8fbc74 | 1170 | r = load_env_file_pairs(NULL, path, &machine_info); |
c7664c07 LP |
1171 | if (r < 0) |
1172 | log_debug_errno(r, "Failed to parse machine-info data of %s: %m", i->name); | |
1173 | } | |
1174 | ||
d58ad743 LP |
1175 | r = load_os_release_pairs(i->path, &os_release); |
1176 | if (r < 0) | |
1177 | log_debug_errno(r, "Failed to read os-release in image, ignoring: %m"); | |
c7664c07 | 1178 | |
bcf94222 LB |
1179 | r = load_extension_release_pairs(i->path, i->name, &extension_release); |
1180 | if (r < 0) | |
1181 | log_debug_errno(r, "Failed to read extension-release in image, ignoring: %m"); | |
1182 | ||
c7664c07 LP |
1183 | free_and_replace(i->hostname, hostname); |
1184 | i->machine_id = machine_id; | |
1185 | strv_free_and_replace(i->machine_info, machine_info); | |
1186 | strv_free_and_replace(i->os_release, os_release); | |
bcf94222 | 1187 | strv_free_and_replace(i->extension_release, extension_release); |
c7664c07 LP |
1188 | |
1189 | break; | |
1190 | } | |
1191 | ||
1192 | case IMAGE_RAW: | |
1193 | case IMAGE_BLOCK: { | |
1194 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; | |
1195 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; | |
1196 | ||
e08f94ac | 1197 | r = loop_device_make_by_path(i->path, O_RDONLY, LO_FLAGS_PARTSCAN, &d); |
c7664c07 LP |
1198 | if (r < 0) |
1199 | return r; | |
1200 | ||
d04faa4e LP |
1201 | r = dissect_image( |
1202 | d->fd, | |
1203 | NULL, NULL, | |
1204 | DISSECT_IMAGE_REQUIRE_ROOT| | |
1205 | DISSECT_IMAGE_RELAX_VAR_CHECK| | |
1206 | DISSECT_IMAGE_USR_NO_ROOT, &m); | |
c7664c07 LP |
1207 | if (r < 0) |
1208 | return r; | |
1209 | ||
1210 | r = dissected_image_acquire_metadata(m); | |
1211 | if (r < 0) | |
1212 | return r; | |
1213 | ||
1214 | free_and_replace(i->hostname, m->hostname); | |
1215 | i->machine_id = m->machine_id; | |
1216 | strv_free_and_replace(i->machine_info, m->machine_info); | |
1217 | strv_free_and_replace(i->os_release, m->os_release); | |
bcf94222 | 1218 | strv_free_and_replace(i->extension_release, m->extension_release); |
c7664c07 LP |
1219 | |
1220 | break; | |
1221 | } | |
1222 | ||
1223 | default: | |
1224 | return -EOPNOTSUPP; | |
1225 | } | |
1226 | ||
1227 | i->metadata_valid = true; | |
1228 | ||
1229 | return 0; | |
1230 | } | |
1231 | ||
30535c16 | 1232 | int image_name_lock(const char *name, int operation, LockFile *ret) { |
99d97afc LP |
1233 | const char *p; |
1234 | ||
30535c16 LP |
1235 | assert(name); |
1236 | assert(ret); | |
1237 | ||
1238 | /* Locks an image name, regardless of the precise path used. */ | |
1239 | ||
99d97afc LP |
1240 | if (streq(name, ".host")) |
1241 | return -EBUSY; | |
1242 | ||
30535c16 LP |
1243 | if (!image_name_is_valid(name)) |
1244 | return -EINVAL; | |
1245 | ||
b6e953f2 LP |
1246 | if (getenv_bool("SYSTEMD_NSPAWN_LOCK") == 0) { |
1247 | *ret = (LockFile) LOCK_FILE_INIT; | |
1248 | return 0; | |
1249 | } | |
1250 | ||
6e5dcce4 | 1251 | (void) mkdir_p("/run/systemd/nspawn/locks", 0700); |
99d97afc LP |
1252 | |
1253 | p = strjoina("/run/systemd/nspawn/locks/name-", name); | |
30535c16 LP |
1254 | return make_lock_file(p, operation, ret); |
1255 | } | |
1256 | ||
d577d4a4 LP |
1257 | bool image_in_search_path( |
1258 | ImageClass class, | |
1259 | const char *root, | |
1260 | const char *image) { | |
1261 | ||
ace9ab19 LP |
1262 | const char *path; |
1263 | ||
1264 | assert(image); | |
1265 | ||
1266 | NULSTR_FOREACH(path, image_search_path[class]) { | |
d577d4a4 | 1267 | const char *p, *q; |
ace9ab19 LP |
1268 | size_t k; |
1269 | ||
d577d4a4 LP |
1270 | if (!empty_or_root(root)) { |
1271 | q = path_startswith(path, root); | |
1272 | if (!q) | |
1273 | continue; | |
1274 | } else | |
1275 | q = path; | |
1276 | ||
1277 | p = path_startswith(q, path); | |
ace9ab19 LP |
1278 | if (!p) |
1279 | continue; | |
1280 | ||
1281 | /* Make sure there's a filename following */ | |
1282 | k = strcspn(p, "/"); | |
1283 | if (k == 0) | |
1284 | continue; | |
1285 | ||
1286 | p += k; | |
1287 | ||
1288 | /* Accept trailing slashes */ | |
1289 | if (p[strspn(p, "/")] == 0) | |
1290 | return true; | |
1291 | ||
1292 | } | |
1293 | ||
1294 | return false; | |
1295 | } | |
1296 | ||
cd61c3bf LP |
1297 | static const char* const image_type_table[_IMAGE_TYPE_MAX] = { |
1298 | [IMAGE_DIRECTORY] = "directory", | |
1299 | [IMAGE_SUBVOLUME] = "subvolume", | |
aceac2f0 | 1300 | [IMAGE_RAW] = "raw", |
eb38edce | 1301 | [IMAGE_BLOCK] = "block", |
cd61c3bf LP |
1302 | }; |
1303 | ||
1304 | DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType); |