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