Chinaunix首页 | 论坛 | 博客
  • 博客访问: 864361
  • 博文数量: 286
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1841
  • 用 户 组: 普通用户
  • 注册时间: 2015-05-09 16:26
文章分类

全部博文(286)

文章存档

2016年(38)

2015年(248)

我的朋友

分类: LINUX

2015-06-16 15:14:19


点击(此处)折叠或打开

  1. function action_flashops()
  2. local e=require"luci.sys"
  3. local t=require"nixio.fs"
  4. local i=t.access("/lib/upgrade/platform.sh")
  5. local o=os.execute([[grep '"rootfs_data"' /proc/mtd >/dev/null 2>&1]])==0
  6. local s="tar -xzC/ >/dev/null 2>&1"
  7. local r="sysupgrade --create-backup - 2>/dev/null"
  8. local e="/tmp/firmware.img"
  9. local function n()
  10. return(os.execute("sysupgrade -T %q >/dev/null"%e)==0)
  11. end


  12. local function h()
  13. return(luci.sys.exec("md5sum %q"%e):match("^([^%s]+)"))
  14. end

  15. local function d()

  16. local e=0

  17. if t.access("/proc/mtd")then
  18. for t in io.lines("/proc/mtd")do
  19. local o,a,o,t=t:match('^([^%s]+)%s+([^%s]+)%s+([^%s]+)%s+"([^%s]+)"')

  20. if t=="linux"or t=="firmware"then
  21. e=tonumber(a,16)
  22. break

  23. end

  24. end
  25. elseif t.access("/proc/partitions")then
  26. for t in io.lines("/proc/partitions")do
  27. local o,o,a,t=t:match('^%s*(%d+)%s+(%d+)%s+([^%s]+)%s+([^%s]+)')
  28. if a and t and not t:match('[0-9]')then
  29. e=tonumber(a)*1024
  30. break
  31. end
  32. end
  33. end
  34. return e
  35. end
  36. local a

  37. luci.http.setfilehandler(
  38. function(o,t,i)
  39. if not a then
  40. if o and o.name=="image"then
  41. a=io.open(e,"w")
  42. else
  43. a=io.popen(s,"w")
  44. end
  45. end
  46. if t then
  47. a:write(t)
  48. end
  49. if i then
  50. a:close()
  51. end
  52. end
  53. )

  54. if luci.http.formvalue("backup")then
  55. local e=ltn12_popen(r)
  56. luci.http.header('Content-Disposition','attachment; filename="backup-%s-%s.tar.gz"'%{
  57. luci.sys.hostname(),os.date("%Y-%m-%d")})
  58. luci.http.prepare_content("application/x-targz")
  59. luci.ltn12.pump.all(e,luci.http.write)
  60. elseif luci.http.formvalue("restore")then
  61. local e=luci.http.formvalue("archive")
  62. if e and#e>0 then
  63. luci.template.render("admin_system/applyreboot")
  64. luci.sys.reboot()
  65. end
  66. elseif luci.http.formvalue("image")or luci.http.formvalue("step")then
  67. local a=tonumber(luci.http.formvalue("step")or 1)
  68. if a==1 then
  69. if n()then
  70. luci.template.render("admin_system/upgrade",{
  71. checksum=h(),
  72. storage=d(),
  73. size=(t.stat(e,"size")or 0),
  74. keep=(not not luci.http.formvalue("keep"))
  75. })
  76. else
  77. t.unlink(e)
  78. luci.template.render("admin_system/flashops",{
  79. reset_avail=o,
  80. upgrade_avail=i,
  81. image_invalid=true
  82. })
  83. end
  84. elseif a==2 then
  85. local t=(luci.http.formvalue("keep")=="1")and""or"-n"
  86. luci.template.render("admin_system/applyreboot",{
  87. title=luci.i18n.translate("Flashing..."),
  88. msg=luci.i18n.translate("The system is flashing now.
    DO NOT POWER OFF THE DEVICE!
    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."
    ),
  89. addr=(#t>0)and"192.168.1.1"or nil
  90. })
  91. fork_exec("killall dropbear uhttpd; sleep 1; /sbin/sysupgrade %s %q"%{t,e})
  92. end
  93. elseif o and luci.http.formvalue("reset")then
  94. luci.template.render("admin_system/applyreboot",{
  95. title=luci.i18n.translate("Erasing..."),
  96. msg=luci.i18n.translate("The system is erasing the configuration partition now and will reboot itself when finished."),
  97. addr="192.168.1.1"
  98. })
  99. fork_exec("killall dropbear uhttpd; sleep 1; mtd -r erase rootfs_data")
  100. else
  101. luci.template.render("admin_system/flashops",{
  102. reset_avail=o,
  103. upgrade_avail=i
  104. })
  105. end
  106. end

阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~