diff options
| author | Karel Zak <kzak@redhat.com> | 2023-03-16 13:41:36 +0100 |
|---|---|---|
| committer | Karel Zak <kzak@redhat.com> | 2023-07-20 23:08:53 +0200 |
| commit | db8f78151c075d25792b6b77b500c1c52a374fe0 (patch) | |
| tree | ccfc1bf72601460f6086f56d1e1f8e7f6c197c63 /sys-utils/losetup.c | |
| parent | 54e4a6b145fd6ef943d93e16de748283e687855d (diff) | |
| download | util-linux-db8f78151c075d25792b6b77b500c1c52a374fe0.tar.gz | |
losetup: add --loop-ref and REF column
The lo_file_name is nowhere used (kernel uses backing file descriptor,
no path) and it was used to store limited info about the backing file path
(64 bytes only!). For backward compatibility, we still fill lo_file_name
with the path, but it's nowhere in the userspace used as the complete
backing file path in sysfs.
This commit introduces a new option to overwrite the default path in
lo_file_name. The idea is to use the reference string by udevd in
/dev/loop/by-ref to address loop devices independently on paths.
Addresses: https://github.com/util-linux/util-linux/issues/2106
Suggested-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'sys-utils/losetup.c')
| -rw-r--r-- | sys-utils/losetup.c | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/sys-utils/losetup.c b/sys-utils/losetup.c index 9e8bd509ac..dfdfc58721 100644 --- a/sys-utils/losetup.c +++ b/sys-utils/losetup.c @@ -48,6 +48,7 @@ enum { COL_MAJMIN, COL_OFFSET, COL_PARTSCAN, + COL_REF, COL_RO, COL_SIZELIMIT, COL_DIO, @@ -76,6 +77,7 @@ static const struct colinfo infos[] = { [COL_NAME] = { "NAME", 0.25, 0, N_("loop device name")}, [COL_OFFSET] = { "OFFSET", 5, SCOLS_FL_RIGHT, N_("offset from the beginning"), SCOLS_JSON_NUMBER}, [COL_PARTSCAN] = { "PARTSCAN", 1, SCOLS_FL_RIGHT, N_("partscan flag set"), SCOLS_JSON_BOOLEAN}, + [COL_REF] = { "REF", 0.1, 0, N_("loop device reference string")}, [COL_RO] = { "RO", 1, SCOLS_FL_RIGHT, N_("read-only device"), SCOLS_JSON_BOOLEAN}, [COL_SIZELIMIT] = { "SIZELIMIT", 5, SCOLS_FL_RIGHT, N_("size limit of the file in bytes"), SCOLS_JSON_NUMBER}, [COL_MAJMIN] = { "MAJ:MIN", 3, 0, N_("loop device major:minor number")}, @@ -291,6 +293,9 @@ static int set_scols_data(struct loopdev_cxt *lc, struct libscols_line *ln) if (loopcxt_get_blocksize(lc, &x) == 0) xasprintf(&np, "%jd", x); break; + case COL_REF: + np = loopcxt_get_refname(lc); + break; default: return -EINVAL; } @@ -423,6 +428,7 @@ static void __attribute__((__noreturn__)) usage(void) fputs(_(" -P, --partscan create a partitioned loop device\n"), out); fputs(_(" -r, --read-only set up a read-only loop device\n"), out); fputs(_(" --direct-io[=<on|off>] open backing file with O_DIRECT\n"), out); + fputs(_(" --loop-ref <string> loop device reference\n"), out); fputs(_(" --show print device name after setup (with -f)\n"), out); fputs(_(" -v, --verbose verbose mode\n"), out); @@ -491,7 +497,8 @@ static int find_unused(struct loopdev_cxt *lc) static int create_loop(struct loopdev_cxt *lc, int nooverlap, int lo_flags, int flags, - const char *file, uint64_t offset, uint64_t sizelimit, + const char *file, const char *refname, + uint64_t offset, uint64_t sizelimit, uint64_t blocksize) { int hasdev = loopcxt_has_device(lc); @@ -580,7 +587,10 @@ static int create_loop(struct loopdev_cxt *lc, loopcxt_set_flags(lc, lo_flags); if (blocksize > 0) loopcxt_set_blocksize(lc, blocksize); - + if (refname && (rc = loopcxt_set_refname(lc, refname))) { + warnx(_("cannot set loop reference string")); + break; + } if ((rc = loopcxt_set_backing_file(lc, file))) { warn(_("%s: failed to use backing file"), file); break; @@ -610,7 +620,7 @@ int main(int argc, char **argv) { struct loopdev_cxt lc; int act = 0, flags = 0, no_overlap = 0, c; - char *file = NULL; + char *file = NULL, *refname = NULL; uint64_t offset = 0, sizelimit = 0, blocksize = 0; int res = 0, showdev = 0, lo_flags = 0; char *outarg = NULL; @@ -621,6 +631,7 @@ int main(int argc, char **argv) OPT_SIZELIMIT = CHAR_MAX + 1, OPT_SHOW, OPT_RAW, + OPT_REF, OPT_DIO, OPT_OUTPUT_ALL }; @@ -645,6 +656,7 @@ int main(int argc, char **argv) { "read-only", no_argument, NULL, 'r' }, { "direct-io", optional_argument, NULL, OPT_DIO }, { "raw", no_argument, NULL, OPT_RAW }, + { "loop-ref", required_argument, NULL, OPT_REF, }, { "show", no_argument, NULL, OPT_SHOW }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, @@ -691,6 +703,9 @@ int main(int argc, char **argv) case 'r': lo_flags |= LO_FLAGS_READ_ONLY; break; + case OPT_REF: + refname = optarg; + break; case 'd': act = A_DELETE; if (!is_loopdev(optarg) || @@ -862,7 +877,7 @@ int main(int argc, char **argv) switch (act) { case A_CREATE: - res = create_loop(&lc, no_overlap, lo_flags, flags, file, + res = create_loop(&lc, no_overlap, lo_flags, flags, file, refname, offset, sizelimit, blocksize); if (res == 0) { if (showdev) |
