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"}, form("admin_system/ipkg"), i18n("a_s_p_ipkg"))
23 entry({"admin", "system", "passwd"}, form("admin_system/passwd"), i18n("a_s_changepw"), 20)
24 entry({"admin", "system", "sshkeys"}, form("admin_system/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_packages()
34 local ipkg = require("luci.model.ipkg")
36 local submit = luci.http.formvalue("submit")
40 local query = luci.http.formvalue("query")
41 query = (query ~= '') and query or nil
44 -- Packets to be installed
45 local install = submit and luci.http.formvaluetable("install")
48 local url = luci.http.formvalue("url")
49 if url and url ~= '' and submit then
58 for k, v in pairs(install) do
59 void, install[k] = ipkg.install(k)
65 local remove = submit and luci.http.formvaluetable("remove")
67 for k, v in pairs(remove) do
68 void, remove[k] = ipkg.remove(k)
74 local update = luci.http.formvalue("update")
76 void, update = ipkg.update()
80 -- Upgrade all packets
81 local upgrade = luci.http.formvalue("upgrade")
83 void, upgrade = ipkg.upgrade()
88 local info = luci.model.ipkg.info(query)
92 -- Sort after status and name
93 for k, v in pairs(info) do
95 for i, j in pairs(pkgs) do
96 local vins = (v.Status and v.Status.installed)
97 local jins = (j.Status and j.Status.installed)
103 if j.Package > v.Package then
109 table.insert(pkgs, x+1, v)
112 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
113 install=install, remove=remove, update=update, upgrade=upgrade})
116 function action_backup()
117 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
118 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
119 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
122 luci.http.setfilehandler(
123 function(meta, chunk, eof)
124 if not restore_fpi then
125 restore_fpi = io.popen(restore_cmd, "w")
128 restore_fpi:write(chunk)
136 local upload = luci.http.formvalue("archive")
137 local backup = luci.http.formvalue("backup")
138 local reset = reset_avail and luci.http.formvalue("reset")
140 if upload and #upload > 0 then
141 luci.template.render("admin_system/applyreboot")
144 luci.util.perror(backup_cmd:format(_keep_pattern()))
145 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
146 luci.http.header('Content-Disposition', 'attachment; filename="backup.tar.gz"')
147 luci.http.prepare_content("application/x-targz")
148 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
150 luci.template.render("admin_system/applyreboot")
151 luci.util.exec("mtd -r erase rootfs_data")
153 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
157 function action_passwd()
158 local p1 = luci.http.formvalue("pwd1")
159 local p2 = luci.http.formvalue("pwd2")
164 stat = luci.sys.user.setpasswd("root", p1)
170 luci.template.render("admin_system/passwd", {stat=stat})
173 function action_reboot()
174 local reboot = luci.http.formvalue("reboot")
175 luci.template.render("admin_system/reboot", {reboot=reboot})
181 function action_upgrade()
182 require("luci.model.uci")
185 local plat = luci.fs.mtime("/lib/upgrade/platform.sh")
186 local tmpfile = "/tmp/firmware.img"
188 local keep_avail = true
191 luci.http.setfilehandler(
192 function(meta, chunk, eof)
194 file = io.open(tmpfile, "w")
205 local fname = luci.http.formvalue("image")
206 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
208 if plat and fname then
209 ret = luci.sys.flash(tmpfile, keepcfg and _keep_pattern())
212 luci.template.render("admin_system/upgrade", {sysupgrade=plat, ret=ret, keep_avail=keep_avail})
215 function _keep_pattern()
217 local files = luci.model.uci.get_all("luci", "flash_keep")
220 for k,v in pairs(files) do
221 kpattern = kpattern .. " " .. v