]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/machine-image.c
path-util: fix path_is_mount_point() for symlinks
[thirdparty/systemd.git] / src / shared / machine-image.c
CommitLineData
cd61c3bf
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <sys/statfs.h>
f2068bcc 23#include <linux/fs.h>
ebd93cb6 24#include <fcntl.h>
cd61c3bf 25
cd61c3bf
LP
26#include "utf8.h"
27#include "btrfs-util.h"
ebeccf9e 28#include "path-util.h"
ebd93cb6 29#include "copy.h"
30535c16 30#include "mkdir.h"
003dffde 31#include "machine-image.h"
cd61c3bf 32
c2ce6a3d 33static const char image_search_path[] =
42c6f2c9 34 "/var/lib/machines\0"
c2ce6a3d 35 "/var/lib/container\0"
42c6f2c9
LP
36 "/usr/local/lib/machines\0"
37 "/usr/lib/machines\0";
c2ce6a3d 38
cd61c3bf
LP
39Image *image_unref(Image *i) {
40 if (!i)
41 return NULL;
42
43 free(i->name);
44 free(i->path);
45 free(i);
46 return NULL;
47}
48
c2ce6a3d 49static int image_new(
cd61c3bf 50 ImageType t,
5fc7f358 51 const char *pretty,
cd61c3bf 52 const char *path,
5fc7f358 53 const char *filename,
cd61c3bf 54 bool read_only,
10f9c755 55 usec_t crtime,
cd61c3bf 56 usec_t mtime,
c2ce6a3d 57 Image **ret) {
cd61c3bf
LP
58
59 _cleanup_(image_unrefp) Image *i = NULL;
cd61c3bf 60
cd61c3bf
LP
61 assert(t >= 0);
62 assert(t < _IMAGE_TYPE_MAX);
5fc7f358
LP
63 assert(pretty);
64 assert(filename);
c2ce6a3d 65 assert(ret);
cd61c3bf 66
c2ce6a3d 67 i = new0(Image, 1);
cd61c3bf
LP
68 if (!i)
69 return -ENOMEM;
70
71 i->type = t;
72 i->read_only = read_only;
10f9c755 73 i->crtime = crtime;
cd61c3bf 74 i->mtime = mtime;
c19de711 75 i->usage = i->usage_exclusive = (uint64_t) -1;
b6b18498 76 i->limit = i->limit_exclusive = (uint64_t) -1;
cd61c3bf 77
5fc7f358 78 i->name = strdup(pretty);
cd61c3bf
LP
79 if (!i->name)
80 return -ENOMEM;
81
5fc7f358
LP
82 if (path)
83 i->path = strjoin(path, "/", filename, NULL);
84 else
85 i->path = strdup(filename);
ebeccf9e 86
5fc7f358
LP
87 if (!i->path)
88 return -ENOMEM;
89
90 path_kill_slashes(i->path);
cd61c3bf 91
c2ce6a3d 92 *ret = i;
cd61c3bf 93 i = NULL;
c2ce6a3d 94
cd61c3bf
LP
95 return 0;
96}
97
5fc7f358
LP
98static int image_make(
99 const char *pretty,
100 int dfd,
101 const char *path,
102 const char *filename,
103 Image **ret) {
104
c2ce6a3d 105 struct stat st;
5fc7f358 106 bool read_only;
cd61c3bf
LP
107 int r;
108
5fc7f358 109 assert(filename);
cd61c3bf 110
c2ce6a3d 111 /* We explicitly *do* follow symlinks here, since we want to
5f129649 112 * allow symlinking trees into /var/lib/machines/, and treat
c2ce6a3d 113 * them normally. */
cd61c3bf 114
5fc7f358 115 if (fstatat(dfd, filename, &st, 0) < 0)
c2ce6a3d 116 return -errno;
cd61c3bf 117
5fc7f358
LP
118 read_only =
119 (path && path_startswith(path, "/usr")) ||
08ff5529 120 (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS);
86e339c8 121
c2ce6a3d 122 if (S_ISDIR(st.st_mode)) {
01b72568
LP
123 _cleanup_close_ int fd = -1;
124 unsigned file_attr = 0;
cd61c3bf 125
c2ce6a3d
LP
126 if (!ret)
127 return 1;
cd61c3bf 128
5fc7f358
LP
129 if (!pretty)
130 pretty = filename;
131
01b72568
LP
132 fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
133 if (fd < 0)
134 return -errno;
135
c2ce6a3d
LP
136 /* btrfs subvolumes have inode 256 */
137 if (st.st_ino == 256) {
c2ce6a3d 138 struct statfs sfs;
cd61c3bf 139
c2ce6a3d
LP
140 if (fstatfs(fd, &sfs) < 0)
141 return -errno;
cd61c3bf 142
c2ce6a3d 143 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
10f9c755 144 BtrfsSubvolInfo info;
b6b18498 145 BtrfsQuotaInfo quota;
cd61c3bf 146
c2ce6a3d 147 /* It's a btrfs subvolume */
cd61c3bf 148
10f9c755
LP
149 r = btrfs_subvol_get_info_fd(fd, &info);
150 if (r < 0)
151 return r;
c2ce6a3d
LP
152
153 r = image_new(IMAGE_SUBVOLUME,
5fc7f358 154 pretty,
c2ce6a3d 155 path,
5fc7f358
LP
156 filename,
157 info.read_only || read_only,
10f9c755 158 info.otime,
c2ce6a3d 159 0,
c2ce6a3d
LP
160 ret);
161 if (r < 0)
162 return r;
163
b6b18498
LP
164 r = btrfs_subvol_get_quota_fd(fd, &quota);
165 if (r >= 0) {
cb81cd80 166 (*ret)->usage = quota.referenced;
c19de711 167 (*ret)->usage_exclusive = quota.exclusive;
b6b18498 168
cb81cd80 169 (*ret)->limit = quota.referenced_max;
b6b18498
LP
170 (*ret)->limit_exclusive = quota.exclusive_max;
171 }
172
c2ce6a3d 173 return 1;
cd61c3bf 174 }
c2ce6a3d 175 }
cd61c3bf 176
01b72568
LP
177 /* If the IMMUTABLE bit is set, we consider the
178 * directory read-only. Since the ioctl is not
179 * supported everywhere we ignore failures. */
180 (void) read_attr_fd(fd, &file_attr);
cd61c3bf 181
01b72568 182 /* It's just a normal directory. */
c2ce6a3d 183 r = image_new(IMAGE_DIRECTORY,
5fc7f358 184 pretty,
c2ce6a3d 185 path,
5fc7f358 186 filename,
01b72568 187 read_only || (file_attr & FS_IMMUTABLE_FL),
c2ce6a3d
LP
188 0,
189 0,
190 ret);
191 if (r < 0)
192 return r;
cd61c3bf 193
c2ce6a3d 194 return 1;
cd61c3bf 195
aceac2f0 196 } else if (S_ISREG(st.st_mode) && endswith(filename, ".raw")) {
10f9c755 197 usec_t crtime = 0;
cd61c3bf 198
aceac2f0 199 /* It's a RAW disk image */
cd61c3bf 200
c2ce6a3d
LP
201 if (!ret)
202 return 1;
cd61c3bf 203
5fc7f358 204 fd_getcrtime_at(dfd, filename, &crtime, 0);
10f9c755 205
5fc7f358
LP
206 if (!pretty)
207 pretty = strndupa(filename, strlen(filename) - 4);
10f9c755 208
aceac2f0 209 r = image_new(IMAGE_RAW,
5fc7f358 210 pretty,
c2ce6a3d 211 path,
5fc7f358
LP
212 filename,
213 !(st.st_mode & 0222) || read_only,
10f9c755 214 crtime,
c2ce6a3d 215 timespec_load(&st.st_mtim),
c2ce6a3d
LP
216 ret);
217 if (r < 0)
218 return r;
cd61c3bf 219
c19de711 220 (*ret)->usage = (*ret)->usage_exclusive = st.st_blocks * 512;
b6b18498
LP
221 (*ret)->limit = (*ret)->limit_exclusive = st.st_size;
222
c2ce6a3d
LP
223 return 1;
224 }
cd61c3bf 225
c2ce6a3d
LP
226 return 0;
227}
cd61c3bf 228
c2ce6a3d
LP
229int image_find(const char *name, Image **ret) {
230 const char *path;
231 int r;
cd61c3bf 232
c2ce6a3d 233 assert(name);
cd61c3bf 234
c2ce6a3d
LP
235 /* There are no images with invalid names */
236 if (!image_name_is_valid(name))
237 return 0;
cd61c3bf 238
c2ce6a3d
LP
239 NULSTR_FOREACH(path, image_search_path) {
240 _cleanup_closedir_ DIR *d = NULL;
cd61c3bf 241
c2ce6a3d
LP
242 d = opendir(path);
243 if (!d) {
244 if (errno == ENOENT)
245 continue;
cd61c3bf 246
c2ce6a3d
LP
247 return -errno;
248 }
cd61c3bf 249
5fc7f358
LP
250 r = image_make(NULL, dirfd(d), path, name, ret);
251 if (r == 0 || r == -ENOENT) {
aceac2f0 252 _cleanup_free_ char *raw = NULL;
5fc7f358 253
aceac2f0
LP
254 raw = strappend(name, ".raw");
255 if (!raw)
5fc7f358
LP
256 return -ENOMEM;
257
aceac2f0 258 r = image_make(NULL, dirfd(d), path, raw, ret);
5fc7f358
LP
259 if (r == 0 || r == -ENOENT)
260 continue;
261 }
c2ce6a3d
LP
262 if (r < 0)
263 return r;
cd61c3bf 264
c2ce6a3d
LP
265 return 1;
266 }
267
5fc7f358 268 if (streq(name, ".host"))
27c88c4e 269 return image_make(".host", AT_FDCWD, NULL, "/", ret);
5fc7f358 270
c2ce6a3d
LP
271 return 0;
272};
273
274int image_discover(Hashmap *h) {
275 const char *path;
276 int r;
277
278 assert(h);
279
280 NULSTR_FOREACH(path, image_search_path) {
281 _cleanup_closedir_ DIR *d = NULL;
282 struct dirent *de;
283
284 d = opendir(path);
285 if (!d) {
286 if (errno == ENOENT)
a67a4c8c 287 continue;
c2ce6a3d
LP
288
289 return -errno;
290 }
291
292 FOREACH_DIRENT_ALL(de, d, return -errno) {
293 _cleanup_(image_unrefp) Image *image = NULL;
294
295 if (!image_name_is_valid(de->d_name))
296 continue;
297
298 if (hashmap_contains(h, de->d_name))
299 continue;
300
5fc7f358 301 r = image_make(NULL, dirfd(d), path, de->d_name, &image);
c2ce6a3d
LP
302 if (r == 0 || r == -ENOENT)
303 continue;
304 if (r < 0)
305 return r;
306
307 r = hashmap_put(h, image->name, image);
308 if (r < 0)
309 return r;
310
311 image = NULL;
cd61c3bf
LP
312 }
313 }
314
5fc7f358
LP
315 if (!hashmap_contains(h, ".host")) {
316 _cleanup_(image_unrefp) Image *image = NULL;
317
318 r = image_make(".host", AT_FDCWD, NULL, "/", &image);
319 if (r < 0)
320 return r;
321
322 r = hashmap_put(h, image->name, image);
323 if (r < 0)
324 return r;
325
326 image = NULL;
327
328 }
329
cd61c3bf
LP
330 return 0;
331}
332
333void image_hashmap_free(Hashmap *map) {
334 Image *i;
335
336 while ((i = hashmap_steal_first(map)))
337 image_unref(i);
338
339 hashmap_free(map);
340}
341
08682124 342int image_remove(Image *i) {
30535c16
LP
343 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
344 int r;
345
08682124
LP
346 assert(i);
347
348 if (path_equal(i->path, "/") ||
349 path_startswith(i->path, "/usr"))
350 return -EROFS;
351
30535c16
LP
352 /* Make sure we don't interfere with a running nspawn */
353 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
354 if (r < 0)
355 return r;
356
ebd93cb6
LP
357 switch (i->type) {
358
359 case IMAGE_SUBVOLUME:
08682124 360 return btrfs_subvol_remove(i->path);
ebd93cb6
LP
361
362 case IMAGE_DIRECTORY:
01b72568
LP
363 /* Allow deletion of read-only directories */
364 (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
365
366 /* fall through */
367
aceac2f0 368 case IMAGE_RAW:
08682124 369 return rm_rf_dangerous(i->path, false, true, false);
ebd93cb6
LP
370
371 default:
372 return -ENOTSUP;
373 }
374}
375
376int image_rename(Image *i, const char *new_name) {
30535c16 377 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
ebd93cb6 378 _cleanup_free_ char *new_path = NULL, *nn = NULL;
01b72568 379 unsigned file_attr = 0;
ebd93cb6
LP
380 int r;
381
382 assert(i);
383
384 if (!image_name_is_valid(new_name))
385 return -EINVAL;
386
387 if (path_equal(i->path, "/") ||
388 path_startswith(i->path, "/usr"))
389 return -EROFS;
390
30535c16
LP
391 /* Make sure we don't interfere with a running nspawn */
392 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
393 if (r < 0)
394 return r;
395
396 /* Make sure nobody takes the new name, between the time we
397 * checked it is currently unused in all search paths, and the
398 * time we take possesion of it */
399 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
400 if (r < 0)
401 return r;
402
ebd93cb6
LP
403 r = image_find(new_name, NULL);
404 if (r < 0)
405 return r;
406 if (r > 0)
407 return -EEXIST;
408
409 switch (i->type) {
410
ebd93cb6 411 case IMAGE_DIRECTORY:
01b72568
LP
412 /* Turn of the immutable bit while we rename the image, so that we can rename it */
413 (void) read_attr_path(i->path, &file_attr);
414
415 if (file_attr & FS_IMMUTABLE_FL)
416 (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
417
418 /* fall through */
419
420 case IMAGE_SUBVOLUME:
ebd93cb6
LP
421 new_path = file_in_same_dir(i->path, new_name);
422 break;
423
aceac2f0 424 case IMAGE_RAW: {
ebd93cb6
LP
425 const char *fn;
426
63c372cb 427 fn = strjoina(new_name, ".raw");
ebd93cb6
LP
428 new_path = file_in_same_dir(i->path, fn);
429 break;
430 }
431
432 default:
433 return -ENOTSUP;
434 }
435
436 if (!new_path)
437 return -ENOMEM;
438
439 nn = strdup(new_name);
440 if (!nn)
441 return -ENOMEM;
442
443 if (renameat2(AT_FDCWD, i->path, AT_FDCWD, new_path, RENAME_NOREPLACE) < 0)
444 return -errno;
445
01b72568
LP
446 /* Restore the immutable bit, if it was set before */
447 if (file_attr & FS_IMMUTABLE_FL)
448 (void) chattr_path(new_path, true, FS_IMMUTABLE_FL);
449
ebd93cb6
LP
450 free(i->path);
451 i->path = new_path;
452 new_path = NULL;
453
454 free(i->name);
455 i->name = nn;
456 nn = NULL;
457
458 return 0;
459}
460
461int image_clone(Image *i, const char *new_name, bool read_only) {
30535c16 462 _cleanup_release_lock_file_ LockFile name_lock = LOCK_FILE_INIT;
ebd93cb6
LP
463 const char *new_path;
464 int r;
465
466 assert(i);
467
468 if (!image_name_is_valid(new_name))
469 return -EINVAL;
470
30535c16
LP
471 /* Make sure nobody takes the new name, between the time we
472 * checked it is currently unused in all search paths, and the
473 * time we take possesion of it */
474 r = image_name_lock(new_name, LOCK_EX|LOCK_NB, &name_lock);
475 if (r < 0)
476 return r;
477
ebd93cb6
LP
478 r = image_find(new_name, NULL);
479 if (r < 0)
480 return r;
481 if (r > 0)
482 return -EEXIST;
483
484 switch (i->type) {
485
486 case IMAGE_SUBVOLUME:
487 case IMAGE_DIRECTORY:
63c372cb 488 new_path = strjoina("/var/lib/machines/", new_name);
ebd93cb6
LP
489
490 r = btrfs_subvol_snapshot(i->path, new_path, read_only, true);
491 break;
492
aceac2f0 493 case IMAGE_RAW:
63c372cb 494 new_path = strjoina("/var/lib/machines/", new_name, ".raw");
ebd93cb6 495
f2068bcc 496 r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
ebd93cb6
LP
497 break;
498
499 default:
500 return -ENOTSUP;
501 }
502
503 if (r < 0)
504 return r;
505
506 return 0;
507}
508
509int image_read_only(Image *i, bool b) {
30535c16 510 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
ebd93cb6
LP
511 int r;
512 assert(i);
513
514 if (path_equal(i->path, "/") ||
515 path_startswith(i->path, "/usr"))
516 return -EROFS;
517
30535c16
LP
518 /* Make sure we don't interfere with a running nspawn */
519 r = image_path_lock(i->path, LOCK_EX|LOCK_NB, &global_lock, &local_lock);
520 if (r < 0)
521 return r;
522
ebd93cb6
LP
523 switch (i->type) {
524
525 case IMAGE_SUBVOLUME:
526 r = btrfs_subvol_set_read_only(i->path, b);
527 if (r < 0)
528 return r;
01b72568
LP
529
530 break;
531
532 case IMAGE_DIRECTORY:
533 /* For simple directory trees we cannot use the access
534 mode of the top-level directory, since it has an
535 effect on the container itself. However, we can
536 use the "immutable" flag, to at least make the
537 top-level directory read-only. It's not as good as
538 a read-only subvolume, but at least something, and
539 we can read the value back.*/
540
541 r = chattr_path(i->path, b, FS_IMMUTABLE_FL);
542 if (r < 0)
543 return r;
544
ebd93cb6
LP
545 break;
546
aceac2f0 547 case IMAGE_RAW: {
ebd93cb6
LP
548 struct stat st;
549
550 if (stat(i->path, &st) < 0)
551 return -errno;
552
553 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
554 return -errno;
f2068bcc
LP
555
556 /* If the images is now read-only, it's a good time to
557 * defrag it, given that no write patterns will
558 * fragment it again. */
559 if (b)
560 (void) btrfs_defrag(i->path);
ebd93cb6
LP
561 break;
562 }
563
ebd93cb6
LP
564 default:
565 return -ENOTSUP;
566 }
567
568 return 0;
08682124
LP
569}
570
30535c16
LP
571int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) {
572 _cleanup_free_ char *p = NULL;
573 LockFile t = LOCK_FILE_INIT;
574 struct stat st;
575 int r;
576
577 assert(path);
578 assert(global);
579 assert(local);
580
581 /* Locks an image path. This actually creates two locks: one
582 * "local" one, next to the image path itself, which might be
583 * shared via NFS. And another "global" one, in /run, that
584 * uses the device/inode number. This has the benefit that we
585 * can even lock a tree that is a mount point, correctly. */
586
587 if (path_equal(path, "/"))
588 return -EBUSY;
589
590 if (!path_is_absolute(path))
591 return -EINVAL;
592
593 if (stat(path, &st) >= 0) {
594 if (asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
595 return -ENOMEM;
596 }
597
598 r = make_lock_file_for(path, operation, &t);
599 if (r < 0)
600 return r;
601
602 if (p) {
603 mkdir_p("/run/systemd/nspawn/locks", 0600);
604
605 r = make_lock_file(p, operation, global);
606 if (r < 0) {
607 release_lock_file(&t);
608 return r;
609 }
610 }
611
612 *local = t;
613 return 0;
614}
615
cb81cd80 616int image_set_limit(Image *i, uint64_t referenced_max) {
d6ce17c7
LP
617 assert(i);
618
619 if (path_equal(i->path, "/") ||
620 path_startswith(i->path, "/usr"))
621 return -EROFS;
622
623 if (i->type != IMAGE_SUBVOLUME)
624 return -ENOTSUP;
625
cb81cd80 626 return btrfs_quota_limit(i->path, referenced_max);
d6ce17c7
LP
627}
628
30535c16
LP
629int image_name_lock(const char *name, int operation, LockFile *ret) {
630 const char *p;
631
632 assert(name);
633 assert(ret);
634
635 /* Locks an image name, regardless of the precise path used. */
636
637 if (!image_name_is_valid(name))
638 return -EINVAL;
639
640 if (streq(name, ".host"))
641 return -EBUSY;
642
643 mkdir_p("/run/systemd/nspawn/locks", 0600);
63c372cb 644 p = strjoina("/run/systemd/nspawn/locks/name-", name);
30535c16
LP
645
646 return make_lock_file(p, operation, ret);
647}
648
649bool image_name_is_valid(const char *s) {
650 if (!filename_is_valid(s))
651 return false;
652
653 if (string_has_cc(s, NULL))
654 return false;
655
656 if (!utf8_is_valid(s))
657 return false;
658
659 /* Temporary files for atomically creating new files */
660 if (startswith(s, ".#"))
661 return false;
662
663 return true;
664}
665
cd61c3bf
LP
666static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
667 [IMAGE_DIRECTORY] = "directory",
668 [IMAGE_SUBVOLUME] = "subvolume",
aceac2f0 669 [IMAGE_RAW] = "raw",
cd61c3bf
LP
670};
671
672DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);