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