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"
28 #include "bus-label.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 = strdup(path);
85 static int image_make(int dfd, const char *name, const char *path, Image **ret) {
92 /* We explicitly *do* follow symlinks here, since we want to
93 * allow symlinking trees into /var/lib/container/, and treat
96 if (fstatat(dfd, name, &st, 0) < 0)
99 if (S_ISDIR(st.st_mode)) {
104 /* btrfs subvolumes have inode 256 */
105 if (st.st_ino == 256) {
106 _cleanup_close_ int fd = -1;
109 fd = openat(dfd, name, O_CLOEXEC|O_NOCTTY|O_DIRECTORY);
113 if (fstatfs(fd, &sfs) < 0)
116 if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
120 /* It's a btrfs subvolume */
122 ro = btrfs_subvol_is_read_only_fd(fd);
126 /* r = btrfs_subvol_get_btime(fd, &btime); */
130 r = image_new(IMAGE_SUBVOLUME,
144 /* It's just a normal directory. */
146 r = image_new(IMAGE_DIRECTORY,
158 } else if (S_ISREG(st.st_mode) && endswith(name, ".gpt")) {
160 /* It's a GPT block device */
165 r = image_new(IMAGE_GPT,
168 !!(st.st_mode & 0111),
169 timespec_load(&st.st_mtim),
181 int image_find(const char *name, Image **ret) {
187 /* There are no images with invalid names */
188 if (!image_name_is_valid(name))
191 NULSTR_FOREACH(path, image_search_path) {
192 _cleanup_closedir_ DIR *d = NULL;
202 r = image_make(dirfd(d), name, path, ret);
203 if (r == 0 || r == -ENOENT)
214 int image_discover(Hashmap *h) {
220 NULSTR_FOREACH(path, image_search_path) {
221 _cleanup_closedir_ DIR *d = NULL;
232 FOREACH_DIRENT_ALL(de, d, return -errno) {
233 _cleanup_(image_unrefp) Image *image = NULL;
235 if (!image_name_is_valid(de->d_name))
238 if (hashmap_contains(h, de->d_name))
241 r = image_make(dirfd(d), de->d_name, path, &image);
242 if (r == 0 || r == -ENOENT)
247 r = hashmap_put(h, image->name, image);
258 void image_hashmap_free(Hashmap *map) {
261 while ((i = hashmap_steal_first(map)))
267 char *image_bus_path(const char *name) {
268 _cleanup_free_ char *e = NULL;
272 e = bus_label_escape(name);
276 return strappend("/org/freedesktop/machine1/image/", e);
279 static const char* const image_type_table[_IMAGE_TYPE_MAX] = {
280 [IMAGE_DIRECTORY] = "directory",
281 [IMAGE_SUBVOLUME] = "subvolume",
285 DEFINE_STRING_TABLE_LOOKUP(image_type, ImageType);