1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2016 Lennart Poettering
12 #include "architecture.h"
13 #include "dissect-image.h"
14 #include "hexdecoct.h"
16 #include "loop-util.h"
17 #include "string-util.h"
19 #include "user-util.h"
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;
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
);
46 static int parse_argv(int argc
, char *argv
[]) {
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
},
69 while ((c
= getopt_long(argc
, argv
, "hmr", options
, NULL
)) >= 0) {
81 arg_action
= ACTION_MOUNT
;
85 arg_flags
|= DISSECT_IMAGE_READ_ONLY
;
89 DissectImageFlags flags
;
91 if (streq(optarg
, "disabled"))
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
;
100 log_error("Unknown --discard= parameter: %s", optarg
);
103 arg_flags
= (arg_flags
& ~DISSECT_IMAGE_DISCARD_ANY
) | flags
;
108 case ARG_ROOT_HASH
: {
112 r
= unhexmem(optarg
, strlen(optarg
), &p
, &l
);
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
);
123 arg_root_hash_size
= l
;
131 assert_not_reached("Unhandled option");
136 switch (arg_action
) {
139 if (optind
+ 1 != argc
) {
140 log_error("Expected a file path as only argument.");
144 arg_image
= argv
[optind
];
145 arg_flags
|= DISSECT_IMAGE_READ_ONLY
;
149 if (optind
+ 2 != argc
) {
150 log_error("Expected a file path and mount point path as only arguments.");
154 arg_image
= argv
[optind
];
155 arg_path
= argv
[optind
+ 1];
159 assert_not_reached("Unknown action.");
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
;
171 log_parse_environment();
174 r
= parse_argv(argc
, argv
);
178 r
= loop_device_make_by_path(arg_image
, (arg_flags
& DISSECT_IMAGE_READ_ONLY
) ? O_RDONLY
: O_RDWR
, &d
);
180 log_error_errno(r
, "Failed to set up loopback device: %m");
184 if (!arg_root_hash
) {
185 r
= root_hash_load(arg_image
, &arg_root_hash
, &arg_root_hash_size
);
187 log_error_errno(r
, "Failed to read root hash file for %s: %m", arg_image
);
192 r
= dissect_image_and_warn(d
->fd
, arg_image
, arg_root_hash
, arg_root_hash_size
, arg_flags
, &m
);
196 switch (arg_action
) {
198 case ACTION_DISSECT
: {
201 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
202 DissectedPartition
*p
= m
->partitions
+ i
;
208 printf("Found %s '%s' partition",
209 p
->rw
? "writable" : "read-only",
210 partition_designator_to_string(i
));
212 if (!sd_id128_is_null(p
->uuid
))
213 printf(" (UUID " SD_ID128_FORMAT_STR
")", SD_ID128_FORMAT_VAL(p
->uuid
));
216 printf(" of type %s", p
->fstype
);
218 if (p
->architecture
!= _ARCHITECTURE_INVALID
)
219 printf(" for %s", architecture_to_string(p
->architecture
));
221 k
= PARTITION_VERITY_OF(i
);
223 printf(" %s verity", m
->partitions
[k
].found
? "with" : "without");
226 printf(" on partition #%i", p
->partno
);
229 printf(" (%s)", p
->node
);
234 r
= dissected_image_acquire_metadata(m
);
236 log_error_errno(r
, "Failed to acquire image metadata: %m");
241 printf(" Hostname: %s\n", m
->hostname
);
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
));
246 if (!strv_isempty(m
->machine_info
)) {
249 STRV_FOREACH_PAIR(p
, q
, m
->machine_info
)
251 p
== m
->machine_info
? "Mach. Info:" : " ",
255 if (!strv_isempty(m
->os_release
)) {
258 STRV_FOREACH_PAIR(p
, q
, m
->os_release
)
260 p
== m
->os_release
? "OS Release:" : " ",
268 r
= dissected_image_decrypt_interactively(m
, NULL
, arg_root_hash
, arg_root_hash_size
, arg_flags
, &di
);
272 r
= dissected_image_mount(m
, arg_path
, UID_INVALID
, arg_flags
);
274 log_error_errno(r
, "Failed to mount image: %m");
279 r
= decrypted_image_relinquish(di
);
281 log_error_errno(r
, "Failed to relinquish DM devices: %m");
286 loop_device_relinquish(d
);
290 assert_not_reached("Unknown action.");
295 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;