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", "System"), 30)
21 entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages", "Paketverwaltung"), 10)
22 entry({"admin", "system", "packages", "ipkg"}, call("action_ipkg"), i18n("a_s_p_ipkg", "IPKG-Konfiguration"))
23 entry({"admin", "system", "passwd"}, call("action_passwd"), i18n("a_s_changepw", "Passwort ändern"), 20)
24 entry({"admin", "system", "sshkeys"}, call("action_sshkeys"), i18n("a_s_sshkeys", "SSH-Schlüssel"), 30)
25 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system", "System"), 40)
26 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab", "Einhängepunkte"), 50)
27 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 60)
28 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash", "Firmwareupgrade"), 70)
29 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot", "Neu starten"), 80)
32 function action_editor()
33 local file = luci.http.formvalue("file", "")
34 local data = luci.http.formvalue("data")
40 stat, err = luci.fs.writefile(file, data)
44 err = luci.util.split(err, " ")
46 msg = table.concat(err, " ")
49 local cnt, err = luci.fs.readfile(file)
51 cnt = luci.util.pcdata(cnt)
53 luci.template.render("admin_system/editor", {fn=file, cnt=cnt, msg=msg})
56 function action_ipkg()
57 local file = "/etc/ipkg.conf"
58 local data = luci.http.formvalue("data")
63 stat, err = luci.fs.writefile(file, data)
66 local cnt = luci.fs.readfile(file)
68 cnt = luci.util.pcdata(cnt)
71 luci.template.render("admin_system/ipkg", {cnt=cnt, msg=err})
74 function action_packages()
75 local ipkg = require("luci.model.ipkg")
77 local submit = luci.http.formvalue("submit")
81 local query = luci.http.formvalue("query")
82 query = (query ~= '') and query or nil
85 -- Packets to be installed
86 local install = submit and luci.http.formvaluetable("install")
89 local url = luci.http.formvalue("url")
90 if url and url ~= '' and submit then
99 for k, v in pairs(install) do
100 void, install[k] = ipkg.install(k)
106 local remove = submit and luci.http.formvaluetable("remove")
108 for k, v in pairs(remove) do
109 void, remove[k] = ipkg.remove(k)
114 -- Update all packets
115 local update = luci.http.formvalue("update")
117 void, update = ipkg.update()
121 -- Upgrade all packets
122 local upgrade = luci.http.formvalue("upgrade")
124 void, upgrade = ipkg.upgrade()
129 local info = luci.model.ipkg.info(query)
133 -- Sort after status and name
134 for k, v in pairs(info) do
136 for i, j in pairs(pkgs) do
137 local vins = (v.Status and v.Status.installed)
138 local jins = (j.Status and j.Status.installed)
144 if j.Package > v.Package then
150 table.insert(pkgs, x+1, v)
153 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
154 install=install, remove=remove, update=update, upgrade=upgrade})
157 function action_backup()
158 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
159 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
160 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
163 luci.http.setfilehandler(
164 function(meta, chunk, eof)
165 if not restore_fpi then
166 restore_fpi = io.popen(restore_cmd, "w")
169 restore_fpi:write(chunk)
177 local upload = luci.http.formvalue("archive")
178 local backup = luci.http.formvalue("backup")
179 local reset = reset_avail and luci.http.formvalue("reset")
181 if upload and #upload > 0 then
182 luci.template.render("admin_system/applyreboot")
185 luci.util.perror(backup_cmd:format(_keep_pattern()))
186 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
187 luci.http.header('Content-Disposition', 'attachment; filename="backup.tar.gz"')
188 luci.http.prepare_content("application/x-targz")
189 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
191 luci.template.render("admin_system/applyreboot")
192 luci.util.exec("mtd -r erase rootfs_data")
194 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
198 function action_passwd()
199 local p1 = luci.http.formvalue("pwd1")
200 local p2 = luci.http.formvalue("pwd2")
205 stat = luci.sys.user.setpasswd("root", p1)
211 luci.template.render("admin_system/passwd", {stat=stat})
214 function action_reboot()
215 local reboot = luci.http.formvalue("reboot")
216 luci.template.render("admin_system/reboot", {reboot=reboot})
222 function action_sshkeys()
223 local file = "/etc/dropbear/authorized_keys"
224 local data = luci.http.formvalue("data")
229 stat, err = luci.fs.writefile(file, data)
232 local cnt = luci.fs.readfile(file)
234 cnt = luci.util.pcdata(cnt)
237 luci.template.render("admin_system/sshkeys", {cnt=cnt, msg=err})
240 function action_upgrade()
241 require("luci.model.uci")
244 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
245 local broadcom = os.execute('grep brcm_ /lib/upgrade/platform.sh >/dev/null 2>&1') == 0
246 local tmpfile = "/tmp/firmware.img"
248 local keep_avail = not broadcom
251 luci.http.setfilehandler(
252 function(meta, chunk, eof)
254 file = io.open(tmpfile, "w")
265 local fname = luci.http.formvalue("image")
266 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
268 if plat and fname then
269 ret = luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
272 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
275 function _keep_pattern()
277 local files = luci.model.uci.get_all("luci", "flash_keep")
280 for k,v in pairs(files) do
281 kpattern = kpattern .. " " .. v