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", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
27 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
28 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
31 local nodes, number = fs.glob("/sys/class/leds/*")
33 entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
36 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
37 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
38 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
39 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
40 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
42 -- call() instead of post() due to upload handling!
43 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
44 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
46 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
47 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
50 function action_clock_status()
51 local set = tonumber(luci.http.formvalue("set"))
52 if set ~= nil and set > 0 then
53 local date = os.date("*t", set)
55 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
56 date.year, date.month, date.day, date.hour, date.min, date.sec
58 luci.sys.call("/etc/init.d/sysfixtime restart")
62 luci.http.prepare_content("application/json")
63 luci.http.write_json({ timestring = os.date("%c") })
66 function action_ntp_restart()
67 if nixio.fs.access("/etc/init.d/sysntpd") then
68 os.execute("/etc/init.d/sysntpd restart")
70 luci.http.prepare_content("text/plain")
74 local function image_supported(image)
75 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
78 local function image_checksum(image)
79 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
82 local function image_sha256_checksum(image)
83 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
86 local function supports_sysupgrade()
87 return nixio.fs.access("/lib/upgrade/platform.sh")
90 local function supports_reset()
91 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
94 local function storage_size()
96 if nixio.fs.access("/proc/mtd") then
97 for l in io.lines("/proc/mtd") do
98 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
99 if n == "linux" or n == "firmware" then
100 size = tonumber(s, 16)
104 elseif nixio.fs.access("/proc/partitions") then
105 for l in io.lines("/proc/partitions") do
106 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
107 if b and n and not n:match('[0-9]') then
108 size = tonumber(b) * 1024
117 function action_flashops()
121 luci.template.render("admin_system/flashops", {
122 reset_avail = supports_reset(),
123 upgrade_avail = supports_sysupgrade()
127 function action_sysupgrade()
128 local fs = require "nixio.fs"
129 local http = require "luci.http"
130 local image_tmp = "/tmp/firmware.img"
134 function(meta, chunk, eof)
135 if not fp and meta and meta.name == "image" then
136 fp = io.open(image_tmp, "w")
147 if not luci.dispatcher.test_post_security() then
153 -- Cancel firmware flash
155 if http.formvalue("cancel") then
157 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
162 -- Initiate firmware flash
164 local step = tonumber(http.formvalue("step")) or 1
166 local force = http.formvalue("force")
167 if image_supported(image_tmp) or force then
168 luci.template.render("admin_system/upgrade", {
169 checksum = image_checksum(image_tmp),
170 sha256ch = image_sha256_checksum(image_tmp),
171 storage = storage_size(),
172 size = (fs.stat(image_tmp, "size") or 0),
173 keep = (not not http.formvalue("keep")),
174 force = (not not http.formvalue("force"))
178 luci.template.render("admin_system/flashops", {
179 reset_avail = supports_reset(),
180 upgrade_avail = supports_sysupgrade(),
186 -- Start sysupgrade flash
188 elseif step == 2 then
189 local keep = (http.formvalue("keep") == "1") and "" or "-n"
190 local force = (http.formvalue("force") == "1") and "-F" or ""
191 luci.template.render("admin_system/applyreboot", {
192 title = luci.i18n.translate("Flashing..."),
193 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."),
194 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
196 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)
200 function action_backup()
201 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
202 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
204 luci.http.prepare_content("application/x-targz")
205 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
208 function action_backupmtdblock()
209 local mv = luci.http.formvalue("mtdblockname") or ""
210 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
213 luci.http.status(400, "Bad Request")
217 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
218 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
220 luci.http.prepare_content("application/octet-stream")
221 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
224 function action_restore()
225 local fs = require "nixio.fs"
226 local http = require "luci.http"
227 local archive_tmp = "/tmp/restore.tar.gz"
231 function(meta, chunk, eof)
232 if not fp and meta and meta.name == "archive" then
233 fp = io.open(archive_tmp, "w")
244 if not luci.dispatcher.test_post_security() then
245 fs.unlink(archive_tmp)
249 local upload = http.formvalue("archive")
250 if upload and #upload > 0 then
251 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
252 luci.template.render("admin_system/applyreboot")
253 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
256 luci.template.render("admin_system/flashops", {
257 reset_avail = supports_reset(),
258 upgrade_avail = supports_sysupgrade(),
259 backup_invalid = true
265 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
268 function action_reset()
269 if supports_reset() then
270 luci.template.render("admin_system/applyreboot", {
271 title = luci.i18n.translate("Erasing..."),
272 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
276 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
280 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
283 function action_reboot()