dd78f1e8c6ae94d4ea129d1354425e01566e65a6
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.controller.admin.system", package.seeall)
17
18 function index()
19 luci.i18n.loadc("base")
20 local i18n = luci.i18n.translate
21
22 entry({"admin", "system"}, alias("admin", "system", "system"), i18n("System"), 30).index = true
23 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("System"), 1)
24 entry({"admin", "system", "admin"}, cbi("admin_system/admin"), i18n("Administration"), 2)
25 entry({"admin", "system", "packages"}, call("action_packages"), i18n("Software"), 10)
26 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
27 entry({"admin", "system", "startup"}, form("admin_system/startup"), i18n("Startup"), 45)
28
29 if nixio.fs.access("/etc/config/fstab") then
30 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
31 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
32 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
33 end
34
35 if nixio.fs.access("/sys/class/leds") then
36 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
37 end
38
39 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
40 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
41 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
42 end
43
44 function action_packages()
45 local ipkg = require("luci.model.ipkg")
46 local submit = luci.http.formvalue("submit")
47 local changes = false
48 local install = { }
49 local remove = { }
50 local stdout = { "" }
51 local stderr = { "" }
52 local out, err
53
54 -- Search query
55 local query = luci.http.formvalue("query")
56 query = (query ~= '') and query or nil
57
58
59 -- Packets to be installed
60 local ninst = submit and luci.http.formvalue("install")
61 local uinst = nil
62
63 -- Install from URL
64 local url = luci.http.formvalue("url")
65 if url and url ~= '' and submit then
66 uinst = url
67 end
68
69 -- Do install
70 if ninst then
71 install[ninst], out, err = ipkg.install(ninst)
72 stdout[#stdout+1] = out
73 stderr[#stderr+1] = err
74 changes = true
75 end
76
77 if uinst then
78 install[uinst], out, err = ipkg.install(uinst)
79 stdout[#stdout+1] = out
80 stderr[#stderr+1] = err
81 changes = true
82 end
83
84 -- Remove packets
85 local rem = submit and luci.http.formvalue("remove")
86 if rem then
87 remove[rem], out, err = ipkg.remove(rem)
88 stdout[#stdout+1] = out
89 stderr[#stderr+1] = err
90 changes = true
91 end
92
93
94 -- Update all packets
95 local update = luci.http.formvalue("update")
96 if update then
97 update, out, err = ipkg.update()
98 stdout[#stdout+1] = out
99 stderr[#stderr+1] = err
100 end
101
102
103 -- Upgrade all packets
104 local upgrade = luci.http.formvalue("upgrade")
105 if upgrade then
106 upgrade, out, err = ipkg.upgrade()
107 stdout[#stdout+1] = out
108 stderr[#stderr+1] = err
109 end
110
111
112 luci.template.render("admin_system/packages", {
113 query = query,
114 install = install,
115 remove = remove,
116 update = update,
117 upgrade = upgrade,
118 stdout = table.concat(stdout, ""),
119 stderr = table.concat(stderr, "")
120 })
121
122 -- Remove index cache
123 if changes then
124 nixio.fs.unlink("/tmp/luci-indexcache")
125 end
126 end
127
128 function action_backup()
129 local sys = require "luci.sys"
130 local fs = require "luci.fs"
131
132 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
133 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
134 local backup_cmd = "tar -czT %s 2>/dev/null"
135
136 local restore_fpi
137 luci.http.setfilehandler(
138 function(meta, chunk, eof)
139 if not restore_fpi then
140 restore_fpi = io.popen(restore_cmd, "w")
141 end
142 if chunk then
143 restore_fpi:write(chunk)
144 end
145 if eof then
146 restore_fpi:close()
147 end
148 end
149 )
150
151 local upload = luci.http.formvalue("archive")
152 local backup = luci.http.formvalue("backup")
153 local reset = reset_avail and luci.http.formvalue("reset")
154
155 if upload and #upload > 0 then
156 luci.template.render("admin_system/applyreboot")
157 luci.sys.reboot()
158 elseif backup then
159 local filelist = "/tmp/luci-backup-list.%d" % os.time()
160
161 sys.call(
162 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
163 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
164 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
165 )
166
167 if fs.access(filelist) then
168 local reader = ltn12_popen(backup_cmd:format(filelist))
169 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
170 luci.sys.hostname(), os.date("%Y-%m-%d")})
171 luci.http.prepare_content("application/x-targz")
172 luci.ltn12.pump.all(reader, luci.http.write)
173 fs.unlink(filelist)
174 end
175 elseif reset then
176 luci.template.render("admin_system/applyreboot")
177 luci.util.exec("mtd -r erase rootfs_data")
178 else
179 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
180 end
181 end
182
183 function action_passwd()
184 local p1 = luci.http.formvalue("pwd1")
185 local p2 = luci.http.formvalue("pwd2")
186 local stat = nil
187
188 if p1 or p2 then
189 if p1 == p2 then
190 stat = luci.sys.user.setpasswd("root", p1)
191 else
192 stat = 10
193 end
194 end
195
196 luci.template.render("admin_system/passwd", {stat=stat})
197 end
198
199 function action_reboot()
200 local reboot = luci.http.formvalue("reboot")
201 luci.template.render("admin_system/reboot", {reboot=reboot})
202 if reboot then
203 luci.sys.reboot()
204 end
205 end
206
207 function action_upgrade()
208 require("luci.model.uci")
209
210 local tmpfile = "/tmp/firmware.img"
211
212 local function image_supported()
213 -- XXX: yay...
214 return ( 0 == os.execute(
215 ". /etc/functions.sh; " ..
216 "include /lib/upgrade; " ..
217 "platform_check_image %q >/dev/null"
218 % tmpfile
219 ) )
220 end
221
222 local function image_checksum()
223 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
224 end
225
226 local function storage_size()
227 local size = 0
228 if nixio.fs.access("/proc/mtd") then
229 for l in io.lines("/proc/mtd") do
230 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
231 if n == "linux" then
232 size = tonumber(s, 16)
233 break
234 end
235 end
236 elseif nixio.fs.access("/proc/partitions") then
237 for l in io.lines("/proc/partitions") do
238 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
239 if b and n and not n:match('[0-9]') then
240 size = tonumber(b) * 1024
241 break
242 end
243 end
244 end
245 return size
246 end
247
248
249 -- Install upload handler
250 local file
251 luci.http.setfilehandler(
252 function(meta, chunk, eof)
253 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
254 file = io.open(tmpfile, "w")
255 end
256 if file and chunk then
257 file:write(chunk)
258 end
259 if file and eof then
260 file:close()
261 end
262 end
263 )
264
265
266 -- Determine state
267 local keep_avail = true
268 local step = tonumber(luci.http.formvalue("step") or 1)
269 local has_image = nixio.fs.access(tmpfile)
270 local has_support = image_supported()
271 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
272 local has_upload = luci.http.formvalue("image")
273
274 -- This does the actual flashing which is invoked inside an iframe
275 -- so don't produce meaningful errors here because the the
276 -- previous pages should arrange the stuff as required.
277 if step == 4 then
278 if has_platform and has_image and has_support then
279 -- Mimetype text/plain
280 luci.http.prepare_content("text/plain")
281 luci.http.write("Starting sysupgrade...\n")
282
283 io.flush()
284
285 -- Now invoke sysupgrade
286 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
287 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
288 keepcfg and "" or "-n", tmpfile
289 })
290
291 luci.ltn12.pump.all(flash, luci.http.write)
292 end
293
294
295 --
296 -- This is step 1-3, which does the user interaction and
297 -- image upload.
298 --
299
300 -- Step 1: file upload, error on unsupported image format
301 elseif not has_image or not has_support or step == 1 then
302 -- If there is an image but user has requested step 1
303 -- or type is not supported, then remove it.
304 if has_image then
305 nixio.fs.unlink(tmpfile)
306 end
307
308 luci.template.render("admin_system/upgrade", {
309 step=1,
310 bad_image=(has_image and not has_support or false),
311 keepavail=keep_avail,
312 supported=has_platform
313 } )
314
315 -- Step 2: present uploaded file, show checksum, confirmation
316 elseif step == 2 then
317 luci.template.render("admin_system/upgrade", {
318 step=2,
319 checksum=image_checksum(),
320 filesize=nixio.fs.stat(tmpfile).size,
321 flashsize=storage_size(),
322 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
323 } )
324
325 -- Step 3: load iframe which calls the actual flash procedure
326 elseif step == 3 then
327 luci.template.render("admin_system/upgrade", {
328 step=3,
329 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
330 } )
331 end
332 end
333
334 function ltn12_popen(command)
335
336 local fdi, fdo = nixio.pipe()
337 local pid = nixio.fork()
338
339 if pid > 0 then
340 fdo:close()
341 local close
342 return function()
343 local buffer = fdi:read(2048)
344 local wpid, stat = nixio.waitpid(pid, "nohang")
345 if not close and wpid and stat == "exited" then
346 close = true
347 end
348
349 if buffer and #buffer > 0 then
350 return buffer
351 elseif close then
352 fdi:close()
353 return nil
354 end
355 end
356 elseif pid == 0 then
357 nixio.dup(fdo, nixio.stdout)
358 fdi:close()
359 fdo:close()
360 nixio.exec("/bin/sh", "-c", command)
361 end
362 end