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"}, template("admin_system/password"), _("Router Password"), 1)
16 entry({"admin", "system", "admin", "password", "json"}, post("action_password"))
18 if fs.access("/etc/config/dropbear") then
19 entry({"admin", "system", "admin", "dropbear"}, cbi("admin_system/dropbear"), _("SSH Access"), 2)
20 entry({"admin", "system", "admin", "sshkeys"}, view("system/sshkeys"), _("SSH-Keys"), 3)
23 entry({"admin", "system", "startup"}, view("system/startup"), _("Startup"), 45)
24 entry({"admin", "system", "crontab"}, view("system/crontab"), _("Scheduled Tasks"), 46)
26 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
27 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
28 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
29 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
32 local nodes, number = fs.glob("/sys/class/leds/*")
34 entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
37 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
38 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
39 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
40 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
41 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
43 -- call() instead of post() due to upload handling!
44 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
45 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
47 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
48 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
51 function action_clock_status()
52 local set = tonumber(luci.http.formvalue("set"))
53 if set ~= nil and set > 0 then
54 local date = os.date("*t", set)
56 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
57 date.year, date.month, date.day, date.hour, date.min, date.sec
59 luci.sys.call("/etc/init.d/sysfixtime restart")
63 luci.http.prepare_content("application/json")
64 luci.http.write_json({ timestring = os.date("%c") })
67 function action_ntp_restart()
68 if nixio.fs.access("/etc/init.d/sysntpd") then
69 os.execute("/etc/init.d/sysntpd restart")
71 luci.http.prepare_content("text/plain")
75 local function image_supported(image)
76 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
79 local function image_checksum(image)
80 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
83 local function image_sha256_checksum(image)
84 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
87 local function supports_sysupgrade()
88 return nixio.fs.access("/lib/upgrade/platform.sh")
91 local function supports_reset()
92 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
95 local function storage_size()
97 if nixio.fs.access("/proc/mtd") then
98 for l in io.lines("/proc/mtd") do
99 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
100 if n == "linux" or n == "firmware" then
101 size = tonumber(s, 16)
105 elseif nixio.fs.access("/proc/partitions") then
106 for l in io.lines("/proc/partitions") do
107 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
108 if b and n and not n:match('[0-9]') then
109 size = tonumber(b) * 1024
118 function action_flashops()
122 luci.template.render("admin_system/flashops", {
123 reset_avail = supports_reset(),
124 upgrade_avail = supports_sysupgrade()
128 function action_sysupgrade()
129 local fs = require "nixio.fs"
130 local http = require "luci.http"
131 local image_tmp = "/tmp/firmware.img"
135 function(meta, chunk, eof)
136 if not fp and meta and meta.name == "image" then
137 fp = io.open(image_tmp, "w")
148 if not luci.dispatcher.test_post_security() then
154 -- Cancel firmware flash
156 if http.formvalue("cancel") then
158 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
163 -- Initiate firmware flash
165 local step = tonumber(http.formvalue("step")) or 1
167 local force = http.formvalue("force")
168 if image_supported(image_tmp) or force then
169 luci.template.render("admin_system/upgrade", {
170 checksum = image_checksum(image_tmp),
171 sha256ch = image_sha256_checksum(image_tmp),
172 storage = storage_size(),
173 size = (fs.stat(image_tmp, "size") or 0),
174 keep = (not not http.formvalue("keep")),
175 force = (not not http.formvalue("force"))
179 luci.template.render("admin_system/flashops", {
180 reset_avail = supports_reset(),
181 upgrade_avail = supports_sysupgrade(),
187 -- Start sysupgrade flash
189 elseif step == 2 then
190 local keep = (http.formvalue("keep") == "1") and "" or "-n"
191 local force = (http.formvalue("force") == "1") and "-F" or ""
192 luci.template.render("admin_system/applyreboot", {
193 title = luci.i18n.translate("Flashing..."),
194 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."),
195 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
197 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)
201 function action_backup()
202 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
203 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
205 luci.http.prepare_content("application/x-targz")
206 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
209 function action_backupmtdblock()
210 local mv = luci.http.formvalue("mtdblockname") or ""
211 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
214 luci.http.status(400, "Bad Request")
218 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
219 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
221 luci.http.prepare_content("application/octet-stream")
222 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
225 function action_restore()
226 local fs = require "nixio.fs"
227 local http = require "luci.http"
228 local archive_tmp = "/tmp/restore.tar.gz"
232 function(meta, chunk, eof)
233 if not fp and meta and meta.name == "archive" then
234 fp = io.open(archive_tmp, "w")
245 if not luci.dispatcher.test_post_security() then
246 fs.unlink(archive_tmp)
250 local upload = http.formvalue("archive")
251 if upload and #upload > 0 then
252 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
253 luci.template.render("admin_system/applyreboot")
254 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
257 luci.template.render("admin_system/flashops", {
258 reset_avail = supports_reset(),
259 upgrade_avail = supports_sysupgrade(),
260 backup_invalid = true
266 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
269 function action_reset()
270 if supports_reset() then
271 luci.template.render("admin_system/applyreboot", {
272 title = luci.i18n.translate("Erasing..."),
273 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
277 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
281 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
284 function action_password()
285 local password = luci.http.formvalue("password")
287 luci.http.status(400, "Bad Request")
291 luci.http.prepare_content("application/json")
292 luci.http.write_json({ code = luci.sys.user.setpasswd("root", password) })
295 function action_reboot()