]>
Commit | Line | Data |
---|---|---|
cd61c3bf LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2013 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
a8fbdf54 TA |
20 | #include <dirent.h> |
21 | #include <errno.h> | |
ebd93cb6 | 22 | #include <fcntl.h> |
a8fbdf54 TA |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <string.h> | |
e306723e | 26 | #include <sys/file.h> |
a8fbdf54 TA |
27 | #include <sys/stat.h> |
28 | #include <unistd.h> | |
8e0b6570 | 29 | #include <linux/fs.h> |
546dbec5 | 30 | |
b5efdb8a | 31 | #include "alloc-util.h" |
cd61c3bf | 32 | #include "btrfs-util.h" |
c8b3094d | 33 | #include "chattr-util.h" |
ebd93cb6 | 34 | #include "copy.h" |
a0956174 | 35 | #include "dirent-util.h" |
3ffd4af2 | 36 | #include "fd-util.h" |
f4f15635 | 37 | #include "fs-util.h" |
a8fbdf54 TA |
38 | #include "hashmap.h" |
39 | #include "lockfile-util.h" | |
40 | #include "log.h" | |
3ffd4af2 | 41 | #include "machine-image.h" |
546dbec5 | 42 | #include "macro.h" |
30535c16 | 43 | #include "mkdir.h" |
8e0b6570 | 44 | #include "path-util.h" |
c6878637 | 45 | #include "rm-rf.h" |
8b43440b | 46 | #include "string-table.h" |
07630cea | 47 | #include "string-util.h" |
8e0b6570 | 48 | #include "strv.h" |
a8fbdf54 | 49 | #include "time-util.h" |
8e0b6570 | 50 | #include "utf8.h" |
a8fbdf54 | 51 | #include "util.h" |
89a5a90c | 52 | #include "xattr-util.h" |
cd61c3bf | 53 | |
c2ce6a3d | 54 | static const char image_search_path[] = |
42c6f2c9 | 55 | "/var/lib/machines\0" |
7d105503 | 56 | "/var/lib/container\0" /* legacy */ |
42c6f2c9 LP |
57 | "/usr/local/lib/machines\0" |
58 | "/usr/lib/machines\0"; | |
c2ce6a3d | 59 | |
cd61c3bf LP |
60 | Image *image_unref(Image *i) { |
61 | if (!i) | |
62 | return NULL; | |
63 | ||
64 | free(i->name); | |
65 | free(i->path); | |
6b430fdb | 66 | return mfree(i); |
cd61c3bf LP |
67 | } |
68 | ||
8e0b6570 LP |
69 | static char **image_settings_path(Image *image) { |
70 | _cleanup_strv_free_ char **l = NULL; | |
71 | char **ret; | |
72 | const char *fn, *s; | |
73 | unsigned i = 0; | |
74 | ||
75 | assert(image); | |
76 | ||
77 | l = new0(char*, 4); | |
78 | if (!l) | |
79 | return NULL; | |
80 | ||
81 | fn = strjoina(image->name, ".nspawn"); | |
82 | ||
83 | FOREACH_STRING(s, "/etc/systemd/nspawn/", "/run/systemd/nspawn/") { | |
84 | l[i] = strappend(s, fn); | |
85 | if (!l[i]) | |
86 | return NULL; | |
87 | ||
88 | i++; | |
89 | } | |
90 | ||
91 | l[i] = file_in_same_dir(image->path, fn); | |
92 | if (!l[i]) | |
93 | return NULL; | |
94 | ||
95 | ret = l; | |
96 | l = NULL; | |
97 | ||
98 | return ret; | |
99 | } | |
100 | ||
c2ce6a3d | 101 | static int image_new( |
cd61c3bf | 102 | ImageType t, |
5fc7f358 | 103 | const char *pretty, |
cd61c3bf | 104 | const char *path, |
5fc7f358 | 105 | const char *filename, |
cd61c3bf | 106 | bool read_only, |
10f9c755 | 107 | usec_t crtime, |
cd61c3bf | 108 | usec_t mtime, |
c2ce6a3d | 109 | Image **ret) { |
cd61c3bf LP |
110 | |
111 | _cleanup_(image_unrefp) Image *i = NULL; | |
cd61c3bf | 112 | |
cd61c3bf LP |
113 | assert(t >= 0); |
114 | assert(t < _IMAGE_TYPE_MAX); | |
5fc7f358 LP |
115 | assert(pretty); |
116 | assert(filename); | |
c2ce6a3d | 117 | assert(ret); |
cd61c3bf | 118 | |
c2ce6a3d | 119 | i = new0(Image, 1); |
cd61c3bf LP |
120 | if (!i) |
121 | return -ENOMEM; | |
122 | ||
123 | i->type = t; | |
124 | i->read_only = read_only; | |
10f9c755 | 125 | i->crtime = crtime; |
cd61c3bf | 126 | i->mtime = mtime; |
c19de711 | 127 | i->usage = i->usage_exclusive = (uint64_t) -1; |
b6b18498 | 128 | i->limit = i->limit_exclusive = (uint64_t) -1; |
cd61c3bf | 129 | |
5fc7f358 | 130 | i->name = strdup(pretty); |
cd61c3bf LP |
131 | if (!i->name) |
132 | return -ENOMEM; | |
133 | ||
5fc7f358 | 134 | if (path) |
605405c6 | 135 | i->path = strjoin(path, "/", filename); |
5fc7f358 LP |
136 | else |
137 | i->path = strdup(filename); | |
ebeccf9e | 138 | |
5fc7f358 LP |
139 | if (!i->path) |
140 | return -ENOMEM; | |
141 | ||
142 | path_kill_slashes(i->path); | |
cd61c3bf | 143 | |
c2ce6a3d | 144 | *ret = i; |
cd61c3bf | 145 | i = NULL; |
c2ce6a3d | 146 | |
cd61c3bf LP |
147 | return 0; |
148 | } | |
149 | ||
5fc7f358 LP |
150 | static int image_make( |
151 | const char *pretty, | |
152 | int dfd, | |
153 | const char *path, | |
154 | const char *filename, | |
155 | Image **ret) { | |
156 | ||
c2ce6a3d | 157 | struct stat st; |
5fc7f358 | 158 | bool read_only; |
cd61c3bf LP |
159 | int r; |
160 | ||
5fc7f358 | 161 | assert(filename); |
cd61c3bf | 162 | |
c2ce6a3d | 163 | /* We explicitly *do* follow symlinks here, since we want to |
5f129649 | 164 | * allow symlinking trees into /var/lib/machines/, and treat |
c2ce6a3d | 165 | * them normally. */ |
cd61c3bf | 166 | |
5fc7f358 | 167 | if (fstatat(dfd, filename, &st, 0) < 0) |
c2ce6a3d | 168 | return -errno; |
cd61c3bf | 169 | |
5fc7f358 LP |
170 | read_only = |
171 | (path && path_startswith(path, "/usr")) || | |
08ff5529 | 172 | (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS); |
86e339c8 | 173 | |
c2ce6a3d | 174 | if (S_ISDIR(st.st_mode)) { |
01b72568 LP |
175 | _cleanup_close_ int fd = -1; |
176 | unsigned file_attr = 0; | |
cd61c3bf | 177 | |
c2ce6a3d LP |
178 | if (!ret) |
179 | return 1; | |
cd61c3bf | 180 | |
5fc7f358 LP |
181 | if (!pretty) |
182 | pretty = filename; | |
183 | ||
01b72568 LP |
184 | fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY); |
185 | if (fd < 0) | |
186 | return -errno; | |
187 | ||
c2ce6a3d LP |
188 | /* btrfs subvolumes have inode 256 */ |
189 | if (st.st_ino == 256) { | |
cd61c3bf | 190 | |
21222ea5 LP |
191 | r = btrfs_is_filesystem(fd); |
192 | if (r < 0) | |
193 | return r; | |
194 | if (r) { | |
10f9c755 | 195 | BtrfsSubvolInfo info; |
cd61c3bf | 196 | |
c2ce6a3d | 197 | /* It's a btrfs subvolume */ |
cd61c3bf | 198 | |
5bcd08db | 199 | r = btrfs_subvol_get_info_fd(fd, 0, &info); |
10f9c755 LP |
200 | if (r < 0) |
201 | return r; | |
c2ce6a3d LP |
202 | |
203 | r = image_new(IMAGE_SUBVOLUME, | |
5fc7f358 | 204 | pretty, |
c2ce6a3d | 205 | path, |
5fc7f358 LP |
206 | filename, |
207 | info.read_only || read_only, | |
10f9c755 | 208 | info.otime, |
c2ce6a3d | 209 | 0, |
c2ce6a3d LP |
210 | ret); |
211 | if (r < 0) | |
212 | return r; | |
213 | ||
5bcd08db LP |
214 | if (btrfs_quota_scan_ongoing(fd) == 0) { |
215 | BtrfsQuotaInfo quota; | |
b6b18498 | 216 | |
5bcd08db LP |
217 | r = btrfs_subvol_get_subtree_quota_fd(fd, 0, "a); |
218 | if (r >= 0) { | |
219 | (*ret)->usage = quota.referenced; | |
220 | (*ret)->usage_exclusive = quota.exclusive; | |
221 | ||
222 | (*ret)->limit = quota.referenced_max; | |
223 | (*ret)->limit_exclusive = quota.exclusive_max; | |
224 | } | |
b6b18498 LP |
225 | } |
226 | ||
c2ce6a3d | 227 | return 1; |
cd61c3bf | 228 | } |
c2ce6a3d | 229 | } |
cd61c3bf | 230 | |
01b72568 LP |
231 | /* If the IMMUTABLE bit is set, we consider the |
232 | * directory read-only. Since the ioctl is not | |
233 | * supported everywhere we ignore failures. */ | |
234 | (void) read_attr_fd(fd, &file_attr); | |
cd61c3bf | 235 | |
01b72568 | 236 | /* It's just a normal directory. */ |
c2ce6a3d | 237 | r = image_new(IMAGE_DIRECTORY, |
5fc7f358 | 238 | pretty, |
c2ce6a3d | 239 | path, |
5fc7f358 | 240 | filename, |
01b72568 | 241 | read_only || (file_attr & FS_IMMUTABLE_FL), |
c2ce6a3d LP |
242 | 0, |
243 | 0, | |
244 | ret); | |
245 | if (r < 0) | |
246 | return r; | |
cd61c3bf | 247 | |
c2ce6a3d | 248 | return 1; |
cd61c3bf | 249 | |
aceac2f0 | 250 | } else if (S_ISREG(st.st_mode) && endswith(filename, ".raw")) { |
10f9c755 | 251 | usec_t crtime = 0; |
cd61c3bf | 252 | |
aceac2f0 | 253 | /* It's a RAW disk image */ |
cd61c3bf | 254 | |
c2ce6a3d LP |
255 | if (!ret) |
256 | return 1; | |
cd61c3bf | 257 | |
5fc7f358 | 258 | fd_getcrtime_at(dfd, filename, &crtime, 0); |
10f9c755 | 259 | |
5fc7f358 LP |
260 | if (!pretty) |
261 | pretty = strndupa(filename, strlen(filename) - 4); | |
10f9c755 | 262 | |
aceac2f0 | 263 | r = image_new(IMAGE_RAW, |
5fc7f358 | 264 | pretty, |
c2ce6a3d | 265 | path, |
5fc7f358 LP |
266 | filename, |
267 | !(st.st_mode & 0222) || read_only, | |
10f9c755 | 268 | crtime, |
c2ce6a3d | 269 | timespec_load(&st.st_mtim), |
c2ce6a3d LP |
270 | ret); |
271 | if (r < 0) | |
272 | return r; | |
cd61c3bf | 273 | |
c19de711 | 274 | (*ret)->usage = (*ret)->usage_exclusive = st.st_blocks * 512; |
b6b18498 LP |
275 | (*ret)->limit = (*ret)->limit_exclusive = st.st_size; |
276 | ||
c2ce6a3d LP |
277 | return 1; |
278 | } | |
cd61c3bf | 279 | |
c2ce6a3d LP |
280 | return 0; |
281 | } | |
cd61c3bf | 282 | |
c2ce6a3d LP |
283 | int image_find(const char *name, Image **ret) { |
284 | const char *path; | |
285 | int r; | |
cd61c3bf | 286 | |
c2ce6a3d | 287 | assert(name); |
cd61c3bf | 288 | |
c2ce6a3d LP |
289 | /* There are no images with invalid names */ |
290 | if (!image_name_is_valid(name)) | |
291 | return 0; | |
cd61c3bf | 292 | |
c2ce6a3d LP |
293 | NULSTR_FOREACH(path, image_search_path) { |
294 | _cleanup_closedir_ DIR *d = NULL; | |
cd61c3bf | 295 | |
c2ce6a3d LP |
296 | d = opendir(path); |
297 | if (!d) { | |
298 | if (errno == ENOENT) | |
299 | continue; | |
cd61c3bf | 300 | |
c2ce6a3d LP |
301 | return -errno; |
302 | } | |
cd61c3bf | 303 | |
5fc7f358 LP |
304 | r = image_make(NULL, dirfd(d), path, name, ret); |
305 | if (r == 0 || r == -ENOENT) { | |
aceac2f0 | 306 | _cleanup_free_ char *raw = NULL; |
5fc7f358 | 307 | |
aceac2f0 LP |
308 | raw = strappend(name, ".raw"); |
309 | if (!raw) | |
5fc7f358 LP |
310 | return -ENOMEM; |
311 | ||
aceac2f0 | 312 | r = image_make(NULL, dirfd(d), path, raw, ret); |
5fc7f358 LP |
313 | if (r == 0 || r == -ENOENT) |
314 | continue; | |
315 | } | |
c2ce6a3d LP |
316 | if (r < 0) |
317 | return r; | |
cd61c3bf | 318 | |
c2ce6a3d LP |
319 | return 1; |
320 | } | |
321 | ||
5fc7f358 | 322 | if (streq(name, ".host")) |
27c88c4e | 323 | return image_make(".host", AT_FDCWD, NULL, "/", ret); |
5fc7f358 | 324 | |
c2ce6a3d LP |
325 | return 0; |
326 | }; | |
327 | ||
328 | int image_discover(Hashmap *h) { | |
329 | const char *path; | |
330 | int r; | |
331 | ||
332 | assert(h); | |
333 | ||
334 | NULSTR_FOREACH(path, image_search_path) { | |
335 | _cleanup_closedir_ DIR *d = NULL; | |
336 | struct dirent *de; | |
337 | ||
338 | d = opendir(path); | |
339 | if (!d) { | |
340 | if (errno == ENOENT) | |
a67a4c8c | 341 | continue; |
c2ce6a3d LP |
342 | |
343 | return -errno; | |
344 | } | |
345 | ||
346 | FOREACH_DIRENT_ALL(de, d, return -errno) { | |
347 | _cleanup_(image_unrefp) Image *image = NULL; | |
348 | ||
349 | if (!image_name_is_valid(de->d_name)) | |
350 | continue; | |
351 | ||
352 | if (hashmap_contains(h, de->d_name)) | |
353 | continue; | |
354 | ||
5fc7f358 | 355 | r = image_make(NULL, dirfd(d), path, de->d_name, &image); |
c2ce6a3d LP |
356 | if (r == 0 || r == -ENOENT) |
357 | continue; | |
358 | if (r < 0) | |
359 | return r; | |
360 | ||
361 | r = hashmap_put(h, image->name, image); | |
362 | if (r < 0) | |
363 | return r; | |
364 | ||
365 | image = NULL; | |
cd61c3bf LP |
366 | } |
367 | } | |
368 | ||
5fc7f358 LP |
369 | if (!hashmap_contains(h, ".host")) { |
370 | _cleanup_(image_unrefp) Image *image = NULL; | |
371 | ||
372 | r = image_make(".host", AT_FDCWD, NULL, "/", &image); | |
373 | if (r < 0) | |
374 | return r; | |
375 | ||
376 | r = hashmap_put(h, image->name, image); | |
377 | if (r < 0) | |
378 | return r; | |
379 | ||
380 | image = NULL; | |
381 | ||
382 | } | |
383 | ||
cd61c3bf LP |
384 | return 0; |
385 | } | |
386 | ||
387 | void image_hashmap_free(Hashmap *map) { | |
388 | Image *i; | |
389 | ||
390 | while ((i = hashmap_steal_first(map))) | |
391 | image_unref(i); | |
392 | ||
393 | hashmap_free(map); | |
394 | } | |
395 | ||
08682124 | 396 | int image_remove(Image *i) { |
30535c16 | 397 | _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; |
8e0b6570 LP |
398 | _cleanup_strv_free_ char **settings = NULL; |
399 | char **j; | |
30535c16 LP |
400 | int r; |
401 | ||
08682124 LP |
402 | assert(i); |
403 | ||
d94c2b06 | 404 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
08682124 LP |
405 | return -EROFS; |
406 | ||
8e0b6570 LP |
407 | settings = image_settings_path(i); |
408 | if (!settings) | |
409 | return -ENOMEM; | |
410 | ||
30535c16 LP |
411 | /* Make sure we don't interfere with a running nspawn */ |
412 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
413 | if (r < 0) | |
414 | return r; | |
415 | ||
ebd93cb6 LP |
416 | switch (i->type) { |
417 | ||
418 | case IMAGE_SUBVOLUME: | |
5bcd08db | 419 | r = btrfs_subvol_remove(i->path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
8e0b6570 LP |
420 | if (r < 0) |
421 | return r; | |
422 | break; | |
ebd93cb6 LP |
423 | |
424 | case IMAGE_DIRECTORY: | |
01b72568 | 425 | /* Allow deletion of read-only directories */ |
a67d68b8 | 426 | (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL); |
8e0b6570 LP |
427 | r = rm_rf(i->path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME); |
428 | if (r < 0) | |
429 | return r; | |
430 | ||
431 | break; | |
01b72568 | 432 | |
aceac2f0 | 433 | case IMAGE_RAW: |
41d1ed05 LP |
434 | if (unlink(i->path) < 0) |
435 | return -errno; | |
8e0b6570 | 436 | break; |
ebd93cb6 LP |
437 | |
438 | default: | |
15411c0c | 439 | return -EOPNOTSUPP; |
ebd93cb6 | 440 | } |
8e0b6570 LP |
441 | |
442 | STRV_FOREACH(j, settings) { | |
443 | if (unlink(*j) < 0 && errno != ENOENT) | |
444 | log_debug_errno(errno, "Failed to unlink %s, ignoring: %m", *j); | |
445 | } | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
450 | static int rename_settings_file(const char *path, const char *new_name) { | |
451 | _cleanup_free_ char *rs = NULL; | |
452 | const char *fn; | |
453 | ||
454 | fn = strjoina(new_name, ".nspawn"); | |
455 | ||
456 | rs = file_in_same_dir(path, fn); | |
457 | if (!rs) | |
458 | return -ENOMEM; | |
459 | ||
460 | return rename_noreplace(AT_FDCWD, path, AT_FDCWD, rs); | |
ebd93cb6 LP |
461 | } |
462 | ||
463 | int image_rename(Image *i, const char *new_name) { | |
30535c16 | 464 | _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT; |
ebd93cb6 | 465 | _cleanup_free_ char *new_path = NULL, *nn = NULL; |
8e0b6570 | 466 | _cleanup_strv_free_ char **settings = NULL; |
01b72568 | 467 | unsigned file_attr = 0; |
8e0b6570 | 468 | char **j; |
ebd93cb6 LP |
469 | int r; |
470 | ||
471 | assert(i); | |
472 | ||
473 | if (!image_name_is_valid(new_name)) | |
474 | return -EINVAL; | |
475 | ||
d94c2b06 | 476 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
ebd93cb6 LP |
477 | return -EROFS; |
478 | ||
8e0b6570 LP |
479 | settings = image_settings_path(i); |
480 | if (!settings) | |
481 | return -ENOMEM; | |
482 | ||
30535c16 LP |
483 | /* Make sure we don't interfere with a running nspawn */ |
484 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
485 | if (r < 0) | |
486 | return r; | |
487 | ||
488 | /* Make sure nobody takes the new name, between the time we | |
489 | * checked it is currently unused in all search paths, and the | |
f8e2f4d6 | 490 | * time we take possession of it */ |
30535c16 LP |
491 | r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock); |
492 | if (r < 0) | |
493 | return r; | |
494 | ||
ebd93cb6 LP |
495 | r = image_find(new_name, NULL); |
496 | if (r < 0) | |
497 | return r; | |
498 | if (r > 0) | |
499 | return -EEXIST; | |
500 | ||
501 | switch (i->type) { | |
502 | ||
ebd93cb6 | 503 | case IMAGE_DIRECTORY: |
01b72568 LP |
504 | /* Turn of the immutable bit while we rename the image, so that we can rename it */ |
505 | (void) read_attr_path(i->path, &file_attr); | |
506 | ||
507 | if (file_attr & FS_IMMUTABLE_FL) | |
a67d68b8 | 508 | (void) chattr_path(i->path, 0, FS_IMMUTABLE_FL); |
01b72568 LP |
509 | |
510 | /* fall through */ | |
511 | ||
512 | case IMAGE_SUBVOLUME: | |
ebd93cb6 LP |
513 | new_path = file_in_same_dir(i->path, new_name); |
514 | break; | |
515 | ||
aceac2f0 | 516 | case IMAGE_RAW: { |
ebd93cb6 LP |
517 | const char *fn; |
518 | ||
63c372cb | 519 | fn = strjoina(new_name, ".raw"); |
ebd93cb6 LP |
520 | new_path = file_in_same_dir(i->path, fn); |
521 | break; | |
522 | } | |
523 | ||
524 | default: | |
15411c0c | 525 | return -EOPNOTSUPP; |
ebd93cb6 LP |
526 | } |
527 | ||
528 | if (!new_path) | |
529 | return -ENOMEM; | |
530 | ||
531 | nn = strdup(new_name); | |
532 | if (!nn) | |
533 | return -ENOMEM; | |
534 | ||
f85ef957 AC |
535 | r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path); |
536 | if (r < 0) | |
537 | return r; | |
ebd93cb6 | 538 | |
01b72568 LP |
539 | /* Restore the immutable bit, if it was set before */ |
540 | if (file_attr & FS_IMMUTABLE_FL) | |
a67d68b8 | 541 | (void) chattr_path(new_path, FS_IMMUTABLE_FL, FS_IMMUTABLE_FL); |
01b72568 | 542 | |
ebd93cb6 LP |
543 | free(i->path); |
544 | i->path = new_path; | |
545 | new_path = NULL; | |
546 | ||
547 | free(i->name); | |
548 | i->name = nn; | |
549 | nn = NULL; | |
550 | ||
8e0b6570 LP |
551 | STRV_FOREACH(j, settings) { |
552 | r = rename_settings_file(*j, new_name); | |
553 | if (r < 0 && r != -ENOENT) | |
554 | log_debug_errno(r, "Failed to rename settings file %s, ignoring: %m", *j); | |
555 | } | |
556 | ||
ebd93cb6 LP |
557 | return 0; |
558 | } | |
559 | ||
8e0b6570 LP |
560 | static int clone_settings_file(const char *path, const char *new_name) { |
561 | _cleanup_free_ char *rs = NULL; | |
562 | const char *fn; | |
563 | ||
564 | fn = strjoina(new_name, ".nspawn"); | |
565 | ||
566 | rs = file_in_same_dir(path, fn); | |
567 | if (!rs) | |
568 | return -ENOMEM; | |
569 | ||
570 | return copy_file_atomic(path, rs, 0664, false, 0); | |
571 | } | |
572 | ||
ebd93cb6 | 573 | int image_clone(Image *i, const char *new_name, bool read_only) { |
30535c16 | 574 | _cleanup_release_lock_file_ LockFile name_lock = LOCK_FILE_INIT; |
8e0b6570 | 575 | _cleanup_strv_free_ char **settings = NULL; |
ebd93cb6 | 576 | const char *new_path; |
8e0b6570 | 577 | char **j; |
ebd93cb6 LP |
578 | int r; |
579 | ||
580 | assert(i); | |
581 | ||
582 | if (!image_name_is_valid(new_name)) | |
583 | return -EINVAL; | |
584 | ||
8e0b6570 LP |
585 | settings = image_settings_path(i); |
586 | if (!settings) | |
587 | return -ENOMEM; | |
588 | ||
30535c16 LP |
589 | /* Make sure nobody takes the new name, between the time we |
590 | * checked it is currently unused in all search paths, and the | |
f8e2f4d6 | 591 | * time we take possession of it */ |
30535c16 LP |
592 | r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock); |
593 | if (r < 0) | |
594 | return r; | |
595 | ||
ebd93cb6 LP |
596 | r = image_find(new_name, NULL); |
597 | if (r < 0) | |
598 | return r; | |
599 | if (r > 0) | |
600 | return -EEXIST; | |
601 | ||
602 | switch (i->type) { | |
603 | ||
604 | case IMAGE_SUBVOLUME: | |
605 | case IMAGE_DIRECTORY: | |
9a50e3ca LP |
606 | /* If we can we'll always try to create a new btrfs subvolume here, even if the source is a plain |
607 | * directory.*/ | |
608 | ||
63c372cb | 609 | new_path = strjoina("/var/lib/machines/", new_name); |
ebd93cb6 | 610 | |
5bcd08db | 611 | r = btrfs_subvol_snapshot(i->path, new_path, (read_only ? BTRFS_SNAPSHOT_READ_ONLY : 0) | BTRFS_SNAPSHOT_FALLBACK_COPY | BTRFS_SNAPSHOT_RECURSIVE | BTRFS_SNAPSHOT_QUOTA); |
9a50e3ca LP |
612 | if (r == -EOPNOTSUPP) { |
613 | /* No btrfs snapshots supported, create a normal directory then. */ | |
614 | ||
615 | r = copy_directory(i->path, new_path, false); | |
616 | if (r >= 0) | |
617 | (void) chattr_path(new_path, read_only ? FS_IMMUTABLE_FL : 0, FS_IMMUTABLE_FL); | |
618 | } else if (r >= 0) | |
619 | /* Enable "subtree" quotas for the copy, if we didn't copy any quota from the source. */ | |
8120ee28 | 620 | (void) btrfs_subvol_auto_qgroup(new_path, 0, true); |
5bcd08db | 621 | |
ebd93cb6 LP |
622 | break; |
623 | ||
aceac2f0 | 624 | case IMAGE_RAW: |
63c372cb | 625 | new_path = strjoina("/var/lib/machines/", new_name, ".raw"); |
ebd93cb6 | 626 | |
f2068bcc | 627 | r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL); |
ebd93cb6 LP |
628 | break; |
629 | ||
630 | default: | |
15411c0c | 631 | return -EOPNOTSUPP; |
ebd93cb6 LP |
632 | } |
633 | ||
634 | if (r < 0) | |
635 | return r; | |
636 | ||
8e0b6570 LP |
637 | STRV_FOREACH(j, settings) { |
638 | r = clone_settings_file(*j, new_name); | |
639 | if (r < 0 && r != -ENOENT) | |
640 | log_debug_errno(r, "Failed to clone settings %s, ignoring: %m", *j); | |
641 | } | |
642 | ||
ebd93cb6 LP |
643 | return 0; |
644 | } | |
645 | ||
646 | int image_read_only(Image *i, bool b) { | |
30535c16 | 647 | _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT; |
ebd93cb6 LP |
648 | int r; |
649 | assert(i); | |
650 | ||
d94c2b06 | 651 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
ebd93cb6 LP |
652 | return -EROFS; |
653 | ||
30535c16 LP |
654 | /* Make sure we don't interfere with a running nspawn */ |
655 | r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock); | |
656 | if (r < 0) | |
657 | return r; | |
658 | ||
ebd93cb6 LP |
659 | switch (i->type) { |
660 | ||
661 | case IMAGE_SUBVOLUME: | |
5bcd08db LP |
662 | |
663 | /* Note that we set the flag only on the top-level | |
664 | * subvolume of the image. */ | |
665 | ||
ebd93cb6 LP |
666 | r = btrfs_subvol_set_read_only(i->path, b); |
667 | if (r < 0) | |
668 | return r; | |
01b72568 LP |
669 | |
670 | break; | |
671 | ||
672 | case IMAGE_DIRECTORY: | |
673 | /* For simple directory trees we cannot use the access | |
674 | mode of the top-level directory, since it has an | |
675 | effect on the container itself. However, we can | |
676 | use the "immutable" flag, to at least make the | |
677 | top-level directory read-only. It's not as good as | |
678 | a read-only subvolume, but at least something, and | |
679 | we can read the value back.*/ | |
680 | ||
a67d68b8 | 681 | r = chattr_path(i->path, b ? FS_IMMUTABLE_FL : 0, FS_IMMUTABLE_FL); |
01b72568 LP |
682 | if (r < 0) |
683 | return r; | |
684 | ||
ebd93cb6 LP |
685 | break; |
686 | ||
aceac2f0 | 687 | case IMAGE_RAW: { |
ebd93cb6 LP |
688 | struct stat st; |
689 | ||
690 | if (stat(i->path, &st) < 0) | |
691 | return -errno; | |
692 | ||
693 | if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0) | |
694 | return -errno; | |
f2068bcc LP |
695 | |
696 | /* If the images is now read-only, it's a good time to | |
697 | * defrag it, given that no write patterns will | |
698 | * fragment it again. */ | |
699 | if (b) | |
700 | (void) btrfs_defrag(i->path); | |
ebd93cb6 LP |
701 | break; |
702 | } | |
703 | ||
ebd93cb6 | 704 | default: |
15411c0c | 705 | return -EOPNOTSUPP; |
ebd93cb6 LP |
706 | } |
707 | ||
708 | return 0; | |
08682124 LP |
709 | } |
710 | ||
30535c16 LP |
711 | int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) { |
712 | _cleanup_free_ char *p = NULL; | |
713 | LockFile t = LOCK_FILE_INIT; | |
714 | struct stat st; | |
715 | int r; | |
716 | ||
717 | assert(path); | |
718 | assert(global); | |
719 | assert(local); | |
720 | ||
721 | /* Locks an image path. This actually creates two locks: one | |
722 | * "local" one, next to the image path itself, which might be | |
723 | * shared via NFS. And another "global" one, in /run, that | |
724 | * uses the device/inode number. This has the benefit that we | |
725 | * can even lock a tree that is a mount point, correctly. */ | |
726 | ||
727 | if (path_equal(path, "/")) | |
728 | return -EBUSY; | |
729 | ||
730 | if (!path_is_absolute(path)) | |
731 | return -EINVAL; | |
732 | ||
733 | if (stat(path, &st) >= 0) { | |
734 | if (asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0) | |
735 | return -ENOMEM; | |
736 | } | |
737 | ||
738 | r = make_lock_file_for(path, operation, &t); | |
739 | if (r < 0) | |
740 | return r; | |
741 | ||
742 | if (p) { | |
7e7cddb2 | 743 | mkdir_p("/run/systemd/nspawn/locks", 0700); |
30535c16 LP |
744 | |
745 | r = make_lock_file(p, operation, global); | |
746 | if (r < 0) { | |
747 | release_lock_file(&t); | |
748 | return r; | |
749 | } | |
546dbec5 LP |
750 | } else |
751 | *global = (LockFile) LOCK_FILE_INIT; | |
30535c16 LP |
752 | |
753 | *local = t; | |
754 | return 0; | |
755 | } | |
756 | ||
cb81cd80 | 757 | int image_set_limit(Image *i, uint64_t referenced_max) { |
d6ce17c7 LP |
758 | assert(i); |
759 | ||
d94c2b06 | 760 | if (IMAGE_IS_VENDOR(i) || IMAGE_IS_HOST(i)) |
d6ce17c7 LP |
761 | return -EROFS; |
762 | ||
763 | if (i->type != IMAGE_SUBVOLUME) | |
15411c0c | 764 | return -EOPNOTSUPP; |
d6ce17c7 | 765 | |
5bcd08db LP |
766 | /* We set the quota both for the subvolume as well as for the |
767 | * subtree. The latter is mostly for historical reasons, since | |
768 | * we didn't use to have a concept of subtree quota, and hence | |
769 | * only modified the subvolume quota. */ | |
770 | ||
771 | (void) btrfs_qgroup_set_limit(i->path, 0, referenced_max); | |
772 | (void) btrfs_subvol_auto_qgroup(i->path, 0, true); | |
773 | return btrfs_subvol_set_subtree_quota_limit(i->path, 0, referenced_max); | |
d6ce17c7 LP |
774 | } |
775 | ||
30535c16 LP |
776 | int image_name_lock(const char *name, int operation, LockFile *ret) { |
777 | const char *p; | |
778 | ||
779 | assert(name); | |
780 | assert(ret); | |
781 | ||
782 | /* Locks an image name, regardless of the precise path used. */ | |
783 | ||
784 | if (!image_name_is_valid(name)) | |
785 | return -EINVAL; | |
786 | ||
787 | if (streq(name, ".host")) | |
788 | return -EBUSY; | |
789 | ||
7e7cddb2 | 790 | mkdir_p("/run/systemd/nspawn/locks", 0700); |
63c372cb | 791 | p = strjoina("/run/systemd/nspawn/locks/name-", name); |
30535c16 LP |
792 | |
793 | return make_lock_file(p, operation, ret); | |
794 | } | |
795 | ||
796 | bool image_name_is_valid(const char *s) { | |
797 | if (!filename_is_valid(s)) | |
798 | return false; | |
799 | ||
800 | if (string_has_cc(s, NULL)) | |
801 | return false; | |
802 | ||
803 | if (!utf8_is_valid(s)) | |
804 | return false; | |
805 | ||
806 | /* Temporary files for atomically creating new files */ | |
807 | if (startswith(s, ".#")) | |
808 | return false; | |
809 | ||
810 | return true; | |
811 | } | |
812 | ||
cd61c3bf LP |
813 | static const char* const image_type_table[_IMAGE_TYPE_MAX] = { |
814 | [IMAGE_DIRECTORY] = "directory", | |
815 | [IMAGE_SUBVOLUME] = "subvolume", | |
aceac2f0 | 816 | [IMAGE_RAW] = "raw", |
cd61c3bf LP |
817 | }; |
818 | ||
819 | DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType); |