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
86 install[uinst], out, err = ipkg.install(uinst)
87 stdout[#stdout+1] = out
88 stderr[#stderr+1] = err
93 local rem = submit and luci.http.formvalue("remove")
95 remove[rem], out, err = ipkg.remove(rem)
96 stdout[#stdout+1] = out
97 stderr[#stderr+1] = err
102 -- Update all packets
103 local update = luci.http.formvalue("update")
105 update, out, err = ipkg.update()
106 stdout[#stdout+1] = out
107 stderr[#stderr+1] = err
111 -- Upgrade all packets
112 local upgrade = luci.http.formvalue("upgrade")
114 upgrade, out, err = ipkg.upgrade()
115 stdout[#stdout+1] = out
116 stderr[#stderr+1] = err
120 luci.template.render("admin_system/packages", {
128 stdout = table.concat(stdout, ""),
129 stderr = table.concat(stderr, "")
132 -- Remove index cache
134 nixio.fs.unlink("/tmp/luci-indexcache")
138 function action_backup()
139 local sys = require "luci.sys"
140 local fs = require "luci.fs"
142 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
143 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
144 local backup_cmd = "tar -czT %s 2>/dev/null"
147 luci.http.setfilehandler(
148 function(meta, chunk, eof)
149 if not restore_fpi then
150 restore_fpi = io.popen(restore_cmd, "w")
153 restore_fpi:write(chunk)
161 local upload = luci.http.formvalue("archive")
162 local backup = luci.http.formvalue("backup")
163 local reset = reset_avail and luci.http.formvalue("reset")
165 if upload and #upload > 0 then
166 luci.template.render("admin_system/applyreboot")
169 local filelist = "/tmp/luci-backup-list.%d" % os.time()
172 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
173 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
174 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
177 if fs.access(filelist) then
178 local reader = ltn12_popen(backup_cmd:format(filelist))
179 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
180 luci.sys.hostname(), os.date("%Y-%m-%d")})
181 luci.http.prepare_content("application/x-targz")
182 luci.ltn12.pump.all(reader, luci.http.write)
186 luci.template.render("admin_system/applyreboot")
187 luci.util.exec("mtd -r erase rootfs_data")
189 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
193 function action_passwd()
194 local p1 = luci.http.formvalue("pwd1")
195 local p2 = luci.http.formvalue("pwd2")
200 stat = luci.sys.user.setpasswd("root", p1)
206 luci.template.render("admin_system/passwd", {stat=stat})
209 function action_reboot()
210 local reboot = luci.http.formvalue("reboot")
211 luci.template.render("admin_system/reboot", {reboot=reboot})
217 function action_upgrade()
218 require("luci.model.uci")
220 local tmpfile = "/tmp/firmware.img"
222 local function image_supported()
224 return ( 0 == os.execute(
225 ". /etc/functions.sh; " ..
226 "include /lib/upgrade; " ..
227 "platform_check_image %q >/dev/null"
232 local function image_checksum()
233 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
236 local function storage_size()
238 if nixio.fs.access("/proc/mtd") then
239 for l in io.lines("/proc/mtd") do
240 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
242 size = tonumber(s, 16)
246 elseif nixio.fs.access("/proc/partitions") then
247 for l in io.lines("/proc/partitions") do
248 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
249 if b and n and not n:match('[0-9]') then
250 size = tonumber(b) * 1024
259 -- Install upload handler
261 luci.http.setfilehandler(
262 function(meta, chunk, eof)
263 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
264 file = io.open(tmpfile, "w")
266 if file and chunk then
277 local keep_avail = true
278 local step = tonumber(luci.http.formvalue("step") or 1)
279 local has_image = nixio.fs.access(tmpfile)
280 local has_support = image_supported()
281 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
282 local has_upload = luci.http.formvalue("image")
284 -- This does the actual flashing which is invoked inside an iframe
285 -- so don't produce meaningful errors here because the the
286 -- previous pages should arrange the stuff as required.
288 if has_platform and has_image and has_support then
289 -- Mimetype text/plain
290 luci.http.prepare_content("text/plain")
291 luci.http.write("Starting sysupgrade...\n")
295 -- Now invoke sysupgrade
296 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
297 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
298 keepcfg and "" or "-n", tmpfile
301 luci.ltn12.pump.all(flash, luci.http.write)
306 -- This is step 1-3, which does the user interaction and
310 -- Step 1: file upload, error on unsupported image format
311 elseif not has_image or not has_support or step == 1 then
312 -- If there is an image but user has requested step 1
313 -- or type is not supported, then remove it.
315 nixio.fs.unlink(tmpfile)
318 luci.template.render("admin_system/upgrade", {
320 bad_image=(has_image and not has_support or false),
321 keepavail=keep_avail,
322 supported=has_platform
325 -- Step 2: present uploaded file, show checksum, confirmation
326 elseif step == 2 then
327 luci.template.render("admin_system/upgrade", {
329 checksum=image_checksum(),
330 filesize=nixio.fs.stat(tmpfile).size,
331 flashsize=storage_size(),
332 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
335 -- Step 3: load iframe which calls the actual flash procedure
336 elseif step == 3 then
337 luci.template.render("admin_system/upgrade", {
339 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
344 function ltn12_popen(command)
346 local fdi, fdo = nixio.pipe()
347 local pid = nixio.fork()
353 local buffer = fdi:read(2048)
354 local wpid, stat = nixio.waitpid(pid, "nohang")
355 if not close and wpid and stat == "exited" then
359 if buffer and #buffer > 0 then
367 nixio.dup(fdo, nixio.stdout)
370 nixio.exec("/bin/sh", "-c", command)