]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/machine-image.c
README: document that we only support util-linux built with --enable-libmount-force...
[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>
b5efdb8a 30#include "alloc-util.h"
cd61c3bf 31#include "btrfs-util.h"
c8b3094d 32#include "chattr-util.h"
ebd93cb6 33#include "copy.h"
a0956174 34#include "dirent-util.h"
3ffd4af2 35#include "fd-util.h"
f4f15635 36#include "fs-util.h"
a8fbdf54
TA
37#include "hashmap.h"
38#include "lockfile-util.h"
39#include "log.h"
40#include "macro.h"
3ffd4af2 41#include "machine-image.h"
30535c16 42#include "mkdir.h"
8e0b6570 43#include "path-util.h"
c6878637 44#include "rm-rf.h"
8b43440b 45#include "string-table.h"
07630cea 46#include "string-util.h"
8e0b6570 47#include "strv.h"
a8fbdf54 48#include "time-util.h"
8e0b6570 49#include "utf8.h"
a8fbdf54 50#include "util.h"
89a5a90c 51#include "xattr-util.h"
cd61c3bf 52
c2ce6a3d 53static const char image_search_path[] =
42c6f2c9 54 "/var/lib/machines\0"
7d105503 55 "/var/lib/container\0" /* legacy */
42c6f2c9
LP
56 "/usr/local/lib/machines\0"
57 "/usr/lib/machines\0";
c2ce6a3d 58
cd61c3bf
LP
59Image *image_unref(Image *i) {
60 if (!i)
61 return NULL;
62
63 free(i->name);
64 free(i->path);
65 free(i);
66 return NULL;
67}
68
8e0b6570
LP
69static 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 101static 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
LP
134 if (path)
135 i->path = strjoin(path, "/", filename, NULL);
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
150static 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, &quota);
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
283int 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
328int 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
387void 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 396int 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
404 if (path_equal(i->path, "/") ||
405 path_startswith(i->path, "/usr"))
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
LP
426 /* Allow deletion of read-only directories */
427 (void) chattr_path(i->path, false, 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
477 if (path_equal(i->path, "/") ||
478 path_startswith(i->path, "/usr"))
479 return -EROFS;
480
8e0b6570
LP
481 settings = image_settings_path(i);
482 if (!settings)
483 return -ENOMEM;
484
30535c16
LP
485 /* Make sure we don't interfere with a running nspawn */
486 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
487 if (r < 0)
488 return r;
489
490 /* Make sure nobody takes the new name, between the time we
491 * checked it is currently unused in all search paths, and the
492 * time we take possesion of it */
493 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
494 if (r < 0)
495 return r;
496
ebd93cb6
LP
497 r = image_find(new_name, NULL);
498 if (r < 0)
499 return r;
500 if (r > 0)
501 return -EEXIST;
502
503 switch (i->type) {
504
ebd93cb6 505 case IMAGE_DIRECTORY:
01b72568
LP
506 /* Turn of the immutable bit while we rename the image, so that we can rename it */
507 (void) read_attr_path(i->path, &file_attr);
508
509 if (file_attr & FS_IMMUTABLE_FL)
510 (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
511
512 /* fall through */
513
514 case IMAGE_SUBVOLUME:
ebd93cb6
LP
515 new_path = file_in_same_dir(i->path, new_name);
516 break;
517
aceac2f0 518 case IMAGE_RAW: {
ebd93cb6
LP
519 const char *fn;
520
63c372cb 521 fn = strjoina(new_name, ".raw");
ebd93cb6
LP
522 new_path = file_in_same_dir(i->path, fn);
523 break;
524 }
525
526 default:
15411c0c 527 return -EOPNOTSUPP;
ebd93cb6
LP
528 }
529
530 if (!new_path)
531 return -ENOMEM;
532
533 nn = strdup(new_name);
534 if (!nn)
535 return -ENOMEM;
536
f85ef957
AC
537 r = rename_noreplace(AT_FDCWD, i->path, AT_FDCWD, new_path);
538 if (r < 0)
539 return r;
ebd93cb6 540
01b72568
LP
541 /* Restore the immutable bit, if it was set before */
542 if (file_attr & FS_IMMUTABLE_FL)
543 (void) chattr_path(new_path, true, FS_IMMUTABLE_FL);
544
ebd93cb6
LP
545 free(i->path);
546 i->path = new_path;
547 new_path = NULL;
548
549 free(i->name);
550 i->name = nn;
551 nn = NULL;
552
8e0b6570
LP
553 STRV_FOREACH(j, settings) {
554 r = rename_settings_file(*j, new_name);
555 if (r < 0 && r != -ENOENT)
556 log_debug_errno(r, "Failed to rename settings file %s, ignoring: %m", *j);
557 }
558
ebd93cb6
LP
559 return 0;
560}
561
8e0b6570
LP
562static int clone_settings_file(const char *path, const char *new_name) {
563 _cleanup_free_ char *rs = NULL;
564 const char *fn;
565
566 fn = strjoina(new_name, ".nspawn");
567
568 rs = file_in_same_dir(path, fn);
569 if (!rs)
570 return -ENOMEM;
571
572 return copy_file_atomic(path, rs, 0664, false, 0);
573}
574
ebd93cb6 575int image_clone(Image *i, const char *new_name, bool read_only) {
30535c16 576 _cleanup_release_lock_file_ LockFile name_lock = LOCK_FILE_INIT;
8e0b6570 577 _cleanup_strv_free_ char **settings = NULL;
ebd93cb6 578 const char *new_path;
8e0b6570 579 char **j;
ebd93cb6
LP
580 int r;
581
582 assert(i);
583
584 if (!image_name_is_valid(new_name))
585 return -EINVAL;
586
8e0b6570
LP
587 settings = image_settings_path(i);
588 if (!settings)
589 return -ENOMEM;
590
30535c16
LP
591 /* Make sure nobody takes the new name, between the time we
592 * checked it is currently unused in all search paths, and the
593 * time we take possesion of it */
594 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
595 if (r < 0)
596 return r;
597
ebd93cb6
LP
598 r = image_find(new_name, NULL);
599 if (r < 0)
600 return r;
601 if (r > 0)
602 return -EEXIST;
603
604 switch (i->type) {
605
606 case IMAGE_SUBVOLUME:
607 case IMAGE_DIRECTORY:
63c372cb 608 new_path = strjoina("/var/lib/machines/", new_name);
ebd93cb6 609
5bcd08db
LP
610 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);
611
612 /* Enable "subtree" quotas for the copy, if we didn't
613 * copy any quota from the source. */
614 (void) btrfs_subvol_auto_qgroup(i->path, 0, true);
615
ebd93cb6
LP
616 break;
617
aceac2f0 618 case IMAGE_RAW:
63c372cb 619 new_path = strjoina("/var/lib/machines/", new_name, ".raw");
ebd93cb6 620
f2068bcc 621 r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
ebd93cb6
LP
622 break;
623
624 default:
15411c0c 625 return -EOPNOTSUPP;
ebd93cb6
LP
626 }
627
628 if (r < 0)
629 return r;
630
8e0b6570
LP
631 STRV_FOREACH(j, settings) {
632 r = clone_settings_file(*j, new_name);
633 if (r < 0 && r != -ENOENT)
634 log_debug_errno(r, "Failed to clone settings %s, ignoring: %m", *j);
635 }
636
ebd93cb6
LP
637 return 0;
638}
639
640int image_read_only(Image *i, bool b) {
30535c16 641 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
ebd93cb6
LP
642 int r;
643 assert(i);
644
645 if (path_equal(i->path, "/") ||
646 path_startswith(i->path, "/usr"))
647 return -EROFS;
648
30535c16
LP
649 /* Make sure we don't interfere with a running nspawn */
650 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
651 if (r < 0)
652 return r;
653
ebd93cb6
LP
654 switch (i->type) {
655
656 case IMAGE_SUBVOLUME:
5bcd08db
LP
657
658 /* Note that we set the flag only on the top-level
659 * subvolume of the image. */
660
ebd93cb6
LP
661 r = btrfs_subvol_set_read_only(i->path, b);
662 if (r < 0)
663 return r;
01b72568
LP
664
665 break;
666
667 case IMAGE_DIRECTORY:
668 /* For simple directory trees we cannot use the access
669 mode of the top-level directory, since it has an
670 effect on the container itself. However, we can
671 use the "immutable" flag, to at least make the
672 top-level directory read-only. It's not as good as
673 a read-only subvolume, but at least something, and
674 we can read the value back.*/
675
676 r = chattr_path(i->path, b, FS_IMMUTABLE_FL);
677 if (r < 0)
678 return r;
679
ebd93cb6
LP
680 break;
681
aceac2f0 682 case IMAGE_RAW: {
ebd93cb6
LP
683 struct stat st;
684
685 if (stat(i->path, &st) < 0)
686 return -errno;
687
688 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
689 return -errno;
f2068bcc
LP
690
691 /* If the images is now read-only, it's a good time to
692 * defrag it, given that no write patterns will
693 * fragment it again. */
694 if (b)
695 (void) btrfs_defrag(i->path);
ebd93cb6
LP
696 break;
697 }
698
ebd93cb6 699 default:
15411c0c 700 return -EOPNOTSUPP;
ebd93cb6
LP
701 }
702
703 return 0;
08682124
LP
704}
705
30535c16
LP
706int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) {
707 _cleanup_free_ char *p = NULL;
708 LockFile t = LOCK_FILE_INIT;
709 struct stat st;
710 int r;
711
712 assert(path);
713 assert(global);
714 assert(local);
715
716 /* Locks an image path. This actually creates two locks: one
717 * "local" one, next to the image path itself, which might be
718 * shared via NFS. And another "global" one, in /run, that
719 * uses the device/inode number. This has the benefit that we
720 * can even lock a tree that is a mount point, correctly. */
721
722 if (path_equal(path, "/"))
723 return -EBUSY;
724
725 if (!path_is_absolute(path))
726 return -EINVAL;
727
728 if (stat(path, &st) >= 0) {
729 if (asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
730 return -ENOMEM;
731 }
732
733 r = make_lock_file_for(path, operation, &t);
734 if (r < 0)
735 return r;
736
737 if (p) {
7e7cddb2 738 mkdir_p("/run/systemd/nspawn/locks", 0700);
30535c16
LP
739
740 r = make_lock_file(p, operation, global);
741 if (r < 0) {
742 release_lock_file(&t);
743 return r;
744 }
745 }
746
747 *local = t;
748 return 0;
749}
750
cb81cd80 751int image_set_limit(Image *i, uint64_t referenced_max) {
d6ce17c7
LP
752 assert(i);
753
754 if (path_equal(i->path, "/") ||
755 path_startswith(i->path, "/usr"))
756 return -EROFS;
757
758 if (i->type != IMAGE_SUBVOLUME)
15411c0c 759 return -EOPNOTSUPP;
d6ce17c7 760
5bcd08db
LP
761 /* We set the quota both for the subvolume as well as for the
762 * subtree. The latter is mostly for historical reasons, since
763 * we didn't use to have a concept of subtree quota, and hence
764 * only modified the subvolume quota. */
765
766 (void) btrfs_qgroup_set_limit(i->path, 0, referenced_max);
767 (void) btrfs_subvol_auto_qgroup(i->path, 0, true);
768 return btrfs_subvol_set_subtree_quota_limit(i->path, 0, referenced_max);
d6ce17c7
LP
769}
770
30535c16
LP
771int image_name_lock(const char *name, int operation, LockFile *ret) {
772 const char *p;
773
774 assert(name);
775 assert(ret);
776
777 /* Locks an image name, regardless of the precise path used. */
778
779 if (!image_name_is_valid(name))
780 return -EINVAL;
781
782 if (streq(name, ".host"))
783 return -EBUSY;
784
7e7cddb2 785 mkdir_p("/run/systemd/nspawn/locks", 0700);
63c372cb 786 p = strjoina("/run/systemd/nspawn/locks/name-", name);
30535c16
LP
787
788 return make_lock_file(p, operation, ret);
789}
790
791bool image_name_is_valid(const char *s) {
792 if (!filename_is_valid(s))
793 return false;
794
795 if (string_has_cc(s, NULL))
796 return false;
797
798 if (!utf8_is_valid(s))
799 return false;
800
801 /* Temporary files for atomically creating new files */
802 if (startswith(s, ".#"))
803 return false;
804
805 return true;
806}
807
cd61c3bf
LP
808static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
809 [IMAGE_DIRECTORY] = "directory",
810 [IMAGE_SUBVOLUME] = "subvolume",
aceac2f0 811 [IMAGE_RAW] = "raw",
cd61c3bf
LP
812};
813
814DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);