xref: /freebsd/share/examples/flua/libjail.lua (revision 73577bf01de5c4677dc54d97f93310286c254780)
1*73577bf0SRyan Moeller#!/usr/libexec/flua
2*73577bf0SRyan Moeller--[[
3*73577bf0SRyan Moeller/*-
4*73577bf0SRyan Moeller * SPDX-License-Identifier: BSD-2-Clause
5*73577bf0SRyan Moeller *
6*73577bf0SRyan Moeller * Copyright (c) 2020, Ryan Moeller <freqlabs@FreeBSD.org>
7*73577bf0SRyan Moeller *
8*73577bf0SRyan Moeller * Redistribution and use in source and binary forms, with or without
9*73577bf0SRyan Moeller * modification, are permitted provided that the following conditions
10*73577bf0SRyan Moeller * are met:
11*73577bf0SRyan Moeller * 1. Redistributions of source code must retain the above copyright
12*73577bf0SRyan Moeller *    notice, this list of conditions and the following disclaimer.
13*73577bf0SRyan Moeller * 2. Redistributions in binary form must reproduce the above copyright
14*73577bf0SRyan Moeller *    notice, this list of conditions and the following disclaimer in the
15*73577bf0SRyan Moeller *    documentation and/or other materials provided with the distribution.
16*73577bf0SRyan Moeller *
17*73577bf0SRyan Moeller * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18*73577bf0SRyan Moeller * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*73577bf0SRyan Moeller * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*73577bf0SRyan Moeller * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21*73577bf0SRyan Moeller * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*73577bf0SRyan Moeller * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*73577bf0SRyan Moeller * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*73577bf0SRyan Moeller * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*73577bf0SRyan Moeller * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*73577bf0SRyan Moeller * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*73577bf0SRyan Moeller * SUCH DAMAGE.
28*73577bf0SRyan Moeller *
29*73577bf0SRyan Moeller * $FreeBSD$
30*73577bf0SRyan Moeller */
31*73577bf0SRyan Moeller]]--
32*73577bf0SRyan Moeller
33*73577bf0SRyan Moellerjail = require("jail")
34*73577bf0SRyan Moellerucl = require("ucl")
35*73577bf0SRyan Moeller
36*73577bf0SRyan Moellername = "demo"
37*73577bf0SRyan Moeller
38*73577bf0SRyan Moeller-- Create a persistent jail named "demo" with all other parameters default.
39*73577bf0SRyan Moellerjid, err = jail.setparams(name, {persist = "true"}, jail.CREATE)
40*73577bf0SRyan Moellerif not jid then
41*73577bf0SRyan Moeller    error(err)
42*73577bf0SRyan Moellerend
43*73577bf0SRyan Moeller
44*73577bf0SRyan Moeller-- Get a list of all known jail parameter names.
45*73577bf0SRyan Moellerallparams = jail.allparams()
46*73577bf0SRyan Moeller
47*73577bf0SRyan Moeller-- Get all the parameters of the jail we created.
48*73577bf0SRyan Moellerjid, res = jail.getparams(name, allparams)
49*73577bf0SRyan Moellerif not jid then
50*73577bf0SRyan Moeller    error(res)
51*73577bf0SRyan Moellerend
52*73577bf0SRyan Moeller
53*73577bf0SRyan Moeller-- Display the jail's parameters as a pretty-printed JSON object.
54*73577bf0SRyan Moellerprint(ucl.to_json(res))
55*73577bf0SRyan Moeller
56*73577bf0SRyan Moeller-- Update the "persist" parameter to "false" to remove the jail.
57*73577bf0SRyan Moellerjid, err = jail.setparams(name, {persist = "false"}, jail.UPDATE)
58*73577bf0SRyan Moellerif not jid then
59*73577bf0SRyan Moeller    error(err)
60*73577bf0SRyan Moellerend
61