diff options
author | Manuel Bentele | 2019-06-23 15:23:45 +0200 |
---|---|---|
committer | Manuel Bentele | 2019-06-23 15:23:45 +0200 |
commit | b800dbf06bef3004aa37dc93ec6d2a311cc8ac23 (patch) | |
tree | 060505ba4fd2a3abe654dc8478ef174a9a94c5cf /lib | |
parent | build-sys: release++ (v2.34) (diff) | |
download | kernel-qcow2-util-linux-b800dbf06bef3004aa37dc93ec6d2a311cc8ac23.tar.gz kernel-qcow2-util-linux-b800dbf06bef3004aa37dc93ec6d2a311cc8ac23.tar.xz kernel-qcow2-util-linux-b800dbf06bef3004aa37dc93ec6d2a311cc8ac23.zip |
lib/losetup: added file format support
losetup can set the file format of each loop device during
creation.
Signed-off-by: Manuel Bentele <development@manuel-bentele.de>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/loopdev.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/loopdev.c b/lib/loopdev.c index ede1b5cdc..676cc02f3 100644 --- a/lib/loopdev.c +++ b/lib/loopdev.c @@ -824,6 +824,75 @@ int loopcxt_get_encrypt_type(struct loopdev_cxt *lc, uint32_t *type) } /* + * @file_fmt_type_str: file format type string. + * @file_fmt_type: returns file format type from the given file format string. + * + * Returns: <0 on error, 0 on success + */ +int parse_file_fmt_type(const char *file_fmt_type_str, uint32_t *file_fmt_type) +{ + int rc = 0; + + if (!strcmp(file_fmt_type_str, "RAW")) + *file_fmt_type = LO_FILE_FMT_RAW; + else if (!strcmp(file_fmt_type_str, "QCOW")) + *file_fmt_type = LO_FILE_FMT_QCOW; + else if (!strcmp(file_fmt_type_str, "VDI")) + *file_fmt_type = LO_FILE_FMT_VDI; + else if (!strcmp(file_fmt_type_str, "VMDK")) + *file_fmt_type = LO_FILE_FMT_VMDK; + else + rc = -EINVAL; + + return rc; +} + +/* + * @lc: context + * @file_fmt_type: returns file format type of the given device + * + * Returns: <0 on error, 0 on success + */ +int loopcxt_get_file_fmt_type(struct loopdev_cxt *lc, uint32_t* file_fmt_type) +{ + struct path_cxt *sysfs = loopcxt_get_sysfs(lc); + int rc = 0; + + if (sysfs) { + /* check if file_fmt_type is accessible and supported by the kernel module */ + char* file_fmt_str = NULL; + if (ul_path_read_string(sysfs, &file_fmt_str, "loop/file_fmt_type") == 0) + rc = parse_file_fmt_type(file_fmt_str, file_fmt_type); + } else + rc = -errno; + + if (rc != 0 && loopcxt_ioctl_enabled(lc)) { + struct loop_info64 *lo = loopcxt_get_info(lc); + if (lo) + *file_fmt_type = lo->lo_file_fmt_type; + } + + return 0; +} + +/* + * @lc: context + * + * Returns (allocated) string with file format type of the current loop device. + */ +char *loopcxt_get_file_fmt_type_string(struct loopdev_cxt *lc) +{ + struct path_cxt *sysfs = loopcxt_get_sysfs(lc); + char *res = NULL; + + if (sysfs) + ul_path_read_string(sysfs, &res, "loop/file_fmt_type"); + + DBG(CXT, ul_debugobj(lc, "loopcxt_get_file_fmt_type_string [%s]", res)); + return res; +} + +/* * @lc: context * @devno: returns crypt name * @@ -1124,6 +1193,23 @@ int loopcxt_set_blocksize(struct loopdev_cxt *lc, uint64_t blocksize) /* * @lc: context + * @file_fmt_type: kernel LO_FILE_FMT_{RAW,QCOW,VDI,VMDK} flags + * + * The setting is removed by loopcxt_set_device() loopcxt_next()! + * + * Returns: 0 on success, <0 on error. + */ +int loopcxt_set_file_fmt_type(struct loopdev_cxt *lc, uint32_t file_fmt_type) { + if (!lc) + return -EINVAL; + lc->info.lo_file_fmt_type = file_fmt_type; + + DBG(CXT, ul_debugobj(lc, "set file_fmt_type=%u", (unsigned) file_fmt_type)); + return 0; +} + +/* + * @lc: context * @flags: kernel LO_FLAGS_{READ_ONLY,USE_AOPS,AUTOCLEAR} flags * * The setting is removed by loopcxt_set_device() loopcxt_next()! |