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)
12 entry({"admin", "system", "admin"}, firstchild(), _("Administration"), 2)
13 entry({"admin", "system", "admin", "password"}, view("system/password"), _("Router Password"), 1)
15 if fs.access("/etc/config/dropbear") then
16 entry({"admin", "system", "admin", "dropbear"}, view("system/dropbear"), _("SSH Access"), 2)
17 entry({"admin", "system", "admin", "sshkeys"}, view("system/sshkeys"), _("SSH-Keys"), 3)
20 entry({"admin", "system", "startup"}, view("system/startup"), _("Startup"), 45)
21 entry({"admin", "system", "crontab"}, view("system/crontab"), _("Scheduled Tasks"), 46)
23 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
24 entry({"admin", "system", "mounts"}, view("system/mounts"), _("Mount Points"), 50)
27 local nodes, number = fs.glob("/sys/class/leds/*")
29 entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
32 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
33 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
34 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
35 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
36 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
38 -- call() instead of post() due to upload handling!
39 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
40 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
42 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
43 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
46 local function image_supported(image)
47 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
50 local function image_checksum(image)
51 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
54 local function image_sha256_checksum(image)
55 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
58 local function supports_sysupgrade()
59 return nixio.fs.access("/lib/upgrade/platform.sh")
62 local function supports_reset()
63 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
66 local function storage_size()
68 if nixio.fs.access("/proc/mtd") then
69 for l in io.lines("/proc/mtd") do
70 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
71 if n == "linux" or n == "firmware" then
72 size = tonumber(s, 16)
76 elseif nixio.fs.access("/proc/partitions") then
77 for l in io.lines("/proc/partitions") do
78 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
79 if b and n and not n:match('[0-9]') then
80 size = tonumber(b) * 1024
89 function action_flashops()
93 luci.template.render("admin_system/flashops", {
94 reset_avail = supports_reset(),
95 upgrade_avail = supports_sysupgrade()
99 function action_sysupgrade()
100 local fs = require "nixio.fs"
101 local http = require "luci.http"
102 local image_tmp = "/tmp/firmware.img"
106 function(meta, chunk, eof)
107 if not fp and meta and meta.name == "image" then
108 fp = io.open(image_tmp, "w")
119 if not luci.dispatcher.test_post_security() then
125 -- Cancel firmware flash
127 if http.formvalue("cancel") then
129 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
134 -- Initiate firmware flash
136 local step = tonumber(http.formvalue("step")) or 1
138 local force = http.formvalue("force")
139 if image_supported(image_tmp) or force then
140 luci.template.render("admin_system/upgrade", {
141 checksum = image_checksum(image_tmp),
142 sha256ch = image_sha256_checksum(image_tmp),
143 storage = storage_size(),
144 size = (fs.stat(image_tmp, "size") or 0),
145 keep = (not not http.formvalue("keep")),
146 force = (not not http.formvalue("force"))
150 luci.template.render("admin_system/flashops", {
151 reset_avail = supports_reset(),
152 upgrade_avail = supports_sysupgrade(),
158 -- Start sysupgrade flash
160 elseif step == 2 then
161 local keep = (http.formvalue("keep") == "1") and "" or "-n"
162 local force = (http.formvalue("force") == "1") and "-F" or ""
163 luci.template.render("admin_system/applyreboot", {
164 title = luci.i18n.translate("Flashing..."),
165 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."),
166 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
168 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)
172 function action_backup()
173 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
174 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
176 luci.http.prepare_content("application/x-targz")
177 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
180 function action_backupmtdblock()
181 local mv = luci.http.formvalue("mtdblockname") or ""
182 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
185 luci.http.status(400, "Bad Request")
189 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
190 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
192 luci.http.prepare_content("application/octet-stream")
193 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
196 function action_restore()
197 local fs = require "nixio.fs"
198 local http = require "luci.http"
199 local archive_tmp = "/tmp/restore.tar.gz"
203 function(meta, chunk, eof)
204 if not fp and meta and meta.name == "archive" then
205 fp = io.open(archive_tmp, "w")
216 if not luci.dispatcher.test_post_security() then
217 fs.unlink(archive_tmp)
221 local upload = http.formvalue("archive")
222 if upload and #upload > 0 then
223 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
224 luci.template.render("admin_system/applyreboot")
225 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
228 luci.template.render("admin_system/flashops", {
229 reset_avail = supports_reset(),
230 upgrade_avail = supports_sysupgrade(),
231 backup_invalid = true
237 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
240 function action_reset()
241 if supports_reset() then
242 luci.template.render("admin_system/applyreboot", {
243 title = luci.i18n.translate("Erasing..."),
244 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
248 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
252 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
255 function action_reboot()