Skip to content

Commit 89d00f2

Browse files
committed
dissect: beef up dissection output
Let's use a proper table for outputting partition information. Let's also put the general information about the image first, and the table after that. Moreover, dissect the image before showing any output, so that we can early on return an error if the image is not valid.
1 parent e3659eb commit 89d00f2

1 file changed

Lines changed: 78 additions & 37 deletions

File tree

src/dissect/dissect.c

Lines changed: 78 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "copy.h"
1212
#include "dissect-image.h"
1313
#include "fd-util.h"
14+
#include "format-table.h"
1415
#include "format-util.h"
1516
#include "fs-util.h"
1617
#include "hexdecoct.h"
@@ -364,53 +365,26 @@ static int run(int argc, char *argv[]) {
364365
switch (arg_action) {
365366

366367
case ACTION_DISSECT: {
368+
_cleanup_(table_unrefp) Table *t = NULL;
367369
uint64_t size;
368-
unsigned i;
369370

370-
for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
371-
DissectedPartition *p = m->partitions + i;
372-
373-
if (!p->found)
374-
continue;
375-
376-
printf("Found %s '%s' partition",
377-
p->rw ? "writable" : "read-only",
378-
partition_designator_to_string(i));
379-
380-
if (!sd_id128_is_null(p->uuid))
381-
printf(" (UUID " SD_ID128_FORMAT_STR ")", SD_ID128_FORMAT_VAL(p->uuid));
382-
383-
if (p->fstype)
384-
printf(" of type %s", p->fstype);
385-
386-
if (p->architecture != _ARCHITECTURE_INVALID)
387-
printf(" for %s", architecture_to_string(p->architecture));
388-
389-
if (dissected_image_can_do_verity(m, i))
390-
printf(" %s verity", dissected_image_has_verity(m, i) ? "with" : "without");
391-
392-
if (p->partno >= 0)
393-
printf(" on partition #%i", p->partno);
394-
395-
if (p->node)
396-
printf(" (%s)", p->node);
397-
398-
putchar('\n');
399-
}
371+
r = dissected_image_acquire_metadata(m);
372+
if (r == -EMEDIUMTYPE)
373+
return log_error_errno(r, "Not a valid OS image, no os-release file included.");
374+
if (r == -ENXIO)
375+
return log_error_errno(r, "No root partition discovered.");
376+
if (r < 0)
377+
return log_error_errno(r, "Failed to acquire image metadata: %m");
400378

401379
printf(" Name: %s\n", basename(arg_image));
402380

403381
if (ioctl(d->fd, BLKGETSIZE64, &size) < 0)
404382
log_debug_errno(errno, "Failed to query size of loopback device: %m");
405383
else {
406-
char t[FORMAT_BYTES_MAX];
407-
printf(" Size: %s\n", format_bytes(t, sizeof(t), size));
384+
char s[FORMAT_BYTES_MAX];
385+
printf(" Size: %s\n", format_bytes(s, sizeof(s), size));
408386
}
409387

410-
r = dissected_image_acquire_metadata(m);
411-
if (r < 0)
412-
return log_error_errno(r, "Failed to acquire image metadata: %m");
413-
414388
if (m->hostname)
415389
printf(" Hostname: %s\n", m->hostname);
416390

@@ -435,6 +409,73 @@ static int run(int argc, char *argv[]) {
435409
*p, *q);
436410
}
437411

412+
putc('\n', stdout);
413+
414+
t = table_new("rw", "designator", "partition uuid", "fstype", "architecture", "verity", "node", "partno");
415+
if (!t)
416+
return log_oom();
417+
418+
(void) table_set_empty_string(t, "-");
419+
(void) table_set_align_percent(t, table_get_cell(t, 0, 7), 100);
420+
421+
for (unsigned i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
422+
DissectedPartition *p = m->partitions + i;
423+
424+
if (!p->found)
425+
continue;
426+
427+
r = table_add_many(
428+
t,
429+
TABLE_STRING, p->rw ? "rw" : "ro",
430+
TABLE_STRING, partition_designator_to_string(i));
431+
if (r < 0)
432+
return table_log_add_error(r);
433+
434+
if (sd_id128_is_null(p->uuid))
435+
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
436+
else
437+
r = table_add_cell(t, NULL, TABLE_UUID, &p->uuid);
438+
if (r < 0)
439+
return table_log_add_error(r);
440+
441+
r = table_add_many(
442+
t,
443+
TABLE_STRING, p->fstype,
444+
TABLE_STRING, architecture_to_string(p->architecture));
445+
if (r < 0)
446+
return table_log_add_error(r);
447+
448+
if (arg_verity_data)
449+
r = table_add_cell(t, NULL, TABLE_STRING, "external");
450+
else if (dissected_image_can_do_verity(m, i))
451+
r = table_add_cell(t, NULL, TABLE_STRING, yes_no(dissected_image_has_verity(m, i)));
452+
else
453+
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
454+
if (r < 0)
455+
return table_log_add_error(r);
456+
457+
458+
if (p->partno < 0) /* no partition table, naked file system */ {
459+
r = table_add_cell(t, NULL, TABLE_STRING, arg_image);
460+
if (r < 0)
461+
return table_log_add_error(r);
462+
463+
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL);
464+
} else {
465+
r = table_add_cell(t, NULL, TABLE_STRING, p->node);
466+
if (r < 0)
467+
return table_log_add_error(r);
468+
469+
r = table_add_cell(t, NULL, TABLE_INT, &p->partno);
470+
}
471+
if (r < 0)
472+
return table_log_add_error(r);
473+
}
474+
475+
r = table_print(t, stdout);
476+
if (r < 0)
477+
return log_error_errno(r, "Failed to dump table: %m");
478+
438479
break;
439480
}
440481

0 commit comments

Comments
 (0)