machine-image.c revision f2068bcce01db31cdc9422f44185f3b49c04d2ce
/*-*- 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 "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;
if (!ret)
return 1;
if (!pretty)
/* btrfs subvolumes have inode 256 */
if (fd < 0)
return -errno;
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;
}
}
/* 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 GPT block device */
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 (!gpt)
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) {
assert(i);
return -EROFS;
switch (i->type) {
case IMAGE_SUBVOLUME:
return btrfs_subvol_remove(i->path);
case IMAGE_DIRECTORY:
case IMAGE_GPT:
return rm_rf_dangerous(i->path, false, true, false);
default:
return -ENOTSUP;
}
}
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
return -EROFS;
if (r < 0)
return r;
if (r > 0)
return -EEXIST;
switch (i->type) {
case IMAGE_SUBVOLUME:
case IMAGE_DIRECTORY:
break;
case IMAGE_GPT: {
const char *fn;
break;
}
default:
return -ENOTSUP;
}
if (!new_path)
return -ENOMEM;
if (!nn)
return -ENOMEM;
return -errno;
return 0;
}
const char *new_path;
int r;
assert(i);
if (!image_name_is_valid(new_name))
return -EINVAL;
if (r < 0)
return r;
if (r > 0)
return -EEXIST;
switch (i->type) {
case IMAGE_SUBVOLUME:
case IMAGE_DIRECTORY:
break;
case IMAGE_GPT:
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;
switch (i->type) {
case IMAGE_SUBVOLUME:
r = btrfs_subvol_set_read_only(i->path, b);
if (r < 0)
return r;
break;
case IMAGE_GPT: {
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;
}
case IMAGE_DIRECTORY:
default:
return -ENOTSUP;
}
return 0;
}
static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
[IMAGE_DIRECTORY] = "directory",
[IMAGE_SUBVOLUME] = "subvolume",
[IMAGE_GPT] = "gpt",
};