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)
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)
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
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)
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)
41 function action_packages()
42 local ipkg = require("luci.model.ipkg")
43 local submit = luci.http.formvalue("submit")
52 local query = luci.http.formvalue("query")
53 query = (query ~= '') and query or nil
56 -- Packets to be installed
57 local ninst = submit and luci.http.formvalue("install")
61 local url = luci.http.formvalue("url")
62 if url and url ~= '' and submit then
68 install[ninst], out, err = ipkg.install(ninst)
69 stdout[#stdout+1] = out
70 stderr[#stderr+1] = err
75 install[uinst], out, err = ipkg.install(uinst)
76 stdout[#stdout+1] = out
77 stderr[#stderr+1] = err
82 local rem = submit and luci.http.formvalue("remove")
84 remove[rem], out, err = ipkg.remove(rem)
85 stdout[#stdout+1] = out
86 stderr[#stderr+1] = err
92 local update = luci.http.formvalue("update")
94 update, out, err = ipkg.update()
95 stdout[#stdout+1] = out
96 stderr[#stderr+1] = err
100 -- Upgrade all packets
101 local upgrade = luci.http.formvalue("upgrade")
103 upgrade, out, err = ipkg.upgrade()
104 stdout[#stdout+1] = out
105 stderr[#stderr+1] = err
109 luci.template.render("admin_system/packages", {
115 stdout = table.concat(stdout, ""),
116 stderr = table.concat(stderr, "")
119 -- Remove index cache
121 nixio.fs.unlink("/tmp/luci-indexcache")
125 function action_backup()
126 local sys = require "luci.sys"
127 local fs = require "luci.fs"
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"
134 luci.http.setfilehandler(
135 function(meta, chunk, eof)
136 if not restore_fpi then
137 restore_fpi = io.popen(restore_cmd, "w")
140 restore_fpi:write(chunk)
148 local upload = luci.http.formvalue("archive")
149 local backup = luci.http.formvalue("backup")
150 local reset = reset_avail and luci.http.formvalue("reset")
152 if upload and #upload > 0 then
153 luci.template.render("admin_system/applyreboot")
156 local filelist = "/tmp/luci-backup-list.%d" % os.time()
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
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)
173 luci.template.render("admin_system/applyreboot")
174 luci.util.exec("mtd -r erase rootfs_data")
176 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
180 function action_passwd()
181 local p1 = luci.http.formvalue("pwd1")
182 local p2 = luci.http.formvalue("pwd2")
187 stat = luci.sys.user.setpasswd("root", p1)
193 luci.template.render("admin_system/passwd", {stat=stat})
196 function action_reboot()
197 local reboot = luci.http.formvalue("reboot")
198 luci.template.render("admin_system/reboot", {reboot=reboot})
204 function action_upgrade()
205 require("luci.model.uci")
207 local tmpfile = "/tmp/firmware.img"
209 local function image_supported()
211 return ( 0 == os.execute(
212 ". /etc/functions.sh; " ..
213 "include /lib/upgrade; " ..
214 "platform_check_image %q >/dev/null"
219 local function image_checksum()
220 return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
223 local function storage_size()
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]+)"')
229 size = tonumber(s, 16)
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
246 -- Install upload handler
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")
253 if file and chunk then
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")
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.
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")
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
288 luci.ltn12.pump.all(flash, luci.http.write)
293 -- This is step 1-3, which does the user interaction and
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.
302 nixio.fs.unlink(tmpfile)
305 luci.template.render("admin_system/upgrade", {
307 bad_image=(has_image and not has_support or false),
308 keepavail=keep_avail,
309 supported=has_platform
312 -- Step 2: present uploaded file, show checksum, confirmation
313 elseif step == 2 then
314 luci.template.render("admin_system/upgrade", {
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")
322 -- Step 3: load iframe which calls the actual flash procedure
323 elseif step == 3 then
324 luci.template.render("admin_system/upgrade", {
326 keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
331 function ltn12_popen(command)
333 local fdi, fdo = nixio.pipe()
334 local pid = nixio.fork()
340 local buffer = fdi:read(2048)
341 local wpid, stat = nixio.waitpid(pid, "nohang")
342 if not close and wpid and stat == "exited" then
346 if buffer and #buffer > 0 then
354 nixio.dup(fdo, nixio.stdout)
357 nixio.exec("/bin/sh", "-c", command)