123e005822986e474c9a2d933c9f94cf3fd0c716
[project/luci.git] / modules / admin-full / luasrc / controller / admin / system.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
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.admin.system", package.seeall)
17
18 function index()
19         entry({"admin", "system"}, alias("admin", "system", "system"), _("System"), 30).index = true
20         entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
21         entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
22
23         if nixio.fs.access("/bin/opkg") then
24                 entry({"admin", "system", "packages"}, call("action_packages"), _("Software"), 10)
25                 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
26         end
27
28         entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
29
30         if nixio.fs.access("/etc/config/fstab") then
31                 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
32                 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
33                 entry({"admin", "system", "fstab", "swap"},  cbi("admin_system/fstab/swap"),  nil).leaf = true
34         end
35
36         if nixio.fs.access("/sys/class/leds") then
37                 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
38         end
39
40         entry({"admin", "system", "backup"}, call("action_backup"), _("Backup / Restore"), 70)
41         entry({"admin", "system", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 80)
42         entry({"admin", "system", "reboot"}, call("action_reboot"), _("Reboot"), 90)
43 end
44
45 function action_packages()
46         local ipkg = require("luci.model.ipkg")
47         local submit = luci.http.formvalue("submit")
48         local changes = false
49         local install = { }
50         local remove  = { }
51         local stdout  = { "" }
52         local stderr  = { "" }
53         local out, err
54
55         -- Display
56         local display = luci.http.formvalue("display") or "installed"
57
58         -- Letter
59         local letter = string.byte(luci.http.formvalue("letter") or "A", 1)
60         letter = (letter == 35 or (letter >= 65 and letter <= 90)) and letter or 65
61
62         -- Search query
63         local query = luci.http.formvalue("query")
64         query = (query ~= '') and query or nil
65
66
67         -- Packets to be installed
68         local ninst = submit and luci.http.formvalue("install")
69         local uinst = nil
70
71         -- Install from URL
72         local url = luci.http.formvalue("url")
73         if url and url ~= '' and submit then
74                 uinst = url
75         end
76
77         -- Do install
78         if ninst then
79                 install[ninst], out, err = ipkg.install(ninst)
80                 stdout[#stdout+1] = out
81                 stderr[#stderr+1] = err
82                 changes = true
83         end
84
85         if uinst then
86                 local pkg
87                 for pkg in luci.util.imatch(uinst) do
88                         install[uinst], out, err = ipkg.install(pkg)
89                         stdout[#stdout+1] = out
90                         stderr[#stderr+1] = err
91                         changes = true
92                 end
93         end
94
95         -- Remove packets
96         local rem = submit and luci.http.formvalue("remove")
97         if rem then
98                 remove[rem], out, err = ipkg.remove(rem)
99                 stdout[#stdout+1] = out
100                 stderr[#stderr+1] = err
101                 changes = true
102         end
103
104
105         -- Update all packets
106         local update = luci.http.formvalue("update")
107         if update then
108                 update, out, err = ipkg.update()
109                 stdout[#stdout+1] = out
110                 stderr[#stderr+1] = err
111         end
112
113
114         -- Upgrade all packets
115         local upgrade = luci.http.formvalue("upgrade")
116         if upgrade then
117                 upgrade, out, err = ipkg.upgrade()
118                 stdout[#stdout+1] = out
119                 stderr[#stderr+1] = err
120         end
121
122
123         luci.template.render("admin_system/packages", {
124                 display = display,
125                 letter  = letter,
126                 query   = query,
127                 install = install,
128                 remove  = remove,
129                 update  = update,
130                 upgrade = upgrade,
131                 stdout  = table.concat(stdout, ""),
132                 stderr  = table.concat(stderr, "")
133         })
134
135         -- Remove index cache
136         if changes then
137                 nixio.fs.unlink("/tmp/luci-indexcache")
138         end
139 end
140
141 function action_backup()
142         local sys = require "luci.sys"
143         local fs  = require "luci.fs"
144
145         local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
146         local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
147         local backup_cmd  = "tar -czT %s 2>/dev/null"
148
149         local restore_fpi
150         luci.http.setfilehandler(
151                 function(meta, chunk, eof)
152                         if not restore_fpi then
153                                 restore_fpi = io.popen(restore_cmd, "w")
154                         end
155                         if chunk then
156                                 restore_fpi:write(chunk)
157                         end
158                         if eof then
159                                 restore_fpi:close()
160                         end
161                 end
162         )
163
164         local upload = luci.http.formvalue("archive")
165         local backup = luci.http.formvalue("backup")
166         local reset  = reset_avail and luci.http.formvalue("reset")
167
168         if upload and #upload > 0 then
169                 luci.template.render("admin_system/applyreboot")
170                 luci.sys.reboot()
171         elseif backup then
172                 local filelist = "/tmp/luci-backup-list.%d" % os.time()
173
174                 sys.call(
175                         "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
176                         "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
177                         "opkg list-changed-conffiles ) | sort -u > %s" % filelist
178                 )
179
180                 if fs.access(filelist) then
181                         local reader = ltn12_popen(backup_cmd:format(filelist))
182                         luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
183                                 luci.sys.hostname(), os.date("%Y-%m-%d")})
184                         luci.http.prepare_content("application/x-targz")
185                         luci.ltn12.pump.all(reader, luci.http.write)
186                         fs.unlink(filelist)
187                 end
188         elseif reset then
189                 luci.template.render("admin_system/applyreboot")
190                 luci.util.exec("mtd -r erase rootfs_data")
191         else
192                 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
193         end
194 end
195
196 function action_passwd()
197         local p1 = luci.http.formvalue("pwd1")
198         local p2 = luci.http.formvalue("pwd2")
199         local stat = nil
200
201         if p1 or p2 then
202                 if p1 == p2 then
203                         stat = luci.sys.user.setpasswd("root", p1)
204                 else
205                         stat = 10
206                 end
207         end
208
209         luci.template.render("admin_system/passwd", {stat=stat})
210 end
211
212 function action_reboot()
213         local reboot = luci.http.formvalue("reboot")
214         luci.template.render("admin_system/reboot", {reboot=reboot})
215         if reboot then
216                 luci.sys.reboot()
217         end
218 end
219
220 function action_upgrade()
221         require("luci.model.uci")
222
223         local tmpfile = "/tmp/firmware.img"
224
225         local function image_supported()
226                 -- XXX: yay...
227                 return ( 0 == os.execute(
228                         ". /etc/functions.sh; " ..
229                         "include /lib/upgrade; " ..
230                         "platform_check_image %q >/dev/null"
231                                 % tmpfile
232                 ) )
233         end
234
235         local function image_checksum()
236                 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
237         end
238
239         local function storage_size()
240                 local size = 0
241                 if nixio.fs.access("/proc/mtd") then
242                         for l in io.lines("/proc/mtd") do
243                                 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
244                                 if n == "linux" then
245                                         size = tonumber(s, 16)
246                                         break
247                                 end
248                         end
249                 elseif nixio.fs.access("/proc/partitions") then
250                         for l in io.lines("/proc/partitions") do
251                                 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
252                                 if b and n and not n:match('[0-9]') then
253                                         size = tonumber(b) * 1024
254                                         break
255                                 end
256                         end
257                 end
258                 return size
259         end
260
261
262         -- Install upload handler
263         local file
264         luci.http.setfilehandler(
265                 function(meta, chunk, eof)
266                         if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
267                                 file = io.open(tmpfile, "w")
268                         end
269                         if file and chunk then
270                                 file:write(chunk)
271                         end
272                         if file and eof then
273                                 file:close()
274                         end
275                 end
276         )
277
278
279         -- Determine state
280         local keep_avail   = true
281         local step         = tonumber(luci.http.formvalue("step") or 1)
282         local has_image    = nixio.fs.access(tmpfile)
283         local has_support  = image_supported()
284         local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
285         local has_upload   = luci.http.formvalue("image")
286
287         -- This does the actual flashing which is invoked inside an iframe
288         -- so don't produce meaningful errors here because the the
289         -- previous pages should arrange the stuff as required.
290         if step == 4 then
291                 if has_platform and has_image and has_support then
292                         -- Mimetype text/plain
293                         luci.http.prepare_content("text/plain")
294                         luci.http.write("Starting sysupgrade...\n")
295
296                         io.flush()
297
298                         -- Now invoke sysupgrade
299                         local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
300                         local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
301                                 keepcfg and "" or "-n", tmpfile
302                         })
303
304                         luci.ltn12.pump.all(flash, luci.http.write)
305                 end
306
307
308         --
309         -- This is step 1-3, which does the user interaction and
310         -- image upload.
311         --
312
313         -- Step 1: file upload, error on unsupported image format
314         elseif not has_image or not has_support or step == 1 then
315                 -- If there is an image but user has requested step 1
316                 -- or type is not supported, then remove it.
317                 if has_image then
318                         nixio.fs.unlink(tmpfile)
319                 end
320
321                 luci.template.render("admin_system/upgrade", {
322                         step=1,
323                         bad_image=(has_image and not has_support or false),
324                         keepavail=keep_avail,
325                         supported=has_platform
326                 } )
327
328         -- Step 2: present uploaded file, show checksum, confirmation
329         elseif step == 2 then
330                 luci.template.render("admin_system/upgrade", {
331                         step=2,
332                         checksum=image_checksum(),
333                         filesize=nixio.fs.stat(tmpfile).size,
334                         flashsize=storage_size(),
335                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
336                 } )
337
338         -- Step 3: load iframe which calls the actual flash procedure
339         elseif step == 3 then
340                 luci.template.render("admin_system/upgrade", {
341                         step=3,
342                         keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
343                 } )
344         end
345 end
346
347 function ltn12_popen(command)
348
349         local fdi, fdo = nixio.pipe()
350         local pid = nixio.fork()
351
352         if pid > 0 then
353                 fdo:close()
354                 local close
355                 return function()
356                         local buffer = fdi:read(2048)
357                         local wpid, stat = nixio.waitpid(pid, "nohang")
358                         if not close and wpid and stat == "exited" then
359                                 close = true
360                         end
361
362                         if buffer and #buffer > 0 then
363                                 return buffer
364                         elseif close then
365                                 fdi:close()
366                                 return nil
367                         end
368                 end
369         elseif pid == 0 then
370                 nixio.dup(fdo, nixio.stdout)
371                 fdi:close()
372                 fdo:close()
373                 nixio.exec("/bin/sh", "-c", command)
374         end
375 end