system.lua 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. --[[
  2. LuCI - Lua Configuration Interface
  3. Copyright 2008 Steven Barth <steven@midlink.org>
  4. Copyright 2008 Jo-Philipp Wich <xm@leipzig.freifunk.net>
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. $Id$
  10. ]]--
  11. module("luci.controller.admin.system", package.seeall)
  12. function index()
  13. entry({"admin", "passwd"}, cbi("admin/passwd"), _("Admin Password"), 10)
  14. entry({"admin", "backup"}, call("action_backup"), _("Backup / Restore"), 80)
  15. entry({"admin", "upgrade"}, call("action_upgrade"), _("Flash Firmware"), 90)
  16. entry({"admin", "reboot"}, call("action_reboot"), _("Reboot"), 100)
  17. end
  18. function action_backup()
  19. local reset_avail = os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]]) == 0
  20. local restore_cmd = "gunzip | tar -xC/ >/dev/null 2>&1"
  21. local backup_cmd = "tar -c %s | gzip 2>/dev/null"
  22. local restore_fpi
  23. luci.http.setfilehandler(
  24. function(meta, chunk, eof)
  25. if not restore_fpi then
  26. restore_fpi = io.popen(restore_cmd, "w")
  27. end
  28. if chunk then
  29. restore_fpi:write(chunk)
  30. end
  31. if eof then
  32. restore_fpi:close()
  33. end
  34. end
  35. )
  36. local upload = luci.http.formvalue("archive")
  37. local backup = luci.http.formvalue("backup")
  38. local reset = reset_avail and luci.http.formvalue("reset")
  39. if upload and #upload > 0 then
  40. luci.template.render("admin/applyreboot")
  41. luci.sys.reboot()
  42. elseif backup then
  43. local reader = ltn12_popen(backup_cmd:format(_keep_pattern()))
  44. luci.http.header('Content-Disposition', 'attachment; filename="backup-%s-%s.tar.gz"' % {
  45. luci.sys.hostname(), os.date("%Y-%m-%d")})
  46. luci.http.prepare_content("application/x-targz")
  47. luci.ltn12.pump.all(reader, luci.http.write)
  48. elseif reset then
  49. luci.template.render("admin/applyreboot")
  50. luci.util.exec("mtd -r erase rootfs_data")
  51. else
  52. luci.template.render("admin/backup", {reset_avail = reset_avail})
  53. end
  54. end
  55. function action_reboot()
  56. local reboot = luci.http.formvalue("reboot")
  57. luci.template.render("admin/reboot", {reboot=reboot})
  58. if reboot then
  59. luci.sys.reboot()
  60. end
  61. end
  62. function action_upgrade()
  63. require("luci.model.uci")
  64. local tmpfile = "/tmp/firmware.img"
  65. local function image_supported()
  66. -- XXX: yay...
  67. return ( 0 == os.execute(
  68. ". /lib/functions.sh; " ..
  69. "include /lib/upgrade; " ..
  70. "platform_check_image %q >/dev/null"
  71. % tmpfile
  72. ) )
  73. end
  74. local function image_checksum()
  75. return (luci.sys.exec("md5sum %q" % tmpfile):match("^([^%s]+)"))
  76. end
  77. local function storage_size()
  78. local size = 0
  79. if nixio.fs.access("/proc/mtd") then
  80. for l in io.lines("/proc/mtd") do
  81. local d, s, e, n = l:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')
  82. if n == "linux" then
  83. size = tonumber(s, 16)
  84. break
  85. end
  86. end
  87. elseif nixio.fs.access("/proc/partitions") then
  88. for l in io.lines("/proc/partitions") do
  89. local x, y, b, n = l:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
  90. if b and n and not n:match('[0-9]') then
  91. size = tonumber(b) * 1024
  92. break
  93. end
  94. end
  95. end
  96. return size
  97. end
  98. -- Install upload handler
  99. local file
  100. luci.http.setfilehandler(
  101. function(meta, chunk, eof)
  102. if not nixio.fs.access(tmpfile) and not file and chunk and #chunk > 0 then
  103. file = io.open(tmpfile, "w")
  104. end
  105. if file and chunk then
  106. file:write(chunk)
  107. end
  108. if file and eof then
  109. file:close()
  110. end
  111. end
  112. )
  113. -- Determine state
  114. local keep_avail = true
  115. local step = tonumber(luci.http.formvalue("step") or 1)
  116. local has_image = nixio.fs.access(tmpfile)
  117. local has_support = image_supported()
  118. local has_platform = nixio.fs.access("/lib/upgrade/platform.sh")
  119. local has_upload = luci.http.formvalue("image")
  120. -- This does the actual flashing which is invoked inside an iframe
  121. -- so don't produce meaningful errors here because the the
  122. -- previous pages should arrange the stuff as required.
  123. if step == 4 then
  124. if has_platform and has_image and has_support then
  125. -- Mimetype text/plain
  126. luci.http.prepare_content("text/plain")
  127. luci.http.write("Starting luci-flash...\n")
  128. -- Now invoke sysupgrade
  129. local keepcfg = keep_avail and luci.http.formvalue("keepcfg") == "1"
  130. local flash = ltn12_popen("/sbin/luci-flash %s %q" %{
  131. keepcfg and "-k %q" % _keep_pattern() or "", tmpfile
  132. })
  133. luci.ltn12.pump.all(flash, luci.http.write)
  134. -- Make sure the device is rebooted
  135. luci.sys.reboot()
  136. end
  137. --
  138. -- This is step 1-3, which does the user interaction and
  139. -- image upload.
  140. --
  141. -- Step 1: file upload, error on unsupported image format
  142. elseif not has_image or not has_support or step == 1 then
  143. -- If there is an image but user has requested step 1
  144. -- or type is not supported, then remove it.
  145. if has_image then
  146. nixio.fs.unlink(tmpfile)
  147. end
  148. luci.template.render("admin/upgrade", {
  149. step=1,
  150. bad_image=(has_image and not has_support or false),
  151. keepavail=keep_avail,
  152. supported=has_platform
  153. } )
  154. -- Step 2: present uploaded file, show checksum, confirmation
  155. elseif step == 2 then
  156. luci.template.render("admin/upgrade", {
  157. step=2,
  158. checksum=image_checksum(),
  159. filesize=nixio.fs.stat(tmpfile).size,
  160. flashsize=storage_size(),
  161. keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
  162. } )
  163. -- Step 3: load iframe which calls the actual flash procedure
  164. elseif step == 3 then
  165. luci.template.render("admin/upgrade", {
  166. step=3,
  167. keepconfig=(keep_avail and luci.http.formvalue("keepcfg") == "1")
  168. } )
  169. end
  170. end
  171. function _keep_pattern()
  172. local kpattern = ""
  173. local files = luci.model.uci.cursor():get_all("luci", "flash_keep")
  174. if files then
  175. kpattern = ""
  176. for k, v in pairs(files) do
  177. if k:sub(1,1) ~= "." and nixio.fs.glob(v)() then
  178. kpattern = kpattern .. " " .. v
  179. end
  180. end
  181. end
  182. return kpattern
  183. end
  184. function ltn12_popen(command)
  185. local fdi, fdo = nixio.pipe()
  186. local pid = nixio.fork()
  187. if pid > 0 then
  188. fdo:close()
  189. local close
  190. return function()
  191. local buffer = fdi:read(2048)
  192. local wpid, stat = nixio.waitpid(pid, "nohang")
  193. if not close and wpid and stat == "exited" then
  194. close = true
  195. end
  196. if buffer and #buffer > 0 then
  197. return buffer
  198. elseif close then
  199. fdi:close()
  200. return nil
  201. end
  202. end
  203. elseif pid == 0 then
  204. nixio.dup(fdo, nixio.stdout)
  205. fdi:close()
  206. fdo:close()
  207. nixio.exec("/bin/sh", "-c", command)
  208. end
  209. end