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) {
94 /* We explicitly *do* follow symlinks here, since we want to
95 * allow symlinking trees into /var/lib/container/, and treat
98 if (fstatat(dfd, name, &st, 0) < 0)
101 if (S_ISDIR(st.st_mode)) {
106 /* btrfs subvolumes have inode 256 */
107 if (st.st_ino == 256) {
108 _cleanup_close_ int fd = -1;
111 fd = openat(dfd, name, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
115 if (fstatfs(fd, &sfs) < 0)
118 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
122 /* It's a btrfs subvolume */
124 ro = btrfs_subvol_is_read_only_fd(fd);
128 /* r = btrfs_subvol_get_btime(fd, &btime); */
132 r = image_new(IMAGE_SUBVOLUME,
146 /* It's just a normal directory. */
148 r = image_new(IMAGE_DIRECTORY,
160 } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
162 /* It's a GPT block device */
167 r = image_new(IMAGE_GPT,
170 !!(st.st_mode & 0111),
171 timespec_load(&st.st_mtim),
183 int image_find(const char *name, Image **ret) {
189 /* There are no images with invalid names */
190 if (!image_name_is_valid(name))
193 NULSTR_FOREACH(path, image_search_path) {
194 _cleanup_closedir_ DIR *d = NULL;
204 r = image_make(dirfd(d), name, path, ret);
205 if (r == 0 || r == -ENOENT)
216 int image_discover(Hashmap *h) {
222 NULSTR_FOREACH(path, image_search_path) {
223 _cleanup_closedir_ DIR *d = NULL;
234 FOREACH_DIRENT_ALL(de, d, return -errno) {
235 _cleanup_(image_unrefp) Image *image = NULL;
237 if (!image_name_is_valid(de->d_name))
240 if (hashmap_contains(h, de->d_name))
243 r = image_make(dirfd(d), de->d_name, path, &image);
244 if (r == 0 || r == -ENOENT)
249 r = hashmap_put(h, image->name, image);
260 void image_hashmap_free(Hashmap *map) {
263 while ((i = hashmap_steal_first(map)))
269 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
270 [IMAGE_DIRECTORY] = "directory",
271 [IMAGE_SUBVOLUME] = "subvolume",
275 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);