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