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)
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)
28 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), i18n("leds", "LEDs"), 60)
29 entry({"admin", "system", "backup"}, call("action_backup"), i18n("a_s_backup"), 70)
30 entry({"admin", "system", "upgrade"}, call("action_upgrade"), i18n("a_s_flash"), 80)
31 entry({"admin", "system", "reboot"}, call("action_reboot"), i18n("reboot"), 90)
34 function action_packages()
35 local ipkg = require("luci.model.ipkg")
37 local submit = luci.http.formvalue("submit")
41 local query = luci.http.formvalue("query")
42 query = (query ~= '') and query or nil
45 -- Packets to be installed
46 local install = submit and luci.http.formvaluetable("install")
49 local url = luci.http.formvalue("url")
50 if url and url ~= '' and submit then
59 for k, v in pairs(install) do
60 void, install[k] = ipkg.install(k)
66 local remove = submit and luci.http.formvaluetable("remove")
68 for k, v in pairs(remove) do
69 void, remove[k] = ipkg.remove(k)
75 local update = luci.http.formvalue("update")
77 void, update = ipkg.update()
81 -- Upgrade all packets
82 local upgrade = luci.http.formvalue("upgrade")
84 void, upgrade = ipkg.upgrade()
89 local info = luci.model.ipkg.info(query and "*"..query.."*")
93 -- Sort after status and name
94 for k, v in pairs(info) do
96 for i, j in pairs(pkgs) do
97 local vins = (v.Status and v.Status.installed)
98 local jins = (j.Status and j.Status.installed)
104 if j.Package > v.Package then
110 table.insert(pkgs, x+1, v)
113 luci.template.render("admin_system/packages", {pkgs=pkgs, query=query,
114 install=install, remove=remove, update=update, upgrade=upgrade})
117 function action_backup()
118 local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
119 local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
120 local backup_cmd = "tar -c %s | gzip 2>/dev/null"
123 luci.http.setfilehandler(
124 function(meta, chunk, eof)
125 if not restore_fpi then
126 restore_fpi = io.popen(restore_cmd, "w")
129 restore_fpi:write(chunk)
137 local upload = luci.http.formvalue("archive")
138 local backup = luci.http.formvalue("backup")
139 local reset = reset_avail and luci.http.formvalue("reset")
141 if upload and #upload > 0 then
142 luci.template.render("admin_system/applyreboot")
145 luci.util.perror(backup_cmd:format(_keep_pattern()))
146 local backup_fpi = io.popen(backup_cmd:format(_keep_pattern()), "r")
147 luci.http.header('Content-Disposition', 'attachment; filename="backup.tar.gz"')
148 luci.http.prepare_content("application/x-targz")
149 luci.ltn12.pump.all(luci.ltn12.source.file(backup_fpi), luci.http.write)
151 luci.template.render("admin_system/applyreboot")
152 luci.util.exec("mtd -r erase rootfs_data")
154 luci.template.render("admin_system/backup", {reset_avail = reset_avail})
158 function action_passwd()
159 local p1 = luci.http.formvalue("pwd1")
160 local p2 = luci.http.formvalue("pwd2")
165 stat = luci.sys.user.setpasswd("root", p1)
171 luci.template.render("admin_system/passwd", {stat=stat})
174 function action_reboot()
175 local reboot = luci.http.formvalue("reboot")
176 luci.template.render("admin_system/reboot", {reboot=reboot})
182 function action_upgrade()
183 require("luci.model.uci")
184 local mtdow = require "luci.sys.mtdow"
185 local writer = mtdow.native_writer()
186 local blocks = writer and writer.blocks
187 local ltn12 = require "luci.ltn12"
192 local filepat = "/tmp/mtdblock.%s"
193 local kfile = "/tmp/mtdappend.tgz"
195 local keep_avail = false
197 for k, block in pairs(blocks) do
198 if block.write == mtdow.WRITE_COMBINED
199 or block.write == mtdow.WRITE_EMULATED then
205 luci.http.setfilehandler(
206 function(meta, chunk, eof)
207 if not meta or not blocks or not blocks[meta.name] then
210 if not uploads[meta.name] then
211 uploads[meta.name] = io.open(filepat % meta.name, "w")
214 uploads[meta.name]:write(chunk)
217 uploads[meta.name]:close()
218 uploads[meta.name] = filepat % meta.name
223 luci.http.formvalue() -- Parse uploads
224 local keepcfg = keep_avail and luci.http.formvalue("keepcfg")
226 local function _kfile()
227 luci.fs.unlink(kfile)
230 local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
233 for k, v in pairs(files) do
234 if k:sub(1,1) ~= "." and luci.fs.glob(v) then
235 kpattern = kpattern .. " '" .. v .. "'"
240 local stat = os.execute("tar czf '%s' %s >/dev/null 2>&1" % {kfile, kpattern})
241 return stat == 0 and kfile
244 for name, file in pairs(uploads) do
245 flash[name] = function()
246 local imgstream = ltn12.source.file(io.open(file))
247 return pcall(writer.write_block, writer,
248 name, imgstream, keepcfg and _kfile())
254 luci.template.render("admin_system/upgrade", {blocks=blocks,
255 flash=flash, keep_avail=keep_avail, reboot=reboot})
257 local pid = posix.fork()
259 os.execute("sleep 1")
260 posix.execp("reboot")