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)) {
119 BtrfsSubvolInfo info;
121 /* It's a btrfs subvolume */
123 r = btrfs_subvol_get_info_fd(fd, &info);
127 r = image_new(IMAGE_SUBVOLUME,
141 /* It's just a normal directory. */
143 r = image_new(IMAGE_DIRECTORY,
155 } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
156 const char *truncated;
159 /* It's a GPT block device */
164 fd_getcrtime_at(dfd, name, &crtime, 0);
166 truncated = strndupa(name, strlen(name) - 4);
168 r = image_new(IMAGE_GPT,
171 !(st.st_mode & 0222),
173 timespec_load(&st.st_mtim),
184 int image_find(const char *name, Image **ret) {
190 /* There are no images with invalid names */
191 if (!image_name_is_valid(name))
194 NULSTR_FOREACH(path, image_search_path) {
195 _cleanup_closedir_ DIR *d = NULL;
205 r = image_make(dirfd(d), name, path, ret);
206 if (r == 0 || r == -ENOENT)
217 int image_discover(Hashmap *h) {
223 NULSTR_FOREACH(path, image_search_path) {
224 _cleanup_closedir_ DIR *d = NULL;
235 FOREACH_DIRENT_ALL(de, d, return -errno) {
236 _cleanup_(image_unrefp) Image *image = NULL;
238 if (!image_name_is_valid(de->d_name))
241 if (hashmap_contains(h, de->d_name))
244 r = image_make(dirfd(d), de->d_name, path, &image);
245 if (r == 0 || r == -ENOENT)
250 r = hashmap_put(h, image->name, image);
261 void image_hashmap_free(Hashmap *map) {
264 while ((i = hashmap_steal_first(map)))
270 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
271 [IMAGE_DIRECTORY] = "directory",
272 [IMAGE_SUBVOLUME] = "subvolume",
276 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);