fstab.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. local mounts = luci.sys.mounts()
  27. v = m:section(Table, mounts, translate("Mounted file systems"))
  28. fs = v:option(DummyValue, "fs", translate("Filesystem"))
  29. mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
  30. avail = v:option(DummyValue, "avail", translate("Available"))
  31. function avail.cfgvalue(self, section)
  32. return luci.tools.webadmin.byte_format(
  33. ( tonumber(mounts[section].available) or 0 ) * 1024
  34. ) .. " / " .. luci.tools.webadmin.byte_format(
  35. ( tonumber(mounts[section].blocks) or 0 ) * 1024
  36. )
  37. end
  38. used = v:option(DummyValue, "used", translate("Used"))
  39. function used.cfgvalue(self, section)
  40. return ( mounts[section].percent or "0%" ) .. " (" ..
  41. luci.tools.webadmin.byte_format(
  42. ( tonumber(mounts[section].used) or 0 ) * 1024
  43. ) .. ")"
  44. end
  45. mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
  46. mount.anonymous = true
  47. mount.addremove = true
  48. mount.template = "cbi/tblsection"
  49. mount.extedit = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
  50. mount.create = function(...)
  51. local sid = TypedSection.create(...)
  52. if sid then
  53. luci.http.redirect(mount.extedit % sid)
  54. return
  55. end
  56. end
  57. mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
  58. dev = mount:option(DummyValue, "device", translate("Device"))
  59. dev.rawhtml = true
  60. dev.cfgvalue = function(self, section)
  61. local v, e
  62. v = m.uci:get("fstab", section, "uuid")
  63. e = v and devices[v:lower()]
  64. if v and e and e.size then
  65. return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
  66. elseif v and e then
  67. return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
  68. elseif v then
  69. return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  70. end
  71. v = m.uci:get("fstab", section, "label")
  72. e = v and devices[v]
  73. if v and e and e.size then
  74. return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
  75. elseif v and e then
  76. return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
  77. elseif v then
  78. return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  79. end
  80. v = Value.cfgvalue(self, section) or "?"
  81. e = v and devices[v]
  82. if v and e and e.size then
  83. return "%s (%d MB)" %{ tp.pcdata(v), e.size }
  84. elseif v and e then
  85. return tp.pcdata(v)
  86. elseif v then
  87. return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
  88. end
  89. end
  90. mp = mount:option(DummyValue, "target", translate("Mount Point"))
  91. mp.cfgvalue = function(self, section)
  92. if m.uci:get("fstab", section, "is_rootfs") == "1" then
  93. return "/overlay"
  94. else
  95. return Value.cfgvalue(self, section) or "?"
  96. end
  97. end
  98. fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
  99. fs.cfgvalue = function(self, section)
  100. local v, e
  101. v = m.uci:get("fstab", section, "uuid")
  102. v = v and v:lower() or m.uci:get("fstab", section, "label")
  103. v = v or m.uci:get("fstab", section, "device")
  104. e = v and devices[v]
  105. return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
  106. end
  107. op = mount:option(DummyValue, "options", translate("Options"))
  108. op.cfgvalue = function(self, section)
  109. return Value.cfgvalue(self, section) or "defaults"
  110. end
  111. rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
  112. rf.cfgvalue = function(self, section)
  113. local target = m.uci:get("fstab", section, "target")
  114. if target == "/" then
  115. return translate("yes")
  116. elseif target == "/overlay" then
  117. return translate("overlay")
  118. else
  119. return translate("no")
  120. end
  121. end
  122. ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
  123. ck.cfgvalue = function(self, section)
  124. return Value.cfgvalue(self, section) == "1"
  125. and translate("yes") or translate("no")
  126. end
  127. 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>."))
  128. swap.anonymous = true
  129. swap.addremove = true
  130. swap.template = "cbi/tblsection"
  131. swap.extedit = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
  132. swap.create = function(...)
  133. local sid = TypedSection.create(...)
  134. if sid then
  135. luci.http.redirect(swap.extedit % sid)
  136. return
  137. end
  138. end
  139. swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
  140. dev = swap:option(DummyValue, "device", translate("Device"))
  141. dev.cfgvalue = function(self, section)
  142. local v
  143. v = m.uci:get("fstab", section, "uuid")
  144. if v then return "UUID: %s" % v end
  145. v = m.uci:get("fstab", section, "label")
  146. if v then return "Label: %s" % v end
  147. v = Value.cfgvalue(self, section) or "?"
  148. e = v and devices[v]
  149. if v and e and e.size then
  150. return "%s (%s MB)" % {v, e.size}
  151. else
  152. return v
  153. end
  154. end
  155. return m