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("admin-core")
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("a_s_packages"), 10)
24 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
25 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("a_s_changepw"), 20)
26 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("a_s_sshkeys"), 30)
27 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("process_head"), 45)
29 if nixio.fs.access("/etc/config/fstab") then
30 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
33 if nixio.fs.access("/sys/class/leds") then
34 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
37 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
38 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("admin_upgrade"), 80)
39 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
42 function action_packages()
43 local ipkg = require("luci.model.ipkg")
44 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 ninst = submit and luci.http.formvalue("install")
59 local url = luci.http.formvalue("url")
60 if url and url ~= '' and submit then
66 _, install[ninst] = ipkg.install(ninst)
71 _, install[uinst] = ipkg.install(uinst)
76 local rem = submit and luci.http.formvalue("remove")
78 _, remove[rem] = ipkg.remove(rem)
84 local update = luci.http.formvalue("update")
86 _, update = ipkg.update()
90 -- Upgrade all packets
91 local upgrade = luci.http.formvalue("upgrade")
93 _, upgrade = ipkg.upgrade()
97 luci.template.render("admin_system/packages", {
98 query=query, install=install, remove=remove, update=update, upgrade=upgrade
101 -- Remove index cache
103 nixio.fs.unlink("/tmp/luci-indexcache")
107 function action_backup()
108 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
109 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
110 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
113 luci.http.setfilehandler(
114 function(meta, chunk, eof)
115 if not restore_fpi then
116 restore_fpi = io.popen(restore_cmd, "w")
119 restore_fpi:write(chunk)
127 local upload = luci.http.formvalue("archive")
128 local backup = luci.http.formvalue("backup")
129 local reset = reset_avail and luci.http.formvalue("reset")
131 if upload and #upload > 0 then
132 luci.template.render("admin_system/applyreboot")
135 luci.util.perror(backup_cmd:format(_keep_pattern()))
136 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
137 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
138 luci.sys.hostname(), os.date("%Y-%m-%d")})
139 luci.http.prepare_content("application/x-targz")
140 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
142 luci.template.render("admin_system/applyreboot")
143 luci.util.exec("mtd -r erase rootfs_data")
145 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
149 function action_passwd()
150 local p1 = luci.http.formvalue("pwd1")
151 local p2 = luci.http.formvalue("pwd2")
156 stat = luci.sys.user.setpasswd("root", p1)
162 luci.template.render("admin_system/passwd", {stat=stat})
165 function action_reboot()
166 local reboot = luci.http.formvalue("reboot")
167 luci.template.render("admin_system/reboot", {reboot=reboot})
173 function action_upgrade()
174 require("luci.model.uci")
176 local tmpfile = "/tmp/firmware.img"
178 local function image_supported()
180 return ( 0 == os.execute(
181 ". /etc/functions.sh; " ..
182 "include /lib/upgrade; " ..
183 "platform_check_image %q >/dev/null"
188 local function image_checksum()
189 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
192 local function storage_size()
194 if nixio.fs.access("/proc/mtd") then
195 for l in io.lines("/proc/mtd") do
196 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
198 size = tonumber(s, 16)
202 elseif nixio.fs.access("/proc/partitions") then
203 for l in io.lines("/proc/partitions") do
204 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
205 if b and n and not n:match('[0-9]') then
206 size = tonumber(b) * 1024
215 -- Install upload handler
217 luci.http.setfilehandler(
218 function(meta, chunk, eof)
219 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
220 file = io.open(tmpfile, "w")
222 if file and chunk then
233 local keep_avail = true
234 local step = tonumber(luci.http.formvalue("step") or 1)
235 local has_image = nixio.fs.access(tmpfile)
236 local has_support = image_supported()
237 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
238 local has_upload = luci.http.formvalue("image")
240 -- This does the actual flashing which is invoked inside an iframe
241 -- so don't produce meaningful errors here because the the
242 -- previous pages should arrange the stuff as required.
244 if has_platform and has_image and has_support then
245 -- Mimetype text/plain
246 luci.http.prepare_content("text/plain")
248 -- Now invoke sysupgrade
249 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
250 local fd = io.popen("/sbin/luci-flash %s %q" %{
251 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
256 local ln = fd:read("*l")
257 if not ln then break end
258 luci.http.write(ln .. "\n")
263 -- Make sure the device is rebooted
269 -- This is step 1-3, which does the user interaction and
273 -- Step 1: file upload, error on unsupported image format
274 elseif not has_image or not has_support or step == 1 then
275 -- If there is an image but user has requested step 1
276 -- or type is not supported, then remove it.
278 nixio.fs.unlink(tmpfile)
281 luci.template.render("admin_system/upgrade", {
283 bad_image=(has_image and not has_support or false),
284 keepavail=keep_avail,
285 supported=has_platform
288 -- Step 2: present uploaded file, show checksum, confirmation
289 elseif step == 2 then
290 luci.template.render("admin_system/upgrade", {
292 checksum=image_checksum(),
293 filesize=nixio.fs.stat(tmpfile).size,
294 flashsize=storage_size(),
295 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
298 -- Step 3: load iframe which calls the actual flash procedure
299 elseif step == 3 then
300 luci.template.render("admin_system/upgrade", {
302 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
307 function _keep_pattern()
309 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
312 for k, v in pairs(files) do
313 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
314 kpattern = kpattern .. " " .. v