Created
March 16, 2023 03:11
-
-
Save antranigv/9460b6be260580f2dcb6c15fa032c020 to your computer and use it in GitHub Desktop.
FreeBSD jailua for Jail management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/libexec/flua | |
local ucl = require('ucl') | |
local jail = require('jail') | |
function usage() | |
print(arg[0] .. " [ list | create jail.ucl | remove jail.ucl [jname] ]") | |
end | |
if #arg < 1 then | |
usage() | |
os.exit() | |
end | |
function remove() | |
print("TBD") | |
end | |
function list() | |
for jparams in jail.list() do | |
print(jparams["jid"] .. " = " .. jparams["name"]) | |
end | |
end | |
function create(arg) | |
-- Check arg count | |
if #arg < 2 then | |
usage() | |
os.exit() | |
end | |
-- initiate parser and parse file | |
parser = ucl.parser() | |
res, err = parser:parse_file(arg[2]) | |
if not res then | |
print('error: ' .. err) | |
os.exit(1) | |
end | |
-- convert all objects to lua | |
conf = parser:get_object() | |
-- get all jail parameters | |
allparams = jail.allparams() | |
-- iterate over jails in conf | |
for jn, jc in pairs(conf) do | |
params = {} | |
io.write("Creating new(?) jail ") | |
io.write(jn) | |
io.write(' :') | |
for i = 1, #allparams do | |
if jc[allparams[i]] ~= nil then | |
params[allparams[i]] = tostring(jc[allparams[i]]) | |
end | |
end | |
-- check if jail exists | |
jid, err = jail.getid(jn) | |
if jid then | |
print(' EXISTS! jid = ' .. jid) | |
os.exit(1) | |
end | |
-- create new jail | |
jid, err = jail.setparams(jn, params, jail.CREATE) | |
if not jid then | |
print(' ERROR: ' .. err) | |
else | |
print(' DONE! jid = ' .. jid) | |
end | |
end | |
end | |
if arg[1] == 'list' then list() | |
elseif arg[1] == 'create' then create(arg) | |
elseif arg[1] == 'remove' then remove(arg) | |
else usage() | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # jls | |
JID IP Address Hostname Path | |
1 127.0.0.2 3285056f.devbsd.zvartnots.loc /usr/local/jails/3285056f | |
2 127.0.0.3 360410a7.devbsd.zvartnots.loc /usr/local/jails/360410a7 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # cat ~/jail.ucl | |
jail0: { | |
path: "/usr/local/jails/jail0", | |
exec.start: "/bin/sh /etc/rc", | |
persist = true | |
} | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua | |
./jailua [ list | create jail.ucl | remove jail.ucl [jname] ] | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua list | |
1 = 3285056f | |
2 = 360410a7 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua create ~/jail.ucl | |
Creating new(?) jail jail0 : DONE! jid = 8 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua list | |
1 = 3285056f | |
2 = 360410a7 | |
8 = jail0 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua create ~/jail.ucl | |
Creating new(?) jail jail0 : EXISTS! jid = 8 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # jail -r 8 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua create ~/jail.ucl | |
Creating new(?) jail jail0 : DONE! jid = 9 | |
root@devbsd:/mnt/zvartnots/FreeBSD/jailua # ./jailua list | |
1 = 3285056f | |
2 = 360410a7 | |
9 = jail0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment