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 luci.i18n.loadc("base")
20 local i18n = luci.i18n.translate
22 entry({"mini", "system"}, alias("mini", "system", "index"), i18n("System"), 40).index = true
23 entry({"mini", "system", "index"}, cbi("mini/system", {autoapply=true}), i18n("General"), 1)
24 entry({"mini", "system", "passwd"}, form("mini/passwd"), i18n("Admin Password"), 10)
25 entry({"mini", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 80)
26 entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 90)
27 entry({"mini", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 100)
30 function action_backup()
31 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
32 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
33 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
36 luci.http.setfilehandler(
37 function(meta, chunk, eof)
38 if not restore_fpi then
39 restore_fpi = io.popen(restore_cmd, "w")
42 restore_fpi:write(chunk)
50 local upload = luci.http.formvalue("archive")
51 local backup = luci.http.formvalue("backup")
52 local reset = reset_avail and luci.http.formvalue("reset")
54 if upload and #upload > 0 then
55 luci.template.render("mini/applyreboot")
58 local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
59 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
60 luci.sys.hostname(), os.date("%Y-%m-%d")})
61 luci.http.prepare_content("application/x-targz")
62 luci.ltn12.pump.all(reader, luci.http.write)
64 luci.template.render("mini/applyreboot")
65 luci.util.exec("mtd -r erase rootfs_data")
67 luci.template.render("mini/backup", {reset_avail = reset_avail})
71 function action_reboot()
72 local reboot = luci.http.formvalue("reboot")
73 luci.template.render("mini/reboot", {reboot=reboot})
79 function action_upgrade()
80 require("luci.model.uci")
82 local tmpfile = "/tmp/firmware.img"
84 local function image_supported()
86 return ( 0 == os.execute(
87 ". /etc/functions.sh; " ..
88 "include /lib/upgrade; " ..
89 "platform_check_image %q >/dev/null"
94 local function image_checksum()
95 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
98 local function storage_size()
100 if nixio.fs.access("/proc/mtd") then
101 for l in io.lines("/proc/mtd") do
102 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
104 size = tonumber(s, 16)
108 elseif nixio.fs.access("/proc/partitions") then
109 for l in io.lines("/proc/partitions") do
110 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
111 if b and n and not n:match('[0-9]') then
112 size = tonumber(b) * 1024
121 -- Install upload handler
123 luci.http.setfilehandler(
124 function(meta, chunk, eof)
125 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
126 file = io.open(tmpfile, "w")
128 if file and chunk then
139 local keep_avail = true
140 local step = tonumber(luci.http.formvalue("step") or 1)
141 local has_image = nixio.fs.access(tmpfile)
142 local has_support = image_supported()
143 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
144 local has_upload = luci.http.formvalue("image")
146 -- This does the actual flashing which is invoked inside an iframe
147 -- so don't produce meaningful errors here because the the
148 -- previous pages should arrange the stuff as required.
150 if has_platform and has_image and has_support then
151 -- Mimetype text/plain
152 luci.http.prepare_content("text/plain")
153 luci.http.write("Starting luci-flash...\n")
155 -- Now invoke sysupgrade
156 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
157 local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
158 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
161 luci.ltn12.pump.all(flash, luci.http.write)
163 -- Make sure the device is rebooted
169 -- This is step 1-3, which does the user interaction and
173 -- Step 1: file upload, error on unsupported image format
174 elseif not has_image or not has_support or step == 1 then
175 -- If there is an image but user has requested step 1
176 -- or type is not supported, then remove it.
178 nixio.fs.unlink(tmpfile)
181 luci.template.render("admin_system/upgrade", {
183 bad_image=(has_image and not has_support or false),
184 keepavail=keep_avail,
185 supported=has_platform
188 -- Step 2: present uploaded file, show checksum, confirmation
189 elseif step == 2 then
190 luci.template.render("admin_system/upgrade", {
192 checksum=image_checksum(),
193 filesize=nixio.fs.stat(tmpfile).size,
194 flashsize=storage_size(),
195 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
198 -- Step 3: load iframe which calls the actual flash procedure
199 elseif step == 3 then
200 luci.template.render("admin_system/upgrade", {
202 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
207 function _keep_pattern()
209 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
212 for k, v in pairs(files) do
213 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
214 kpattern = kpattern .. " " .. v
221 function ltn12_popen(command)
223 local fdi, fdo = nixio.pipe()
224 local pid = nixio.fork()
230 local buffer = fdi:read(2048)
231 local wpid, stat = nixio.waitpid(pid, "nohang")
232 if not close and wpid and stat == "exited" then
236 if buffer and #buffer > 0 then
244 nixio.dup(fdo, nixio.stdout)
247 nixio.exec("/bin/sh", "-c", command)