Merge pull request #24571 from yuwata/dissect-loop-image-use-backing-file

dissect: save image path to LoopDevice and use it when dissect loop device
This commit is contained in:
Yu Watanabe
2022-09-07 23:45:29 +09:00
committed by GitHub
14 changed files with 190 additions and 257 deletions
-1
View File
@@ -2065,7 +2065,6 @@ int setup_namespace(
r = dissect_loop_device(
loop_device,
root_image,
&verity,
root_image_options,
dissect_image_flags,
-1
View File
@@ -946,7 +946,6 @@ static int run(int argc, char *argv[]) {
return log_error_errno(r, "Failed to set up loopback device for %s: %m", arg_image);
r = dissect_loop_device_and_warn(
arg_image,
d,
&arg_verity_settings,
NULL,
+16 -77
View File
@@ -42,71 +42,6 @@ static bool arg_enabled = true;
static bool arg_root_enabled = true;
static int arg_root_rw = -1;
static int open_parent_block_device(dev_t devnum, int *ret_fd) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
const char *name, *devtype, *node;
sd_device *parent;
dev_t pn;
int fd, r;
assert(ret_fd);
r = sd_device_new_from_devnum(&d, 'b', devnum);
if (r < 0)
return log_debug_errno(r, "Failed to create device object for block device "DEVNUM_FORMAT_STR": %m",
DEVNUM_FORMAT_VAL(devnum));
if (sd_device_get_devname(d, &name) < 0) {
r = sd_device_get_syspath(d, &name);
if (r < 0) {
log_device_debug_errno(d, r, "Device " DEVNUM_FORMAT_STR " does not have a name, ignoring: %m",
DEVNUM_FORMAT_VAL(devnum));
return 0;
}
}
r = sd_device_get_parent(d, &parent);
if (r < 0) {
log_device_debug_errno(d, r, "Not a partitioned device, ignoring: %m");
return 0;
}
/* Does it have a devtype? */
r = sd_device_get_devtype(parent, &devtype);
if (r < 0) {
log_device_debug_errno(parent, r, "Parent doesn't have a device type, ignoring: %m");
return 0;
}
/* Is this a disk or a partition? We only care for disks... */
if (!streq(devtype, "disk")) {
log_device_debug(parent, "Parent isn't a raw disk, ignoring.");
return 0;
}
/* Does it have a device node? */
r = sd_device_get_devname(parent, &node);
if (r < 0) {
log_device_debug_errno(parent, r, "Parent device does not have device node, ignoring: %m");
return 0;
}
log_device_debug(d, "Root device %s.", node);
r = sd_device_get_devnum(parent, &pn);
if (r < 0) {
log_device_debug_errno(parent, r, "Parent device is not a proper block device, ignoring: %m");
return 0;
}
fd = open(node, O_RDONLY|O_CLOEXEC|O_NOCTTY);
if (fd < 0)
return log_error_errno(errno, "Failed to open %s: %m", node);
*ret_fd = fd;
return 1;
}
static int add_cryptsetup(const char *id, const char *what, bool rw, bool require, char **device) {
#if HAVE_LIBCRYPTSETUP
_cleanup_free_ char *e = NULL, *n = NULL, *d = NULL;
@@ -694,27 +629,31 @@ static int add_root_mount(void) {
}
static int enumerate_partitions(dev_t devnum) {
_cleanup_close_ int fd = -1;
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
_cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
_cleanup_free_ char *devname = NULL;
int r, k;
r = open_parent_block_device(devnum, &fd);
if (r <= 0)
return r;
r = block_get_whole_disk(devnum, &devnum);
if (r < 0)
return log_debug_errno(r, "Failed to get whole block device for " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devnum));
r = devname_from_devnum(S_IFBLK, devnum, &devname);
if (r < 0)
return log_debug_errno(r, "Failed to get device node of " DEVNUM_FORMAT_STR ": %m",
DEVNUM_FORMAT_VAL(devnum));
/* Let's take a LOCK_SH lock on the block device, in case udevd is already running. If we don't take
* the lock, udevd might end up issuing BLKRRPART in the middle, and we don't want that, since that
* might remove all partitions while we are operating on them. */
if (flock(fd, LOCK_SH) < 0)
return log_error_errno(errno, "Failed to lock root block device: %m");
r = loop_device_open(devname, O_RDONLY, LOCK_SH, &loop);
if (r < 0)
return log_debug_errno(r, "Failed to opne %s: %m", devname);
r = dissect_image(
fd,
NULL,
r = dissect_loop_device(
loop,
NULL, NULL,
/* diskseq= */ 0,
UINT64_MAX,
USEC_INFINITY,
DISSECT_IMAGE_GPT_ONLY|
DISSECT_IMAGE_USR_NO_ROOT,
&m);
-1
View File
@@ -5754,7 +5754,6 @@ static int run(int argc, char *argv[]) {
}
r = dissect_loop_device_and_warn(
arg_image,
loop,
&arg_verity_settings,
NULL,
-1
View File
@@ -367,7 +367,6 @@ static int portable_extract_by_path(
r = dissect_loop_device(
d,
path,
NULL, NULL,
DISSECT_IMAGE_READ_ONLY |
DISSECT_IMAGE_GENERIC_ROOT |
+74 -9
View File
@@ -509,29 +509,41 @@ int block_device_resize_partition(
return RET_NERRNO(ioctl(fd, BLKPG, &ba));
}
int block_device_remove_all_partitions(int fd) {
struct stat stat;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
static int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
sd_device *part;
int r, k = 0;
const char *s;
int r;
if (fstat(fd, &stat) < 0)
return -errno;
assert(dev);
assert(ret);
r = sd_device_new_from_devnum(&dev, 'b', stat.st_rdev);
r = sd_device_get_subsystem(dev, &s);
if (r < 0)
return r;
if (!streq(s, "block"))
return -ENOTBLK;
r = sd_device_get_devtype(dev, &s);
if (r < 0)
return r;
if (!streq(s, "disk")) /* Refuse invocation on partition block device, insist on "whole" device */
return -EINVAL;
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_parent(e, dev);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_subsystem(e, "block", true);
r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
if (r < 0)
return r;
@@ -539,6 +551,44 @@ int block_device_remove_all_partitions(int fd) {
if (r < 0)
return r;
*ret = TAKE_PTR(e);
return 0;
}
int block_device_remove_all_partitions(sd_device *dev, int fd) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
_cleanup_close_ int fd_close = -1;
sd_device *part;
int r, k = 0;
assert(dev || fd >= 0);
if (!dev) {
struct stat st;
if (fstat(fd, &st) < 0)
return -errno;
r = sd_device_new_from_stat_rdev(&dev_unref, &st);
if (r < 0)
return r;
dev = dev_unref;
}
r = partition_enumerator_new(dev, &e);
if (r < 0)
return r;
if (fd < 0) {
fd_close = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|O_RDONLY);
if (fd_close < 0)
return fd_close;
fd = fd_close;
}
FOREACH_DEVICE(e, part) {
const char *v, *devname;
int nr;
@@ -571,3 +621,18 @@ int block_device_remove_all_partitions(int fd) {
return k;
}
int block_device_has_partitions(sd_device *dev) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
int r;
assert(dev);
/* Checks if the specified device currently has partitions. */
r = partition_enumerator_new(dev, &e);
if (r < 0)
return r;
return !!sd_device_enumerator_get_device_first(e);
}
+4 -1
View File
@@ -3,6 +3,8 @@
#include <sys/types.h>
#include "sd-device.h"
#include "macro.h"
#include "stdio-util.h"
#include "string-util.h"
@@ -34,4 +36,5 @@ int path_get_whole_disk(const char *path, bool backing, dev_t *ret);
int block_device_add_partition(int fd, const char *name, int nr, uint64_t start, uint64_t size);
int block_device_remove_partition(int fd, const char *name, int nr);
int block_device_resize_partition(int fd, int nr, uint64_t start, uint64_t size);
int block_device_remove_all_partitions(int fd);
int block_device_remove_all_partitions(sd_device *dev, int fd);
int block_device_has_partitions(sd_device *dev);
-1
View File
@@ -1198,7 +1198,6 @@ int image_read_metadata(Image *i) {
r = dissect_loop_device(
d,
i->path,
NULL, NULL,
DISSECT_IMAGE_GENERIC_ROOT |
DISSECT_IMAGE_REQUIRE_ROOT |
+20 -72
View File
@@ -193,12 +193,10 @@ static int make_partition_devname(
int dissect_image(
int fd,
const char *original_path,
const char *devname,
const char *image_path,
const VeritySettings *verity,
const MountOptions *mount_options,
uint64_t diskseq,
uint64_t uevent_seqnum_not_before,
usec_t timestamp_not_before,
DissectImageFlags flags,
DissectedImage **ret) {
@@ -208,17 +206,16 @@ int dissect_image(
bool is_gpt, is_mbr, multiple_generic = false,
generic_rw = false, /* initialize to appease gcc */
generic_growfs = false;
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_(dissected_image_unrefp) DissectedImage *m = NULL;
_cleanup_(blkid_free_probep) blkid_probe b = NULL;
_cleanup_free_ char *generic_node = NULL;
sd_id128_t generic_uuid = SD_ID128_NULL;
const char *pttype = NULL, *sysname = NULL, *devname = NULL;
const char *pttype = NULL;
blkid_partlist pl;
int r, generic_nr = -1, n_partitions;
struct stat st;
assert(fd >= 0);
assert(devname);
assert(ret);
assert(!verity || verity->designator < 0 || IN_SET(verity->designator, PARTITION_ROOT, PARTITION_USR));
assert(!verity || verity->root_hash || verity->root_hash_size == 0);
@@ -262,16 +259,6 @@ int dissect_image(
}
}
if (fstat(fd, &st) < 0)
return -errno;
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
r = sd_device_new_from_stat_rdev(&d, &st);
if (r < 0)
return r;
b = blkid_new_probe();
if (!b)
return -ENOMEM;
@@ -305,49 +292,21 @@ int dissect_image(
.has_init_system = -1,
};
r = sd_device_get_sysname(d, &sysname);
if (r < 0)
return log_debug_errno(r, "Failed to get device sysname: %m");
if (original_path) {
if (image_path) {
_cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL;
r = path_extract_filename(original_path, &extracted_filename);
r = path_extract_filename(image_path, &extracted_filename);
if (r < 0)
return r;
r = raw_strip_suffixes(extracted_filename, &name_stripped);
if (r < 0)
return r;
free_and_replace(m->image_name, name_stripped);
} else if (startswith(sysname, "loop")) {
_cleanup_free_ char *extracted_filename = NULL, *name_stripped = NULL;
const char *full_path;
/* Note that the backing_file reference resolves symlinks, while for sysext images we want the original path */
r = sd_device_get_sysattr_value(d, "loop/backing_file", &full_path);
if (r < 0)
log_debug_errno(r, "Failed to lookup image name via loop device backing file sysattr, ignoring: %m");
else {
r = path_extract_filename(full_path, &extracted_filename);
if (r < 0)
return r;
r = raw_strip_suffixes(extracted_filename, &name_stripped);
if (r < 0)
return r;
}
free_and_replace(m->image_name, name_stripped);
} else {
r = free_and_strdup(&m->image_name, sysname);
if (r < 0)
return r;
}
r = sd_device_get_devname(d, &devname);
if (r < 0)
return log_debug_errno(r, "Failed to get device devname: %m");
if (!image_name_is_valid(m->image_name)) {
log_debug("Image name %s is not valid, ignoring", strempty(m->image_name));
m->image_name = mfree(m->image_name);
if (!image_name_is_valid(name_stripped))
log_debug("Image name %s is not valid, ignoring.", strna(name_stripped));
else
m->image_name = TAKE_PTR(name_stripped);
}
if ((!(flags & DISSECT_IMAGE_GPT_ONLY) &&
@@ -764,7 +723,7 @@ int dissect_image(
}
if (designator != _PARTITION_DESIGNATOR_INVALID) {
_cleanup_free_ char *t = NULL, *n = NULL, *o = NULL, *l = NULL;
_cleanup_free_ char *t = NULL, *o = NULL, *l = NULL;
const char *options = NULL;
if (m->partitions[designator].found) {
@@ -786,10 +745,6 @@ int dissect_image(
return -ENOMEM;
}
n = strdup(node);
if (!n)
return -ENOMEM;
if (label) {
l = strdup(label);
if (!l)
@@ -809,7 +764,7 @@ int dissect_image(
.rw = rw,
.growfs = growfs,
.architecture = architecture,
.node = TAKE_PTR(n),
.node = TAKE_PTR(node),
.fstype = TAKE_PTR(t),
.label = TAKE_PTR(l),
.uuid = id,
@@ -842,7 +797,7 @@ int dissect_image(
break;
case 0xEA: { /* Boot Loader Spec extended $BOOT partition */
_cleanup_free_ char *n = NULL, *o = NULL;
_cleanup_free_ char *o = NULL;
sd_id128_t id = SD_ID128_NULL;
const char *sid, *options = NULL;
@@ -854,10 +809,6 @@ int dissect_image(
if (sid)
(void) sd_id128_from_string(sid, &id);
n = strdup(node);
if (!n)
return -ENOMEM;
options = mount_options_from_designator(mount_options, PARTITION_XBOOTLDR);
if (options) {
o = strdup(options);
@@ -871,7 +822,7 @@ int dissect_image(
.rw = true,
.growfs = false,
.architecture = _ARCHITECTURE_INVALID,
.node = TAKE_PTR(n),
.node = TAKE_PTR(node),
.uuid = id,
.mount_options = TAKE_PTR(o),
.offset = (uint64_t) start * 512,
@@ -2812,22 +2763,21 @@ finish:
}
int dissect_loop_device_and_warn(
const char *name,
const LoopDevice *loop,
const VeritySettings *verity,
const MountOptions *mount_options,
DissectImageFlags flags,
DissectedImage **ret) {
const char *name;
int r;
assert(loop);
assert(loop->fd >= 0);
if (!name)
name = ASSERT_PTR(loop->node);
name = ASSERT_PTR(loop->backing_file ?: loop->node);
r = dissect_loop_device(loop, name, verity, mount_options, flags, ret);
r = dissect_loop_device(loop, verity, mount_options, flags, ret);
switch (r) {
case -EOPNOTSUPP:
@@ -2980,7 +2930,7 @@ int mount_image_privately_interactively(
if (r < 0)
return log_error_errno(r, "Failed to set up loopback device for %s: %m", image);
r = dissect_loop_device_and_warn(image, d, &verity, NULL, flags, &dissected_image);
r = dissect_loop_device_and_warn(d, &verity, NULL, flags, &dissected_image);
if (r < 0)
return r;
@@ -3093,7 +3043,6 @@ int verity_dissect_and_mount(
r = dissect_loop_device(
loop_device,
src,
&verity,
options,
dissect_image_flags,
@@ -3102,7 +3051,6 @@ int verity_dissect_and_mount(
if (!verity.data_path && r == -ENOPKG)
r = dissect_loop_device(
loop_device,
src,
&verity,
options,
dissect_image_flags | DISSECT_IMAGE_NO_PARTITION_TABLE,
+11 -4
View File
@@ -253,12 +253,19 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(MountOptions*, mount_options_free_all);
const char* mount_options_from_designator(const MountOptions *options, PartitionDesignator designator);
int probe_filesystem(const char *node, char **ret_fstype);
int dissect_image(int fd, const char *original_path, const VeritySettings *verity, const MountOptions *mount_options, uint64_t diskseq, uint64_t uevent_seqnum_not_before, usec_t timestamp_not_before, DissectImageFlags flags, DissectedImage **ret);
static inline int dissect_loop_device(const LoopDevice *loop, const char *original_path, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret) {
int dissect_image(
int fd,
const char *devname,
const char *image_path,
const VeritySettings *verity,
const MountOptions *mount_options,
DissectImageFlags flags,
DissectedImage **ret);
static inline int dissect_loop_device(const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret) {
assert(loop);
return dissect_image(loop->fd, original_path, verity, mount_options, loop->diskseq, loop->uevent_seqnum_not_before, loop->timestamp_not_before, flags, ret);
return dissect_image(loop->fd, loop->node, loop->backing_file ?: loop->node, verity, mount_options, flags, ret);
}
int dissect_loop_device_and_warn(const char *name, const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
int dissect_loop_device_and_warn(const LoopDevice *loop, const VeritySettings *verity, const MountOptions *mount_options, DissectImageFlags flags, DissectedImage **ret);
DissectedImage* dissected_image_unref(DissectedImage *m);
DEFINE_TRIVIAL_CLEANUP_FUNC(DissectedImage*, dissected_image_unref);
+56 -79
View File
@@ -26,6 +26,7 @@
#include "loop-util.h"
#include "missing_loop.h"
#include "parse-util.h"
#include "path-util.h"
#include "random-util.h"
#include "stat-util.h"
#include "stdio-util.h"
@@ -70,53 +71,6 @@ static int get_current_uevent_seqnum(uint64_t *ret) {
return 0;
}
static int device_has_block_children(sd_device *d) {
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
const char *main_ss, *main_dt;
int r;
assert(d);
/* Checks if the specified device currently has block device children (i.e. partition block
* devices). */
r = sd_device_get_subsystem(d, &main_ss);
if (r < 0)
return r;
if (!streq(main_ss, "block"))
return -EINVAL;
r = sd_device_get_devtype(d, &main_dt);
if (r < 0)
return r;
if (!streq(main_dt, "disk")) /* Refuse invocation on partition block device, insist on "whole" device */
return -EINVAL;
r = sd_device_enumerator_new(&e);
if (r < 0)
return r;
r = sd_device_enumerator_allow_uninitialized(e);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_parent(e, d);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
if (r < 0)
return r;
r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
if (r < 0)
return r;
return !!sd_device_enumerator_get_device_first(e);
}
static int open_lock_fd(int primary_fd, int operation) {
_cleanup_close_ int lock_fd = -1;
@@ -134,6 +88,7 @@ static int open_lock_fd(int primary_fd, int operation) {
}
static int loop_configure(
sd_device *dev,
int fd,
int nr,
const struct loop_config *c,
@@ -142,8 +97,6 @@ static int loop_configure(
usec_t *ret_timestamp_not_before,
int *ret_lock_fd) {
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
_cleanup_free_ char *sysname = NULL;
_cleanup_close_ int lock_fd = -1;
struct loop_info64 info_copy;
uint64_t seqnum;
@@ -155,13 +108,6 @@ static int loop_configure(
assert(c);
assert(try_loop_configure);
if (asprintf(&sysname, "loop%i", nr) < 0)
return -ENOMEM;
r = sd_device_new_from_subsystem_sysname(&d, "block", sysname);
if (r < 0)
return r;
/* Let's lock the device before we do anything. We take the BSD lock on a second, separately opened
* fd for the device. udev after all watches for close() events (specifically IN_CLOSE_WRITE) on
* block devices to reprobe them, hence by having a separate fd we will later close() we can ensure
@@ -178,7 +124,7 @@ static int loop_configure(
* superficially is detached but still has partition block devices associated for it. Let's then
* manually remove the partitions via BLKPG, and tell the caller we did that via EUCLEAN, so they try
* again. */
r = device_has_block_children(d);
r = block_device_has_partitions(dev);
if (r < 0)
return r;
if (r > 0) {
@@ -191,7 +137,7 @@ static int loop_configure(
/* Unbound but has children? Remove all partitions, and report this to the caller, to try
* again, and count this as an attempt. */
r = block_device_remove_all_partitions(fd);
r = block_device_remove_all_partitions(dev, fd);
if (r < 0)
return r;
@@ -351,6 +297,7 @@ fail:
}
static int loop_device_make_internal(
const char *path,
int fd,
int open_flags,
uint64_t offset,
@@ -359,8 +306,9 @@ static int loop_device_make_internal(
int lock_op,
LoopDevice **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int direct_io_fd = -1;
_cleanup_free_ char *node = NULL;
_cleanup_free_ char *node = NULL, *backing_file = NULL;
bool try_loop_configure = true;
struct loop_config config;
LoopDevice *d;
@@ -388,6 +336,18 @@ static int loop_device_make_internal(
return r;
}
if (path) {
r = path_make_absolute_cwd(path, &backing_file);
if (r < 0)
return r;
path_simplify(backing_file);
} else {
r = fd_get_path(fd, &backing_file);
if (r < 0)
return r;
}
f_flags = fcntl(fd, F_GETFL);
if (f_flags < 0)
return -errno;
@@ -458,14 +418,19 @@ static int loop_device_make_internal(
if (asprintf(&node, "/dev/loop%i", nr) < 0)
return -ENOMEM;
loop = open(node, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
dev = sd_device_unref(dev);
r = sd_device_new_from_devname(&dev, node);
if (r < 0)
return r;
loop = sd_device_open(dev, O_CLOEXEC|O_NONBLOCK|O_NOCTTY|open_flags);
if (loop < 0) {
/* Somebody might've gotten the same number from the kernel, used the device,
* and called LOOP_CTL_REMOVE on it. Let's retry with a new number. */
if (!ERRNO_IS_DEVICE_ABSENT(errno))
return -errno;
} else {
r = loop_configure(loop, nr, &config, &try_loop_configure, &seqnum, &timestamp, &lock_fd);
r = loop_configure(dev, loop, nr, &config, &try_loop_configure, &seqnum, &timestamp, &lock_fd);
if (r >= 0) {
loop_with_fd = TAKE_FD(loop);
break;
@@ -547,6 +512,8 @@ static int loop_device_make_internal(
.node = TAKE_PTR(node),
.nr = nr,
.devno = st.st_rdev,
.dev = TAKE_PTR(dev),
.backing_file = TAKE_PTR(backing_file),
.diskseq = diskseq,
.uevent_seqnum_not_before = seqnum,
.timestamp_not_before = timestamp,
@@ -585,6 +552,7 @@ int loop_device_make(
assert(ret);
return loop_device_make_internal(
NULL,
fd,
open_flags,
offset,
@@ -652,7 +620,7 @@ int loop_device_make_by_path(
direct ? "enabled" : "disabled",
direct != (direct_flags != 0) ? " (O_DIRECT was requested but not supported)" : "");
return loop_device_make_internal(fd, open_flags, 0, 0, loop_flags, lock_op, ret);
return loop_device_make_internal(path, fd, open_flags, 0, 0, loop_flags, lock_op, ret);
}
LoopDevice* loop_device_unref(LoopDevice *d) {
@@ -698,7 +666,7 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
if (flock(d->fd, LOCK_EX) < 0)
log_debug_errno(errno, "Failed to lock loop block device, ignoring: %m");
r = block_device_remove_all_partitions(d->fd);
r = block_device_remove_all_partitions(d->dev, d->fd);
if (r < 0)
log_debug_errno(r, "Failed to remove partitions of loopback block device, ignoring: %m");
@@ -722,6 +690,8 @@ LoopDevice* loop_device_unref(LoopDevice *d) {
}
free(d->node);
sd_device_unref(d->dev);
free(d->backing_file);
return mfree(d);
}
@@ -746,8 +716,9 @@ int loop_device_open_full(
int lock_op,
LoopDevice **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
_cleanup_close_ int fd = -1, lock_fd = -1;
_cleanup_free_ char *p = NULL;
_cleanup_free_ char *p = NULL, *backing_file = NULL;
struct loop_info64 info;
uint64_t diskseq = 0;
struct stat st;
@@ -770,6 +741,10 @@ int loop_device_open_full(
if (!S_ISBLK(st.st_mode))
return -ENOTBLK;
r = sd_device_new_from_stat_rdev(&dev, &st);
if (r < 0)
return r;
if (fd < 0) {
/* If loop_fd is provided through the argument, then we reopen the inode here, instead of
* keeping just a dup() clone of it around, since we want to ensure that the O_DIRECT
@@ -782,11 +757,19 @@ int loop_device_open_full(
}
if (ioctl(loop_fd, LOOP_GET_STATUS64, &info) >= 0) {
const char *s;
#if HAVE_VALGRIND_MEMCHECK_H
/* Valgrind currently doesn't know LOOP_GET_STATUS64. Remove this once it does */
VALGRIND_MAKE_MEM_DEFINED(&info, sizeof(info));
#endif
nr = info.lo_number;
if (sd_device_get_sysattr_value(dev, "loop/backing_file", &s) >= 0) {
backing_file = strdup(s);
if (!backing_file)
return -ENOMEM;
}
}
r = fd_get_diskseq(loop_fd, &diskseq);
@@ -799,21 +782,13 @@ int loop_device_open_full(
return lock_fd;
}
if (loop_path) {
/* If loop_path is provided, then honor it. */
p = strdup(loop_path);
if (!p)
return -ENOMEM;
} else if (nr >= 0) {
/* This is a loopback block device. Use its index. */
if (asprintf(&p, "/dev/loop%i", nr) < 0)
return -ENOMEM;
} else {
/* This is a non-loopback block device. Let's get the path to the device node. */
r = devname_from_stat_rdev(&st, &p);
if (r < 0)
return r;
}
r = sd_device_get_devname(dev, &loop_path);
if (r < 0)
return r;
p = strdup(loop_path);
if (!p)
return -ENOMEM;
d = new(LoopDevice, 1);
if (!d)
@@ -824,6 +799,8 @@ int loop_device_open_full(
.lock_fd = TAKE_FD(lock_fd),
.nr = nr,
.node = TAKE_PTR(p),
.dev = TAKE_PTR(dev),
.backing_file = TAKE_PTR(backing_file),
.relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */
.devno = st.st_rdev,
.diskseq = diskseq,
+4
View File
@@ -1,6 +1,8 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#pragma once
#include "sd-device.h"
#include "macro.h"
#include "time-util.h"
@@ -14,6 +16,8 @@ struct LoopDevice {
int nr; /* The loopback device index (i.e. 4 for /dev/loop4); if this object encapsulates a non-loopback block device, set to -1 */
dev_t devno;
char *node;
sd_device *dev;
char *backing_file;
bool relinquished;
uint64_t diskseq; /* Block device sequence number, monothonically incremented by the kernel on create/attach, or 0 if we don't know */
uint64_t uevent_seqnum_not_before; /* uevent sequm right before we attached the loopback device, or UINT64_MAX if we don't know */
-1
View File
@@ -540,7 +540,6 @@ static int merge_subprocess(Hashmap *images, const char *workspace) {
return log_error_errno(r, "Failed to set up loopback device for %s: %m", img->path);
r = dissect_loop_device_and_warn(
img->path,
d,
&verity_settings,
NULL,
+5 -9
View File
@@ -39,7 +39,6 @@ static void* thread_func(void *ptr) {
_cleanup_(loop_device_unrefp) LoopDevice *loop = NULL;
_cleanup_(umount_and_rmdir_and_freep) char *mounted = NULL;
_cleanup_(dissected_image_unrefp) DissectedImage *dissected = NULL;
_cleanup_free_ char *path = NULL;
if (now(CLOCK_MONOTONIC) >= end) {
log_notice("Time's up, exiting thread's loop");
@@ -54,15 +53,12 @@ static void* thread_func(void *ptr) {
if (r < 0)
log_error_errno(r, "Failed to allocate loopback device: %m");
assert_se(r >= 0);
assert_se(loop->dev);
assert_se(loop->backing_file);
log_notice("Acquired loop device %s, will mount on %s", loop->node, mounted);
r = fd_get_path(fd, &path);
if (r < 0)
log_error_errno(r, "Failed to get path from fd: %m");
assert_se(r >= 0);
r = dissect_loop_device(loop, path, NULL, NULL, DISSECT_IMAGE_READ_ONLY, &dissected);
r = dissect_loop_device(loop, NULL, NULL, DISSECT_IMAGE_READ_ONLY, &dissected);
if (r < 0)
log_error_errno(r, "Failed dissect loopback device %s: %m", loop->node);
assert_se(r >= 0);
@@ -226,7 +222,7 @@ static int run(int argc, char *argv[]) {
pthread_t threads[arg_n_threads];
sd_id128_t id;
assert_se(dissect_loop_device(loop, p, NULL, NULL, 0, &dissected) >= 0);
assert_se(dissect_loop_device(loop, NULL, NULL, 0, &dissected) >= 0);
assert_se(dissected->partitions[PARTITION_ESP].found);
assert_se(dissected->partitions[PARTITION_ESP].node);
@@ -250,7 +246,7 @@ static int run(int argc, char *argv[]) {
assert_se(make_filesystem(dissected->partitions[PARTITION_HOME].node, "ext4", "home", id, true) >= 0);
dissected = dissected_image_unref(dissected);
assert_se(dissect_loop_device(loop, p, NULL, NULL, 0, &dissected) >= 0);
assert_se(dissect_loop_device(loop, NULL, NULL, 0, &dissected) >= 0);
assert_se(mkdtemp_malloc(NULL, &mounted) >= 0);