2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.controller.mini.system", package.seeall)
19 entry({"mini", "system"}, alias("mini", "system", "index"), _("System"), 40).index = true
20 entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), _("General"), 1)
21 entry({"mini", "system", "passwd"}, form("mini/passwd"), _("Admin Password"), 10)
22 entry({"mini", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 80)
23 entry({"mini", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 90)
24 entry({"mini", "system", "reboot"}, call("action_reboot"), _("Reboot"), 100)
27 function action_backup()
28 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
29 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
30 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
33 luci.http.setfilehandler(
34 function(meta, chunk, eof)
35 if not restore_fpi then
36 restore_fpi = io.popen(restore_cmd, "w")
39 restore_fpi:write(chunk)
47 local upload = luci.http.formvalue("archive")
48 local backup = luci.http.formvalue("backup")
49 local reset = reset_avail and luci.http.formvalue("reset")
51 if upload and #upload > 0 then
52 luci.template.render("mini/applyreboot")
55 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
56 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
57 luci.sys.hostname(), os.date("%Y-%m-%d")})
58 luci.http.prepare_content("application/x-targz")
59 luci.ltn12.pump.all(reader, luci.http.write)
61 luci.template.render("mini/applyreboot")
62 luci.util.exec("mtd -r erase rootfs_data")
64 luci.template.render("mini/backup", {reset_avail = reset_avail})
68 function action_reboot()
69 local reboot = luci.http.formvalue("reboot")
70 luci.template.render("mini/reboot", {reboot=reboot})
76 function action_upgrade()
77 require("luci.model.uci")
79 local tmpfile = "/tmp/firmware.img"
81 local function image_supported()
83 return ( 0 == os.execute(
84 ". /etc/functions.sh; " ..
85 "include /lib/upgrade; " ..
86 "platform_check_image %q >/dev/null"
91 local function image_checksum()
92 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
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]+)"')
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 -- Install upload handler
120 luci.http.setfilehandler(
121 function(meta, chunk, eof)
122 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
123 file = io.open(tmpfile, "w")
125 if file and chunk then
136 local keep_avail = true
137 local step = tonumber(luci.http.formvalue("step") or 1)
138 local has_image = nixio.fs.access(tmpfile)
139 local has_support = image_supported()
140 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
141 local has_upload = luci.http.formvalue("image")
143 -- This does the actual flashing which is invoked inside an iframe
144 -- so don't produce meaningful errors here because the the
145 -- previous pages should arrange the stuff as required.
147 if has_platform and has_image and has_support then
148 -- Mimetype text/plain
149 luci.http.prepare_content("text/plain")
150 luci.http.write("Starting luci-flash...\n")
152 -- Now invoke sysupgrade
153 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
154 local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
155 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
158 luci.ltn12.pump.all(flash, luci.http.write)
160 -- Make sure the device is rebooted
166 -- This is step 1-3, which does the user interaction and
170 -- Step 1: file upload, error on unsupported image format
171 elseif not has_image or not has_support or step == 1 then
172 -- If there is an image but user has requested step 1
173 -- or type is not supported, then remove it.
175 nixio.fs.unlink(tmpfile)
178 luci.template.render("mini/upgrade", {
180 bad_image=(has_image and not has_support or false),
181 keepavail=keep_avail,
182 supported=has_platform
185 -- Step 2: present uploaded file, show checksum, confirmation
186 elseif step == 2 then
187 luci.template.render("mini/upgrade", {
189 checksum=image_checksum(),
190 filesize=nixio.fs.stat(tmpfile).size,
191 flashsize=storage_size(),
192 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
195 -- Step 3: load iframe which calls the actual flash procedure
196 elseif step == 3 then
197 luci.template.render("mini/upgrade", {
199 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
204 function _keep_pattern()
206 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
209 for k, v in pairs(files) do
210 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
211 kpattern = kpattern .. " " .. v
218 function ltn12_popen(command)
220 local fdi, fdo = nixio.pipe()
221 local pid = nixio.fork()
227 local buffer = fdi:read(2048)
228 local wpid, stat = nixio.waitpid(pid, "nohang")
229 if not close and wpid and stat == "exited" then
233 if buffer and #buffer > 0 then
241 nixio.dup(fdo, nixio.stdout)
244 nixio.exec("/bin/sh", "-c", command)