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