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 /lib/loopdev.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 'lib/loopdev.c')
| -rw-r--r-- | lib/loopdev.c | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/lib/loopdev.c b/lib/loopdev.c index dd9ead3ee3..b3c537cd58 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -749,6 +749,26 @@ char *loopcxt_get_backing_file(struct loopdev_cxt *lc) /* * @lc: context + * + * Returns (allocated) string with loop reference. The same as backing file by + * default. + */ +char *loopcxt_get_refname(struct loopdev_cxt *lc) +{ + char *res = NULL; + struct loop_info64 *lo = loopcxt_get_info(lc); + + if (lo) { + lo->lo_file_name[LO_NAME_SIZE - 1] = '\0'; + res = strdup((char *) lo->lo_file_name); + } + + DBG(CXT, ul_debugobj(lc, "get_refname [%s]", res)); + return res; +} + +/* + * @lc: context * @offset: returns offset number for the given device * * Returns: <0 on error, 0 on success @@ -1182,6 +1202,28 @@ int loopcxt_set_flags(struct loopdev_cxt *lc, uint32_t flags) /* * @lc: context + * @refname: reference name (used to overwrite lo_file_name where is backing + * file by default) + * + * The setting is removed by loopcxt_set_device() loopcxt_next()! + * + * Returns: 0 on success, <0 on error. + */ +int loopcxt_set_refname(struct loopdev_cxt *lc, const char *refname) +{ + if (!lc) + return -EINVAL; + + memset(lc->config.info.lo_file_name, 0, sizeof(lc->config.info.lo_file_name)); + if (refname) + xstrncpy((char *)lc->config.info.lo_file_name, refname, LO_NAME_SIZE); + + DBG(CXT, ul_debugobj(lc, "set refname=%s", (char *)lc->config.info.lo_file_name)); + return 0; +} + +/* + * @lc: context * @filename: backing file path (the path will be canonicalized) * * The setting is removed by loopcxt_set_device() loopcxt_next()! @@ -1197,9 +1239,10 @@ int loopcxt_set_backing_file(struct loopdev_cxt *lc, const char *filename) if (!lc->filename) return -errno; - xstrncpy((char *)lc->config.info.lo_file_name, lc->filename, LO_NAME_SIZE); + if (!lc->config.info.lo_file_name[0]) + loopcxt_set_refname(lc, lc->filename); - DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->config.info.lo_file_name)); + DBG(CXT, ul_debugobj(lc, "set backing file=%s", lc->filename)); return 0; } |
