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>
26 #include "btrfs-util.h"
27 #include "path-util.h"
30 static const char image_search_path[] =
31 "/var/lib/container\0"
34 Image *image_unref(Image *i) {
53 _cleanup_(image_unrefp) Image *i = NULL;
56 assert(t < _IMAGE_TYPE_MAX);
65 i->read_only = read_only;
69 i->name = strdup(name);
74 i->path = strjoin(path, "/", name, NULL);
78 path_kill_slashes(i->path);
87 static int image_make(int dfd, const char *name, const char *path, Image **ret) {
95 /* We explicitly *do* follow symlinks here, since we want to
96 * allow symlinking trees into /var/lib/container/, and treat
99 if (fstatat(dfd, name, &st, 0) < 0)
102 writable = faccessat(dfd, name, W_OK, AT_EACCESS) >= 0;
104 if (S_ISDIR(st.st_mode)) {
109 /* btrfs subvolumes have inode 256 */
110 if (st.st_ino == 256) {
111 _cleanup_close_ int fd = -1;
114 fd = openat(dfd, name, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
118 if (fstatfs(fd, &sfs) < 0)
121 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
122 BtrfsSubvolInfo info;
124 /* It's a btrfs subvolume */
126 r = btrfs_subvol_get_info_fd(fd, &info);
130 r = image_new(IMAGE_SUBVOLUME,
133 info.read_only || !writable,
144 /* It's just a normal directory. */
146 r = image_new(IMAGE_DIRECTORY,
158 } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
159 const char *truncated;
162 /* It's a GPT block device */
167 fd_getcrtime_at(dfd, name, &crtime, 0);
169 truncated = strndupa(name, strlen(name) - 4);
171 r = image_new(IMAGE_GPT,
174 !(st.st_mode & 0222) || !writable,
176 timespec_load(&st.st_mtim),
187 int image_find(const char *name, Image **ret) {
193 /* There are no images with invalid names */
194 if (!image_name_is_valid(name))
197 NULSTR_FOREACH(path, image_search_path) {
198 _cleanup_closedir_ DIR *d = NULL;
208 r = image_make(dirfd(d), name, path, ret);
209 if (r == 0 || r == -ENOENT)
220 int image_discover(Hashmap *h) {
226 NULSTR_FOREACH(path, image_search_path) {
227 _cleanup_closedir_ DIR *d = NULL;
238 FOREACH_DIRENT_ALL(de, d, return -errno) {
239 _cleanup_(image_unrefp) Image *image = NULL;
241 if (!image_name_is_valid(de->d_name))
244 if (hashmap_contains(h, de->d_name))
247 r = image_make(dirfd(d), de->d_name, path, &image);
248 if (r == 0 || r == -ENOENT)
253 r = hashmap_put(h, image->name, image);
264 void image_hashmap_free(Hashmap *map) {
267 while ((i = hashmap_steal_first(map)))
273 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
274 [IMAGE_DIRECTORY] = "directory",
275 [IMAGE_SUBVOLUME] = "subvolume",
279 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);