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
16 module("luci.controller.admin.system", package.seeall)
19 luci.i18n.loadc("base")
20 local i18n = luci.i18n.translate
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", "packages"}, call("action_packages"), i18n("Software"), 10)
25 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
26 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
27 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
28 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
30 if nixio.fs.access("/etc/config/fstab") then
31 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("Mount Points"), 50)
34 if nixio.fs.access("/sys/class/leds") then
35 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
38 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
39 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
40 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
43 function action_packages()
44 local ipkg = require("luci.model.ipkg")
45 local submit = luci.http.formvalue("submit")
51 local query = luci.http.formvalue("query")
52 query = (query ~= '') and query or nil
55 -- Packets to be installed
56 local ninst = submit and luci.http.formvalue("install")
60 local url = luci.http.formvalue("url")
61 if url and url ~= '' and submit then
67 _, install[ninst] = ipkg.install(ninst)
72 _, install[uinst] = ipkg.install(uinst)
77 local rem = submit and luci.http.formvalue("remove")
79 _, remove[rem] = ipkg.remove(rem)
85 local update = luci.http.formvalue("update")
87 _, update = ipkg.update()
91 -- Upgrade all packets
92 local upgrade = luci.http.formvalue("upgrade")
94 _, upgrade = ipkg.upgrade()
98 luci.template.render("admin_system/packages", {
99 query=query, install=install, remove=remove, update=update, upgrade=upgrade
102 -- Remove index cache
104 nixio.fs.unlink("/tmp/luci-indexcache")
108 function action_backup()
109 local sys = require "luci.sys"
110 local fs = require "luci.fs"
112 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
113 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
114 local backup_cmd = "tar -czT %s 2>/dev/null"
117 luci.http.setfilehandler(
118 function(meta, chunk, eof)
119 if not restore_fpi then
120 restore_fpi = io.popen(restore_cmd, "w")
123 restore_fpi:write(chunk)
131 local upload = luci.http.formvalue("archive")
132 local backup = luci.http.formvalue("backup")
133 local reset = reset_avail and luci.http.formvalue("reset")
135 if upload and #upload > 0 then
136 luci.template.render("admin_system/applyreboot")
139 local filelist = "/tmp/luci-backup-list.%d" % os.time()
142 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
143 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
144 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
147 if fs.access(filelist) then
148 local reader = ltn12_popen(backup_cmd:format(filelist))
149 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
150 luci.sys.hostname(), os.date("%Y-%m-%d")})
151 luci.http.prepare_content("application/x-targz")
152 luci.ltn12.pump.all(reader, luci.http.write)
156 luci.template.render("admin_system/applyreboot")
157 luci.util.exec("mtd -r erase rootfs_data")
159 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
163 function action_passwd()
164 local p1 = luci.http.formvalue("pwd1")
165 local p2 = luci.http.formvalue("pwd2")
170 stat = luci.sys.user.setpasswd("root", p1)
176 luci.template.render("admin_system/passwd", {stat=stat})
179 function action_reboot()
180 local reboot = luci.http.formvalue("reboot")
181 luci.template.render("admin_system/reboot", {reboot=reboot})
187 function action_upgrade()
188 require("luci.model.uci")
190 local tmpfile = "/tmp/firmware.img"
192 local function image_supported()
194 return ( 0 == os.execute(
195 ". /etc/functions.sh; " ..
196 "include /lib/upgrade; " ..
197 "platform_check_image %q >/dev/null"
202 local function image_checksum()
203 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
206 local function storage_size()
208 if nixio.fs.access("/proc/mtd") then
209 for l in io.lines("/proc/mtd") do
210 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
212 size = tonumber(s, 16)
216 elseif nixio.fs.access("/proc/partitions") then
217 for l in io.lines("/proc/partitions") do
218 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
219 if b and n and not n:match('[0-9]') then
220 size = tonumber(b) * 1024
229 -- Install upload handler
231 luci.http.setfilehandler(
232 function(meta, chunk, eof)
233 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
234 file = io.open(tmpfile, "w")
236 if file and chunk then
247 local keep_avail = true
248 local step = tonumber(luci.http.formvalue("step") or 1)
249 local has_image = nixio.fs.access(tmpfile)
250 local has_support = image_supported()
251 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
252 local has_upload = luci.http.formvalue("image")
254 -- This does the actual flashing which is invoked inside an iframe
255 -- so don't produce meaningful errors here because the the
256 -- previous pages should arrange the stuff as required.
258 if has_platform and has_image and has_support then
259 -- Mimetype text/plain
260 luci.http.prepare_content("text/plain")
261 luci.http.write("Starting sysupgrade...\n")
265 -- Now invoke sysupgrade
266 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
267 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
268 keepcfg and "" or "-n", tmpfile
271 luci.ltn12.pump.all(flash, luci.http.write)
276 -- This is step 1-3, which does the user interaction and
280 -- Step 1: file upload, error on unsupported image format
281 elseif not has_image or not has_support or step == 1 then
282 -- If there is an image but user has requested step 1
283 -- or type is not supported, then remove it.
285 nixio.fs.unlink(tmpfile)
288 luci.template.render("admin_system/upgrade", {
290 bad_image=(has_image and not has_support or false),
291 keepavail=keep_avail,
292 supported=has_platform
295 -- Step 2: present uploaded file, show checksum, confirmation
296 elseif step == 2 then
297 luci.template.render("admin_system/upgrade", {
299 checksum=image_checksum(),
300 filesize=nixio.fs.stat(tmpfile).size,
301 flashsize=storage_size(),
302 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
305 -- Step 3: load iframe which calls the actual flash procedure
306 elseif step == 3 then
307 luci.template.render("admin_system/upgrade", {
309 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
314 function ltn12_popen(command)
316 local fdi, fdo = nixio.pipe()
317 local pid = nixio.fork()
323 local buffer = fdi:read(2048)
324 local wpid, stat = nixio.waitpid(pid, "nohang")
325 if not close and wpid and stat == "exited" then
329 if buffer and #buffer > 0 then
337 nixio.dup(fdo, nixio.stdout)
340 nixio.exec("/bin/sh", "-c", command)