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