Example #1
0
int main(int argc, char *argv[]) {
        _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
        _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
        int r, i;

        log_set_max_level(LOG_DEBUG);

        if (argc < 2) {
                log_error("Requires one command line argument.");
                return EXIT_FAILURE;
        }

        r = loop_device_make_by_path(argv[1], O_RDONLY, &d);
        if (r < 0) {
                log_error_errno(r, "Failed to set up loopback device: %m");
                return EXIT_FAILURE;
        }

        r = dissect_image(d->fd, NULL, 0, &m);
        if (r < 0) {
                log_error_errno(r, "Failed to dissect image: %m");
                return EXIT_FAILURE;
        }

        for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {

                if (!m->partitions[i].found)
                        continue;

                printf("Found %s partition, %s of type %s at #%i (%s)\n",
                       partition_designator_to_string(i),
                       m->partitions[i].rw ? "writable" : "read-only",
                       strna(m->partitions[i].fstype),
                       m->partitions[i].partno,
                       strna(m->partitions[i].node));
        }

        return EXIT_SUCCESS;
}
Example #2
0
int main(int argc, char *argv[]) {
        _cleanup_(loop_device_unrefp) LoopDevice *d = NULL;
        _cleanup_(decrypted_image_unrefp) DecryptedImage *di = NULL;
        _cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
        int r;

        log_parse_environment();
        log_open();

        r = parse_argv(argc, argv);
        if (r <= 0)
                goto finish;

        r = loop_device_make_by_path(arg_image, (arg_flags & DISSECT_IMAGE_READ_ONLY) ? O_RDONLY : O_RDWR, &d);
        if (r < 0) {
                log_error_errno(r, "Failed to set up loopback device: %m");
                goto finish;
        }

        if (!arg_root_hash) {
                r = root_hash_load(arg_image, &arg_root_hash, &arg_root_hash_size);
                if (r < 0) {
                        log_error_errno(r, "Failed to read root hash file for %s: %m", arg_image);
                        goto finish;
                }
        }

        r = dissect_image(d->fd, arg_root_hash, arg_root_hash_size, arg_flags, &m);
        if (r == -ENOPKG) {
                log_error_errno(r, "Couldn't identify a suitable partition table or file system in %s.", arg_image);
                goto finish;
        }
        if (r == -EADDRNOTAVAIL) {
                log_error_errno(r, "No root partition for specified root hash found in %s.", arg_image);
                goto finish;
        }
        if (r == -ENOTUNIQ) {
                log_error_errno(r, "Multiple suitable root partitions found in image %s.", arg_image);
                goto finish;
        }
        if (r == -ENXIO) {
                log_error_errno(r, "No suitable root partition found in image %s.", arg_image);
                goto finish;
        }
        if (r == -EPROTONOSUPPORT) {
                log_error_errno(r, "Device %s is loopback block device with partition scanning turned off, please turn it on.", arg_image);
                goto finish;
        }
        if (r < 0) {
                log_error_errno(r, "Failed to dissect image: %m");
                goto finish;
        }

        switch (arg_action) {

        case ACTION_DISSECT: {
                unsigned i;

                for (i = 0; i < _PARTITION_DESIGNATOR_MAX; i++) {
                        DissectedPartition *p = m->partitions + i;
                        int k;

                        if (!p->found)
                                continue;

                        printf("Found %s '%s' partition",
                               p->rw ? "writable" : "read-only",
                               partition_designator_to_string(i));

                        if (!sd_id128_is_null(p->uuid))
                                printf(" (UUID " SD_ID128_FORMAT_STR ")", SD_ID128_FORMAT_VAL(p->uuid));

                        if (p->fstype)
                                printf(" of type %s", p->fstype);

                        if (p->architecture != _ARCHITECTURE_INVALID)
                                printf(" for %s", architecture_to_string(p->architecture));

                        k = PARTITION_VERITY_OF(i);
                        if (k >= 0)
                                printf(" %s verity", m->partitions[k].found ? "with" : "without");

                        if (p->partno >= 0)
                                printf(" on partition #%i", p->partno);

                        if (p->node)
                                printf(" (%s)", p->node);

                        putchar('\n');
                }

                r = dissected_image_acquire_metadata(m);
                if (r < 0) {
                        log_error_errno(r, "Failed to acquire image metadata: %m");
                        goto finish;
                }

                if (m->hostname)
                        printf("  Hostname: %s\n", m->hostname);

                if (!sd_id128_is_null(m->machine_id))
                        printf("Machine ID: " SD_ID128_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(m->machine_id));

                if (!strv_isempty(m->machine_info)) {
                        char **p, **q;

                        STRV_FOREACH_PAIR(p, q, m->machine_info)
                                printf("%s %s=%s\n",
                                       p == m->machine_info ? "Mach. Info:" : "           ",
                                       *p, *q);
                }

                if (!strv_isempty(m->os_release)) {
                        char **p, **q;

                        STRV_FOREACH_PAIR(p, q, m->os_release)
                                printf("%s %s=%s\n",
                                       p == m->os_release ? "OS Release:" : "           ",
                                       *p, *q);
                }

                break;
        }

        case ACTION_MOUNT:
                r = dissected_image_decrypt_interactively(m, NULL, arg_root_hash, arg_root_hash_size, arg_flags, &di);
                if (r < 0)
                        goto finish;

                r = dissected_image_mount(m, arg_path, UID_INVALID, arg_flags);
                if (r < 0) {
                        log_error_errno(r, "Failed to mount image: %m");
                        goto finish;
                }

                if (di) {
                        r = decrypted_image_relinquish(di);
                        if (r < 0) {
                                log_error_errno(r, "Failed to relinquish DM devices: %m");
                                goto finish;
                        }
                }

                loop_device_relinquish(d);
                break;

        default:
                assert_not_reached("Unknown action.");
        }

finish:
        free(arg_root_hash);
        return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}