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