From 8c1be37e5b438bfdb640cfd39700bf074c66820c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 1 Dec 2016 20:25:26 +0100 Subject: util-lib: split out image dissecting code and loopback code from nspawn This adds two new APIs to systemd: - loop-util.h is a simple internal API for allocating, setting up and releasing loopback block devices. - dissect-image.h is an internal API for taking apart disk images and figuring out what the purpose of each partition is. Both APIs are basically refactored versions of similar code in nspawn. This rework should permit us to reuse this in other places than just nspawn in the future. Specifically: to implement RootImage= in the service image, similar to RootDirectory=, but operating on a disk image; to unify the gpt-auto-discovery generator code with the discovery logic in nspawn; to add new API to machined for determining the OS version of a disk image (i.e. not just running containers). This PR does not make any such changes however, it just provides the new reworked API. The reworked code is also slightly more powerful than the nspawn original one. When pointing it to an image or block device with a naked file system (i.e. no partition table) it will simply make it the root device. --- src/shared/loop-util.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 src/shared/loop-util.c (limited to 'src/shared/loop-util.c') diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c new file mode 100644 index 0000000000..8be4dbf938 --- /dev/null +++ b/src/shared/loop-util.c @@ -0,0 +1,157 @@ +/*** + This file is part of systemd. + + Copyright 2016 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include +#include + +#include "alloc-util.h" +#include "fd-util.h" +#include "loop-util.h" + +int loop_device_make(int fd, int open_flags, LoopDevice **ret) { + const struct loop_info64 info = { + .lo_flags = LO_FLAGS_AUTOCLEAR|LO_FLAGS_PARTSCAN|(open_flags == O_RDONLY ? LO_FLAGS_READ_ONLY : 0), + }; + + _cleanup_close_ int control = -1, loop = -1; + _cleanup_free_ char *loopdev = NULL; + struct stat st; + LoopDevice *d; + int nr; + + assert(fd >= 0); + assert(ret); + assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); + + if (fstat(fd, &st) < 0) + return -errno; + + if (S_ISBLK(st.st_mode)) { + int copy; + + /* If this is already a block device, store a copy of the fd as it is */ + + copy = fcntl(fd, F_DUPFD_CLOEXEC, 3); + if (copy < 0) + return -errno; + + d = new0(LoopDevice, 1); + if (!d) + return -ENOMEM; + + *d = (LoopDevice) { + .fd = copy, + .nr = -1, + }; + + *ret = d; + + return 0; + } + + if (!S_ISREG(st.st_mode)) + return -EINVAL; + + control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); + if (control < 0) + return -errno; + + nr = ioctl(control, LOOP_CTL_GET_FREE); + if (nr < 0) + return -errno; + + if (asprintf(&loopdev, "/dev/loop%i", nr) < 0) + return -ENOMEM; + + loop = open(loopdev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); + if (loop < 0) + return -errno; + + if (ioctl(loop, LOOP_SET_FD, fd) < 0) + return -errno; + + if (ioctl(loop, LOOP_SET_STATUS64, &info) < 0) + return -errno; + + d = new(LoopDevice, 1); + if (!d) + return -ENOMEM; + + *d = (LoopDevice) { + .fd = loop, + .node = loopdev, + .nr = nr, + }; + + loop = -1; + loopdev = NULL; + + *ret = d; + + return (*ret)->fd; +} + +int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret) { + _cleanup_close_ int fd = -1; + + assert(path); + assert(ret); + assert(IN_SET(open_flags, O_RDWR, O_RDONLY)); + + fd = open(path, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags); + if (fd < 0) + return -errno; + + return loop_device_make(fd, open_flags, ret); +} + +LoopDevice* loop_device_unref(LoopDevice *d) { + if (!d) + return NULL; + + if (d->fd >= 0) { + + if (d->nr >= 0) { + if (ioctl(d->fd, LOOP_CLR_FD) < 0) + log_debug_errno(errno, "Failed to clear loop device: %m"); + + } + + safe_close(d->fd); + } + + if (d->nr >= 0) { + _cleanup_close_ int control = -1; + + control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); + if (control < 0) + log_debug_errno(errno, "Failed to open loop control device: %m"); + else { + if (ioctl(control, LOOP_CTL_REMOVE, d->nr) < 0) + log_debug_errno(errno, "Failed to remove loop device: %m"); + } + } + + free(d->node); + free(d); + + return NULL; +} -- cgit v1.2.3-54-g00ecf From a2ea3b2fc82bdd92cf782de61365113bb6ed9ca7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 2 Dec 2016 19:32:09 +0100 Subject: dissect: add small "systemd-dissect" tool as wrapper around dissect-image.c This adds a small tool that may be used to look into OS images, and mount them to any place. This is mostly a friendlier version of test-dissect-image.c. I am not sure this should really become a proper command of systemd, hence for now do not install it into bindir, but simply libexecdir. This tool is already pretty useful since you can mount image files with it, honouring the various partitions correctly. I figure this is going to become more interesting if the dissctor learns luks and verity support. --- .gitignore | 1 + Makefile.am | 12 +++ src/dissect/Makefile | 1 + src/dissect/dissect.c | 206 +++++++++++++++++++++++++++++++++++++++++++++++++ src/shared/loop-util.c | 13 +++- src/shared/loop-util.h | 4 +- 6 files changed, 234 insertions(+), 3 deletions(-) create mode 120000 src/dissect/Makefile create mode 100644 src/dissect/dissect.c (limited to 'src/shared/loop-util.c') diff --git a/.gitignore b/.gitignore index 405b07ef4d..baa975d813 100644 --- a/.gitignore +++ b/.gitignore @@ -67,6 +67,7 @@ /systemd-debug-generator /systemd-delta /systemd-detect-virt +/systemd-dissect /systemd-escape /systemd-export /systemd-firstboot diff --git a/Makefile.am b/Makefile.am index c47a07a1f0..c6adf3a65c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -404,6 +404,11 @@ rootlibexec_PROGRAMS = \ systemd-socket-proxyd \ systemd-update-done +if HAVE_BLKID +rootlibexec_PROGRAMS += \ + systemd-dissect +endif + if HAVE_UTMP rootlibexec_PROGRAMS += \ systemd-update-utmp @@ -3073,6 +3078,13 @@ systemd_notify_SOURCES = \ systemd_notify_LDADD = \ libsystemd-shared.la +# ------------------------------------------------------------------------------ +systemd_dissect_SOURCES = \ + src/dissect/dissect.c + +systemd_dissect_LDADD = \ + libsystemd-shared.la + # ------------------------------------------------------------------------------ systemd_path_SOURCES = \ src/path/path.c diff --git a/src/dissect/Makefile b/src/dissect/Makefile new file mode 120000 index 0000000000..d0b0e8e008 --- /dev/null +++ b/src/dissect/Makefile @@ -0,0 +1 @@ +../Makefile \ No newline at end of file diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c new file mode 100644 index 0000000000..93ece05948 --- /dev/null +++ b/src/dissect/dissect.c @@ -0,0 +1,206 @@ +/*** + This file is part of systemd. + + Copyright 2016 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + systemd is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with systemd; If not, see . +***/ + +#include +#include +#include + +#include "architecture.h" +#include "dissect-image.h" +#include "log.h" +#include "loop-util.h" +#include "string-util.h" +#include "util.h" + +static enum { + ACTION_DISSECT, + ACTION_MOUNT, +} arg_action = ACTION_DISSECT; +static const char *arg_image = NULL; +static const char *arg_path = NULL; +static bool arg_read_only = false; + +static void help(void) { + printf("%s [OPTIONS...] IMAGE\n" + "%s [OPTIONS...] --mount IMAGE PATH\n" + "Dissect a file system OS image.\n\n" + " -h --help Show this help\n" + " --version Show package version\n" + " -m --mount Mount the image to the specified directory\n" + " -r --read-only Mount read-only\n", + program_invocation_short_name, + program_invocation_short_name); +} + +static int parse_argv(int argc, char *argv[]) { + + enum { + ARG_VERSION = 0x100, + }; + + static const struct option options[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, ARG_VERSION }, + { "mount", no_argument, NULL, 'm' }, + { "read-only", no_argument, NULL, 'r' }, + {} + }; + + int c; + + assert(argc >= 0); + assert(argv); + + while ((c = getopt_long(argc, argv, "hmr", options, NULL)) >= 0) { + + switch (c) { + + case 'h': + help(); + return 0; + + case ARG_VERSION: + return version(); + + case 'm': + arg_action = ACTION_MOUNT; + break; + + case 'r': + arg_read_only = true; + break; + + case '?': + return -EINVAL; + + default: + assert_not_reached("Unhandled option"); + } + + } + + switch (arg_action) { + + case ACTION_DISSECT: + if (optind + 1 != argc) { + log_error("Expected a file path as only argument."); + return -EINVAL; + } + + arg_image = argv[optind]; + arg_read_only = true; + break; + + case ACTION_MOUNT: + if (optind + 2 != argc) { + log_error("Expected a file path and mount point path as only arguments."); + return -EINVAL; + } + + arg_image = argv[optind]; + arg_path = argv[optind + 1]; + break; + + default: + assert_not_reached("Unknown action."); + } + + return 1; +} + +int main(int argc, char *argv[]) { + _cleanup_(loop_device_unrefp) LoopDevice *d = 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_read_only ? O_RDONLY : O_RDWR, &d); + if (r < 0) { + log_error_errno(r, "Failed to set up loopback device: %m"); + goto finish; + } + + r = dissect_image(d->fd, &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 < 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; + + if (!p->found) + continue; + + printf("Found %s '%s' partition", + p->rw ? "writable" : "read-only", + partition_designator_to_string(i)); + + if (p->fstype) + printf(" of type %s", p->fstype); + + if (p->architecture != _ARCHITECTURE_INVALID) + printf(" for %s", architecture_to_string(p->architecture)); + + if (p->partno >= 0) + printf(" on partition #%i", p->partno); + + if (p->node) + printf(" (%s)", p->node); + + putchar('\n'); + } + + break; + } + + case ACTION_MOUNT: + r = dissected_image_mount(m, arg_path, + (arg_read_only ? DISSECTED_IMAGE_READ_ONLY : 0) | + DISSECTED_IMAGE_DISCARD_ON_LOOP); + if (r < 0) { + log_error_errno(r, "Failed to mount image: %m"); + goto finish; + } + + loop_device_relinquish(d); + break; + + default: + assert_not_reached("Unknown action."); + } + +finish: + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/src/shared/loop-util.c b/src/shared/loop-util.c index 8be4dbf938..047e213634 100644 --- a/src/shared/loop-util.c +++ b/src/shared/loop-util.c @@ -129,7 +129,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { if (d->fd >= 0) { - if (d->nr >= 0) { + if (d->nr >= 0 && !d->relinquished) { if (ioctl(d->fd, LOOP_CLR_FD) < 0) log_debug_errno(errno, "Failed to clear loop device: %m"); @@ -138,7 +138,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) { safe_close(d->fd); } - if (d->nr >= 0) { + if (d->nr >= 0 && !d->relinquished) { _cleanup_close_ int control = -1; control = open("/dev/loop-control", O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); @@ -155,3 +155,12 @@ LoopDevice* loop_device_unref(LoopDevice *d) { return NULL; } + +void loop_device_relinquish(LoopDevice *d) { + assert(d); + + /* Don't attempt to clean up the loop device anymore from this point on. Leave the clean-ing up to the kernel + * itself, using the loop device "auto-clear" logic we already turned on when creating the device. */ + + d->relinquished = true; +} diff --git a/src/shared/loop-util.h b/src/shared/loop-util.h index 5c847c4ac3..45fead5f18 100644 --- a/src/shared/loop-util.h +++ b/src/shared/loop-util.h @@ -29,11 +29,13 @@ struct LoopDevice { int fd; int nr; char *node; + bool relinquished; }; int loop_device_make(int fd, int open_flags, LoopDevice **ret); int loop_device_make_by_path(const char *path, int open_flags, LoopDevice **ret); LoopDevice* loop_device_unref(LoopDevice *d); - DEFINE_TRIVIAL_CLEANUP_FUNC(LoopDevice*, loop_device_unref); + +void loop_device_relinquish(LoopDevice *d); -- cgit v1.2.3-54-g00ecf