]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/image.c
4f59c578883a5794e97afe8a6e40e76818944b09
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
[] =
32 "/var/lib/container\0"
33 "/usr/local/lib/machines\0"
34 "/usr/lib/machines\0";
36 Image
*image_unref(Image
*i
) {
55 _cleanup_(image_unrefp
) Image
*i
= NULL
;
58 assert(t
< _IMAGE_TYPE_MAX
);
67 i
->read_only
= read_only
;
71 i
->name
= strdup(name
);
76 i
->path
= strjoin(path
, "/", name
, NULL
);
80 path_kill_slashes(i
->path
);
89 static int image_make(int dfd
, const char *name
, const char *path
, Image
**ret
) {
97 /* We explicitly *do* follow symlinks here, since we want to
98 * allow symlinking trees into /var/lib/container/, and treat
101 if (fstatat(dfd
, name
, &st
, 0) < 0)
104 writable
= faccessat(dfd
, name
, W_OK
, AT_EACCESS
) >= 0;
106 if (S_ISDIR(st
.st_mode
)) {
111 /* btrfs subvolumes have inode 256 */
112 if (st
.st_ino
== 256) {
113 _cleanup_close_
int fd
= -1;
116 fd
= openat(dfd
, name
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
120 if (fstatfs(fd
, &sfs
) < 0)
123 if (F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
)) {
124 BtrfsSubvolInfo info
;
126 /* It's a btrfs subvolume */
128 r
= btrfs_subvol_get_info_fd(fd
, &info
);
132 r
= image_new(IMAGE_SUBVOLUME
,
135 info
.read_only
|| !writable
,
146 /* It's just a normal directory. */
148 r
= image_new(IMAGE_DIRECTORY
,
160 } else if (S_ISREG(st
.st_mode
) && endswith(name
, ".gpt")) {
161 const char *truncated
;
164 /* It's a GPT block device */
169 fd_getcrtime_at(dfd
, name
, &crtime
, 0);
171 truncated
= strndupa(name
, strlen(name
) - 4);
173 r
= image_new(IMAGE_GPT
,
176 !(st
.st_mode
& 0222) || !writable
,
178 timespec_load(&st
.st_mtim
),
189 int image_find(const char *name
, Image
**ret
) {
195 /* There are no images with invalid names */
196 if (!image_name_is_valid(name
))
199 NULSTR_FOREACH(path
, image_search_path
) {
200 _cleanup_closedir_
DIR *d
= NULL
;
210 r
= image_make(dirfd(d
), name
, path
, ret
);
211 if (r
== 0 || r
== -ENOENT
)
222 int image_discover(Hashmap
*h
) {
228 NULSTR_FOREACH(path
, image_search_path
) {
229 _cleanup_closedir_
DIR *d
= NULL
;
240 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
241 _cleanup_(image_unrefp
) Image
*image
= NULL
;
243 if (!image_name_is_valid(de
->d_name
))
246 if (hashmap_contains(h
, de
->d_name
))
249 r
= image_make(dirfd(d
), de
->d_name
, path
, &image
);
250 if (r
== 0 || r
== -ENOENT
)
255 r
= hashmap_put(h
, image
->name
, image
);
266 void image_hashmap_free(Hashmap
*map
) {
269 while ((i
= hashmap_steal_first(map
)))
275 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
276 [IMAGE_DIRECTORY
] = "directory",
277 [IMAGE_SUBVOLUME
] = "subvolume",
281 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);