1
0
mirror of https://github.com/systemd/systemd.git synced 2024-11-06 08:26:52 +03:00

loop-util: fill in the loopback number, even a posteriori

This commit is contained in:
Lennart Poettering 2019-07-04 20:11:52 +02:00
parent f1443709e0
commit b26c39ad2c

View File

@ -30,7 +30,7 @@ int loop_device_make_full(
struct loop_info64 info; struct loop_info64 info;
struct stat st; struct stat st;
LoopDevice *d; LoopDevice *d;
int nr, r; int nr = -1, r;
assert(fd >= 0); assert(fd >= 0);
assert(ret); assert(ret);
@ -40,6 +40,14 @@ int loop_device_make_full(
return -errno; return -errno;
if (S_ISBLK(st.st_mode)) { if (S_ISBLK(st.st_mode)) {
if (ioctl(loop, LOOP_GET_STATUS64, &info) >= 0) {
/* Oh! This is a loopback device? That's interesting! */
nr = info.lo_number;
if (asprintf(&loopdev, "/dev/loop%i", nr) < 0)
return -ENOMEM;
}
if (offset == 0 && IN_SET(size, 0, UINT64_MAX)) { if (offset == 0 && IN_SET(size, 0, UINT64_MAX)) {
int copy; int copy;
@ -55,7 +63,8 @@ int loop_device_make_full(
*d = (LoopDevice) { *d = (LoopDevice) {
.fd = copy, .fd = copy,
.nr = -1, .nr = nr,
.node = TAKE_PTR(loopdev),
.relinquished = true, /* It's not allocated by us, don't destroy it when this object is freed */ .relinquished = true, /* It's not allocated by us, don't destroy it when this object is freed */
}; };
@ -179,8 +188,10 @@ void loop_device_relinquish(LoopDevice *d) {
int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) { int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) {
_cleanup_close_ int loop_fd = -1; _cleanup_close_ int loop_fd = -1;
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;
struct loop_info64 info;
struct stat st; struct stat st;
LoopDevice *d; LoopDevice *d;
int nr;
assert(loop_path); assert(loop_path);
assert(ret); assert(ret);
@ -191,10 +202,14 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) {
if (fstat(loop_fd, &st) < 0) if (fstat(loop_fd, &st) < 0)
return -errno; return -errno;
if (!S_ISBLK(st.st_mode)) if (!S_ISBLK(st.st_mode))
return -ENOTBLK; return -ENOTBLK;
if (ioctl(loop_fd, LOOP_GET_STATUS64, &info) >= 0)
nr = info.lo_number;
else
nr = -1;
p = strdup(loop_path); p = strdup(loop_path);
if (!p) if (!p)
return -ENOMEM; return -ENOMEM;
@ -205,7 +220,7 @@ int loop_device_open(const char *loop_path, int open_flags, LoopDevice **ret) {
*d = (LoopDevice) { *d = (LoopDevice) {
.fd = TAKE_FD(loop_fd), .fd = TAKE_FD(loop_fd),
.nr = -1, .nr = nr,
.node = TAKE_PTR(p), .node = TAKE_PTR(p),
.relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */ .relinquished = true, /* It's not ours, don't try to destroy it when this object is freed */
}; };