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