From 7762f465946daefbb77bb79be99ec82e13816d0b Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Fri, 13 Aug 2021 10:00:50 +0000 Subject: [PATCH] util/linux: add helper for `BLK_IOC_FLSBUF` ioctl Add a helper method to call `ioctl(fd, BLK_IOC_FLUSH_BUFFER, 0)` from python. NB: the ioctl number 0x1261 is wrong on at least alpha and sparc. A later test will use this call so we should catch the usage of it on those platforms. --- osbuild/util/linux.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/osbuild/util/linux.py b/osbuild/util/linux.py index ec230f8d..2ccbfc61 100644 --- a/osbuild/util/linux.py +++ b/osbuild/util/linux.py @@ -32,6 +32,8 @@ FS_IOC_SETFLAGS = 0x40086602 FS_IMMUTABLE_FL = 0x00000010 +BLK_IOC_FLSBUF = 0x00001261 + def ioctl_get_immutable(fd: int): """Query FS_IMMUTABLE_FL @@ -91,3 +93,26 @@ def ioctl_toggle_immutable(fd: int, set_to: bool): else: flags[0] &= ~FS_IMMUTABLE_FL fcntl.ioctl(fd, FS_IOC_SETFLAGS, flags, False) + + +def ioctl_blockdev_flushbuf(fd: int): + """Flush the block device buffer cache + + NB: This function needs the `CAP_SYS_ADMIN` capability. + + Arguments + --------- + fd + File-descriptor of a block device to operate on. + + Raises + ------ + OSError + If the underlying ioctl fails, a matching `OSError` + will be raised. + """ + + if not isinstance(fd, int) or fd < 0: + raise ValueError(f"Invalid file descriptor: '{fd}'") + + fcntl.ioctl(fd, BLK_IOC_FLSBUF, 0)