luci-mod-system: replace builtin opkg support with luci-app-opkg
[project/luci.git] / modules / luci-mod-system / luasrc / controller / admin / system.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Copyright 2008-2011 Jo-Philipp Wich <jow@openwrt.org>
3 -- Licensed to the public under the Apache License 2.0.
4
5 module("luci.controller.admin.system", package.seeall)
6
7 function index()
8 local fs = require "nixio.fs"
9
10 entry({"admin", "system", "system"}, cbi("admin_system/system"), _("System"), 1)
11 entry({"admin", "system", "clock_status"}, post_on({ set = true }, "action_clock_status"))
12
13 entry({"admin", "system", "admin"}, cbi("admin_system/admin"), _("Administration"), 2)
14 entry({"admin", "system", "startup"}, form("admin_system/startup"), _("Startup"), 45)
15 entry({"admin", "system", "crontab"}, form("admin_system/crontab"), _("Scheduled Tasks"), 46)
16
17 if fs.access("/sbin/block") and fs.access("/etc/config/fstab") then
18 entry({"admin", "system", "fstab"}, cbi("admin_system/fstab"), _("Mount Points"), 50)
19 entry({"admin", "system", "fstab", "mount"}, cbi("admin_system/fstab/mount"), nil).leaf = true
20 entry({"admin", "system", "fstab", "swap"}, cbi("admin_system/fstab/swap"), nil).leaf = true
21 end
22
23 local nodes, number = fs.glob("/sys/class/leds/*")
24 if number > 0 then
25 entry({"admin", "system", "leds"}, cbi("admin_system/leds"), _("<abbr title=\"Light Emitting Diode\">LED</abbr> Configuration"), 60)
26 end
27
28 entry({"admin", "system", "flashops"}, call("action_flashops"), _("Backup / Flash Firmware"), 70)
29 entry({"admin", "system", "flashops", "reset"}, post("action_reset"))
30 entry({"admin", "system", "flashops", "backup"}, post("action_backup"))
31 entry({"admin", "system", "flashops", "backupmtdblock"}, post("action_backupmtdblock"))
32 entry({"admin", "system", "flashops", "backupfiles"}, form("admin_system/backupfiles"))
33
34 -- call() instead of post() due to upload handling!
35 entry({"admin", "system", "flashops", "restore"}, call("action_restore"))
36 entry({"admin", "system", "flashops", "sysupgrade"}, call("action_sysupgrade"))
37
38 entry({"admin", "system", "reboot"}, template("admin_system/reboot"), _("Reboot"), 90)
39 entry({"admin", "system", "reboot", "call"}, post("action_reboot"))
40 end
41
42 function action_clock_status()
43 local set = tonumber(luci.http.formvalue("set"))
44 if set ~= nil and set > 0 then
45 local date = os.date("*t", set)
46 if date then
47 luci.sys.call("date -s '%04d-%02d-%02d %02d:%02d:%02d'" %{
48 date.year, date.month, date.day, date.hour, date.min, date.sec
49 })
50 luci.sys.call("/etc/init.d/sysfixtime restart")
51 end
52 end
53
54 luci.http.prepare_content("application/json")
55 luci.http.write_json({ timestring = os.date("%c") })
56 end
57
58 local function image_supported(image)
59 return (os.execute("sysupgrade -T %q >/dev/null" % image) == 0)
60 end
61
62 local function image_checksum(image)
63 return (luci.sys.exec("md5sum %q" % image):match("^([^%s]+)"))
64 end
65
66 local function image_sha256_checksum(image)
67 return (luci.sys.exec("sha256sum %q" % image):match("^([^%s]+)"))
68 end
69
70 local function supports_sysupgrade()
71 return nixio.fs.access("/lib/upgrade/platform.sh")
72 end
73
74 local function supports_reset()
75 return (os.execute([[grep -sq "^overlayfs:/overlay / overlay " /proc/mounts]]) == 0)
76 end
77
78 local function storage_size()
79 local size = 0
80 if nixio.fs.access("/proc/mtd") then
81 for l in io.lines("/proc/mtd") do
82 local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
83 if n == "linux" or n == "firmware" then
84 size = tonumber(s, 16)
85 break
86 end
87 end
88 elseif nixio.fs.access("/proc/partitions") then
89 for l in io.lines("/proc/partitions") do
90 local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
91 if b and n and not n:match('[0-9]') then
92 size = tonumber(b) * 1024
93 break
94 end
95 end
96 end
97 return size
98 end
99
100
101 function action_flashops()
102 --
103 -- Overview
104 --
105 luci.template.render("admin_system/flashops", {
106 reset_avail = supports_reset(),
107 upgrade_avail = supports_sysupgrade()
108 })
109 end
110
111 function action_sysupgrade()
112 local fs = require "nixio.fs"
113 local http = require "luci.http"
114 local image_tmp = "/tmp/firmware.img"
115
116 local fp
117 http.setfilehandler(
118 function(meta, chunk, eof)
119 if not fp and meta and meta.name == "image" then
120 fp = io.open(image_tmp, "w")
121 end
122 if fp and chunk then
123 fp:write(chunk)
124 end
125 if fp and eof then
126 fp:close()
127 end
128 end
129 )
130
131 if not luci.dispatcher.test_post_security() then
132 fs.unlink(image_tmp)
133 return
134 end
135
136 --
137 -- Cancel firmware flash
138 --
139 if http.formvalue("cancel") then
140 fs.unlink(image_tmp)
141 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
142 return
143 end
144
145 --
146 -- Initiate firmware flash
147 --
148 local step = tonumber(http.formvalue("step")) or 1
149 if step == 1 then
150 local force = http.formvalue("force")
151 if image_supported(image_tmp) or force then
152 luci.template.render("admin_system/upgrade", {
153 checksum = image_checksum(image_tmp),
154 sha256ch = image_sha256_checksum(image_tmp),
155 storage = storage_size(),
156 size = (fs.stat(image_tmp, "size") or 0),
157 keep = (not not http.formvalue("keep")),
158 force = (not not http.formvalue("force"))
159 })
160 else
161 fs.unlink(image_tmp)
162 luci.template.render("admin_system/flashops", {
163 reset_avail = supports_reset(),
164 upgrade_avail = supports_sysupgrade(),
165 image_invalid = true
166 })
167 end
168
169 --
170 -- Start sysupgrade flash
171 --
172 elseif step == 2 then
173 local keep = (http.formvalue("keep") == "1") and "" or "-n"
174 local force = (http.formvalue("force") == "1") and "-F" or ""
175 luci.template.render("admin_system/applyreboot", {
176 title = luci.i18n.translate("Flashing..."),
177 msg = luci.i18n.translate("The system is flashing now.<br /> DO NOT POWER OFF THE DEVICE!<br /> Wait a few minutes before you try to reconnect. It might be necessary to renew the address of your computer to reach the device again, depending on your settings."),
178 addr = (#keep > 0) and (#force > 0) and "192.168.1.1" or nil
179 })
180 luci.sys.process.exec({ "/bin/sh", "-c","sleep 1; killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %s %q" %{ keep, force, image_tmp } }, nil, nil, true)
181 end
182 end
183
184 function action_backup()
185 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"'
186 %{ luci.sys.hostname(), os.date("%Y-%m-%d") })
187
188 luci.http.prepare_content("application/x-targz")
189 luci.sys.process.exec({ "/sbin/sysupgrade", "--create-backup", "-" }, luci.http.write)
190 end
191
192 function action_backupmtdblock()
193 local mv = luci.http.formvalue("mtdblockname") or ""
194 local m, n = mv:match('^([^%s%./"]+)/%d+/(%d+)$')
195
196 if not m and n then
197 luci.http.status(400, "Bad Request")
198 return
199 end
200
201 luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s-%s.bin"'
202 %{ luci.sys.hostname(), m, os.date("%Y-%m-%d") })
203
204 luci.http.prepare_content("application/octet-stream")
205 luci.sys.process.exec({ "/bin/dd", "if=/dev/mtd%s" % n, "conv=fsync,notrunc" }, luci.http.write)
206 end
207
208 function action_restore()
209 local fs = require "nixio.fs"
210 local http = require "luci.http"
211 local archive_tmp = "/tmp/restore.tar.gz"
212
213 local fp
214 http.setfilehandler(
215 function(meta, chunk, eof)
216 if not fp and meta and meta.name == "archive" then
217 fp = io.open(archive_tmp, "w")
218 end
219 if fp and chunk then
220 fp:write(chunk)
221 end
222 if fp and eof then
223 fp:close()
224 end
225 end
226 )
227
228 if not luci.dispatcher.test_post_security() then
229 fs.unlink(archive_tmp)
230 return
231 end
232
233 local upload = http.formvalue("archive")
234 if upload and #upload > 0 then
235 if os.execute("gunzip -t %q >/dev/null 2>&1" % archive_tmp) == 0 then
236 luci.template.render("admin_system/applyreboot")
237 os.execute("tar -C / -xzf %q >/dev/null 2>&1" % archive_tmp)
238 luci.sys.reboot()
239 else
240 luci.template.render("admin_system/flashops", {
241 reset_avail = supports_reset(),
242 upgrade_avail = supports_sysupgrade(),
243 backup_invalid = true
244 })
245 end
246 return
247 end
248
249 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
250 end
251
252 function action_reset()
253 if supports_reset() then
254 luci.template.render("admin_system/applyreboot", {
255 title = luci.i18n.translate("Erasing..."),
256 msg = luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
257 addr = "192.168.1.1"
258 })
259
260 luci.sys.process.exec({ "/bin/sh", "-c", "sleep 1; killall dropbear uhttpd; sleep 1; jffs2reset -y && reboot" }, nil, nil, true)
261 return
262 end
263
264 http.redirect(luci.dispatcher.build_url('admin/system/flashops'))
265 end
266
267 function action_passwd()
268 local p1 = luci.http.formvalue("pwd1")
269 local p2 = luci.http.formvalue("pwd2")
270 local stat = nil
271
272 if p1 or p2 then
273 if p1 == p2 then
274 stat = luci.sys.user.setpasswd("root", p1)
275 else
276 stat = 10
277 end
278 end
279
280 luci.template.render("admin_system/passwd", {stat=stat})
281 end
282
283 function action_reboot()
284 luci.sys.reboot()
285 end