]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/machine/image.c
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
) {
56 _cleanup_(image_unrefp
) Image
*i
= NULL
;
59 assert(t
< _IMAGE_TYPE_MAX
);
69 i
->read_only
= read_only
;
73 i
->name
= strdup(pretty
);
78 i
->path
= strjoin(path
, "/", filename
, NULL
);
80 i
->path
= strdup(filename
);
85 path_kill_slashes(i
->path
);
93 static int image_make(
106 /* We explicitly *do* follow symlinks here, since we want to
107 * allow symlinking trees into /var/lib/container/, and treat
110 if (fstatat(dfd
, filename
, &st
, 0) < 0)
114 (path
&& path_startswith(path
, "/usr")) ||
115 (faccessat(dfd
, filename
, W_OK
, AT_EACCESS
) < 0 && errno
== EROFS
);
117 if (S_ISDIR(st
.st_mode
)) {
125 /* btrfs subvolumes have inode 256 */
126 if (st
.st_ino
== 256) {
127 _cleanup_close_
int fd
= -1;
130 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_NOCTTY
|O_DIRECTORY
);
134 if (fstatfs(fd
, &sfs
) < 0)
137 if (F_TYPE_EQUAL(sfs
.f_type
, BTRFS_SUPER_MAGIC
)) {
138 BtrfsSubvolInfo info
;
140 /* It's a btrfs subvolume */
142 r
= btrfs_subvol_get_info_fd(fd
, &info
);
146 r
= image_new(IMAGE_SUBVOLUME
,
150 info
.read_only
|| read_only
,
161 /* It's just a normal directory. */
163 r
= image_new(IMAGE_DIRECTORY
,
176 } else if (S_ISREG(st
.st_mode
) && endswith(filename
, ".gpt")) {
179 /* It's a GPT block device */
184 fd_getcrtime_at(dfd
, filename
, &crtime
, 0);
187 pretty
= strndupa(filename
, strlen(filename
) - 4);
189 r
= image_new(IMAGE_GPT
,
193 !(st
.st_mode
& 0222) || read_only
,
195 timespec_load(&st
.st_mtim
),
206 int image_find(const char *name
, Image
**ret
) {
212 /* There are no images with invalid names */
213 if (!image_name_is_valid(name
))
216 NULSTR_FOREACH(path
, image_search_path
) {
217 _cleanup_closedir_
DIR *d
= NULL
;
227 r
= image_make(NULL
, dirfd(d
), path
, name
, ret
);
228 if (r
== 0 || r
== -ENOENT
) {
229 _cleanup_free_
char *gpt
= NULL
;
231 gpt
= strappend(name
, ".gpt");
235 r
= image_make(NULL
, dirfd(d
), path
, gpt
, ret
);
236 if (r
== 0 || r
== -ENOENT
)
245 if (streq(name
, ".host"))
246 return image_make(".host", AT_FDCWD
, NULL
, "/", ret
);
251 int image_discover(Hashmap
*h
) {
257 NULSTR_FOREACH(path
, image_search_path
) {
258 _cleanup_closedir_
DIR *d
= NULL
;
269 FOREACH_DIRENT_ALL(de
, d
, return -errno
) {
270 _cleanup_(image_unrefp
) Image
*image
= NULL
;
272 if (!image_name_is_valid(de
->d_name
))
275 if (hashmap_contains(h
, de
->d_name
))
278 r
= image_make(NULL
, dirfd(d
), path
, de
->d_name
, &image
);
279 if (r
== 0 || r
== -ENOENT
)
284 r
= hashmap_put(h
, image
->name
, image
);
292 if (!hashmap_contains(h
, ".host")) {
293 _cleanup_(image_unrefp
) Image
*image
= NULL
;
295 r
= image_make(".host", AT_FDCWD
, NULL
, "/", &image
);
299 r
= hashmap_put(h
, image
->name
, image
);
310 void image_hashmap_free(Hashmap
*map
) {
313 while ((i
= hashmap_steal_first(map
)))
319 static const char* const image_type_table
[_IMAGE_TYPE_MAX
] = {
320 [IMAGE_DIRECTORY
] = "directory",
321 [IMAGE_SUBVOLUME
] = "subvolume",
325 DEFINE_STRING_TABLE_LOOKUP(image_type
, ImageType
);