system.lua 6.3 KB

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