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", "system"}, view("system/system"), _("System"), 1)
11 entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
12 entry({"admin", "system", "ntp_restart"}, call("action_ntp_restart"), nil).leaf = true
14 entry({"admin", "system", "admin"}, firstchild(), _("Administration"), 2)
15 entry({"admin", "system", "admin", "password"}, view("system/password"), _("Router Password"), 1)
17 if fs.access("/etc/config/dropbear") then
18 entry({"admin", "system", "admin", "dropbear"}, view("system/dropbear"), _("SSH Access"), 2)
19 entry({"admin", "system", "admin", "sshkeys"}, view("system/sshkeys"), _("SSH-Keys"), 3)
22 entry({"admin", "system", "startup"}, view("system/startup"), _("Startup"), 45)
23 entry({"admin", "system", "crontab"}, view("system/crontab"), _("Scheduled Tasks"), 46)
25 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
26 entry({"admin", "system", "mounts"}, view("system/mounts"), _("Mount Points"), 50)
29 local nodes, number = fs.glob("/sys/class/leds/*")
31 entry({"admin", "system", "leds"}, view("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", "backupmtdblock"}, post("action_backupmtdblock"))
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_ntp_restart()
65 if nixio.fs.access("/etc/init.d/sysntpd") then
66 os.execute("/etc/init.d/sysntpd restart")
68 luci.http.prepare_content("text/plain")
72 local function image_supported(image)
73 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
76 local function image_checksum(image)
77 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
80 local function image_sha256_checksum(image)
81 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
84 local function supports_sysupgrade()
85 return nixio.fs.access("/lib/upgrade/platform.sh")
88 local function supports_reset()
89 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
92 local function storage_size()
94 if nixio.fs.access("/proc/mtd") then
95 for l in io.lines("/proc/mtd") do
96 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
97 if n == "linux" or n == "firmware" then
98 size = tonumber(s, 16)
102 elseif nixio.fs.access("/proc/partitions") then
103 for l in io.lines("/proc/partitions") do
104 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
105 if b and n and not n:match('[0-9]') then
106 size = tonumber(b) * 1024
115 function action_flashops()
119 luci.template.render("admin_system/flashops", {
120 reset_avail = supports_reset(),
121 upgrade_avail = supports_sysupgrade()
125 function action_sysupgrade()
126 local fs = require "nixio.fs"
127 local http = require "luci.http"
128 local image_tmp = "/tmp/firmware.img"
132 function(meta, chunk, eof)
133 if not fp and meta and meta.name == "image" then
134 fp = io.open(image_tmp, "w")
145 if not luci.dispatcher.test_post_security() then
151 -- Cancel firmware flash
153 if http.formvalue("cancel") then
155 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
160 -- Initiate firmware flash
162 local step = tonumber(http.formvalue("step")) or 1
164 local force = http.formvalue("force")
165 if image_supported(image_tmp) or force then
166 luci.template.render("admin_system/upgrade", {
167 checksum = image_checksum(image_tmp),
168 sha256ch = image_sha256_checksum(image_tmp),
169 storage = storage_size(),
170 size = (fs.stat(image_tmp, "size") or 0),
171 keep = (not not http.formvalue("keep")),
172 force = (not not http.formvalue("force"))
176 luci.template.render("admin_system/flashops", {
177 reset_avail = supports_reset(),
178 upgrade_avail = supports_sysupgrade(),
184 -- Start sysupgrade flash
186 elseif step == 2 then
187 local keep = (http.formvalue("keep") == "1") and "" or "-n"
188 local force = (http.formvalue("force") == "1") and "-F" or ""
189 luci.template.render("admin_system/applyreboot", {
190 title = luci.i18n.translate("Flashing..."),
191 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."),
192 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
194 luci.sys.process.exec({ "/bin/sh", "-c","sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %s %q" %{ keep, force, image_tmp } }, nil, nil, true)
198 function action_backup()
199 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
200 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
202 luci.http.prepare_content("application/x-targz")
203 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
206 function action_backupmtdblock()
207 local mv = luci.http.formvalue("mtdblockname") or ""
208 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
211 luci.http.status(400, "Bad Request")
215 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
216 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
218 luci.http.prepare_content("application/octet-stream")
219 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
222 function action_restore()
223 local fs = require "nixio.fs"
224 local http = require "luci.http"
225 local archive_tmp = "/tmp/restore.tar.gz"
229 function(meta, chunk, eof)
230 if not fp and meta and meta.name == "archive" then
231 fp = io.open(archive_tmp, "w")
242 if not luci.dispatcher.test_post_security() then
243 fs.unlink(archive_tmp)
247 local upload = http.formvalue("archive")
248 if upload and #upload > 0 then
249 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
250 luci.template.render("admin_system/applyreboot")
251 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
254 luci.template.render("admin_system/flashops", {
255 reset_avail = supports_reset(),
256 upgrade_avail = supports_sysupgrade(),
257 backup_invalid = true
263 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
266 function action_reset()
267 if supports_reset() then
268 luci.template.render("admin_system/applyreboot", {
269 title = luci.i18n.translate("Erasing..."),
270 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
274 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
278 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
281 function action_reboot()