5 Copyright (C) 2014, Cisco Systems, Inc.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Author: Petar Koretic <petar.koretic@sartura.hr>
17 local uci = require "luci.model.uci"
18 local util = require "luci.util"
19 local nixio = require "nixio"
21 module("luci.controller.lxc", package.seeall)
23 function fork_exec(command)
24 local pid = nixio.fork()
31 -- patch stdin, out, err to /dev/null
32 local null = nixio.open("/dev/null", "w+")
34 nixio.dup(null, nixio.stderr)
35 nixio.dup(null, nixio.stdout)
36 nixio.dup(null, nixio.stdin)
37 if null:fileno() > 2 then
42 -- replace with target command
43 nixio.exec("/bin/sh", "-c", command)
48 page = node("admin", "services", "lxc")
49 page.target = cbi("lxc")
50 page.title = _("LXC Containers")
53 page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
56 page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
59 page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
62 page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
65 page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
70 function lxc_get_downloadable()
71 local target = lxc_get_arch_target()
74 local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list --no-validate --server %s'
75 % util.shellquote(uci.cursor():get("lxc", "lxc", "url")), 'r')
78 for line in f:lines() do
79 local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$")
80 if dist and version and dist_target == target then
81 templates[#templates+1] = "%s:%s" %{ dist, version }
87 luci.http.prepare_content("application/json")
88 luci.http.write_json(templates)
91 function lxc_create(lxc_name, lxc_template)
92 luci.http.prepare_content("text/plain")
94 if not pcall(dofile, "/etc/openwrt_release") then
95 return luci.http.write("1")
98 local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
100 luci.http.write(util.ubus("lxc", "create", {
102 template = "download",
104 "--server", uci.cursor():get("lxc", "lxc", "url"),
107 "--release", lxc_release,
108 "--arch", lxc_get_arch_target()
113 function lxc_action(lxc_action, lxc_name)
114 local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
116 luci.http.prepare_content("application/json")
117 luci.http.write_json(ec and {} or data)
120 function lxc_get_config_path()
121 local f = io.open("/etc/lxc/lxc.conf", "r")
122 local content = f:read("*all")
124 local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
132 function lxc_configuration_get(lxc_name)
133 luci.http.prepare_content("text/plain")
135 local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
136 local content = f:read("*all")
139 luci.http.write(content)
142 function lxc_configuration_set(lxc_name)
143 luci.http.prepare_content("text/plain")
145 local lxc_configuration = luci.http.formvalue("lxc_configuration")
147 if lxc_configuration == nil then
148 return luci.http.write("1")
151 local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
153 return luci.http.write("2")
156 f:write(lxc_configuration)
162 function lxc_get_arch_target()
163 local target = nixio.uname().machine
173 for k, v in pairs(target_map) do
174 if target:find(k) then