1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
--[[
LuCI LXC module
Copyright (C) 2014, Cisco Systems, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Author: Petar Koretic <petar.koretic@sartura.hr>
]]--
local uci = require "luci.model.uci"
local util = require "luci.util"
local nixio = require "nixio"
module("luci.controller.lxc", package.seeall)
function fork_exec(command)
local pid = nixio.fork()
if pid > 0 then
return
elseif pid == 0 then
-- change to root dir
nixio.chdir("/")
-- patch stdin, out, err to /dev/null
local null = nixio.open("/dev/null", "w+")
if null then
nixio.dup(null, nixio.stderr)
nixio.dup(null, nixio.stdout)
nixio.dup(null, nixio.stdin)
if null:fileno() > 2 then
null:close()
end
end
-- replace with target command
nixio.exec("/bin/sh", "-c", command)
end
end
function index()
page = node("admin", "services", "lxc")
page.target = cbi("lxc")
page.title = _("LXC Containers")
page.order = 70
page = entry({"admin", "services", "lxc_create"}, call("lxc_create"), nil)
page.leaf = true
page = entry({"admin", "services", "lxc_action"}, call("lxc_action"), nil)
page.leaf = true
page = entry({"admin", "services", "lxc_get_downloadable"}, call("lxc_get_downloadable"), nil)
page.leaf = true
page = entry({"admin", "services", "lxc_configuration_get"}, call("lxc_configuration_get"), nil)
page.leaf = true
page = entry({"admin", "services", "lxc_configuration_set"}, call("lxc_configuration_set"), nil)
page.leaf = true
end
function lxc_get_downloadable()
local target = lxc_get_arch_target()
local templates = {}
local f = io.popen('sh /usr/share/lxc/templates/lxc-download --list --no-validate --server %s'
% util.shellquote(uci.cursor():get("lxc", "lxc", "url")), 'r')
local line
for line in f:lines() do
local dist, version, dist_target = line:match("^(%S+)%s+(%S+)%s+(%S+)%s+default%s+%S+$")
if dist and version and dist_target == target then
templates[#templates+1] = "%s:%s" %{ dist, version }
end
end
f:close()
luci.http.prepare_content("application/json")
luci.http.write_json(templates)
end
function lxc_create(lxc_name, lxc_template)
luci.http.prepare_content("text/plain")
if not pcall(dofile, "/etc/openwrt_release") then
return luci.http.write("1")
end
local lxc_dist, lxc_release = lxc_template:match("^(.+):(.+)$")
luci.http.write(util.ubus("lxc", "create", {
name = lxc_name,
template = "download",
args = {
"--server", uci.cursor():get("lxc", "lxc", "url"),
"--no-validate",
"--dist", lxc_dist,
"--release", lxc_release,
"--arch", lxc_get_arch_target()
}
}))
end
function lxc_action(lxc_action, lxc_name)
local data, ec = util.ubus("lxc", lxc_action, lxc_name and { name = lxc_name } or {})
luci.http.prepare_content("application/json")
luci.http.write_json(ec and {} or data)
end
function lxc_get_config_path()
local f = io.open("/etc/lxc/lxc.conf", "r")
local content = f:read("*all")
f:close()
local ret = content:match('^%s*lxc.lxcpath%s*=%s*([^%s]*)')
if ret then
return ret .. "/"
else
return "/srv/lxc/"
end
end
function lxc_configuration_get(lxc_name)
luci.http.prepare_content("text/plain")
local f = io.open(lxc_get_config_path() .. lxc_name .. "/config", "r")
local content = f:read("*all")
f:close()
luci.http.write(content)
end
function lxc_configuration_set(lxc_name)
luci.http.prepare_content("text/plain")
local lxc_configuration = luci.http.formvalue("lxc_configuration")
if lxc_configuration == nil then
return luci.http.write("1")
end
local f, err = io.open(lxc_get_config_path() .. lxc_name .. "/config","w+")
if not f then
return luci.http.write("2")
end
f:write(lxc_configuration)
f:close()
luci.http.write("0")
end
function lxc_get_arch_target()
local target = nixio.uname().machine
local target_map {
armv5 = "armel",
armv6 = "armel",
armv7 = "armhf",
armv8 = "arm64",
x86_64 = "amd64"
}
local k, v
for k, v in pairs(target_map) do
if target:find(k) then
return v
end
end
return target
end
|