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("admin-core")
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("a_s_changepw"), 10)
25 entry({"mini", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 80)
26 entry({"mini", "system", "upgrade"}, call("action_upgrade"), i18n("admin_upgrade"), 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 luci.util.perror(backup_cmd:format(_keep_pattern()))
59 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
60 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
61 luci.sys.hostname(), os.date("%Y-%m-%d")})
62 luci.http.prepare_content("application/x-targz")
63 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
65 luci.template.render("mini/applyreboot")
66 luci.util.exec("mtd -r erase rootfs_data")
68 luci.template.render("mini/backup", {reset_avail = reset_avail})
72 function action_reboot()
73 local reboot = luci.http.formvalue("reboot")
74 luci.template.render("mini/reboot", {reboot=reboot})
80 function action_upgrade()
81 require("luci.model.uci")
83 local tmpfile = "/tmp/firmware.img"
85 local function image_supported()
87 return ( 0 == os.execute(
88 ". /etc/functions.sh; " ..
89 "include /lib/upgrade; " ..
90 "platform_check_image %q >/dev/null"
95 local function image_checksum()
96 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
99 local function storage_size()
101 if luci.fs.access("/proc/mtd") then
102 for l in io.lines("/proc/mtd") do
103 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
105 size = tonumber(s, 16)
109 elseif luci.fs.access("/proc/partitions") then
110 for l in io.lines("/proc/partitions") do
111 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
112 if b and n and not n:match('[0-9]') then
113 size = tonumber(b) * 1024
122 -- Install upload handler
124 luci.http.setfilehandler(
125 function(meta, chunk, eof)
126 if not luci.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
127 file = io.open(tmpfile, "w")
129 if file and chunk then
140 local keep_avail = true
141 local step = tonumber(luci.http.formvalue("step") or 1)
142 local has_image = luci.fs.access(tmpfile)
143 local has_support = image_supported()
144 local has_platform = luci.fs.access("/lib/upgrade/platform.sh")
145 local has_upload = luci.http.formvalue("image")
147 -- This does the actual flashing which is invoked inside an iframe
148 -- so don't produce meaningful errors here because the the
149 -- previous pages should arrange the stuff as required.
151 if has_platform and has_image and has_support then
152 -- Mimetype text/plain
153 luci.http.prepare_content("text/plain")
155 -- Now invoke sysupgrade
156 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
157 local fd = io.popen("/sbin/luci-flash %s %q" %{
158 keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
163 local ln = fd:read("*l")
164 if not ln then break end
165 luci.http.write(ln .. "\n")
170 -- Make sure the device is rebooted
176 -- This is step 1-3, which does the user interaction and
180 -- Step 1: file upload, error on unsupported image format
181 elseif not has_image or not has_support or step == 1 then
182 -- If there is an image but user has requested step 1
183 -- or type is not supported, then remove it.
185 luci.fs.unlink(tmpfile)
188 luci.template.render("admin_system/upgrade", {
190 bad_image=(has_image and not has_support or false),
191 keepavail=keep_avail,
192 supported=has_platform
195 -- Step 2: present uploaded file, show checksum, confirmation
196 elseif step == 2 then
197 luci.template.render("admin_system/upgrade", {
199 checksum=image_checksum(),
200 filesize=luci.fs.stat(tmpfile).size,
201 flashsize=storage_size(),
202 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
205 -- Step 3: load iframe which calls the actual flash procedure
206 elseif step == 3 then
207 luci.template.render("admin_system/upgrade", {
209 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
214 function _keep_pattern()
216 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
219 for k, v in pairs(files) do
220 if k:sub(1,1) ~= "." and luci.fs.glob(v) then
221 kpattern = kpattern .. " " .. v