startup.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Copyright 2010-2012 Jo-Philipp Wich <jow@openwrt.org>
  3. -- Copyright 2010 Manuel Munz <freifunk at somakoma dot de>
  4. -- Licensed to the public under the Apache License 2.0.
  5. local fs = require "nixio.fs"
  6. local sys = require "luci.sys"
  7. local inits = { }
  8. for _, name in ipairs(sys.init.names()) do
  9. local index = sys.init.index(name)
  10. local enabled = sys.init.enabled(name)
  11. if index < 255 then
  12. inits["%02i.%s" % { index, name }] = {
  13. name = name,
  14. index = tostring(index),
  15. enabled = enabled
  16. }
  17. end
  18. end
  19. m = SimpleForm("initmgr", translate("Initscripts"), translate("You can enable or disable installed init scripts here. Changes will applied after a device reboot.<br /><strong>Warning: If you disable essential init scripts like \"network\", your device might become inaccessible!</strong>"))
  20. m.reset = false
  21. m.submit = false
  22. s = m:section(Table, inits)
  23. i = s:option(DummyValue, "index", translate("Start priority"))
  24. n = s:option(DummyValue, "name", translate("Initscript"))
  25. e = s:option(Button, "endisable", translate("Enable/Disable"))
  26. e.render = function(self, section, scope)
  27. if inits[section].enabled then
  28. self.title = translate("Enabled")
  29. self.inputstyle = "save"
  30. else
  31. self.title = translate("Disabled")
  32. self.inputstyle = "reset"
  33. end
  34. Button.render(self, section, scope)
  35. end
  36. e.write = function(self, section)
  37. if inits[section].enabled then
  38. inits[section].enabled = false
  39. return sys.init.disable(inits[section].name)
  40. else
  41. inits[section].enabled = true
  42. return sys.init.enable(inits[section].name)
  43. end
  44. end
  45. start = s:option(Button, "start", translate("Start"))
  46. start.inputstyle = "apply"
  47. start.write = function(self, section)
  48. sys.call("/etc/init.d/%s %s >/dev/null" %{ inits[section].name, self.option })
  49. end
  50. restart = s:option(Button, "restart", translate("Restart"))
  51. restart.inputstyle = "reload"
  52. restart.write = start.write
  53. stop = s:option(Button, "stop", translate("Stop"))
  54. stop.inputstyle = "remove"
  55. stop.write = start.write
  56. f = SimpleForm("rc", translate("Local Startup"),
  57. translate("This is the content of /etc/rc.local. Insert your own commands here (in front of 'exit 0') to execute them at the end of the boot process."))
  58. t = f:field(TextValue, "rcs")
  59. t.rmempty = true
  60. t.rows = 20
  61. function t.cfgvalue()
  62. return fs.readfile("/etc/rc.local") or ""
  63. end
  64. function f.handle(self, state, data)
  65. if state == FORM_VALID then
  66. if data.rcs then
  67. fs.writefile("/etc/rc.local", data.rcs:gsub("\r\n", "\n"))
  68. end
  69. end
  70. return true
  71. end
  72. return m, f