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