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"}, alias("admin", "system", "system"), i18n("system"), 30).index = true
21 entry({"admin", "system", "system"}, cbi("admin_system/system"), i18n("system"), 1)
22 entry({"admin", "system", "packages"}, call("action_packages"), i18n("a_s_packages"), 10)
23 entry({"admin", "system", "packages", "ipkg"}, form("admin_system/ipkg"), i18n("a_s_p_ipkg"))
24 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("a_s_changepw"), 20)
25 entry({"admin", "system", "sshkeys"}, form("admin_system/sshkeys"), i18n("a_s_sshkeys"), 30)
26 entry({"admin", "system", "processes"}, form("admin_system/processes"), i18n("process_head"), 45)
27 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), i18n("a_s_fstab"), 50)
29 if luci.fs.isdirectory("/sys/class/leds") then
30 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
33 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
34 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 80)
35 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
38 function action_packages()
39 local ipkg = require("luci.model.ipkg")
41 local submit = luci.http.formvalue("submit")
45 local query = luci.http.formvalue("query")
46 query = (query ~= '') and query or nil
49 -- Packets to be installed
50 local install = submit and luci.http.formvaluetable("install")
53 local url = luci.http.formvalue("url")
54 if url and url ~= '' and submit then
63 for k, v in pairs(install) do
64 void, install[k] = ipkg.install(k)
70 local remove = submit and luci.http.formvaluetable("remove")
72 for k, v in pairs(remove) do
73 void, remove[k] = ipkg.remove(k)
79 local update = luci.http.formvalue("update")
81 void, update = ipkg.update()
85 -- Upgrade all packets
86 local upgrade = luci.http.formvalue("upgrade")
88 void, upgrade = ipkg.upgrade()
93 local info = luci.model.ipkg.info(query and "*"..query.."*")
97 -- Sort after status and name
98 for k, v in pairs(info) do
100 for i, j in pairs(pkgs) do
101 local vins = (v.Status and v.Status.installed)
102 local jins = (j.Status and j.Status.installed)
108 if j.Package > v.Package then
114 table.insert(pkgs, x+1, v)
117 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
118 install=install, remove=remove, update=update, upgrade=upgrade})
121 function action_backup()
122 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
123 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
124 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
127 luci.http.setfilehandler(
128 function(meta, chunk, eof)
129 if not restore_fpi then
130 restore_fpi = io.popen(restore_cmd, "w")
133 restore_fpi:write(chunk)
141 local upload = luci.http.formvalue("archive")
142 local backup = luci.http.formvalue("backup")
143 local reset = reset_avail and luci.http.formvalue("reset")
145 if upload and #upload > 0 then
146 luci.template.render("admin_system/applyreboot")
149 luci.util.perror(backup_cmd:format(_keep_pattern()))
150 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
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(luci.ltn12.source.file(backup_fpi), luci.http.write)
156 luci.template.render("admin_system/applyreboot")
157 luci.util.exec("mtd -r erase rootfs_data")
159 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
163 function action_passwd()
164 local p1 = luci.http.formvalue("pwd1")
165 local p2 = luci.http.formvalue("pwd2")
170 stat = luci.sys.user.setpasswd("root", p1)
176 luci.template.render("admin_system/passwd", {stat=stat})
179 function action_reboot()
180 local reboot = luci.http.formvalue("reboot")
181 luci.template.render("admin_system/reboot", {reboot=reboot})
187 function action_upgrade()
188 require("luci.model.uci")
191 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
192 local tmpfile = "/tmp/firmware.img"
193 local keep_avail = true
196 luci.http.setfilehandler(
197 function(meta, chunk, eof)
199 file = io.open(tmpfile, "w")
210 local fname = luci.http.formvalue("image")
211 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
213 if plat and fname then
215 return luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
219 luci.http.prepare_content("text/html")
220 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
223 function _keep_pattern()
225 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
228 for k, v in pairs(files) do
229 if k:sub(1,1) ~= "." and luci.fs.glob(v) then
230 kpattern = kpattern .. " " .. v