Project

General

Profile

nbd-timeout.py

python3 nbd-timeout.py /dev/nbd0 - Mehdi Abaakouk, 05/22/2020 09:13 PM

 
1
"""
2
Pythonified linux asm-generic/ioctl.h .
3

4
Produce IOCTL command numbers from their individual components, simplifying
5
C header conversion to python (keeping magic constants and differences to
6
C code to a minimum).
7

8
Common parameter meanings:
9
    type (8-bits unsigned integer)
10
        Driver-imposed ioctl number.
11
    nr (8-bits unsigned integer)
12
        Driver-imposed ioctl function number.
13
"""
14
import sys
15
import os
16
import fcntl 
17

    
18
_IOC_NRBITS = 8
19
_IOC_TYPEBITS = 8
20
_IOC_SIZEBITS = 14
21
_IOC_DIRBITS = 2
22

    
23
_IOC_NRMASK = (1 << _IOC_NRBITS) - 1
24
_IOC_TYPEMASK = (1 << _IOC_TYPEBITS) - 1
25
_IOC_SIZEMASK = (1 << _IOC_SIZEBITS) - 1
26
_IOC_DIRMASK = (1 << _IOC_DIRBITS) - 1
27

    
28
_IOC_NRSHIFT = 0
29
_IOC_TYPESHIFT = _IOC_NRSHIFT + _IOC_NRBITS
30
_IOC_SIZESHIFT = _IOC_TYPESHIFT + _IOC_TYPEBITS
31
_IOC_DIRSHIFT = _IOC_SIZESHIFT + _IOC_SIZEBITS
32

    
33
IOC_NONE = 0
34
IOC_WRITE = 1
35
IOC_READ = 2
36

    
37
def IOC(dir, type, nr, size):
38
    """
39
    dir
40
        One of IOC_NONE, IOC_WRITE, IOC_READ, or IOC_READ|IOC_WRITE.
41
        Direction is from the application's point of view, not kernel's.
42
    size (14-bits unsigned integer)
43
        Size of the buffer passed to ioctl's "arg" argument.
44
    """
45
    assert dir <= _IOC_DIRMASK, dir
46
    assert type <= _IOC_TYPEMASK, type
47
    assert nr <= _IOC_NRMASK, nr
48
    assert size <= _IOC_SIZEMASK, size
49
    return (dir << _IOC_DIRSHIFT) | (type << _IOC_TYPESHIFT) | (nr << _IOC_NRSHIFT) | (size << _IOC_SIZESHIFT)
50

    
51
def IO(type, nr):
52
    """
53
    An ioctl with no parameters.
54
    """
55
    return IOC(IOC_NONE, type, nr, 0)
56

    
57
# NOTE(sileht): default is 30 seconds
58

    
59
NBD_SET_TIMEOUT = IO( 0xab, 9 )
60

    
61
if len(sys.argv) == 2:
62
    with open(sys.argv[1], "wb") as fd:
63
        fcntl.ioctl(fd, NBD_SET_TIMEOUT, 60)
64
else:
65
    print("device missing")
66