]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.h
Merge pull request #7816 from poettering/chase-pid
[thirdparty/systemd.git] / src / shared / dissect-image.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2016 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <stdbool.h>
24
25 #include "sd-id128.h"
26
27 #include "macro.h"
28
29 typedef struct DissectedImage DissectedImage;
30 typedef struct DissectedPartition DissectedPartition;
31 typedef struct DecryptedImage DecryptedImage;
32
33 struct DissectedPartition {
34 bool found:1;
35 bool rw:1;
36 int partno; /* -1 if there was no partition and the images contains a file system directly */
37 int architecture; /* Intended architecture: either native, secondary or unset (-1). */
38 sd_id128_t uuid; /* Partition entry UUID as reported by the GPT */
39 char *fstype;
40 char *node;
41 char *decrypted_node;
42 char *decrypted_fstype;
43 };
44
45 enum {
46 PARTITION_ROOT,
47 PARTITION_ROOT_SECONDARY, /* Secondary architecture */
48 PARTITION_HOME,
49 PARTITION_SRV,
50 PARTITION_ESP,
51 PARTITION_SWAP,
52 PARTITION_ROOT_VERITY, /* verity data for the PARTITION_ROOT partition */
53 PARTITION_ROOT_SECONDARY_VERITY, /* verity data for the PARTITION_ROOT_SECONDARY partition */
54 _PARTITION_DESIGNATOR_MAX,
55 _PARTITION_DESIGNATOR_INVALID = -1
56 };
57
58 static inline int PARTITION_VERITY_OF(int p) {
59 if (p == PARTITION_ROOT)
60 return PARTITION_ROOT_VERITY;
61 if (p == PARTITION_ROOT_SECONDARY)
62 return PARTITION_ROOT_SECONDARY_VERITY;
63 return _PARTITION_DESIGNATOR_INVALID;
64 }
65
66 typedef enum DissectImageFlags {
67 DISSECT_IMAGE_READ_ONLY = 1 << 0,
68 DISSECT_IMAGE_DISCARD_ON_LOOP = 1 << 1, /* Turn on "discard" if on a loop device and file system supports it */
69 DISSECT_IMAGE_DISCARD = 1 << 2, /* Turn on "discard" if file system supports it, on all block devices */
70 DISSECT_IMAGE_DISCARD_ON_CRYPTO = 1 << 3, /* Turn on "discard" also on crypto devices */
71 DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP |
72 DISSECT_IMAGE_DISCARD |
73 DISSECT_IMAGE_DISCARD_ON_CRYPTO,
74 DISSECT_IMAGE_GPT_ONLY = 1 << 4, /* Only recognize images with GPT partition tables */
75 DISSECT_IMAGE_REQUIRE_ROOT = 1 << 5, /* Don't accept disks without root partition */
76 DISSECT_IMAGE_MOUNT_ROOT_ONLY = 1 << 6, /* Mount only the root partition */
77 DISSECT_IMAGE_MOUNT_NON_ROOT_ONLY = 1 << 7, /* Mount only non-root partitions */
78 } DissectImageFlags;
79
80 struct DissectedImage {
81 bool encrypted:1;
82 bool verity:1; /* verity available and usable */
83 bool can_verity:1; /* verity available, but not necessarily used */
84
85 DissectedPartition partitions[_PARTITION_DESIGNATOR_MAX];
86
87 char *hostname;
88 sd_id128_t machine_id;
89 char **machine_info;
90 char **os_release;
91 };
92
93 int probe_filesystem(const char *node, char **ret_fstype);
94 int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DissectedImage **ret);
95
96 DissectedImage* dissected_image_unref(DissectedImage *m);
97 DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);
98
99 int dissected_image_decrypt(DissectedImage *m, const char *passphrase, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DecryptedImage **ret);
100 int dissected_image_decrypt_interactively(DissectedImage *m, const char *passphrase, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DecryptedImage **ret);
101 int dissected_image_mount(DissectedImage *m, const char *dest, uid_t uid_shift, DissectImageFlags flags);
102
103 int dissected_image_acquire_metadata(DissectedImage *m);
104
105 DecryptedImage* decrypted_image_unref(DecryptedImage *p);
106 DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref);
107 int decrypted_image_relinquish(DecryptedImage *d);
108
109 const char* partition_designator_to_string(int i) _const_;
110 int partition_designator_from_string(const char *name) _pure_;
111
112 int root_hash_load(const char *image, void **ret, size_t *ret_size);