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 luci.i18n.loadc("base")
20 local i18n = luci.i18n.translate
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", "packages"}, call("action_packages"), i18n("Software"), 10)
25 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"))
26 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("Admin Password"), 20)
27 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("<abbr title=\"Secure Shell\">SSH</abbr>-Keys"), 30)
28 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("Processes"), 45)
30 if nixio.fs.access("/etc/config/fstab") then
31 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("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"), i18n("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
40 entry({"admin", "system", "backup"}, call("action_backup"), i18n("Backup / Restore"), 70)
41 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("Flash Firmware"), 80)
42 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("Reboot"), 90)
45 function action_packages()
46 local ipkg = require("luci.model.ipkg")
47 local submit = luci.http.formvalue("submit")
53 local query = luci.http.formvalue("query")
54 query = (query ~= '') and query or nil
57 -- Packets to be installed
58 local ninst = submit and luci.http.formvalue("install")
62 local url = luci.http.formvalue("url")
63 if url and url ~= '' and submit then
69 _, install[ninst] = ipkg.install(ninst)
74 _, install[uinst] = ipkg.install(uinst)
79 local rem = submit and luci.http.formvalue("remove")
81 _, remove[rem] = ipkg.remove(rem)
87 local update = luci.http.formvalue("update")
89 _, update = ipkg.update()
93 -- Upgrade all packets
94 local upgrade = luci.http.formvalue("upgrade")
96 _, upgrade = ipkg.upgrade()
100 luci.template.render("admin_system/packages", {
101 query=query, install=install, remove=remove, update=update, upgrade=upgrade
104 -- Remove index cache
106 nixio.fs.unlink("/tmp/luci-indexcache")
110 function action_backup()
111 local sys = require "luci.sys"
112 local fs = require "luci.fs"
114 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
115 local restore_cmd = "tar -xzC/ >/dev/null 2>&1"
116 local backup_cmd = "tar -czT %s 2>/dev/null"
119 luci.http.setfilehandler(
120 function(meta, chunk, eof)
121 if not restore_fpi then
122 restore_fpi = io.popen(restore_cmd, "w")
125 restore_fpi:write(chunk)
133 local upload = luci.http.formvalue("archive")
134 local backup = luci.http.formvalue("backup")
135 local reset = reset_avail and luci.http.formvalue("reset")
137 if upload and #upload > 0 then
138 luci.template.render("admin_system/applyreboot")
141 local filelist = "/tmp/luci-backup-list.%d" % os.time()
144 "( find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' /etc/sysupgrade.conf " ..
145 "/lib/upgrade/keep.d/* 2>/dev/null) -type f 2>/dev/null; " ..
146 "opkg list-changed-conffiles ) | sort -u > %s" % filelist
149 if fs.access(filelist) then
150 local reader = ltn12_popen(backup_cmd:format(filelist))
151 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
152 luci.sys.hostname(), os.date("%Y-%m-%d")})
153 luci.http.prepare_content("application/x-targz")
154 luci.ltn12.pump.all(reader, luci.http.write)
158 luci.template.render("admin_system/applyreboot")
159 luci.util.exec("mtd -r erase rootfs_data")
161 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
165 function action_passwd()
166 local p1 = luci.http.formvalue("pwd1")
167 local p2 = luci.http.formvalue("pwd2")
172 stat = luci.sys.user.setpasswd("root", p1)
178 luci.template.render("admin_system/passwd", {stat=stat})
181 function action_reboot()
182 local reboot = luci.http.formvalue("reboot")
183 luci.template.render("admin_system/reboot", {reboot=reboot})
189 function action_upgrade()
190 require("luci.model.uci")
192 local tmpfile = "/tmp/firmware.img"
194 local function image_supported()
196 return ( 0 == os.execute(
197 ". /etc/functions.sh; " ..
198 "include /lib/upgrade; " ..
199 "platform_check_image %q >/dev/null"
204 local function image_checksum()
205 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
208 local function storage_size()
210 if nixio.fs.access("/proc/mtd") then
211 for l in io.lines("/proc/mtd") do
212 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
214 size = tonumber(s, 16)
218 elseif nixio.fs.access("/proc/partitions") then
219 for l in io.lines("/proc/partitions") do
220 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
221 if b and n and not n:match('[0-9]') then
222 size = tonumber(b) * 1024
231 -- Install upload handler
233 luci.http.setfilehandler(
234 function(meta, chunk, eof)
235 if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
236 file = io.open(tmpfile, "w")
238 if file and chunk then
249 local keep_avail = true
250 local step = tonumber(luci.http.formvalue("step") or 1)
251 local has_image = nixio.fs.access(tmpfile)
252 local has_support = image_supported()
253 local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
254 local has_upload = luci.http.formvalue("image")
256 -- This does the actual flashing which is invoked inside an iframe
257 -- so don't produce meaningful errors here because the the
258 -- previous pages should arrange the stuff as required.
260 if has_platform and has_image and has_support then
261 -- Mimetype text/plain
262 luci.http.prepare_content("text/plain")
263 luci.http.write("Starting sysupgrade...\n")
267 -- Now invoke sysupgrade
268 local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
269 local flash = ltn12_popen("/sbin/sysupgrade %s %q" %{
270 keepcfg and "" or "-n", tmpfile
273 luci.ltn12.pump.all(flash, luci.http.write)
278 -- This is step 1-3, which does the user interaction and
282 -- Step 1: file upload, error on unsupported image format
283 elseif not has_image or not has_support or step == 1 then
284 -- If there is an image but user has requested step 1
285 -- or type is not supported, then remove it.
287 nixio.fs.unlink(tmpfile)
290 luci.template.render("admin_system/upgrade", {
292 bad_image=(has_image and not has_support or false),
293 keepavail=keep_avail,
294 supported=has_platform
297 -- Step 2: present uploaded file, show checksum, confirmation
298 elseif step == 2 then
299 luci.template.render("admin_system/upgrade", {
301 checksum=image_checksum(),
302 filesize=nixio.fs.stat(tmpfile).size,
303 flashsize=storage_size(),
304 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
307 -- Step 3: load iframe which calls the actual flash procedure
308 elseif step == 3 then
309 luci.template.render("admin_system/upgrade", {
311 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
316 function ltn12_popen(command)
318 local fdi, fdo = nixio.pipe()
319 local pid = nixio.fork()
325 local buffer = fdi:read(2048)
326 local wpid, stat = nixio.waitpid(pid, "nohang")
327 if not close and wpid and stat == "exited" then
331 if buffer and #buffer > 0 then
339 nixio.dup(fdo, nixio.stdout)
342 nixio.exec("/bin/sh", "-c", command)