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
+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,