https://www.unitedbsd.com/d/590-testing-the-lua-kernel-interpreter UnitedBSD Loading... This site is best viewed in a modern browser with JavaScript enabled. Something went wrong while trying to load the full version of this site. Try hard-refreshing this page to fix the error. Testing the Lua kernel interpreter JuvenalUrbino I was curious to see lua(4) in action, so I decided to perform some quick test. printing a "hello, world!" script First of all, load the relevant kernel modules: $ modload lua $ modload luasystm To have them loaded automatically at boot: printf '%s\n%s\n' 'lua' 'luasystm' >> /etc/modules.conf If everything's right the kernel buffer will show: [ 6.176500] lua0: Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio And modstat will return: NAME CLASS SOURCE FLAG REFS SIZE REQUIRES lua misc filesys - 1 - - luasystm misc filesys - 0 - lua Let's enhance the verbosity of the lua(4) module: $ sysctl -w kern.lua.verbose=1 kern.lua.verbose: 0 -> 1 Without further ado, we can now proceed to test the built-in kernel parser, using luactl(8): 1. create a state to load our scripts $ luactl create s1 testing s1 created $ luactl Name Creator Description s1 user testing 2. load luasystm bindings on state s1 (required for print()) $ luactl require s1 systm systm required by s1 3. Write a sample 'hello world' script: $ cat << eof > hello.lua -- hello world! systm.print("hello world!\n") eof $ luactl load s1 ./hello.lua ./hello.lua loaded into s1 $ dmesg | tail -1 [ 2769.687618] hello world! Notice that the absolute path is required for luactl to read the script more testing 1) parsing simple functions -- sum func function add(x, y) return x + y end systm.print("the sum of 16 and 32 is:\n") systm.print (add(16, 32)) systm.print ("\n") -- factorial func function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end systm.print("the factorial of 7 is:\n") systm.print (fact(7)) $ luactl load s1 ./sample.lua ./sample.lua loaded into s1 $ dmesg | grep -A 5 sample [ 4283.160428] lua0: loading ./sample.lua into state s1 [ 4283.170432] the sum of 16 and 32 is: [ 4283.170432] 48 [ 4283.170432] the factorial of 7 is: [ 4283.180433] 5040 2) displaying OS info -- print system info function uname() tbl = {"copyright", "cpu_model", "machine", "machine_arch", "osrelease", "os type", "kernel_ident", "version"} for i = 1, #tbl do systm.print(systm[tbl[i]] .. "\n") end end uname() Which produces: [ 5519.710421] lua0: loading ./sys.lua into state s1 [ 5519.720425] Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, [ 5519.730424] 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, [ 5519.730424] 2018, 2019, 2020 The NetBSD Foundation, Inc. All rights reserved. [ 5519.740424] Copyright (c) 1982, 1986, 1989, 1991, 1993 [ 5519.750425] The Regents of the University of California. All rights reserved. [ 5519.760427] raspberrypi,3-model-b [ 5519.760427] evbarm [ 5519.760427] aarch64 [ 5519.760427] 9.2_STABLE [ 5519.770428] NetBSD [ 5519.770428] GENERIC64 [ 5519.770428] NetBSD 9.2_STABLE (GENERIC64) #0: Fri Aug 20 19:33:44 UTC 2021 [ 5519.780428] mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/GENERIC64 3) display hostname function hostname() local f = io.popen ("/bin/hostname") local host = f:read("*a") or "" f:close() host =string.gsub(host, "\n$", "") systm.print (host) end systm.print("Welcome to:\n") hostname() This is not going to work: $ luactl load s1 ./host.lua luactl: LUALOAD: Invalid argument ---------------------------------------------------------------------