519c15bc63e6c7db922ca2e862134b85d659ff9e
[project/luci.git] / modules / admin-mini / luasrc / controller / mini / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
6
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
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 $Id$
14 ]]--
15
16 module("luci.controller.mini.system", package.seeall)
17
18 function index()
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)
25 end
26
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"
31
32 local restore_fpi
33 luci.http.setfilehandler(
34 function(meta, chunk, eof)
35 if not restore_fpi then
36 restore_fpi = io.popen(restore_cmd, "w")
37 end
38 if chunk then
39 restore_fpi:write(chunk)
40 end
41 if eof then
42 restore_fpi:close()
43 end
44 end
45 )
46
47 local upload = luci.http.formvalue("archive")
48 local backup = luci.http.formvalue("backup")
49 local reset = reset_avail and luci.http.formvalue("reset")
50
51 if upload and #upload > 0 then
52 luci.template.render("mini/applyreboot")
53 luci.sys.reboot()
54 elseif backup then
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)
60 elseif reset then
61 luci.template.render("mini/applyreboot")
62 luci.util.exec("mtd -r erase rootfs_data")
63 else
64 luci.template.render("mini/backup", {reset_avail = reset_avail})
65 end
66 end
67
68 function action_reboot()
69 local reboot = luci.http.formvalue("reboot")
70 luci.template.render("mini/reboot", {reboot=reboot})
71 if reboot then
72 luci.sys.reboot()
73 end
74 end
75
76 function action_upgrade()
77 require("luci.model.uci")
78
79 local tmpfile = "/tmp/firmware.img"
80
81 local function image_supported()
82 -- XXX: yay...
83 return ( 0 == os.execute(
84 ". /etc/functions.sh; " ..
85 "include /lib/upgrade; " ..
86 "platform_check_image %q >/dev/null"
87 % tmpfile
88 ) )
89 end
90
91 local function image_checksum()
92 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
93 end
94
95 local function storage_size()
96 local size = 0
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]+)"')
100 if n == "linux" then
101 size = tonumber(s, 16)
102 break
103 end
104 end
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
110 break
111 end
112 end
113 end
114 return size
115 end
116
117
118 -- Install upload handler
119 local file
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")
124 end
125 if file and chunk then
126 file:write(chunk)
127 end
128 if file and eof then
129 file:close()
130 end
131 end
132 )
133
134
135 -- Determine state
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")
142
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.
146 if step == 4 then
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")
151
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
156 })
157
158 luci.ltn12.pump.all(flash, luci.http.write)
159
160 -- Make sure the device is rebooted
161 luci.sys.reboot()
162 end
163
164
165 --
166 -- This is step 1-3, which does the user interaction and
167 -- image upload.
168 --
169
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.
174 if has_image then
175 nixio.fs.unlink(tmpfile)
176 end
177
178 luci.template.render("mini/upgrade", {
179 step=1,
180 bad_image=(has_image and not has_support or false),
181 keepavail=keep_avail,
182 supported=has_platform
183 } )
184
185 -- Step 2: present uploaded file, show checksum, confirmation
186 elseif step == 2 then
187 luci.template.render("mini/upgrade", {
188 step=2,
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")
193 } )
194
195 -- Step 3: load iframe which calls the actual flash procedure
196 elseif step == 3 then
197 luci.template.render("mini/upgrade", {
198 step=3,
199 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
200 } )
201 end
202 end
203
204 function _keep_pattern()
205 local kpattern = ""
206 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
207 if files then
208 kpattern = ""
209 for k, v in pairs(files) do
210 if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
211 kpattern = kpattern .. " " .. v
212 end
213 end
214 end
215 return kpattern
216 end
217
218 function ltn12_popen(command)
219
220 local fdi, fdo = nixio.pipe()
221 local pid = nixio.fork()
222
223 if pid > 0 then
224 fdo:close()
225 local close
226 return function()
227 local buffer = fdi:read(2048)
228 local wpid, stat = nixio.waitpid(pid, "nohang")
229 if not close and wpid and stat == "exited" then
230 close = true
231 end
232
233 if buffer and #buffer > 0 then
234 return buffer
235 elseif close then
236 fdi:close()
237 return nil
238 end
239 end
240 elseif pid == 0 then
241 nixio.dup(fdo, nixio.stdout)
242 fdi:close()
243 fdo:close()
244 nixio.exec("/bin/sh", "-c", command)
245 end
246 end