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