OpenWrt Forum Archive

Topic: Invoking kernel modules by LuCI

The content of this topic has been archived on 1 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hello,

To perform some actions with target hardware memory I had to use Linux kernel modules that write/read something in its memory.

For example. I have kernel module named action.ko that writes something to target memory (uses functions like readw(), writew(), ...). How can I run this Linux kernel module depending on user options?

LuCI page => User set option for this memory operation and press button => Running Linux kernel module action.ko

What steps should I pass to realise this? I really had to do kernel memory access via Linux kernel modules.

For example on LuCI page we have:
Enable Action ( )
Param 1 [      ]
Param 2 [      ]
Param 3 [      ]

Then user are doing:
Enable Action (*)
Param 1 [text1]
Param 2 [      ]
Param 3 [number3]

Pressing Save and Apply and LuCI performs running of our module:
insmod action.ko param1=text1 param3=number3
(winthout param2)

I didn't understand about uci_oncommit actions and how implement script depending on the configuration values...

Any help will be appreciated. And examples will be great.

Stanislav

An example:
Create a form under:
models/cbi/insmod.lua
with the following content:

local command = "insmod action.ko"

f = SimpleForm("insmod", "Custom Kernel Module Loader")

f:field(Flag, "enable", "Enable kernel module")

-- Create 3 not mandatory (rmempty) text fields
f:field(Value, "parm1", "Parameter 1").rmempty = true
f:field(Value, "parm2", "Parameter 2").rmempty = true
f:field(Value, "parm3", "Parameter 2").rmempty = true


function f.handle(self, state, data)
    -- If all mandatory fields are filled and module should be enabled
    if state == FORM_VALID and data.enable == "1" then
        -- Clear aneable field
        data.enable = nil
    
        -- Check for parameters and append them if set
        for i, parm in ipairs({"parm1", "parm2", "parm3"}) do
            if data[parm] then
                command = "%s '%s'" % {command, data[parm]}
                
                -- Clear form field
                data[parm] = nil
            end
        end
    
        -- Execute and save return code
        local stat = luci.sys.call(command)
            
        -- Check whether action succeeded (return code: 0)
        if stat == 0 then
            f.message = "Module successfully loaded"
        else
            f.errmessage = "Failed to load module. Code: %i" % stat
        end
    end
end

-- Return form
return f

and add a line like:

entry({"admin", "system", "insmod"}, form("insmod"), "Kernel Module", 100)

to the index-function of a controller of your choice (or create a new one).
You will find the site under "Kernel Module" in the "System" menu.

(Last edited by CyrusFF on 1 Oct 2008, 14:40)

Big thanks, Cyrus! LuCI realy rules.

I've tried. But what if I want to keep configuration saved? In other words to make board remember all parameters written by user (flag, parm1, ...).

And another question. In what folder should I place file action.ko.

I realy need help about all this. :-(

The discussion might have continued from here.