- Title:
- Testing the Lua kernel interpreter
- Authors:
- Paolo Vincenzo Olivo
- Date:
- Topics:
- NetBSD
- Id:
- d727aa
I was curious to see <https://man.netbsd.org/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:
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 (https://man.netbsd.org/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 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 lua0: loading ./sample.lua into state s1 the sum of 16 and 32 is: 48 the factorial of 7 is: 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] .. "\n") end end
uname()
Which produces: lua0: loading ./sys.lua into state s1 Copyright (c) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020 The NetBSD Foundation, Inc. All rights reserved. Copyright (c) 1982, 1986, 1989, 1991, 1993 The Regents of the University of California. All rights reserved.
raspberrypi,3-model-b evbarm aarch64 9.2_STABLE NetBSD GENERIC64 NetBSD 9.2_STABLE (GENERIC64) #0: Fri Aug 20 19:33:44 UTC 2021 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()