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", "admin"}, form("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", "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)
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
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)
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)
44 function action_packages()
45 local ipkg = require("luci.model.ipkg")
46 local submit = luci.http.formvalue("submit")
52 local query = luci.http.formvalue("query")
53 query = (query ~= '') and query or nil
56 -- Packets to be installed
57 local ninst = submit and luci.http.formvalue("install")
61 local url = luci.http.formvalue("url")
62 if url and url ~= '' and submit then
68 _, install[ninst] = ipkg.install(ninst)
73 _, install[uinst] = ipkg.install(uinst)
78 local rem = submit and luci.http.formvalue("remove")
80 _, remove[rem] = ipkg.remove(rem)
86 local update = luci.http.formvalue("update")
88 _, update = ipkg.update()
92 -- Upgrade all packets
93 local upgrade = luci.http.formvalue("upgrade")
95 _, upgrade = ipkg.upgrade()
99 luci.template.render("admin_system/packages", {
100 query=query, install=install, remove=remove, update=update, upgrade=upgrade
103 -- Remove index cache
105 nixio.fs.unlink("/tmp/luci-indexcache")
109 function action_backup()
110 local sys = require "luci.sys"
111 local fs = require "luci.fs"
113 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
114 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
115 local backup_cmd = "tar -czT %s 2>/dev/null"
118 luci.http.setfilehandler(
119 function(meta, chunk, eof)
120 if not restore_fpi then
121 restore_fpi = io.popen(restore_cmd, "w")
124 restore_fpi:write(chunk)
132 local upload = luci.http.formvalue("archive")
133 local backup = luci.http.formvalue("backup")
134 local reset = reset_avail and luci.http.formvalue("reset")
136 if upload and #upload > 0 then
137 luci.template.render("admin_system/applyreboot")
140 local filelist = "/tmp/luci-backup-list.%d" % os.time()
143 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
144 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
145 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
148 if fs.access(filelist) then
149 local reader = ltn12_popen(backup_cmd:format(filelist))
150 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
151 luci.sys.hostname(), os.date("%Y-%m-%d")})
152 luci.http.prepare_content("application/x-targz")
153 luci.ltn12.pump.all(reader, 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 nixio.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 nixio.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 nixio.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 = nixio.fs.access(tmpfile)
251 local has_support = image_supported()
252 local has_platform = nixio.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 -- Mimetype text/plain
261 luci.http.prepare_content("text/plain")
262 luci.http.write("Starting sysupgrade...\n")
266 -- Now invoke sysupgrade
267 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
268 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
269 keepcfg and "" or "-n", tmpfile
272 luci.ltn12.pump.all(flash, luci.http.write)
277 -- This is step 1-3, which does the user interaction and
281 -- Step 1: file upload, error on unsupported image format
282 elseif not has_image or not has_support or step == 1 then
283 -- If there is an image but user has requested step 1
284 -- or type is not supported, then remove it.
286 nixio.fs.unlink(tmpfile)
289 luci.template.render("admin_system/upgrade", {
291 bad_image=(has_image and not has_support or false),
292 keepavail=keep_avail,
293 supported=has_platform
296 -- Step 2: present uploaded file, show checksum, confirmation
297 elseif step == 2 then
298 luci.template.render("admin_system/upgrade", {
300 checksum=image_checksum(),
301 filesize=nixio.fs.stat(tmpfile).size,
302 flashsize=storage_size(),
303 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
306 -- Step 3: load iframe which calls the actual flash procedure
307 elseif step == 3 then
308 luci.template.render("admin_system/upgrade", {
310 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
315 function ltn12_popen(command)
317 local fdi, fdo = nixio.pipe()
318 local pid = nixio.fork()
324 local buffer = fdi:read(2048)
325 local wpid, stat = nixio.waitpid(pid, "nohang")
326 if not close and wpid and stat == "exited" then
330 if buffer and #buffer > 0 then
338 nixio.dup(fdo, nixio.stdout)
341 nixio.exec("/bin/sh", "-c", command)