machine-image.c revision 63c372cb9df3bee01e3bf8cd7f96f336bddda846
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
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 <fcntl.h>
#include "strv.h"
#include "utf8.h"
#include "btrfs-util.h"
#include "path-util.h"
#include "copy.h"
#include "mkdir.h"
#include "machine-image.h"
static const char image_search_path[] =
if (!i)
return NULL;
free(i);
return NULL;
}
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 */
return -errno;
/* It's a btrfs subvolume */
if (r < 0)
return r;
r = image_new(IMAGE_SUBVOLUME,
path,
0,
ret);
if (r < 0)
return r;
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;
};
int image_discover(Hashmap *h) {
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);
}
int image_remove(Image *i) {
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:
return btrfs_subvol_remove(i->path);
case IMAGE_DIRECTORY:
/* Allow deletion of read-only directories */
/* fall through */
case IMAGE_RAW:
return rm_rf_dangerous(i->path, false, true, false);
default:
return -ENOTSUP;
}
}
_cleanup_release_lock_file_ LockFile global_lock = LOCK_FILE_INIT, local_lock = LOCK_FILE_INIT, name_lock = LOCK_FILE_INIT;
unsigned file_attr = 0;
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
return -EROFS;
/* 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 -ENOTSUP;
}
if (!new_path)
return -ENOMEM;
if (!nn)
return -ENOMEM;
return -errno;
/* Restore the immutable bit, if it was set before */
if (file_attr & FS_IMMUTABLE_FL)
return 0;
}
const char *new_path;
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
/* 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:
break;
case IMAGE_RAW:
break;
default:
return -ENOTSUP;
}
if (r < 0)
return r;
return 0;
}
int image_read_only(Image *i, bool b) {
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:
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 -ENOTSUP;
}
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;
}
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;
}
static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
[IMAGE_DIRECTORY] = "directory",
[IMAGE_SUBVOLUME] = "subvolume",
[IMAGE_RAW] = "raw",
};