diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-09-24 11:33:21 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-09-24 14:21:08 +0200 |
commit | 2a5c5f47f95978c6eb5c52b41b4e931e63afa4f5 (patch) | |
tree | 6ccc8475a1def3396c73d6b19df048b5b5261a0f /modules/luci-mod-system/luasrc/controller | |
parent | cc786f9833a2fd294885ba979b7e68671a5aa122 (diff) |
luci-mod-system: reimplement flashops as client side view
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-mod-system/luasrc/controller')
-rw-r--r-- | modules/luci-mod-system/luasrc/controller/admin/system.lua | 219 |
1 files changed, 1 insertions, 218 deletions
diff --git a/modules/luci-mod-system/luasrc/controller/admin/system.lua b/modules/luci-mod-system/luasrc/controller/admin/system.lua index d6e1dc7815..c7d8ac5323 100644 --- a/modules/luci-mod-system/luasrc/controller/admin/system.lua +++ b/modules/luci-mod-system/luasrc/controller/admin/system.lua @@ -29,229 +29,12 @@ function index() entry({"admin", "system", "leds"}, view("system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60) end - entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70) - entry({"admin", "system", "flashops", "reset"}, post("action_reset")) - entry({"admin", "system", "flashops", "backup"}, post("action_backup")) - entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock")) - entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles")) - - -- call() instead of post() due to upload handling! - entry({"admin", "system", "flashops", "restore"}, call("action_restore")) - entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade")) + entry({"admin", "system", "flash"}, view("system/flash"), _("Backup / Flash Firmware"), 70) entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90) entry({"admin", "system", "reboot", "call"}, post("action_reboot")) end -local function image_supported(image) - return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0) -end - -local function image_checksum(image) - return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)")) -end - -local function image_sha256_checksum(image) - return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)")) -end - -local function supports_sysupgrade() - return nixio.fs.access("/lib/upgrade/platform.sh") -end - -local function supports_reset() - return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0) -end - -local function storage_size() - local size = 0 - if nixio.fs.access("/proc/mtd") then - for l in io.lines("/proc/mtd") do - local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"') - if n == "linux" or n == "firmware" then - size = tonumber(s, 16) - break - end - end - elseif nixio.fs.access("/proc/partitions") then - for l in io.lines("/proc/partitions") do - local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)') - if b and n and not n:match('[0-9]') then - size = tonumber(b) * 1024 - break - end - end - end - return size -end - - -function action_flashops() - -- - -- Overview - -- - luci.template.render("admin_system/flashops", { - reset_avail = supports_reset(), - upgrade_avail = supports_sysupgrade() - }) -end - -function action_sysupgrade() - local fs = require "nixio.fs" - local http = require "luci.http" - local image_tmp = "/tmp/firmware.img" - - local fp - http.setfilehandler( - function(meta, chunk, eof) - if not fp and meta and meta.name == "image" then - fp = io.open(image_tmp, "w") - end - if fp and chunk then - fp:write(chunk) - end - if fp and eof then - fp:close() - end - end - ) - - if not luci.dispatcher.test_post_security() then - fs.unlink(image_tmp) - return - end - - -- - -- Cancel firmware flash - -- - if http.formvalue("cancel") then - fs.unlink(image_tmp) - http.redirect(luci.dispatcher.build_url('admin/system/flashops')) - return - end - - -- - -- Initiate firmware flash - -- - local step = tonumber(http.formvalue("step")) or 1 - if step == 1 then - local force = http.formvalue("force") - if image_supported(image_tmp) or force then - luci.template.render("admin_system/upgrade", { - checksum = image_checksum(image_tmp), - sha256ch = image_sha256_checksum(image_tmp), - storage = storage_size(), - size = (fs.stat(image_tmp, "size") or 0), - keep = (not not http.formvalue("keep")), - force = (not not http.formvalue("force")) - }) - else - fs.unlink(image_tmp) - luci.template.render("admin_system/flashops", { - reset_avail = supports_reset(), - upgrade_avail = supports_sysupgrade(), - image_invalid = true - }) - end - - -- - -- Start sysupgrade flash - -- - elseif step == 2 then - local keep = (http.formvalue("keep") == "1") and "" or "-n" - local force = (http.formvalue("force") == "1") and "-F" or "" - luci.template.render("admin_system/applyreboot", { - title = luci.i18n.translate("Flashing..."), - 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."), - addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil - }) - 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) - end -end - -function action_backup() - luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' - %{ luci.sys.hostname(), os.date("%Y-%m-%d") }) - - luci.http.prepare_content("application/x-targz") - luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write) -end - -function action_backupmtdblock() - local mv = luci.http.formvalue("mtdblockname") or "" - local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$') - - if not m and n then - luci.http.status(400, "Bad Request") - return - end - - luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"' - %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") }) - - luci.http.prepare_content("application/octet-stream") - luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write) -end - -function action_restore() - local fs = require "nixio.fs" - local http = require "luci.http" - local archive_tmp = "/tmp/restore.tar.gz" - - local fp - http.setfilehandler( - function(meta, chunk, eof) - if not fp and meta and meta.name == "archive" then - fp = io.open(archive_tmp, "w") - end - if fp and chunk then - fp:write(chunk) - end - if fp and eof then - fp:close() - end - end - ) - - if not luci.dispatcher.test_post_security() then - fs.unlink(archive_tmp) - return - end - - local upload = http.formvalue("archive") - if upload and #upload > 0 then - if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then - luci.template.render("admin_system/applyreboot") - os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp) - luci.sys.reboot() - else - luci.template.render("admin_system/flashops", { - reset_avail = supports_reset(), - upgrade_avail = supports_sysupgrade(), - backup_invalid = true - }) - end - return - end - - http.redirect(luci.dispatcher.build_url('admin/system/flashops')) -end - -function action_reset() - if supports_reset() then - luci.template.render("admin_system/applyreboot", { - title = luci.i18n.translate("Erasing..."), - msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."), - addr = "192.168.1.1" - }) - - luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true) - return - end - - http.redirect(luci.dispatcher.build_url('admin/system/flashops')) -end - function action_reboot() luci.sys.reboot() end |