1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include <linux/loop.h>
8 #include "architecture.h"
9 #include "dissect-image.h"
10 #include "hexdecoct.h"
12 #include "loop-util.h"
13 #include "main-func.h"
14 #include "string-util.h"
16 #include "user-util.h"
22 } arg_action
= ACTION_DISSECT
;
23 static const char *arg_image
= NULL
;
24 static const char *arg_path
= NULL
;
25 static DissectImageFlags arg_flags
= DISSECT_IMAGE_REQUIRE_ROOT
|DISSECT_IMAGE_DISCARD_ON_LOOP
;
26 static void *arg_root_hash
= NULL
;
27 static size_t arg_root_hash_size
= 0;
29 STATIC_DESTRUCTOR_REGISTER(arg_root_hash
, freep
);
31 static void help(void) {
32 printf("%s [OPTIONS...] IMAGE\n"
33 "%s [OPTIONS...] --mount IMAGE PATH\n"
34 "Dissect a file system OS image.\n\n"
35 " -h --help Show this help\n"
36 " --version Show package version\n"
37 " -m --mount Mount the image to the specified directory\n"
38 " -r --read-only Mount read-only\n"
39 " --discard=MODE Choose 'discard' mode (disabled, loop, all, crypto)\n"
40 " --root-hash=HASH Specify root hash for verity\n",
41 program_invocation_short_name
,
42 program_invocation_short_name
);
45 static int parse_argv(int argc
, char *argv
[]) {
53 static const struct option options
[] = {
54 { "help", no_argument
, NULL
, 'h' },
55 { "version", no_argument
, NULL
, ARG_VERSION
},
56 { "mount", no_argument
, NULL
, 'm' },
57 { "read-only", no_argument
, NULL
, 'r' },
58 { "discard", required_argument
, NULL
, ARG_DISCARD
},
59 { "root-hash", required_argument
, NULL
, ARG_ROOT_HASH
},
68 while ((c
= getopt_long(argc
, argv
, "hmr", options
, NULL
)) >= 0) {
80 arg_action
= ACTION_MOUNT
;
84 arg_flags
|= DISSECT_IMAGE_READ_ONLY
;
88 DissectImageFlags flags
;
90 if (streq(optarg
, "disabled"))
92 else if (streq(optarg
, "loop"))
93 flags
= DISSECT_IMAGE_DISCARD_ON_LOOP
;
94 else if (streq(optarg
, "all"))
95 flags
= DISSECT_IMAGE_DISCARD_ON_LOOP
| DISSECT_IMAGE_DISCARD
;
96 else if (streq(optarg
, "crypt"))
97 flags
= DISSECT_IMAGE_DISCARD_ANY
;
99 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
100 "Unknown --discard= parameter: %s",
102 arg_flags
= (arg_flags
& ~DISSECT_IMAGE_DISCARD_ANY
) | flags
;
107 case ARG_ROOT_HASH
: {
111 r
= unhexmem(optarg
, strlen(optarg
), &p
, &l
);
113 return log_error_errno(r
, "Failed to parse root hash '%s': %m", optarg
);
114 if (l
< sizeof(sd_id128_t
)) {
115 log_error("Root hash must be at least 128bit long: %s", optarg
);
122 arg_root_hash_size
= l
;
130 assert_not_reached("Unhandled option");
135 switch (arg_action
) {
138 if (optind
+ 1 != argc
)
139 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
140 "Expected a file path as only argument.");
142 arg_image
= argv
[optind
];
143 arg_flags
|= DISSECT_IMAGE_READ_ONLY
;
147 if (optind
+ 2 != argc
)
148 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
149 "Expected a file path and mount point path as only arguments.");
151 arg_image
= argv
[optind
];
152 arg_path
= argv
[optind
+ 1];
156 assert_not_reached("Unknown action.");
162 static int run(int argc
, char *argv
[]) {
163 _cleanup_(loop_device_unrefp
) LoopDevice
*d
= NULL
;
164 _cleanup_(decrypted_image_unrefp
) DecryptedImage
*di
= NULL
;
165 _cleanup_(dissected_image_unrefp
) DissectedImage
*m
= NULL
;
168 log_parse_environment();
171 r
= parse_argv(argc
, argv
);
175 r
= loop_device_make_by_path(arg_image
, (arg_flags
& DISSECT_IMAGE_READ_ONLY
) ? O_RDONLY
: O_RDWR
, LO_FLAGS_PARTSCAN
, &d
);
177 return log_error_errno(r
, "Failed to set up loopback device: %m");
179 if (!arg_root_hash
) {
180 r
= root_hash_load(arg_image
, &arg_root_hash
, &arg_root_hash_size
);
182 return log_error_errno(r
, "Failed to read root hash file for %s: %m", arg_image
);
185 r
= dissect_image_and_warn(d
->fd
, arg_image
, arg_root_hash
, arg_root_hash_size
, arg_flags
, &m
);
189 switch (arg_action
) {
191 case ACTION_DISSECT
: {
194 for (i
= 0; i
< _PARTITION_DESIGNATOR_MAX
; i
++) {
195 DissectedPartition
*p
= m
->partitions
+ i
;
201 printf("Found %s '%s' partition",
202 p
->rw
? "writable" : "read-only",
203 partition_designator_to_string(i
));
205 if (!sd_id128_is_null(p
->uuid
))
206 printf(" (UUID " SD_ID128_FORMAT_STR
")", SD_ID128_FORMAT_VAL(p
->uuid
));
209 printf(" of type %s", p
->fstype
);
211 if (p
->architecture
!= _ARCHITECTURE_INVALID
)
212 printf(" for %s", architecture_to_string(p
->architecture
));
214 k
= PARTITION_VERITY_OF(i
);
216 printf(" %s verity", m
->partitions
[k
].found
? "with" : "without");
219 printf(" on partition #%i", p
->partno
);
222 printf(" (%s)", p
->node
);
227 r
= dissected_image_acquire_metadata(m
);
229 return log_error_errno(r
, "Failed to acquire image metadata: %m");
232 printf(" Hostname: %s\n", m
->hostname
);
234 if (!sd_id128_is_null(m
->machine_id
))
235 printf("Machine ID: " SD_ID128_FORMAT_STR
"\n", SD_ID128_FORMAT_VAL(m
->machine_id
));
237 if (!strv_isempty(m
->machine_info
)) {
240 STRV_FOREACH_PAIR(p
, q
, m
->machine_info
)
242 p
== m
->machine_info
? "Mach. Info:" : " ",
246 if (!strv_isempty(m
->os_release
)) {
249 STRV_FOREACH_PAIR(p
, q
, m
->os_release
)
251 p
== m
->os_release
? "OS Release:" : " ",
259 r
= dissected_image_decrypt_interactively(m
, NULL
, arg_root_hash
, arg_root_hash_size
, arg_flags
, &di
);
263 r
= dissected_image_mount(m
, arg_path
, UID_INVALID
, arg_flags
);
265 return log_error_errno(r
, "Failed to mount image: %m");
268 r
= decrypted_image_relinquish(di
);
270 return log_error_errno(r
, "Failed to relinquish DM devices: %m");
273 loop_device_relinquish(d
);
277 assert_not_reached("Unknown action.");
283 DEFINE_MAIN_FUNCTION(run
);