2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
14 module("luci.controller.admin.system", package.seeall)
17 luci.i18n.loadc("admin-core")
18 local i18n = luci.i18n.translate
20 entry({"admin", "system"}, template("admin_system/index"), i18n("system"), 30)
21 entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages"), 10)
22 entry({"admin", "system", "packages", "ipkg"}, call("action_ipkg"), i18n("a_s_p_ipkg"))
23 entry({"admin", "system", "passwd"}, call("action_passwd"), i18n("a_s_changepw"), 20)
24 entry({"admin", "system", "sshkeys"}, call("action_sshkeys"), i18n("a_s_sshkeys"), 30)
25 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system"), 40)
26 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
27 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
28 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
29 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 80)
30 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
33 function action_editor()
34 local file = luci.http.formvalue("file", "")
35 local data = luci.http.formvalue("data")
41 stat, err = luci.fs.writefile(file, data)
45 err = luci.util.split(err, " ")
47 msg = table.concat(err, " ")
50 local cnt, err = luci.fs.readfile(file)
52 cnt = luci.util.pcdata(cnt)
54 luci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
57 function action_ipkg()
58 local file = "/etc/ipkg.conf"
59 local data = luci.http.formvalue("data")
64 stat, err = luci.fs.writefile(file, data)
67 local cnt = luci.fs.readfile(file)
69 cnt = luci.util.pcdata(cnt)
72 luci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
75 function action_packages()
76 local ipkg = require("luci.model.ipkg")
78 local submit = luci.http.formvalue("submit")
82 local query = luci.http.formvalue("query")
83 query = (query ~= '') and query or nil
86 -- Packets to be installed
87 local install = submit and luci.http.formvaluetable("install")
90 local url = luci.http.formvalue("url")
91 if url and url ~= '' and submit then
100 for k, v in pairs(install) do
101 void, install[k] = ipkg.install(k)
107 local remove = submit and luci.http.formvaluetable("remove")
109 for k, v in pairs(remove) do
110 void, remove[k] = ipkg.remove(k)
115 -- Update all packets
116 local update = luci.http.formvalue("update")
118 void, update = ipkg.update()
122 -- Upgrade all packets
123 local upgrade = luci.http.formvalue("upgrade")
125 void, upgrade = ipkg.upgrade()
130 local info = luci.model.ipkg.info(query)
134 -- Sort after status and name
135 for k, v in pairs(info) do
137 for i, j in pairs(pkgs) do
138 local vins = (v.Status and v.Status.installed)
139 local jins = (j.Status and j.Status.installed)
145 if j.Package > v.Package then
151 table.insert(pkgs, x+1, v)
154 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
155 install=install, remove=remove, update=update, upgrade=upgrade})
158 function action_backup()
159 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
160 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
161 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
164 luci.http.setfilehandler(
165 function(meta, chunk, eof)
166 if not restore_fpi then
167 restore_fpi = io.popen(restore_cmd, "w")
170 restore_fpi:write(chunk)
178 local upload = luci.http.formvalue("archive")
179 local backup = luci.http.formvalue("backup")
180 local reset = reset_avail and luci.http.formvalue("reset")
182 if upload and #upload > 0 then
183 luci.template.render("admin_system/applyreboot")
186 luci.util.perror(backup_cmd:format(_keep_pattern()))
187 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
188 luci.http.header('Content-Disposition', 'attachment; filename="backup.tar.gz"')
189 luci.http.prepare_content("application/x-targz")
190 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
192 luci.template.render("admin_system/applyreboot")
193 luci.util.exec("mtd -r erase rootfs_data")
195 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
199 function action_passwd()
200 local p1 = luci.http.formvalue("pwd1")
201 local p2 = luci.http.formvalue("pwd2")
206 stat = luci.sys.user.setpasswd("root", p1)
212 luci.template.render("admin_system/passwd", {stat=stat})
215 function action_reboot()
216 local reboot = luci.http.formvalue("reboot")
217 luci.template.render("admin_system/reboot", {reboot=reboot})
223 function action_sshkeys()
224 local file = "/etc/dropbear/authorized_keys"
225 local data = luci.http.formvalue("data")
230 stat, err = luci.fs.writefile(file, data)
233 local cnt = luci.fs.readfile(file)
235 cnt = luci.util.pcdata(cnt)
238 luci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
241 function action_upgrade()
242 require("luci.model.uci")
245 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
246 local broadcom = os.execute('grep brcm_ /lib/upgrade/platform.sh >/dev/null 2>&1') == 0
247 local tmpfile = "/tmp/firmware.img"
249 local keep_avail = not broadcom
252 luci.http.setfilehandler(
253 function(meta, chunk, eof)
255 file = io.open(tmpfile, "w")
266 local fname = luci.http.formvalue("image")
267 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
269 if plat and fname then
270 ret = luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
273 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
276 function _keep_pattern()
278 local files = luci.model.uci.get_all("luci", "flash_keep")
281 for k,v in pairs(files) do
282 kpattern = kpattern .. " " .. v