]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/dissect/dissect.c
0e12f51be6c4211cc399746717fad817927c0bb5
[thirdparty/systemd.git] / src / dissect / dissect.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <getopt.h>
6
7 #include "architecture.h"
8 #include "dissect-image.h"
9 #include "hexdecoct.h"
10 #include "log.h"
11 #include "loop-util.h"
12 #include "main-func.h"
13 #include "string-util.h"
14 #include "strv.h"
15 #include "user-util.h"
16 #include "util.h"
17
18 static enum {
19 ACTION_DISSECT,
20 ACTION_MOUNT,
21 } arg_action = ACTION_DISSECT;
22 static const char *arg_image = NULL;
23 static const char *arg_path = NULL;
24 static DissectImageFlags arg_flags = DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_DISCARD_ON_LOOP;
25 static void *arg_root_hash = NULL;
26 static size_t arg_root_hash_size = 0;
27
28 STATIC_DESTRUCTOR_REGISTER(arg_root_hash, freep);
29
30 static void help(void) {
31 printf("%s [OPTIONS...] IMAGE\n"
32 "%s [OPTIONS...] --mount IMAGE PATH\n"
33 "Dissect a file system OS image.\n\n"
34 " -h --help Show this help\n"
35 " --version Show package version\n"
36 " -m --mount Mount the image to the specified directory\n"
37 " -r --read-only Mount read-only\n"
38 " --discard=MODE Choose 'discard' mode (disabled, loop, all, crypto)\n"
39 " --root-hash=HASH Specify root hash for verity\n",
40 program_invocation_short_name,
41 program_invocation_short_name);
42 }
43
44 static int parse_argv(int argc, char *argv[]) {
45
46 enum {
47 ARG_VERSION = 0x100,
48 ARG_DISCARD,
49 ARG_ROOT_HASH,
50 };
51
52 static const struct option options[] = {
53 { "help", no_argument, NULL, 'h' },
54 { "version", no_argument, NULL, ARG_VERSION },
55 { "mount", no_argument, NULL, 'm' },
56 { "read-only", no_argument, NULL, 'r' },
57 { "discard", required_argument, NULL, ARG_DISCARD },
58 { "root-hash", required_argument, NULL, ARG_ROOT_HASH },
59 {}
60 };
61
62 int c, r;
63
64 assert(argc >= 0);
65 assert(argv);
66
67 while ((c = getopt_long(argc, argv, "hmr", options, NULL)) >= 0) {
68
69 switch (c) {
70
71 case 'h':
72 help();
73 return 0;
74
75 case ARG_VERSION:
76 return version();
77
78 case 'm':
79 arg_action = ACTION_MOUNT;
80 break;
81
82 case 'r':
83 arg_flags |= DISSECT_IMAGE_READ_ONLY;
84 break;
85
86 case ARG_DISCARD: {
87 DissectImageFlags flags;
88
89 if (streq(optarg, "disabled"))
90 flags = 0;
91 else if (streq(optarg, "loop"))
92 flags = DISSECT_IMAGE_DISCARD_ON_LOOP;
93 else if (streq(optarg, "all"))
94 flags = DISSECT_IMAGE_DISCARD_ON_LOOP | DISSECT_IMAGE_DISCARD;
95 else if (streq(optarg, "crypt"))
96 flags = DISSECT_IMAGE_DISCARD_ANY;
97 else {
98 log_error("Unknown --discard= parameter: %s", optarg);
99 return -EINVAL;
100 }
101 arg_flags = (arg_flags & ~DISSECT_IMAGE_DISCARD_ANY) | flags;
102
103 break;
104 }
105
106 case ARG_ROOT_HASH: {
107 void *p;
108 size_t l;
109
110 r = unhexmem(optarg, strlen(optarg), &p, &l);
111 if (r < 0)
112 return log_error_errno(r, "Failed to parse root hash '%s': %m", optarg);
113 if (l < sizeof(sd_id128_t)) {
114 log_error("Root hash must be at least 128bit long: %s", optarg);
115 free(p);
116 return -EINVAL;
117 }
118
119 free(arg_root_hash);
120 arg_root_hash = p;
121 arg_root_hash_size = l;
122 break;
123 }
124
125 case '?':
126 return -EINVAL;
127
128 default:
129 assert_not_reached("Unhandled option");
130 }
131
132 }
133
134 switch (arg_action) {
135
136 case ACTION_DISSECT:
137 if (optind + 1 != argc) {
138 log_error("Expected a file path as only argument.");
139 return -EINVAL;
140 }
141
142 arg_image = argv[optind];
143 arg_flags |= DISSECT_IMAGE_READ_ONLY;
144 break;
145
146 case ACTION_MOUNT:
147 if (optind + 2 != argc) {
148 log_error("Expected a file path and mount point path as only arguments.");
149 return -EINVAL;
150 }
151
152 arg_image = argv[optind];
153 arg_path = argv[optind + 1];
154 break;
155
156 default:
157 assert_not_reached("Unknown action.");
158 }
159
160 return 1;
161 }
162
163 static int run(int argc, char *argv[]) {
164 _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
165 _cleanup_(decrypted_image_unrefp) DecryptedImage *di = NULL;
166 _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
167 int r;
168
169 log_parse_environment();
170 log_open();
171
172 r = parse_argv(argc, argv);
173 if (r <= 0)
174 return r;
175
176 r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, &d);
177 if (r < 0)
178 return log_error_errno(r, "Failed to set up loopback device: %m");
179
180 if (!arg_root_hash) {
181 r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size);
182 if (r < 0)
183 return log_error_errno(r, "Failed to read root hash file for %s: %m", arg_image);
184 }
185
186 r = dissect_image_and_warn(d->fd, arg_image, arg_root_hash, arg_root_hash_size, arg_flags, &m);
187 if (r < 0)
188 return r;
189
190 switch (arg_action) {
191
192 case ACTION_DISSECT: {
193 unsigned i;
194
195 for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
196 DissectedPartition *p = m->partitions + i;
197 int k;
198
199 if (!p->found)
200 continue;
201
202 printf("Found %s '%s' partition",
203 p->rw ? "writable" : "read-only",
204 partition_designator_to_string(i));
205
206 if (!sd_id128_is_null(p->uuid))
207 printf(" (UUID " SD_ID128_FORMAT_STR ")", SD_ID128_FORMAT_VAL(p->uuid));
208
209 if (p->fstype)
210 printf(" of type %s", p->fstype);
211
212 if (p->architecture != _ARCHITECTURE_INVALID)
213 printf(" for %s", architecture_to_string(p->architecture));
214
215 k = PARTITION_VERITY_OF(i);
216 if (k >= 0)
217 printf(" %s verity", m->partitions[k].found ? "with" : "without");
218
219 if (p->partno >= 0)
220 printf(" on partition #%i", p->partno);
221
222 if (p->node)
223 printf(" (%s)", p->node);
224
225 putchar('\n');
226 }
227
228 r = dissected_image_acquire_metadata(m);
229 if (r < 0)
230 return log_error_errno(r, "Failed to acquire image metadata: %m");
231
232 if (m->hostname)
233 printf(" Hostname: %s\n", m->hostname);
234
235 if (!sd_id128_is_null(m->machine_id))
236 printf("Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->machine_id));
237
238 if (!strv_isempty(m->machine_info)) {
239 char **p, **q;
240
241 STRV_FOREACH_PAIR(p, q, m->machine_info)
242 printf("%s %s=%s\n",
243 p == m->machine_info ? "Mach. Info:" : " ",
244 *p, *q);
245 }
246
247 if (!strv_isempty(m->os_release)) {
248 char **p, **q;
249
250 STRV_FOREACH_PAIR(p, q, m->os_release)
251 printf("%s %s=%s\n",
252 p == m->os_release ? "OS Release:" : " ",
253 *p, *q);
254 }
255
256 break;
257 }
258
259 case ACTION_MOUNT:
260 r = dissected_image_decrypt_interactively(m, NULL, arg_root_hash, arg_root_hash_size, arg_flags, &di);
261 if (r < 0)
262 return r;
263
264 r = dissected_image_mount(m, arg_path, UID_INVALID, arg_flags);
265 if (r < 0)
266 return log_error_errno(r, "Failed to mount image: %m");
267
268 if (di) {
269 r = decrypted_image_relinquish(di);
270 if (r < 0)
271 return log_error_errno(r, "Failed to relinquish DM devices: %m");
272 }
273
274 loop_device_relinquish(d);
275 break;
276
277 default:
278 assert_not_reached("Unknown action.");
279 }
280
281 return 0;
282 }
283
284 DEFINE_MAIN_FUNCTION(run);