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)
28 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
30 if luci.fs.isdirectory("/sys/class/leds") then
31 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
34 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
35 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("admin_upgrade"), 80)
36 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
39 function action_packages()
40 local ipkg = require("luci.model.ipkg")
42 local submit = luci.http.formvalue("submit")
46 local query = luci.http.formvalue("query")
47 query = (query ~= '') and query or nil
50 -- Packets to be installed
51 local install = submit and luci.http.formvaluetable("install")
54 local url = luci.http.formvalue("url")
55 if url and url ~= '' and submit then
64 for k, v in pairs(install) do
65 void, install[k] = ipkg.install(k)
71 local remove = submit and luci.http.formvaluetable("remove")
73 for k, v in pairs(remove) do
74 void, remove[k] = ipkg.remove(k)
80 local update = luci.http.formvalue("update")
82 void, update = ipkg.update()
86 -- Upgrade all packets
87 local upgrade = luci.http.formvalue("upgrade")
89 void, upgrade = ipkg.upgrade()
94 local info = luci.model.ipkg.info(query and "*"..query.."*")
98 -- Sort after status and name
99 for k, v in pairs(info) do
101 for i, j in pairs(pkgs) do
102 local vins = (v.Status and v.Status.installed)
103 local jins = (j.Status and j.Status.installed)
109 if j.Package > v.Package then
115 table.insert(pkgs, x+1, v)
118 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
119 install=install, remove=remove, update=update, upgrade=upgrade})
122 function action_backup()
123 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
124 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
125 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
128 luci.http.setfilehandler(
129 function(meta, chunk, eof)
130 if not restore_fpi then
131 restore_fpi = io.popen(restore_cmd, "w")
134 restore_fpi:write(chunk)
142 local upload = luci.http.formvalue("archive")
143 local backup = luci.http.formvalue("backup")
144 local reset = reset_avail and luci.http.formvalue("reset")
146 if upload and #upload > 0 then
147 luci.template.render("admin_system/applyreboot")
150 luci.util.perror(backup_cmd:format(_keep_pattern()))
151 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
152 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
153 luci.sys.hostname(), os.date("%Y-%m-%d")})
154 luci.http.prepare_content("application/x-targz")
155 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
157 luci.template.render("admin_system/applyreboot")
158 luci.util.exec("mtd -r erase rootfs_data")
160 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
164 function action_passwd()
165 local p1 = luci.http.formvalue("pwd1")
166 local p2 = luci.http.formvalue("pwd2")
171 stat = luci.sys.user.setpasswd("root", p1)
177 luci.template.render("admin_system/passwd", {stat=stat})
180 function action_reboot()
181 local reboot = luci.http.formvalue("reboot")
182 luci.template.render("admin_system/reboot", {reboot=reboot})
188 function action_upgrade()
189 require("luci.model.uci")
191 local tmpfile = "/tmp/firmware.img"
193 local function image_supported()
195 return ( 0 == os.execute(
196 ". /etc/functions.sh; " ..
197 "include /lib/upgrade; " ..
198 "platform_check_image %q >/dev/null"
203 local function image_checksum()
204 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
207 local function storage_size()
209 if luci.fs.access("/proc/mtd") then
210 for l in io.lines("/proc/mtd") do
211 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
213 size = tonumber(s, 16)
217 elseif luci.fs.access("/proc/partitions") then
218 for l in io.lines("/proc/partitions") do
219 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
220 if b and n and not n:match('[0-9]') then
221 size = tonumber(b) * 1024
230 -- Install upload handler
232 luci.http.setfilehandler(
233 function(meta, chunk, eof)
234 if not luci.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
235 file = io.open(tmpfile, "w")
237 if file and chunk then
248 local keep_avail = true
249 local step = tonumber(luci.http.formvalue("step") or 1)
250 local has_image = luci.fs.access(tmpfile)
251 local has_support = image_supported()
252 local has_platform = luci.fs.access("/lib/upgrade/platform.sh")
253 local has_upload = luci.http.formvalue("image")
255 -- This does the actual flashing which is invoked inside an iframe
256 -- so don't produce meaningful errors here because the the
257 -- previous pages should arrange the stuff as required.
259 if has_platform and has_image and has_support then
260 -- Next line is to bypass luci.http layer
261 luci.http.context.eoh = true
263 -- Now invoke sysupgrade
264 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
265 os.execute("/sbin/luci-flash %s %q" %{
266 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
269 -- Make sure the device is rebooted
275 -- This is step 1-3, which does the user interaction and
279 -- Step 1: file upload, error on unsupported image format
280 elseif not has_image or not has_support or step == 1 then
281 -- If there is an image but user has requested step 1
282 -- or type is not supported, then remove it.
284 luci.fs.unlink(tmpfile)
287 luci.template.render("admin_system/upgrade", {
289 bad_image=(has_image and not has_support or false),
290 keepavail=keep_avail,
291 supported=has_platform
294 -- Step 2: present uploaded file, show checksum, confirmation
295 elseif step == 2 then
296 luci.template.render("admin_system/upgrade", {
298 checksum=image_checksum(),
299 filesize=luci.fs.stat(tmpfile).size,
300 flashsize=storage_size(),
301 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
304 -- Step 3: load iframe which calls the actual flash procedure
305 elseif step == 3 then
306 luci.template.render("admin_system/upgrade", {
308 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
313 function _keep_pattern()
315 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
318 for k, v in pairs(files) do
319 if k:sub(1,1) ~= "." and luci.fs.glob(v) then
320 kpattern = kpattern .. " " .. v