fstab.lua 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. -- Copyright 2008 Steven Barth <steven@midlink.org>
  2. -- Licensed to the public under the Apache License 2.0.
  3. require("luci.tools.webadmin")
  4. local fs = require "nixio.fs"
  5. local util = require "nixio.util"
  6. local tp = require "luci.template.parser"
  7. local block = io.popen("block info", "r")
  8. local ln, dev, devices = nil, nil, {}
  9. repeat
  10. ln = block:read("*l")
  11. dev = ln and ln:match("^/dev/(.-):")
  12. if dev then
  13. local e, s, key, val = { }
  14. for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
  15. e[key:lower()] = val
  16. devices[val] = e
  17. end
  18. s = tonumber((fs.readfile("/sys/class/block/%s/size" % dev)))
  19. e.dev = "/dev/%s" % dev
  20. e.size = s and math.floor(s / 2048)
  21. devices[e.dev] = e
  22. end
  23. until not ln
  24. block:close()
  25. m = Map("fstab", translate("Mount Points"))
  26. s = m:section(TypedSection, "global", translate("Global Settings"))
  27. s.addremove = false
  28. s.anonymous = true
  29. detect = s:option(Button, "block_detect", translate("Generate Config"), translate("Find all currently attached filesystems and swap and replace configuration with defaults based on what was detected"))
  30. detect.inputstyle = "reload"
  31. detect.write = function(self, section)
  32. luci.sys.call("block detect >/etc/config/fstab")
  33. luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
  34. end
  35. o = s:option(Flag, "anon_swap", translate("Anonymous Swap"), translate("Mount swap not specifically configured"))
  36. o.default = o.disabled
  37. o.rmempty = false
  38. o = s:option(Flag, "anon_mount", translate("Anonymous Mount"), translate("Mount filesystems not specifically configured"))
  39. o.default = o.disabled
  40. o.rmempty = false
  41. o = s:option(Flag, "auto_swap", translate("Automount Swap"), translate("Automatically mount swap on hotplug"))
  42. o.default = o.enabled
  43. o.rmempty = false
  44. o = s:option(Flag, "auto_mount", translate("Automount Filesystem"), translate("Automatically mount filesystems on hotplug"))
  45. o.default = o.enabled
  46. o.rmempty = false
  47. o = s:option(Flag, "check_fs", translate("Check fileystems before mount"), translate("Automatically check filesystem for errors before mounting"))
  48. o.default = o.disabled
  49. o.rmempty = false
  50. local mounts = luci.sys.mounts()
  51. local non_system_mounts = {}
  52. for rawmount, val in pairs(mounts) do
  53. if (string.find(val.mountpoint, "/tmp/.jail") == nil) then
  54. repeat
  55. val.umount = false
  56. if (val.mountpoint == "/") then
  57. break
  58. elseif (val.mountpoint == "/overlay") then
  59. break
  60. elseif (val.mountpoint == "/rom") then
  61. break
  62. elseif (val.mountpoint == "/tmp") then
  63. break
  64. elseif (val.mountpoint == "/tmp/shm") then
  65. break
  66. elseif (val.mountpoint == "/tmp/upgrade") then
  67. break
  68. elseif (val.mountpoint == "/dev") then
  69. break
  70. end
  71. val.umount = true
  72. until true
  73. non_system_mounts[rawmount] = val
  74. end
  75. end
  76. v = m:section(Table, non_system_mounts, translate("Mounted file systems"))
  77. fs = v:option(DummyValue, "fs", translate("Filesystem"))
  78. mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
  79. avail = v:option(DummyValue, "avail", translate("Available"))
  80. function avail.cfgvalue(self, section)
  81. return luci.tools.webadmin.byte_format(
  82. ( tonumber(mounts[section].available) or 0 ) * 1024
  83. ) .. " / " .. luci.tools.webadmin.byte_format(
  84. ( tonumber(mounts[section].blocks) or 0 ) * 1024
  85. )
  86. end
  87. used = v:option(DummyValue, "used", translate("Used"))
  88. function used.cfgvalue(self, section)
  89. return ( mounts[section].percent or "0%" ) .. " (" ..
  90. luci.tools.webadmin.byte_format(
  91. ( tonumber(mounts[section].used) or 0 ) * 1024
  92. ) .. ")"
  93. end
  94. unmount = v:option(Button, "unmount", translate("Unmount"))
  95. unmount.render = function(self, section, scope)
  96. if non_system_mounts[section].umount then
  97. self.title = translate("Unmount")
  98. self.inputstyle = "remove"
  99. Button.render(self, section, scope)
  100. end
  101. end
  102. unmount.write = function(self, section)
  103. if non_system_mounts[section].umount then
  104. luci.sys.call("/bin/umount '%s'" % luci.util.shellstartsqescape(non_system_mounts[section].mountpoint))
  105. return luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
  106. end
  107. end
  108. mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
  109. mount.anonymous = true
  110. mount.addremove = true
  111. mount.template = "cbi/tblsection"
  112. mount.extedit = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
  113. mount.create = function(...)
  114. local sid = TypedSection.create(...)
  115. if sid then
  116. luci.http.redirect(mount.extedit % sid)
  117. return
  118. end
  119. end
  120. mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
  121. dev = mount:option(DummyValue, "device", translate("Device"))
  122. dev.rawhtml = true
  123. dev.cfgvalue = function(self, section)
  124. local v, e
  125. v = m.uci:get("fstab", section, "uuid")
  126. e = v and devices[v:lower()]
  127. if v and e and e.size then
  128. return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
  129. elseif v and e then
  130. return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
  131. elseif v then
  132. return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  133. end
  134. v = m.uci:get("fstab", section, "label")
  135. e = v and devices[v]
  136. if v and e and e.size then
  137. return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
  138. elseif v and e then
  139. return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
  140. elseif v then
  141. return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  142. end
  143. v = Value.cfgvalue(self, section) or "?"
  144. e = v and devices[v]
  145. if v and e and e.size then
  146. return "%s (%d MB)" %{ tp.pcdata(v), e.size }
  147. elseif v and e then
  148. return tp.pcdata(v)
  149. elseif v then
  150. return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  151. end
  152. end
  153. mp = mount:option(DummyValue, "target", translate("Mount Point"))
  154. mp.cfgvalue = function(self, section)
  155. if m.uci:get("fstab", section, "is_rootfs") == "1" then
  156. return "/overlay"
  157. else
  158. return Value.cfgvalue(self, section) or "?"
  159. end
  160. end
  161. fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
  162. fs.cfgvalue = function(self, section)
  163. local v, e
  164. v = m.uci:get("fstab", section, "uuid")
  165. v = v and v:lower() or m.uci:get("fstab", section, "label")
  166. v = v or m.uci:get("fstab", section, "device")
  167. e = v and devices[v]
  168. return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
  169. end
  170. op = mount:option(DummyValue, "options", translate("Options"))
  171. op.cfgvalue = function(self, section)
  172. return Value.cfgvalue(self, section) or "defaults"
  173. end
  174. rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
  175. rf.cfgvalue = function(self, section)
  176. local target = m.uci:get("fstab", section, "target")
  177. if target == "/" then
  178. return translate("yes")
  179. elseif target == "/overlay" then
  180. return translate("overlay")
  181. else
  182. return translate("no")
  183. end
  184. end
  185. ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
  186. ck.cfgvalue = function(self, section)
  187. return Value.cfgvalue(self, section) == "1"
  188. and translate("yes") or translate("no")
  189. end
  190. swap = m:section(TypedSection, "swap", "SWAP", translate("If your physical memory is insufficient unused data can be temporarily swapped to a swap-device resulting in a higher amount of usable <abbr title=\"Random Access Memory\">RAM</abbr>. Be aware that swapping data is a very slow process as the swap-device cannot be accessed with the high datarates of the <abbr title=\"Random Access Memory\">RAM</abbr>."))
  191. swap.anonymous = true
  192. swap.addremove = true
  193. swap.template = "cbi/tblsection"
  194. swap.extedit = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
  195. swap.create = function(...)
  196. local sid = TypedSection.create(...)
  197. if sid then
  198. luci.http.redirect(swap.extedit % sid)
  199. return
  200. end
  201. end
  202. swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
  203. dev = swap:option(DummyValue, "device", translate("Device"))
  204. dev.cfgvalue = function(self, section)
  205. local v
  206. v = m.uci:get("fstab", section, "uuid")
  207. if v then return "UUID: %s" % v end
  208. v = m.uci:get("fstab", section, "label")
  209. if v then return "Label: %s" % v end
  210. v = Value.cfgvalue(self, section) or "?"
  211. e = v and devices[v]
  212. if v and e and e.size then
  213. return "%s (%s MB)" % {v, e.size}
  214. else
  215. return v
  216. end
  217. end
  218. return m