]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/dissect-image.h
30a12cb54080c0e6d4015002079f7bac4fcbd98c
[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 "macro.h"
26
27 typedef struct DissectedImage DissectedImage;
28 typedef struct DissectedPartition DissectedPartition;
29 typedef struct DecryptedImage DecryptedImage;
30
31 struct DissectedPartition {
32 bool found:1;
33 bool rw:1;
34 int partno; /* -1 if there was no partition and the images contains a file system directly */
35 int architecture; /* Intended architecture: either native, secondary or unset (-1). */
36 sd_id128_t uuid; /* Partition entry UUID as reported by the GPT */
37 char *fstype;
38 char *node;
39 char *decrypted_node;
40 char *decrypted_fstype;
41 };
42
43 enum {
44 PARTITION_ROOT,
45 PARTITION_ROOT_SECONDARY, /* Secondary architecture */
46 PARTITION_HOME,
47 PARTITION_SRV,
48 PARTITION_ESP,
49 PARTITION_SWAP,
50 PARTITION_ROOT_VERITY, /* verity data for the PARTITION_ROOT partition */
51 PARTITION_ROOT_SECONDARY_VERITY, /* verity data for the PARTITION_ROOT_SECONDARY partition */
52 _PARTITION_DESIGNATOR_MAX,
53 _PARTITION_DESIGNATOR_INVALID = -1
54 };
55
56 static inline int PARTITION_VERITY_OF(int p) {
57 if (p == PARTITION_ROOT)
58 return PARTITION_ROOT_VERITY;
59 if (p == PARTITION_ROOT_SECONDARY)
60 return PARTITION_ROOT_SECONDARY_VERITY;
61 return _PARTITION_DESIGNATOR_INVALID;
62 }
63
64 typedef enum DissectImageFlags {
65 DISSECT_IMAGE_READ_ONLY = 1,
66 DISSECT_IMAGE_DISCARD_ON_LOOP = 2, /* Turn on "discard" if on a loop device and file system supports it */
67 DISSECT_IMAGE_DISCARD = 4, /* Turn on "discard" if file system supports it, on all block devices */
68 DISSECT_IMAGE_DISCARD_ON_CRYPTO = 8, /* Turn on "discard" also on crypto devices */
69 DISSECT_IMAGE_DISCARD_ANY = DISSECT_IMAGE_DISCARD_ON_LOOP |
70 DISSECT_IMAGE_DISCARD |
71 DISSECT_IMAGE_DISCARD_ON_CRYPTO,
72 DISSECT_IMAGE_GPT_ONLY = 16, /* Only recognize images with GPT partition tables */
73 DISSECT_IMAGE_REQUIRE_ROOT = 32, /* Don't accept disks without root partition */
74 } DissectImageFlags;
75
76 struct DissectedImage {
77 bool encrypted:1;
78 bool verity:1; /* verity available and usable */
79 bool can_verity:1; /* verity available, but not necessarily used */
80
81 DissectedPartition partitions[_PARTITION_DESIGNATOR_MAX];
82
83 char *hostname;
84 sd_id128_t machine_id;
85 char **machine_info;
86 char **os_release;
87 };
88
89 int dissect_image(int fd, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DissectedImage **ret);
90
91 DissectedImage* dissected_image_unref(DissectedImage *m);
92 DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);
93
94 int dissected_image_decrypt(DissectedImage *m, const char *passphrase, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DecryptedImage **ret);
95 int dissected_image_decrypt_interactively(DissectedImage *m, const char *passphrase, const void *root_hash, size_t root_hash_size, DissectImageFlags flags, DecryptedImage **ret);
96 int dissected_image_mount(DissectedImage *m, const char *dest, DissectImageFlags flags);
97
98 int dissected_image_acquire_metadata(DissectedImage *m);
99
100 DecryptedImage* decrypted_image_unref(DecryptedImage *p);
101 DEFINE_TRIVIAL_CLEANUP_FUNC(DecryptedImage*, decrypted_image_unref);
102 int decrypted_image_relinquish(DecryptedImage *d);
103
104 const char* partition_designator_to_string(int i) _const_;
105 int partition_designator_from_string(const char *name) _pure_;
106
107 int root_hash_load(const char *image, void **ret, size_t *ret_size);