2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2009 Jo-Philipp Wich <xm@subsignal.org>
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
11 http://www.apache.org/licenses/LICENSE-2.0
16 module("luci.controller.admin.system", package.seeall)
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)
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"))
28 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
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
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)
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)
45 function action_packages()
46 local ipkg = require("luci.model.ipkg")
47 local submit = luci.http.formvalue("submit")
56 local display = luci.http.formvalue("display") or "installed"
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
63 local query = luci.http.formvalue("query")
64 query = (query ~= '') and query or nil
67 -- Packets to be installed
68 local ninst = submit and luci.http.formvalue("install")
72 local url = luci.http.formvalue("url")
73 if url and url ~= '' and submit then
79 install[ninst], out, err = ipkg.install(ninst)
80 stdout[#stdout+1] = out
81 stderr[#stderr+1] = err
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
96 local rem = submit and luci.http.formvalue("remove")
98 remove[rem], out, err = ipkg.remove(rem)
99 stdout[#stdout+1] = out
100 stderr[#stderr+1] = err
105 -- Update all packets
106 local update = luci.http.formvalue("update")
108 update, out, err = ipkg.update()
109 stdout[#stdout+1] = out
110 stderr[#stderr+1] = err
114 -- Upgrade all packets
115 local upgrade = luci.http.formvalue("upgrade")
117 upgrade, out, err = ipkg.upgrade()
118 stdout[#stdout+1] = out
119 stderr[#stderr+1] = err
123 luci.template.render("admin_system/packages", {
131 stdout = table.concat(stdout, ""),
132 stderr = table.concat(stderr, "")
135 -- Remove index cache
137 nixio.fs.unlink("/tmp/luci-indexcache")
141 function action_backup()
142 local sys = require "luci.sys"
143 local fs = require "luci.fs"
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"
150 luci.http.setfilehandler(
151 function(meta, chunk, eof)
152 if not restore_fpi then
153 restore_fpi = io.popen(restore_cmd, "w")
156 restore_fpi:write(chunk)
164 local upload = luci.http.formvalue("archive")
165 local backup = luci.http.formvalue("backup")
166 local reset = reset_avail and luci.http.formvalue("reset")
168 if upload and #upload > 0 then
169 luci.template.render("admin_system/applyreboot")
172 local filelist = "/tmp/luci-backup-list.%d" % os.time()
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
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)
189 luci.template.render("admin_system/applyreboot")
190 luci.util.exec("mtd -r erase rootfs_data")
192 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
196 function action_passwd()
197 local p1 = luci.http.formvalue("pwd1")
198 local p2 = luci.http.formvalue("pwd2")
203 stat = luci.sys.user.setpasswd("root", p1)
209 luci.template.render("admin_system/passwd", {stat=stat})
212 function action_reboot()
213 local reboot = luci.http.formvalue("reboot")
214 luci.template.render("admin_system/reboot", {reboot=reboot})
220 function action_upgrade()
221 require("luci.model.uci")
223 local tmpfile = "/tmp/firmware.img"
225 local function image_supported()
227 return ( 0 == os.execute(
228 ". /etc/functions.sh; " ..
229 "include /lib/upgrade; " ..
230 "platform_check_image %q >/dev/null"
235 local function image_checksum()
236 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
239 local function storage_size()
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]+)"')
245 size = tonumber(s, 16)
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
262 -- Install upload handler
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")
269 if file and chunk then
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")
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.
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")
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
304 luci.ltn12.pump.all(flash, luci.http.write)
309 -- This is step 1-3, which does the user interaction and
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.
318 nixio.fs.unlink(tmpfile)
321 luci.template.render("admin_system/upgrade", {
323 bad_image=(has_image and not has_support or false),
324 keepavail=keep_avail,
325 supported=has_platform
328 -- Step 2: present uploaded file, show checksum, confirmation
329 elseif step == 2 then
330 luci.template.render("admin_system/upgrade", {
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")
338 -- Step 3: load iframe which calls the actual flash procedure
339 elseif step == 3 then
340 luci.template.render("admin_system/upgrade", {
342 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
347 function ltn12_popen(command)
349 local fdi, fdo = nixio.pipe()
350 local pid = nixio.fork()
356 local buffer = fdi:read(2048)
357 local wpid, stat = nixio.waitpid(pid, "nohang")
358 if not close and wpid and stat == "exited" then
362 if buffer and #buffer > 0 then
370 nixio.dup(fdo, nixio.stdout)
373 nixio.exec("/bin/sh", "-c", command)