1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
5 module("luci.controller.admin.system", package.seeall)
8 local fs = require "nixio.fs"
10 entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
11 entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
12 entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
14 entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
16 if fs.access("/bin/opkg") then
17 entry({"admin", "system", "packages"}, post_on({ exec = "1" }, "action_packages"), _("Software"), 10)
18 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
21 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
22 entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
24 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
25 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
26 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
27 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
30 local nodes, number = fs.glob("/sys/class/leds/*")
32 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
35 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
36 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
37 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
38 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
40 -- call() instead of post() due to upload handling!
41 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
42 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
44 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
45 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
48 function action_clock_status()
49 local set = tonumber(luci.http.formvalue("set"))
50 if set ~= nil and set > 0 then
51 local date = os.date("*t", set)
53 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
54 date.year, date.month, date.day, date.hour, date.min, date.sec
56 luci.sys.call("/etc/init.d/sysfixtime restart")
60 luci.http.prepare_content("application/json")
61 luci.http.write_json({ timestring = os.date("%c") })
64 function action_packages()
65 local fs = require "nixio.fs"
66 local ipkg = require "luci.model.ipkg"
67 local submit = (luci.http.formvalue("exec") == "1")
77 local display = luci.http.formvalue("display") or "available"
80 local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
81 letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
84 local query = luci.http.formvalue("query")
85 query = (query ~= '') and query or nil
90 -- Packets to be installed
91 local ninst = luci.http.formvalue("install")
95 local url = luci.http.formvalue("url")
96 if url and url ~= '' then
102 install[ninst], out, err = ipkg.install(ninst)
103 stdout[#stdout+1] = out
104 stderr[#stderr+1] = err
110 for pkg in luci.util.imatch(uinst) do
111 install[uinst], out, err = ipkg.install(pkg)
112 stdout[#stdout+1] = out
113 stderr[#stderr+1] = err
119 local rem = luci.http.formvalue("remove")
121 remove[rem], out, err = ipkg.remove(rem)
122 stdout[#stdout+1] = out
123 stderr[#stderr+1] = err
128 -- Update all packets
129 update = luci.http.formvalue("update")
131 update, out, err = ipkg.update()
132 stdout[#stdout+1] = out
133 stderr[#stderr+1] = err
137 -- Upgrade all packets
138 upgrade = luci.http.formvalue("upgrade")
140 upgrade, out, err = ipkg.upgrade()
141 stdout[#stdout+1] = out
142 stderr[#stderr+1] = err
148 local no_lists = true
149 local old_lists = false
150 if fs.access("/var/opkg-lists/") then
152 for list in fs.dir("/var/opkg-lists/") do
154 if (fs.stat("/var/opkg-lists/"..list, "mtime") or 0) < (os.time() - (24 * 60 * 60)) then
162 luci.template.render("admin_system/packages", {
171 old_lists = old_lists,
172 stdout = table.concat(stdout, ""),
173 stderr = table.concat(stderr, "")
176 -- Remove index cache
178 fs.unlink("/tmp/luci-indexcache")
182 local function image_supported(image)
183 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
186 local function image_checksum(image)
187 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
190 local function image_sha256_checksum(image)
191 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
194 local function supports_sysupgrade()
195 return nixio.fs.access("/lib/upgrade/platform.sh")
198 local function supports_reset()
199 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
202 local function storage_size()
204 if nixio.fs.access("/proc/mtd") then
205 for l in io.lines("/proc/mtd") do
206 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
207 if n == "linux" or n == "firmware" then
208 size = tonumber(s, 16)
212 elseif nixio.fs.access("/proc/partitions") then
213 for l in io.lines("/proc/partitions") do
214 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
215 if b and n and not n:match('[0-9]') then
216 size = tonumber(b) * 1024
225 function action_flashops()
229 luci.template.render("admin_system/flashops", {
230 reset_avail = supports_reset(),
231 upgrade_avail = supports_sysupgrade()
235 function action_sysupgrade()
236 local fs = require "nixio.fs"
237 local http = require "luci.http"
238 local image_tmp = "/tmp/firmware.img"
242 function(meta, chunk, eof)
243 if not fp and meta and meta.name == "image" then
244 fp = io.open(image_tmp, "w")
255 if not luci.dispatcher.test_post_security() then
261 -- Cancel firmware flash
263 if http.formvalue("cancel") then
265 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
270 -- Initiate firmware flash
272 local step = tonumber(http.formvalue("step") or 1)
274 if image_supported(image_tmp) then
275 luci.template.render("admin_system/upgrade", {
276 checksum = image_checksum(image_tmp),
277 sha256ch = image_sha256_checksum(image_tmp),
278 storage = storage_size(),
279 size = (fs.stat(image_tmp, "size") or 0),
280 keep = (not not http.formvalue("keep"))
284 luci.template.render("admin_system/flashops", {
285 reset_avail = supports_reset(),
286 upgrade_avail = supports_sysupgrade(),
291 -- Start sysupgrade flash
293 elseif step == 2 then
294 local keep = (http.formvalue("keep") == "1") and "" or "-n"
295 luci.template.render("admin_system/applyreboot", {
296 title = luci.i18n.translate("Flashing..."),
297 msg = luci.i18n.translate("The system is flashing now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
298 addr = (#keep > 0) and "192.168.1.1" or nil
300 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
304 function action_backup()
305 local reader = ltn12_popen("sysupgrade --create-backup - 2>/dev/null")
308 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' %{
313 luci.http.prepare_content("application/x-targz")
314 luci.ltn12.pump.all(reader, luci.http.write)
317 function action_restore()
318 local fs = require "nixio.fs"
319 local http = require "luci.http"
320 local archive_tmp = "/tmp/restore.tar.gz"
324 function(meta, chunk, eof)
325 if not fp and meta and meta.name == "archive" then
326 fp = io.open(archive_tmp, "w")
337 if not luci.dispatcher.test_post_security() then
338 fs.unlink(archive_tmp)
342 local upload = http.formvalue("archive")
343 if upload and #upload > 0 then
344 luci.template.render("admin_system/applyreboot")
345 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
350 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
353 function action_reset()
354 if supports_reset() then
355 luci.template.render("admin_system/applyreboot", {
356 title = luci.i18n.translate("Erasing..."),
357 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
361 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot")
365 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
368 function action_passwd()
369 local p1 = luci.http.formvalue("pwd1")
370 local p2 = luci.http.formvalue("pwd2")
375 stat = luci.sys.user.setpasswd("root", p1)
381 luci.template.render("admin_system/passwd", {stat=stat})
384 function action_reboot()
388 function fork_exec(command)
389 local pid = nixio.fork()
393 -- change to root dir
396 -- patch stdin, out, err to /dev/null
397 local null = nixio.open("/dev/null", "w+")
399 nixio.dup(null, nixio.stderr)
400 nixio.dup(null, nixio.stdout)
401 nixio.dup(null, nixio.stdin)
402 if null:fileno() > 2 then
407 -- replace with target command
408 nixio.exec("/bin/sh", "-c", command)
412 function ltn12_popen(command)
414 local fdi, fdo = nixio.pipe()
415 local pid = nixio.fork()
421 local buffer = fdi:read(2048)
422 local wpid, stat = nixio.waitpid(pid, "nohang")
423 if not close and wpid and stat == "exited" then
427 if buffer and #buffer > 0 then
435 nixio.dup(fdo, nixio.stdout)
438 nixio.exec("/bin/sh", "-c", command)