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"}, cbi("admin_system/system"), _("System"), 1)
11 entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
13 entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
14 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
15 entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
17 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
18 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
19 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
20 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
23 local nodes, number = fs.glob("/sys/class/leds/*")
25 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
28 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
29 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
30 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
31 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
32 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
34 -- call() instead of post() due to upload handling!
35 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
36 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
38 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
39 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
42 function action_clock_status()
43 local set = tonumber(luci.http.formvalue("set"))
44 if set ~= nil and set > 0 then
45 local date = os.date("*t", set)
47 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
48 date.year, date.month, date.day, date.hour, date.min, date.sec
50 luci.sys.call("/etc/init.d/sysfixtime restart")
54 luci.http.prepare_content("application/json")
55 luci.http.write_json({ timestring = os.date("%c") })
58 local function image_supported(image)
59 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
62 local function image_checksum(image)
63 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
66 local function image_sha256_checksum(image)
67 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
70 local function supports_sysupgrade()
71 return nixio.fs.access("/lib/upgrade/platform.sh")
74 local function supports_reset()
75 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
78 local function storage_size()
80 if nixio.fs.access("/proc/mtd") then
81 for l in io.lines("/proc/mtd") do
82 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
83 if n == "linux" or n == "firmware" then
84 size = tonumber(s, 16)
88 elseif nixio.fs.access("/proc/partitions") then
89 for l in io.lines("/proc/partitions") do
90 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
91 if b and n and not n:match('[0-9]') then
92 size = tonumber(b) * 1024
101 function action_flashops()
105 luci.template.render("admin_system/flashops", {
106 reset_avail = supports_reset(),
107 upgrade_avail = supports_sysupgrade()
111 function action_sysupgrade()
112 local fs = require "nixio.fs"
113 local http = require "luci.http"
114 local image_tmp = "/tmp/firmware.img"
118 function(meta, chunk, eof)
119 if not fp and meta and meta.name == "image" then
120 fp = io.open(image_tmp, "w")
131 if not luci.dispatcher.test_post_security() then
137 -- Cancel firmware flash
139 if http.formvalue("cancel") then
141 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
146 -- Initiate firmware flash
148 local step = tonumber(http.formvalue("step")) or 1
150 local force = http.formvalue("force")
151 if image_supported(image_tmp) or force then
152 luci.template.render("admin_system/upgrade", {
153 checksum = image_checksum(image_tmp),
154 sha256ch = image_sha256_checksum(image_tmp),
155 storage = storage_size(),
156 size = (fs.stat(image_tmp, "size") or 0),
157 keep = (not not http.formvalue("keep")),
158 force = (not not http.formvalue("force"))
162 luci.template.render("admin_system/flashops", {
163 reset_avail = supports_reset(),
164 upgrade_avail = supports_sysupgrade(),
170 -- Start sysupgrade flash
172 elseif step == 2 then
173 local keep = (http.formvalue("keep") == "1") and "" or "-n"
174 local force = (http.formvalue("force") == "1") and "-F" or ""
175 luci.template.render("admin_system/applyreboot", {
176 title = luci.i18n.translate("Flashing..."),
177 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."),
178 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
180 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)
184 function action_backup()
185 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
186 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
188 luci.http.prepare_content("application/x-targz")
189 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
192 function action_backupmtdblock()
193 local mv = luci.http.formvalue("mtdblockname") or ""
194 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
197 luci.http.status(400, "Bad Request")
201 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
202 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
204 luci.http.prepare_content("application/octet-stream")
205 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
208 function action_restore()
209 local fs = require "nixio.fs"
210 local http = require "luci.http"
211 local archive_tmp = "/tmp/restore.tar.gz"
215 function(meta, chunk, eof)
216 if not fp and meta and meta.name == "archive" then
217 fp = io.open(archive_tmp, "w")
228 if not luci.dispatcher.test_post_security() then
229 fs.unlink(archive_tmp)
233 local upload = http.formvalue("archive")
234 if upload and #upload > 0 then
235 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
236 luci.template.render("admin_system/applyreboot")
237 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
240 luci.template.render("admin_system/flashops", {
241 reset_avail = supports_reset(),
242 upgrade_avail = supports_sysupgrade(),
243 backup_invalid = true
249 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
252 function action_reset()
253 if supports_reset() then
254 luci.template.render("admin_system/applyreboot", {
255 title = luci.i18n.translate("Erasing..."),
256 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
260 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
264 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
267 function action_passwd()
268 local p1 = luci.http.formvalue("pwd1")
269 local p2 = luci.http.formvalue("pwd2")
274 stat = luci.sys.user.setpasswd("root", p1)
280 luci.template.render("admin_system/passwd", {stat=stat})
283 function action_reboot()