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 if fs.access("/sys/class/leds") then
31 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
34 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
35 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
36 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
37 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
39 -- call() instead of post() due to upload handling!
40 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
41 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
43 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
44 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
47 function action_clock_status()
48 local set = tonumber(luci.http.formvalue("set"))
49 if set ~= nil and set > 0 then
50 local date = os.date("*t", set)
52 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
53 date.year, date.month, date.day, date.hour, date.min, date.sec
55 luci.sys.call("/etc/init.d/sysfixtime restart")
59 luci.http.prepare_content("application/json")
60 luci.http.write_json({ timestring = os.date("%c") })
63 function action_packages()
64 local fs = require "nixio.fs"
65 local ipkg = require "luci.model.ipkg"
66 local submit = (luci.http.formvalue("exec") == "1")
76 local display = luci.http.formvalue("display") or "installed"
79 local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
80 letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
83 local query = luci.http.formvalue("query")
84 query = (query ~= '') and query or nil
89 -- Packets to be installed
90 local ninst = luci.http.formvalue("install")
94 local url = luci.http.formvalue("url")
95 if url and url ~= '' then
101 install[ninst], out, err = ipkg.install(ninst)
102 stdout[#stdout+1] = out
103 stderr[#stderr+1] = err
109 for pkg in luci.util.imatch(uinst) do
110 install[uinst], out, err = ipkg.install(pkg)
111 stdout[#stdout+1] = out
112 stderr[#stderr+1] = err
118 local rem = luci.http.formvalue("remove")
120 remove[rem], out, err = ipkg.remove(rem)
121 stdout[#stdout+1] = out
122 stderr[#stderr+1] = err
127 -- Update all packets
128 update = luci.http.formvalue("update")
130 update, out, err = ipkg.update()
131 stdout[#stdout+1] = out
132 stderr[#stderr+1] = err
136 -- Upgrade all packets
137 upgrade = luci.http.formvalue("upgrade")
139 upgrade, out, err = ipkg.upgrade()
140 stdout[#stdout+1] = out
141 stderr[#stderr+1] = err
147 local no_lists = true
148 local old_lists = false
149 if fs.access("/var/opkg-lists/") then
151 for list in fs.dir("/var/opkg-lists/") do
153 if (fs.stat("/var/opkg-lists/"..list, "mtime") or 0) < (os.time() - (24 * 60 * 60)) then
161 luci.template.render("admin_system/packages", {
170 old_lists = old_lists,
171 stdout = table.concat(stdout, ""),
172 stderr = table.concat(stderr, "")
175 -- Remove index cache
177 fs.unlink("/tmp/luci-indexcache")
181 local function image_supported(image)
182 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
185 local function image_checksum(image)
186 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
189 local function image_sha256_checksum(image)
190 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
193 local function supports_sysupgrade()
194 return nixio.fs.access("/lib/upgrade/platform.sh")
197 local function supports_reset()
198 return (os.execute([[grep -sqE '"rootfs_data"|"ubi"' /proc/mtd]]) == 0)
201 local function storage_size()
203 if nixio.fs.access("/proc/mtd") then
204 for l in io.lines("/proc/mtd") do
205 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
206 if n == "linux" or n == "firmware" then
207 size = tonumber(s, 16)
211 elseif nixio.fs.access("/proc/partitions") then
212 for l in io.lines("/proc/partitions") do
213 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
214 if b and n and not n:match('[0-9]') then
215 size = tonumber(b) * 1024
224 function action_flashops()
228 luci.template.render("admin_system/flashops", {
229 reset_avail = supports_reset(),
230 upgrade_avail = supports_sysupgrade()
234 function action_sysupgrade()
235 local fs = require "nixio.fs"
236 local http = require "luci.http"
237 local image_tmp = "/tmp/firmware.img"
241 function(meta, chunk, eof)
242 if not fp and meta and meta.name == "image" then
243 fp = io.open(image_tmp, "w")
254 if not luci.dispatcher.test_post_security() then
260 -- Cancel firmware flash
262 if http.formvalue("cancel") then
264 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
269 -- Initiate firmware flash
271 local step = tonumber(http.formvalue("step") or 1)
273 if image_supported(image_tmp) then
274 luci.template.render("admin_system/upgrade", {
275 checksum = image_checksum(image_tmp),
276 sha256ch = image_sha256_checksum(image_tmp),
277 storage = storage_size(),
278 size = (fs.stat(image_tmp, "size") or 0),
279 keep = (not not http.formvalue("keep"))
283 luci.template.render("admin_system/flashops", {
284 reset_avail = supports_reset(),
285 upgrade_avail = supports_sysupgrade(),
290 -- Start sysupgrade flash
292 elseif step == 2 then
293 local keep = (http.formvalue("keep") == "1") and "" or "-n"
294 luci.template.render("admin_system/applyreboot", {
295 title = luci.i18n.translate("Flashing..."),
296 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."),
297 addr = (#keep > 0) and "192.168.1.1" or nil
299 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q" %{ keep, image_tmp })
303 function action_backup()
304 local reader = ltn12_popen("sysupgrade --create-backup - 2>/dev/null")
307 'Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' %{
312 luci.http.prepare_content("application/x-targz")
313 luci.ltn12.pump.all(reader, luci.http.write)
316 function action_restore()
317 local fs = require "nixio.fs"
318 local http = require "luci.http"
319 local archive_tmp = "/tmp/restore.tar.gz"
323 function(meta, chunk, eof)
324 if not fp and meta and meta.name == "archive" then
325 fp = io.open(archive_tmp, "w")
336 if not luci.dispatcher.test_post_security() then
337 fs.unlink(archive_tmp)
341 local upload = http.formvalue("archive")
342 if upload and #upload > 0 then
343 luci.template.render("admin_system/applyreboot")
344 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
349 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
352 function action_reset()
353 if supports_reset() then
354 luci.template.render("admin_system/applyreboot", {
355 title = luci.i18n.translate("Erasing..."),
356 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
360 fork_exec("sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot")
364 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
367 function action_passwd()
368 local p1 = luci.http.formvalue("pwd1")
369 local p2 = luci.http.formvalue("pwd2")
374 stat = luci.sys.user.setpasswd("root", p1)
380 luci.template.render("admin_system/passwd", {stat=stat})
383 function action_reboot()
387 function fork_exec(command)
388 local pid = nixio.fork()
392 -- change to root dir
395 -- patch stdin, out, err to /dev/null
396 local null = nixio.open("/dev/null", "w+")
398 nixio.dup(null, nixio.stderr)
399 nixio.dup(null, nixio.stdout)
400 nixio.dup(null, nixio.stdin)
401 if null:fileno() > 2 then
406 -- replace with target command
407 nixio.exec("/bin/sh", "-c", command)
411 function ltn12_popen(command)
413 local fdi, fdo = nixio.pipe()
414 local pid = nixio.fork()
420 local buffer = fdi:read(2048)
421 local wpid, stat = nixio.waitpid(pid, "nohang")
422 if not close and wpid and stat == "exited" then
426 if buffer and #buffer > 0 then
434 nixio.dup(fdo, nixio.stdout)
437 nixio.exec("/bin/sh", "-c", command)