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 entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
20 entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
21 entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
23 if nixio.fs.access("/bin/opkg") then
24 entry({"admin", "system", "packages"}, call("action_packages"), _("Software"), 10)
25 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
28 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
30 if nixio.fs.access("/etc/config/fstab") then
31 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
32 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
33 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
36 if nixio.fs.access("/sys/class/leds") then
37 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
40 entry({"admin", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 70)
41 entry({"admin", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 80)
42 entry({"admin", "system", "reboot"}, call("action_reboot"), _("Reboot"), 90)
45 function action_packages()
46 local ipkg = require("luci.model.ipkg")
47 local submit = luci.http.formvalue("submit")
56 local query = luci.http.formvalue("query")
57 query = (query ~= '') and query or nil
60 -- Packets to be installed
61 local ninst = submit and luci.http.formvalue("install")
65 local url = luci.http.formvalue("url")
66 if url and url ~= '' and submit then
72 install[ninst], out, err = ipkg.install(ninst)
73 stdout[#stdout+1] = out
74 stderr[#stderr+1] = err
79 install[uinst], out, err = ipkg.install(uinst)
80 stdout[#stdout+1] = out
81 stderr[#stderr+1] = err
86 local rem = submit and luci.http.formvalue("remove")
88 remove[rem], out, err = ipkg.remove(rem)
89 stdout[#stdout+1] = out
90 stderr[#stderr+1] = err
96 local update = luci.http.formvalue("update")
98 update, out, err = ipkg.update()
99 stdout[#stdout+1] = out
100 stderr[#stderr+1] = err
104 -- Upgrade all packets
105 local upgrade = luci.http.formvalue("upgrade")
107 upgrade, out, err = ipkg.upgrade()
108 stdout[#stdout+1] = out
109 stderr[#stderr+1] = err
113 luci.template.render("admin_system/packages", {
119 stdout = table.concat(stdout, ""),
120 stderr = table.concat(stderr, "")
123 -- Remove index cache
125 nixio.fs.unlink("/tmp/luci-indexcache")
129 function action_backup()
130 local sys = require "luci.sys"
131 local fs = require "luci.fs"
133 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
134 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
135 local backup_cmd = "tar -czT %s 2>/dev/null"
138 luci.http.setfilehandler(
139 function(meta, chunk, eof)
140 if not restore_fpi then
141 restore_fpi = io.popen(restore_cmd, "w")
144 restore_fpi:write(chunk)
152 local upload = luci.http.formvalue("archive")
153 local backup = luci.http.formvalue("backup")
154 local reset = reset_avail and luci.http.formvalue("reset")
156 if upload and #upload > 0 then
157 luci.template.render("admin_system/applyreboot")
160 local filelist = "/tmp/luci-backup-list.%d" % os.time()
163 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
164 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
165 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
168 if fs.access(filelist) then
169 local reader = ltn12_popen(backup_cmd:format(filelist))
170 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
171 luci.sys.hostname(), os.date("%Y-%m-%d")})
172 luci.http.prepare_content("application/x-targz")
173 luci.ltn12.pump.all(reader, luci.http.write)
177 luci.template.render("admin_system/applyreboot")
178 luci.util.exec("mtd -r erase rootfs_data")
180 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
184 function action_passwd()
185 local p1 = luci.http.formvalue("pwd1")
186 local p2 = luci.http.formvalue("pwd2")
191 stat = luci.sys.user.setpasswd("root", p1)
197 luci.template.render("admin_system/passwd", {stat=stat})
200 function action_reboot()
201 local reboot = luci.http.formvalue("reboot")
202 luci.template.render("admin_system/reboot", {reboot=reboot})
208 function action_upgrade()
209 require("luci.model.uci")
211 local tmpfile = "/tmp/firmware.img"
213 local function image_supported()
215 return ( 0 == os.execute(
216 ". /etc/functions.sh; " ..
217 "include /lib/upgrade; " ..
218 "platform_check_image %q >/dev/null"
223 local function image_checksum()
224 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
227 local function storage_size()
229 if nixio.fs.access("/proc/mtd") then
230 for l in io.lines("/proc/mtd") do
231 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
233 size = tonumber(s, 16)
237 elseif nixio.fs.access("/proc/partitions") then
238 for l in io.lines("/proc/partitions") do
239 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
240 if b and n and not n:match('[0-9]') then
241 size = tonumber(b) * 1024
250 -- Install upload handler
252 luci.http.setfilehandler(
253 function(meta, chunk, eof)
254 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
255 file = io.open(tmpfile, "w")
257 if file and chunk then
268 local keep_avail = true
269 local step = tonumber(luci.http.formvalue("step") or 1)
270 local has_image = nixio.fs.access(tmpfile)
271 local has_support = image_supported()
272 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
273 local has_upload = luci.http.formvalue("image")
275 -- This does the actual flashing which is invoked inside an iframe
276 -- so don't produce meaningful errors here because the the
277 -- previous pages should arrange the stuff as required.
279 if has_platform and has_image and has_support then
280 -- Mimetype text/plain
281 luci.http.prepare_content("text/plain")
282 luci.http.write("Starting sysupgrade...\n")
286 -- Now invoke sysupgrade
287 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
288 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
289 keepcfg and "" or "-n", tmpfile
292 luci.ltn12.pump.all(flash, luci.http.write)
297 -- This is step 1-3, which does the user interaction and
301 -- Step 1: file upload, error on unsupported image format
302 elseif not has_image or not has_support or step == 1 then
303 -- If there is an image but user has requested step 1
304 -- or type is not supported, then remove it.
306 nixio.fs.unlink(tmpfile)
309 luci.template.render("admin_system/upgrade", {
311 bad_image=(has_image and not has_support or false),
312 keepavail=keep_avail,
313 supported=has_platform
316 -- Step 2: present uploaded file, show checksum, confirmation
317 elseif step == 2 then
318 luci.template.render("admin_system/upgrade", {
320 checksum=image_checksum(),
321 filesize=nixio.fs.stat(tmpfile).size,
322 flashsize=storage_size(),
323 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
326 -- Step 3: load iframe which calls the actual flash procedure
327 elseif step == 3 then
328 luci.template.render("admin_system/upgrade", {
330 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
335 function ltn12_popen(command)
337 local fdi, fdo = nixio.pipe()
338 local pid = nixio.fork()
344 local buffer = fdi:read(2048)
345 local wpid, stat = nixio.waitpid(pid, "nohang")
346 if not close and wpid and stat == "exited" then
350 if buffer and #buffer > 0 then
358 nixio.dup(fdo, nixio.stdout)
361 nixio.exec("/bin/sh", "-c", command)