/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "alloc-util.h"
#include "btrfs-util.h"
#include "chattr-util.h"
#include "copy.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "fs-util.h"
#include "hashmap.h"
#include "lockfile-util.h"
#include "log.h"
#include "macro.h"
#include "machine-image.h"
#include "mkdir.h"
#include "path-util.h"
#include "rm-rf.h"
#include "string-table.h"
#include "string-util.h"
#include "strv.h"
#include "time-util.h"
#include "utf8.h"
#include "util.h"
#include "xattr-util.h"
static const char image_search_path[] =
if (!i)
return NULL;
free(i);
return NULL;
}
_cleanup_strv_free_ char **l = NULL;
char **ret;
const char *fn, *s;
unsigned i = 0;
l = new0(char*, 4);
if (!l)
return NULL;
if (!l[i])
return NULL;
i++;
}
if (!l[i])
return NULL;
ret = l;
l = NULL;
return ret;
}
static int image_new(
ImageType t,
const char *pretty,
const char *path,
const char *filename,
bool read_only,
assert(t >= 0);
assert(t < _IMAGE_TYPE_MAX);
if (!i)
return -ENOMEM;
i->type = t;
if (!i->name)
return -ENOMEM;
if (path)
else
if (!i->path)
return -ENOMEM;
path_kill_slashes(i->path);
*ret = i;
i = NULL;
return 0;
}
static int image_make(
const char *pretty,
int dfd,
const char *path,
const char *filename,
bool read_only;
int r;
/* We explicitly *do* follow symlinks here, since we want to
* them normally. */
return -errno;
unsigned file_attr = 0;
if (!ret)
return 1;
if (!pretty)
if (fd < 0)
return -errno;
/* btrfs subvolumes have inode 256 */
r = btrfs_is_filesystem(fd);
if (r < 0)
return r;
if (r) {
/* It's a btrfs subvolume */
if (r < 0)
return r;
r = image_new(IMAGE_SUBVOLUME,
path,
0,
ret);
if (r < 0)
return r;
if (btrfs_quota_scan_ongoing(fd) == 0) {
if (r >= 0) {
}
}
return 1;
}
}
/* If the IMMUTABLE bit is set, we consider the
* directory read-only. Since the ioctl is not
* supported everywhere we ignore failures. */
/* It's just a normal directory. */
r = image_new(IMAGE_DIRECTORY,
path,
0,
0,
ret);
if (r < 0)
return r;
return 1;
/* It's a RAW disk image */
if (!ret)
return 1;
if (!pretty)
path,
ret);
if (r < 0)
return r;
return 1;
}
return 0;
}
const char *path;
int r;
/* There are no images with invalid names */
if (!image_name_is_valid(name))
return 0;
if (!d) {
continue;
return -errno;
}
if (r == 0 || r == -ENOENT) {
if (!raw)
return -ENOMEM;
if (r == 0 || r == -ENOENT)
continue;
}
if (r < 0)
return r;
return 1;
}
return 0;
};
const char *path;
int r;
assert(h);
if (!d) {
continue;
return -errno;
}
continue;
continue;
if (r == 0 || r == -ENOENT)
continue;
if (r < 0)
return r;
if (r < 0)
return r;
}
}
if (!hashmap_contains(h, ".host")) {
if (r < 0)
return r;
if (r < 0)
return r;
}
return 0;
}
Image *i;
while ((i = hashmap_steal_first(map)))
image_unref(i);
}
char **j;
int r;
assert(i);
return -EROFS;
settings = image_settings_path(i);
if (!settings)
return -ENOMEM;
/* Make sure we don't interfere with a running nspawn */
if (r < 0)
return r;
switch (i->type) {
case IMAGE_SUBVOLUME:
if (r < 0)
return r;
break;
case IMAGE_DIRECTORY:
/* Allow deletion of read-only directories */
if (r < 0)
return r;
break;
case IMAGE_RAW:
return -errno;
break;
default:
return -EOPNOTSUPP;
}
}
return 0;
}
const char *fn;
if (!rs)
return -ENOMEM;
}
_cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
unsigned file_attr = 0;
char **j;
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
return -EROFS;
settings = image_settings_path(i);
if (!settings)
return -ENOMEM;
/* Make sure we don't interfere with a running nspawn */
if (r < 0)
return r;
/* Make sure nobody takes the new name, between the time we
* checked it is currently unused in all search paths, and the
* time we take possesion of it */
if (r < 0)
return r;
if (r < 0)
return r;
if (r > 0)
return -EEXIST;
switch (i->type) {
case IMAGE_DIRECTORY:
/* Turn of the immutable bit while we rename the image, so that we can rename it */
if (file_attr & FS_IMMUTABLE_FL)
/* fall through */
case IMAGE_SUBVOLUME:
break;
case IMAGE_RAW: {
const char *fn;
break;
}
default:
return -EOPNOTSUPP;
}
if (!new_path)
return -ENOMEM;
if (!nn)
return -ENOMEM;
if (r < 0)
return r;
/* Restore the immutable bit, if it was set before */
if (file_attr & FS_IMMUTABLE_FL)
r = rename_settings_file(*j, new_name);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to rename settings file %s, ignoring: %m", *j);
}
return 0;
}
const char *fn;
if (!rs)
return -ENOMEM;
}
const char *new_path;
char **j;
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
settings = image_settings_path(i);
if (!settings)
return -ENOMEM;
/* Make sure nobody takes the new name, between the time we
* checked it is currently unused in all search paths, and the
* time we take possesion of it */
if (r < 0)
return r;
if (r < 0)
return r;
if (r > 0)
return -EEXIST;
switch (i->type) {
case IMAGE_SUBVOLUME:
case IMAGE_DIRECTORY:
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);
/* Enable "subtree" quotas for the copy, if we didn't
* copy any quota from the source. */
(void) btrfs_subvol_auto_qgroup(i->path, 0, true);
break;
case IMAGE_RAW:
break;
default:
return -EOPNOTSUPP;
}
if (r < 0)
return r;
r = clone_settings_file(*j, new_name);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to clone settings %s, ignoring: %m", *j);
}
return 0;
}
int r;
assert(i);
return -EROFS;
/* Make sure we don't interfere with a running nspawn */
if (r < 0)
return r;
switch (i->type) {
case IMAGE_SUBVOLUME:
/* Note that we set the flag only on the top-level
* subvolume of the image. */
r = btrfs_subvol_set_read_only(i->path, b);
if (r < 0)
return r;
break;
case IMAGE_DIRECTORY:
/* For simple directory trees we cannot use the access
mode of the top-level directory, since it has an
effect on the container itself. However, we can
use the "immutable" flag, to at least make the
top-level directory read-only. It's not as good as
a read-only subvolume, but at least something, and
we can read the value back.*/
if (r < 0)
return r;
break;
case IMAGE_RAW: {
return -errno;
return -errno;
/* If the images is now read-only, it's a good time to
* defrag it, given that no write patterns will
* fragment it again. */
if (b)
(void) btrfs_defrag(i->path);
break;
}
default:
return -EOPNOTSUPP;
}
return 0;
}
_cleanup_free_ char *p = NULL;
LockFile t = LOCK_FILE_INIT;
int r;
/* Locks an image path. This actually creates two locks: one
* "local" one, next to the image path itself, which might be
* shared via NFS. And another "global" one, in /run, that
* can even lock a tree that is a mount point, correctly. */
return -EBUSY;
if (!path_is_absolute(path))
return -EINVAL;
if (asprintf(&p, "/run/systemd/nspawn/locks/inode-%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
return -ENOMEM;
}
if (r < 0)
return r;
if (p) {
if (r < 0) {
release_lock_file(&t);
return r;
}
}
*local = t;
return 0;
}
assert(i);
return -EROFS;
if (i->type != IMAGE_SUBVOLUME)
return -EOPNOTSUPP;
/* We set the quota both for the subvolume as well as for the
* subtree. The latter is mostly for historical reasons, since
* we didn't use to have a concept of subtree quota, and hence
* only modified the subvolume quota. */
(void) btrfs_subvol_auto_qgroup(i->path, 0, true);
}
const char *p;
/* Locks an image name, regardless of the precise path used. */
if (!image_name_is_valid(name))
return -EINVAL;
return -EBUSY;
}
bool image_name_is_valid(const char *s) {
if (!filename_is_valid(s))
return false;
if (string_has_cc(s, NULL))
return false;
if (!utf8_is_valid(s))
return false;
/* Temporary files for atomically creating new files */
if (startswith(s, ".#"))
return false;
return true;
}
[IMAGE_DIRECTORY] = "directory",
[IMAGE_SUBVOLUME] = "subvolume",
[IMAGE_RAW] = "raw",
};