]>
Commit | Line | Data |
---|---|---|
a2ea3b2f LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2016 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
20 | #include <fcntl.h> | |
21 | #include <stdio.h> | |
22 | #include <getopt.h> | |
23 | ||
24 | #include "architecture.h" | |
25 | #include "dissect-image.h" | |
4623e8e6 | 26 | #include "hexdecoct.h" |
a2ea3b2f LP |
27 | #include "log.h" |
28 | #include "loop-util.h" | |
29 | #include "string-util.h" | |
30 | #include "util.h" | |
31 | ||
32 | static enum { | |
33 | ACTION_DISSECT, | |
34 | ACTION_MOUNT, | |
35 | } arg_action = ACTION_DISSECT; | |
36 | static const char *arg_image = NULL; | |
37 | static const char *arg_path = NULL; | |
e0f9e7bd | 38 | static DissectImageFlags arg_flags = DISSECT_IMAGE_REQUIRE_ROOT|DISSECT_IMAGE_DISCARD_ON_LOOP; |
4623e8e6 LP |
39 | static void *arg_root_hash = NULL; |
40 | static size_t arg_root_hash_size = 0; | |
a2ea3b2f LP |
41 | |
42 | static void help(void) { | |
43 | printf("%s [OPTIONS...] IMAGE\n" | |
44 | "%s [OPTIONS...] --mount IMAGE PATH\n" | |
45 | "Dissect a file system OS image.\n\n" | |
46 | " -h --help Show this help\n" | |
47 | " --version Show package version\n" | |
48 | " -m --mount Mount the image to the specified directory\n" | |
18b5886e | 49 | " -r --read-only Mount read-only\n" |
4623e8e6 LP |
50 | " --discard=MODE Choose 'discard' mode (disabled, loop, all, crypto)\n" |
51 | " --root-hash=HASH Specify root hash for verity\n", | |
a2ea3b2f LP |
52 | program_invocation_short_name, |
53 | program_invocation_short_name); | |
54 | } | |
55 | ||
56 | static int parse_argv(int argc, char *argv[]) { | |
57 | ||
58 | enum { | |
59 | ARG_VERSION = 0x100, | |
18b5886e | 60 | ARG_DISCARD, |
4623e8e6 | 61 | ARG_ROOT_HASH, |
a2ea3b2f LP |
62 | }; |
63 | ||
64 | static const struct option options[] = { | |
65 | { "help", no_argument, NULL, 'h' }, | |
66 | { "version", no_argument, NULL, ARG_VERSION }, | |
67 | { "mount", no_argument, NULL, 'm' }, | |
68 | { "read-only", no_argument, NULL, 'r' }, | |
18b5886e | 69 | { "discard", required_argument, NULL, ARG_DISCARD }, |
4623e8e6 | 70 | { "root-hash", required_argument, NULL, ARG_ROOT_HASH }, |
a2ea3b2f LP |
71 | {} |
72 | }; | |
73 | ||
4623e8e6 | 74 | int c, r; |
a2ea3b2f LP |
75 | |
76 | assert(argc >= 0); | |
77 | assert(argv); | |
78 | ||
79 | while ((c = getopt_long(argc, argv, "hmr", options, NULL)) >= 0) { | |
80 | ||
81 | switch (c) { | |
82 | ||
83 | case 'h': | |
84 | help(); | |
85 | return 0; | |
86 | ||
87 | case ARG_VERSION: | |
88 | return version(); | |
89 | ||
90 | case 'm': | |
91 | arg_action = ACTION_MOUNT; | |
92 | break; | |
93 | ||
94 | case 'r': | |
18b5886e LP |
95 | arg_flags |= DISSECT_IMAGE_READ_ONLY; |
96 | break; | |
97 | ||
971e2ef0 ZJS |
98 | case ARG_DISCARD: { |
99 | DissectImageFlags flags; | |
100 | ||
18b5886e | 101 | if (streq(optarg, "disabled")) |
971e2ef0 | 102 | flags = 0; |
18b5886e | 103 | else if (streq(optarg, "loop")) |
971e2ef0 | 104 | flags = DISSECT_IMAGE_DISCARD_ON_LOOP; |
18b5886e | 105 | else if (streq(optarg, "all")) |
971e2ef0 | 106 | flags = DISSECT_IMAGE_DISCARD_ON_LOOP | DISSECT_IMAGE_DISCARD; |
18b5886e | 107 | else if (streq(optarg, "crypt")) |
971e2ef0 | 108 | flags = DISSECT_IMAGE_DISCARD_ANY; |
18b5886e LP |
109 | else { |
110 | log_error("Unknown --discard= parameter: %s", optarg); | |
111 | return -EINVAL; | |
112 | } | |
971e2ef0 | 113 | arg_flags = (arg_flags & ~DISSECT_IMAGE_DISCARD_ANY) | flags; |
18b5886e | 114 | |
a2ea3b2f | 115 | break; |
971e2ef0 | 116 | } |
a2ea3b2f | 117 | |
4623e8e6 LP |
118 | case ARG_ROOT_HASH: { |
119 | void *p; | |
120 | size_t l; | |
121 | ||
122 | r = unhexmem(optarg, strlen(optarg), &p, &l); | |
123 | if (r < 0) | |
124 | return log_error_errno(r, "Failed to parse root hash: %s", optarg); | |
125 | if (l < sizeof(sd_id128_t)) { | |
126 | log_error("Root hash must be at least 128bit long: %s", optarg); | |
127 | free(p); | |
128 | return -EINVAL; | |
129 | } | |
130 | ||
131 | free(arg_root_hash); | |
132 | arg_root_hash = p; | |
133 | arg_root_hash_size = l; | |
134 | break; | |
135 | } | |
136 | ||
a2ea3b2f LP |
137 | case '?': |
138 | return -EINVAL; | |
139 | ||
140 | default: | |
141 | assert_not_reached("Unhandled option"); | |
142 | } | |
143 | ||
144 | } | |
145 | ||
146 | switch (arg_action) { | |
147 | ||
148 | case ACTION_DISSECT: | |
149 | if (optind + 1 != argc) { | |
150 | log_error("Expected a file path as only argument."); | |
151 | return -EINVAL; | |
152 | } | |
153 | ||
154 | arg_image = argv[optind]; | |
18b5886e | 155 | arg_flags |= DISSECT_IMAGE_READ_ONLY; |
a2ea3b2f LP |
156 | break; |
157 | ||
158 | case ACTION_MOUNT: | |
159 | if (optind + 2 != argc) { | |
160 | log_error("Expected a file path and mount point path as only arguments."); | |
161 | return -EINVAL; | |
162 | } | |
163 | ||
164 | arg_image = argv[optind]; | |
165 | arg_path = argv[optind + 1]; | |
166 | break; | |
167 | ||
168 | default: | |
169 | assert_not_reached("Unknown action."); | |
170 | } | |
171 | ||
172 | return 1; | |
173 | } | |
174 | ||
175 | int main(int argc, char *argv[]) { | |
176 | _cleanup_(loop_device_unrefp) LoopDevice *d = NULL; | |
18b5886e | 177 | _cleanup_(decrypted_image_unrefp) DecryptedImage *di = NULL; |
a2ea3b2f LP |
178 | _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL; |
179 | int r; | |
180 | ||
181 | log_parse_environment(); | |
182 | log_open(); | |
183 | ||
184 | r = parse_argv(argc, argv); | |
185 | if (r <= 0) | |
186 | goto finish; | |
187 | ||
18b5886e | 188 | r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, &d); |
a2ea3b2f LP |
189 | if (r < 0) { |
190 | log_error_errno(r, "Failed to set up loopback device: %m"); | |
191 | goto finish; | |
192 | } | |
193 | ||
78ebe980 LP |
194 | if (!arg_root_hash) { |
195 | r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size); | |
196 | if (r < 0) { | |
197 | log_error_errno(r, "Failed to read root hash file for %s: %m", arg_image); | |
198 | goto finish; | |
199 | } | |
200 | } | |
201 | ||
e0f9e7bd | 202 | r = dissect_image(d->fd, arg_root_hash, arg_root_hash_size, arg_flags, &m); |
a2ea3b2f LP |
203 | if (r == -ENOPKG) { |
204 | log_error_errno(r, "Couldn't identify a suitable partition table or file system in %s.", arg_image); | |
205 | goto finish; | |
206 | } | |
4623e8e6 LP |
207 | if (r == -EADDRNOTAVAIL) { |
208 | log_error_errno(r, "No root partition for specified root hash found in %s.", arg_image); | |
209 | goto finish; | |
210 | } | |
94c4c622 LP |
211 | if (r == -ENOTUNIQ) { |
212 | log_error_errno(r, "Multiple suitable root partitions found in image %s.", arg_image); | |
213 | goto finish; | |
214 | } | |
215 | if (r == -ENXIO) { | |
216 | log_error_errno(r, "No suitable root partition found in image %s.", arg_image); | |
217 | goto finish; | |
218 | } | |
759aaedc LP |
219 | if (r == -EPROTONOSUPPORT) { |
220 | log_error_errno(r, "Device %s is loopback block device with partition scanning turned off, please turn it on.", arg_image); | |
221 | goto finish; | |
222 | } | |
a2ea3b2f LP |
223 | if (r < 0) { |
224 | log_error_errno(r, "Failed to dissect image: %m"); | |
225 | goto finish; | |
226 | } | |
227 | ||
228 | switch (arg_action) { | |
229 | ||
230 | case ACTION_DISSECT: { | |
231 | unsigned i; | |
232 | ||
233 | for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) { | |
234 | DissectedPartition *p = m->partitions + i; | |
4623e8e6 | 235 | int k; |
a2ea3b2f LP |
236 | |
237 | if (!p->found) | |
238 | continue; | |
239 | ||
240 | printf("Found %s '%s' partition", | |
241 | p->rw ? "writable" : "read-only", | |
242 | partition_designator_to_string(i)); | |
243 | ||
be30ad41 LP |
244 | if (!sd_id128_is_null(p->uuid)) |
245 | printf(" (UUID " SD_ID128_FORMAT_STR ")", SD_ID128_FORMAT_VAL(p->uuid)); | |
246 | ||
a2ea3b2f LP |
247 | if (p->fstype) |
248 | printf(" of type %s", p->fstype); | |
249 | ||
250 | if (p->architecture != _ARCHITECTURE_INVALID) | |
251 | printf(" for %s", architecture_to_string(p->architecture)); | |
252 | ||
4623e8e6 LP |
253 | k = PARTITION_VERITY_OF(i); |
254 | if (k >= 0) | |
255 | printf(" %s verity", m->partitions[k].found ? "with" : "without"); | |
256 | ||
a2ea3b2f LP |
257 | if (p->partno >= 0) |
258 | printf(" on partition #%i", p->partno); | |
259 | ||
260 | if (p->node) | |
261 | printf(" (%s)", p->node); | |
262 | ||
263 | putchar('\n'); | |
264 | } | |
265 | ||
266 | break; | |
267 | } | |
268 | ||
269 | case ACTION_MOUNT: | |
4623e8e6 | 270 | r = dissected_image_decrypt_interactively(m, NULL, arg_root_hash, arg_root_hash_size, arg_flags, &di); |
18b5886e LP |
271 | if (r < 0) |
272 | goto finish; | |
273 | ||
274 | r = dissected_image_mount(m, arg_path, arg_flags); | |
a2ea3b2f LP |
275 | if (r < 0) { |
276 | log_error_errno(r, "Failed to mount image: %m"); | |
277 | goto finish; | |
278 | } | |
279 | ||
18b5886e LP |
280 | if (di) { |
281 | r = decrypted_image_relinquish(di); | |
282 | if (r < 0) { | |
283 | log_error_errno(r, "Failed to relinquish DM devices: %m"); | |
284 | goto finish; | |
285 | } | |
286 | } | |
287 | ||
a2ea3b2f LP |
288 | loop_device_relinquish(d); |
289 | break; | |
290 | ||
291 | default: | |
292 | assert_not_reached("Unknown action."); | |
293 | } | |
294 | ||
295 | finish: | |
4623e8e6 | 296 | free(arg_root_hash); |
a2ea3b2f LP |
297 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
298 | } |