1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
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.
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.
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/>.
22 #include <sys/statfs.h>
27 #include "btrfs-util.h"
28 #include "path-util.h"
31 #include "machine-image.h"
33 static const char image_search_path[] =
35 "/var/lib/container\0"
36 "/usr/local/lib/machines\0"
37 "/usr/lib/machines\0";
39 Image *image_unref(Image *i) {
59 _cleanup_(image_unrefp) Image *i = NULL;
62 assert(t < _IMAGE_TYPE_MAX);
72 i->read_only = read_only;
75 i->usage = i->usage_exclusive = (uint64_t) -1;
76 i->limit = i->limit_exclusive = (uint64_t) -1;
78 i->name = strdup(pretty);
83 i->path = strjoin(path, "/", filename, NULL);
85 i->path = strdup(filename);
90 path_kill_slashes(i->path);
98 static int image_make(
102 const char *filename,
111 /* We explicitly *do* follow symlinks here, since we want to
112 * allow symlinking trees into /var/lib/machines/, and treat
115 if (fstatat(dfd, filename, &st, 0) < 0)
119 (path && path_startswith(path, "/usr")) ||
120 (faccessat(dfd, filename, W_OK, AT_EACCESS) < 0 && errno == EROFS);
122 if (S_ISDIR(st.st_mode)) {
123 _cleanup_close_ int fd = -1;
124 unsigned file_attr = 0;
132 fd = openat(dfd, filename, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
136 /* btrfs subvolumes have inode 256 */
137 if (st.st_ino == 256) {
140 if (fstatfs(fd, &sfs) < 0)
143 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
144 BtrfsSubvolInfo info;
145 BtrfsQuotaInfo quota;
147 /* It's a btrfs subvolume */
149 r = btrfs_subvol_get_info_fd(fd, &info);
153 r = image_new(IMAGE_SUBVOLUME,
157 info.read_only || read_only,
164 r = btrfs_subvol_get_quota_fd(fd, "a);
166 (*ret)->usage = quota.referenced;
167 (*ret)->usage_exclusive = quota.exclusive;
169 (*ret)->limit = quota.referenced_max;
170 (*ret)->limit_exclusive = quota.exclusive_max;
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);
182 /* It's just a normal directory. */
183 r = image_new(IMAGE_DIRECTORY,
187 read_only || (file_attr & FS_IMMUTABLE_FL),
196 } else if (S_ISREG(st.st_mode) && endswith(filename, ".raw")) {
199 /* It's a RAW disk image */
204 fd_getcrtime_at(dfd, filename, &crtime, 0);
207 pretty = strndupa(filename, strlen(filename) - 4);
209 r = image_new(IMAGE_RAW,
213 !(st.st_mode & 0222) || read_only,
215 timespec_load(&st.st_mtim),
220 (*ret)->usage = (*ret)->usage_exclusive = st.st_blocks * 512;
221 (*ret)->limit = (*ret)->limit_exclusive = st.st_size;
229 int image_find(const char *name, Image **ret) {
235 /* There are no images with invalid names */
236 if (!image_name_is_valid(name))
239 NULSTR_FOREACH(path, image_search_path) {
240 _cleanup_closedir_ DIR *d = NULL;
250 r = image_make(NULL, dirfd(d), path, name, ret);
251 if (r == 0 || r == -ENOENT) {
252 _cleanup_free_ char *raw = NULL;
254 raw = strappend(name, ".raw");
258 r = image_make(NULL, dirfd(d), path, raw, ret);
259 if (r == 0 || r == -ENOENT)
268 if (streq(name, ".host"))
269 return image_make(".host", AT_FDCWD, NULL, "/", ret);
274 int image_discover(Hashmap *h) {
280 NULSTR_FOREACH(path, image_search_path) {
281 _cleanup_closedir_ DIR *d = NULL;
292 FOREACH_DIRENT_ALL(de, d, return -errno) {
293 _cleanup_(image_unrefp) Image *image = NULL;
295 if (!image_name_is_valid(de->d_name))
298 if (hashmap_contains(h, de->d_name))
301 r = image_make(NULL, dirfd(d), path, de->d_name, &image);
302 if (r == 0 || r == -ENOENT)
307 r = hashmap_put(h, image->name, image);
315 if (!hashmap_contains(h, ".host")) {
316 _cleanup_(image_unrefp) Image *image = NULL;
318 r = image_make(".host", AT_FDCWD, NULL, "/", &image);
322 r = hashmap_put(h, image->name, image);
333 void image_hashmap_free(Hashmap *map) {
336 while ((i = hashmap_steal_first(map)))
342 int image_remove(Image *i) {
343 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
348 if (path_equal(i->path, "/") ||
349 path_startswith(i->path, "/usr"))
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);
359 case IMAGE_SUBVOLUME:
360 return btrfs_subvol_remove(i->path);
362 case IMAGE_DIRECTORY:
363 /* Allow deletion of read-only directories */
364 (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
369 return rm_rf_dangerous(i->path, false, true, false);
376 int image_rename(Image *i, const char *new_name) {
377 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
378 _cleanup_free_ char *new_path = NULL, *nn = NULL;
379 unsigned file_attr = 0;
384 if (!image_name_is_valid(new_name))
387 if (path_equal(i->path, "/") ||
388 path_startswith(i->path, "/usr"))
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);
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);
403 r = image_find(new_name, NULL);
411 case IMAGE_DIRECTORY:
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);
415 if (file_attr & FS_IMMUTABLE_FL)
416 (void) chattr_path(i->path, false, FS_IMMUTABLE_FL);
420 case IMAGE_SUBVOLUME:
421 new_path = file_in_same_dir(i->path, new_name);
427 fn = strjoina(new_name, ".raw");
428 new_path = file_in_same_dir(i->path, fn);
439 nn = strdup(new_name);
443 if (renameat2(AT_FDCWD, i->path, AT_FDCWD, new_path, RENAME_NOREPLACE) < 0)
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);
461 int image_clone(Image *i, const char *new_name, bool read_only) {
462 _cleanup_release_lock_file_ LockFile name_lock = LOCK_FILE_INIT;
463 const char *new_path;
468 if (!image_name_is_valid(new_name))
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);
478 r = image_find(new_name, NULL);
486 case IMAGE_SUBVOLUME:
487 case IMAGE_DIRECTORY:
488 new_path = strjoina("/var/lib/machines/", new_name);
490 r = btrfs_subvol_snapshot(i->path, new_path, read_only, true);
494 new_path = strjoina("/var/lib/machines/", new_name, ".raw");
496 r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
509 int image_read_only(Image *i, bool b) {
510 _cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT;
514 if (path_equal(i->path, "/") ||
515 path_startswith(i->path, "/usr"))
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);
525 case IMAGE_SUBVOLUME:
526 r = btrfs_subvol_set_read_only(i->path, b);
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.*/
541 r = chattr_path(i->path, b, FS_IMMUTABLE_FL);
550 if (stat(i->path, &st) < 0)
553 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
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. */
560 (void) btrfs_defrag(i->path);
571 int image_path_lock(const char *path, int operation, LockFile *global, LockFile *local) {
572 _cleanup_free_ char *p = NULL;
573 LockFile t = LOCK_FILE_INIT;
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. */
587 if (path_equal(path, "/"))
590 if (!path_is_absolute(path))
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)
598 r = make_lock_file_for(path, operation, &t);
603 mkdir_p("/run/systemd/nspawn/locks", 0600);
605 r = make_lock_file(p, operation, global);
607 release_lock_file(&t);
616 int image_set_limit(Image *i, uint64_t referenced_max) {
619 if (path_equal(i->path, "/") ||
620 path_startswith(i->path, "/usr"))
623 if (i->type != IMAGE_SUBVOLUME)
626 return btrfs_quota_limit(i->path, referenced_max);
629 int image_name_lock(const char *name, int operation, LockFile *ret) {
635 /* Locks an image name, regardless of the precise path used. */
637 if (!image_name_is_valid(name))
640 if (streq(name, ".host"))
643 mkdir_p("/run/systemd/nspawn/locks", 0600);
644 p = strjoina("/run/systemd/nspawn/locks/name-", name);
646 return make_lock_file(p, operation, ret);
649 bool image_name_is_valid(const char *s) {
650 if (!filename_is_valid(s))
653 if (string_has_cc(s, NULL))
656 if (!utf8_is_valid(s))
659 /* Temporary files for atomically creating new files */
660 if (startswith(s, ".#"))
666 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
667 [IMAGE_DIRECTORY] = "directory",
668 [IMAGE_SUBVOLUME] = "subvolume",
672 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);