2559ac16cc6d710867dda567701d5d5764aa74ca
[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         luci.i18n.loadc("base")
20         local i18n = luci.i18n.translate
21
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)
28 end
29
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"
34         
35         local restore_fpi 
36         luci.http.setfilehandler(
37                 function(meta, chunk, eof)
38                         if not restore_fpi then
39                                 restore_fpi = io.popen(restore_cmd, "w")
40                         end
41                         if chunk then
42                                 restore_fpi:write(chunk)
43                         end
44                         if eof then
45                                 restore_fpi:close()
46                         end
47                 end
48         )
49                   
50         local upload = luci.http.formvalue("archive")
51         local backup = luci.http.formvalue("backup")
52         local reset  = reset_avail and luci.http.formvalue("reset")
53         
54         if upload and #upload > 0 then
55                 luci.template.render("mini/applyreboot")
56                 luci.sys.reboot()
57         elseif backup then
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)
63         elseif reset then
64                 luci.template.render("mini/applyreboot")
65                 luci.util.exec("mtd -r erase rootfs_data")
66         else
67                 luci.template.render("mini/backup", {reset_avail = reset_avail})
68         end
69 end
70
71 function action_reboot()
72         local reboot = luci.http.formvalue("reboot")
73         luci.template.render("mini/reboot", {reboot=reboot})
74         if reboot then
75                 luci.sys.reboot()
76         end
77 end
78
79 function action_upgrade()
80         require("luci.model.uci")
81
82         local tmpfile = "/tmp/firmware.img"
83         
84         local function image_supported()
85                 -- XXX: yay...
86                 return ( 0 == os.execute(
87                         ". /etc/functions.sh; " ..
88                         "include /lib/upgrade; " ..
89                         "platform_check_image %q >/dev/null"
90                                 % tmpfile
91                 ) )
92         end
93         
94         local function image_checksum()
95                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
96         end
97         
98         local function storage_size()
99                 local size = 0
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]+)"')
103                                 if n == "linux" then
104                                         size = tonumber(s, 16)
105                                         break
106                                 end
107                         end
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
113                                         break
114                                 end
115                         end
116                 end
117                 return size
118         end
119
120
121         -- Install upload handler
122         local file
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")
127                         end
128                         if file and chunk then
129                                 file:write(chunk)
130                         end
131                         if file and eof then
132                                 file:close()
133                         end
134                 end
135         )
136
137
138         -- Determine state
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")
145         
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.
149         if step == 4 then
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")
154
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
159                         })
160
161                         luci.ltn12.pump.all(flash, luci.http.write)
162
163                         -- Make sure the device is rebooted
164                         luci.sys.reboot()
165                 end
166
167
168         --
169         -- This is step 1-3, which does the user interaction and
170         -- image upload.
171         --
172
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.
177                 if has_image then
178                         nixio.fs.unlink(tmpfile)
179                 end
180                         
181                 luci.template.render("mini/upgrade", {
182                         step=1,
183                         bad_image=(has_image and not has_support or false),
184                         keepavail=keep_avail,
185                         supported=has_platform
186                 } )
187
188         -- Step 2: present uploaded file, show checksum, confirmation
189         elseif step == 2 then
190                 luci.template.render("mini/upgrade", {
191                         step=2,
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")
196                 } )
197         
198         -- Step 3: load iframe which calls the actual flash procedure
199         elseif step == 3 then
200                 luci.template.render("mini/upgrade", {
201                         step=3,
202                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
203                 } )
204         end     
205 end
206
207 function _keep_pattern()
208         local kpattern = ""
209         local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
210         if files then
211                 kpattern = ""
212                 for k, v in pairs(files) do
213                         if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
214                                 kpattern = kpattern .. " " ..  v
215                         end
216                 end
217         end
218         return kpattern
219 end
220
221 function ltn12_popen(command)
222
223         local fdi, fdo = nixio.pipe()
224         local pid = nixio.fork()
225
226         if pid > 0 then
227                 fdo:close()
228                 local close
229                 return function()
230                         local buffer = fdi:read(2048)
231                         local wpid, stat = nixio.waitpid(pid, "nohang")
232                         if not close and wpid and stat == "exited" then
233                                 close = true
234                         end
235
236                         if buffer and #buffer > 0 then
237                                 return buffer
238                         elseif close then
239                                 fdi:close()
240                                 return nil
241                         end
242                 end
243         elseif pid == 0 then
244                 nixio.dup(fdo, nixio.stdout)
245                 fdi:close()
246                 fdo:close()
247                 nixio.exec("/bin/sh", "-c", command)
248         end
249 end