2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
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
15 module("luci.controller.admin.system", package.seeall)
18 luci.i18n.loadc("base")
19 local i18n = luci.i18n.translate
21 entry({"admin", "system"}, alias("admin", "system", "system"), i18n("System"), 30).index = true
22 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("System"), 1)
23 entry({"admin", "system", "packages"}, call("action_packages"), i18n("Software"), 10)
24 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
25 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
26 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
27 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
29 if nixio.fs.access("/etc/config/fstab") then
30 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
33 if nixio.fs.access("/sys/class/leds") then
34 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
37 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
38 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
39 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
42 function action_packages()
43 local ipkg = require("luci.model.ipkg")
45 local submit = luci.http.formvalue("submit")
50 local query = luci.http.formvalue("query")
51 query = (query ~= '') and query or nil
54 -- Packets to be installed
55 local install = submit and luci.http.formvaluetable("install")
58 local url = luci.http.formvalue("url")
59 if url and url ~= '' and submit then
69 for k, v in pairs(install) do
70 void, install[k] = ipkg.install(k)
77 local remove = submit and luci.http.formvaluetable("remove")
79 for k, v in pairs(remove) do
80 void, remove[k] = ipkg.remove(k)
87 local update = luci.http.formvalue("update")
89 void, update = ipkg.update()
93 -- Upgrade all packets
94 local upgrade = luci.http.formvalue("upgrade")
96 void, upgrade = ipkg.upgrade()
101 local info = luci.model.ipkg.info(query and "*"..query.."*")
105 -- Sort after status and name
106 for k, v in pairs(info) do
108 for i, j in pairs(pkgs) do
109 local vins = (v.Status and v.Status.installed)
110 local jins = (j.Status and j.Status.installed)
116 if j.Package > v.Package then
122 table.insert(pkgs, x+1, v)
125 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
126 install=install, remove=remove, update=update, upgrade=upgrade})
128 -- Remove index cache
130 nixio.fs.unlink("/tmp/luci-indexcache")
134 function action_backup()
135 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
136 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
137 local backup_cmd = "tar -cz %s 2>/dev/null"
140 luci.http.setfilehandler(
141 function(meta, chunk, eof)
142 if not restore_fpi then
143 restore_fpi = io.popen(restore_cmd, "w")
146 restore_fpi:write(chunk)
154 local upload = luci.http.formvalue("archive")
155 local backup = luci.http.formvalue("backup")
156 local reset = reset_avail and luci.http.formvalue("reset")
158 if upload and #upload > 0 then
159 luci.template.render("admin_system/applyreboot")
162 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
163 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
164 luci.sys.hostname(), os.date("%Y-%m-%d")})
165 luci.http.prepare_content("application/x-targz")
166 luci.ltn12.pump.all(reader, luci.http.write)
168 luci.template.render("admin_system/applyreboot")
169 luci.util.exec("mtd -r erase rootfs_data")
171 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
175 function action_passwd()
176 local p1 = luci.http.formvalue("pwd1")
177 local p2 = luci.http.formvalue("pwd2")
182 stat = luci.sys.user.setpasswd("root", p1)
188 luci.template.render("admin_system/passwd", {stat=stat})
191 function action_reboot()
192 local reboot = luci.http.formvalue("reboot")
193 luci.template.render("admin_system/reboot", {reboot=reboot})
199 function action_upgrade()
200 require("luci.model.uci")
202 local tmpfile = "/tmp/firmware.img"
204 local function image_supported()
206 return ( 0 == os.execute(
207 ". /etc/functions.sh; " ..
208 "include /lib/upgrade; " ..
209 "platform_check_image %q >/dev/null"
214 local function image_checksum()
215 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
218 local function storage_size()
220 if nixio.fs.access("/proc/mtd") then
221 for l in io.lines("/proc/mtd") do
222 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
224 size = tonumber(s, 16)
228 elseif nixio.fs.access("/proc/partitions") then
229 for l in io.lines("/proc/partitions") do
230 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
231 if b and n and not n:match('[0-9]') then
232 size = tonumber(b) * 1024
241 -- Install upload handler
243 luci.http.setfilehandler(
244 function(meta, chunk, eof)
245 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
246 file = io.open(tmpfile, "w")
248 if file and chunk then
259 local keep_avail = true
260 local step = tonumber(luci.http.formvalue("step") or 1)
261 local has_image = nixio.fs.access(tmpfile)
262 local has_support = image_supported()
263 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
264 local has_upload = luci.http.formvalue("image")
266 -- This does the actual flashing which is invoked inside an iframe
267 -- so don't produce meaningful errors here because the the
268 -- previous pages should arrange the stuff as required.
270 if has_platform and has_image and has_support then
271 -- Mimetype text/plain
272 luci.http.prepare_content("text/plain")
274 -- Now invoke sysupgrade
275 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
276 local fd = io.popen("/sbin/luci-flash %s %q" %{
277 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
282 local ln = fd:read("*l")
283 if not ln then break end
284 luci.http.write(ln .. "\n")
289 -- Make sure the device is rebooted
295 -- This is step 1-3, which does the user interaction and
299 -- Step 1: file upload, error on unsupported image format
300 elseif not has_image or not has_support or step == 1 then
301 -- If there is an image but user has requested step 1
302 -- or type is not supported, then remove it.
304 nixio.fs.unlink(tmpfile)
307 luci.template.render("admin_system/upgrade", {
309 bad_image=(has_image and not has_support or false),
310 keepavail=keep_avail,
311 supported=has_platform
314 -- Step 2: present uploaded file, show checksum, confirmation
315 elseif step == 2 then
316 luci.template.render("admin_system/upgrade", {
318 checksum=image_checksum(),
319 filesize=nixio.fs.stat(tmpfile).size,
320 flashsize=storage_size(),
321 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
324 -- Step 3: load iframe which calls the actual flash procedure
325 elseif step == 3 then
326 luci.template.render("admin_system/upgrade", {
328 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
333 function _keep_pattern()
335 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
338 for k, v in pairs(files) do
339 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
340 kpattern = kpattern .. " " .. v
347 function ltn12_popen(command)
349 local fdi, fdo = nixio.pipe()
350 local pid = nixio.fork()
356 local buffer = fdi:read(2048)
357 local wpid, stat = nixio.waitpid(pid, "nohang")
358 if not close and wpid and stat == "exited" then
362 if buffer and #buffer > 0 then
370 nixio.dup(fdo, nixio.stdout)
373 nixio.exec("/bin/sh", "-c", command)