1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
4 require("luci.tools.webadmin")
6 local fs = require "nixio.fs"
7 local util = require "nixio.util"
8 local tp = require "luci.template.parser"
10 local block = io.popen("block info", "r")
11 local ln, dev, devices = nil, nil, {}
15 dev = ln and ln:match("^/dev/(.-):")
18 local e, s, key, val = { }
20 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
25 s = tonumber((fs.readfile("/sys/class/block/%s/size" % dev)))
27 e.dev = "/dev/%s" % dev
28 e.size = s and math.floor(s / 2048)
37 m = Map("fstab", translate("Mount Points"))
38 s = m:section(TypedSection, "global", translate("Global Settings"))
42 o = s:option(Flag, "anon_swap", translate("Anonymous Swap"), translate("Mount swap not specifically configured"))
43 o.default = o.disabled
46 o = s:option(Flag, "anon_mount", translate("Anonymous Mount"), translate("Mount filesystems not specifically configured"))
47 o.default = o.disabled
50 o = s:option(Flag, "auto_swap", translate("Automount Swap"), translate("Automatically mount swap on hotplug"))
54 o = s:option(Flag, "auto_mount", translate("Automount Filesystem"), translate("Automatically mount filesystems on hotplug"))
58 o = s:option(Flag, "check_fs", translate("Check fileystems before mount"), translate("Automatically check filesystem for errors before mounting"))
59 o.default = o.disabled
62 local mounts = luci.sys.mounts()
63 local non_system_mounts = {}
64 for rawmount, val in pairs(mounts) do
65 if (string.find(val.mountpoint, "/tmp/.jail") == nil) then
68 if (val.mountpoint == "/") then
70 elseif (val.mountpoint == "/overlay") then
72 elseif (val.mountpoint == "/rom") then
74 elseif (val.mountpoint == "/tmp") then
76 elseif (val.mountpoint == "/tmp/shm") then
78 elseif (val.mountpoint == "/tmp/upgrade") then
80 elseif (val.mountpoint == "/dev") then
85 non_system_mounts[rawmount] = val
89 v = m:section(Table, non_system_mounts, translate("Mounted file systems"))
91 fs = v:option(DummyValue, "fs", translate("Filesystem"))
93 mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
95 avail = v:option(DummyValue, "avail", translate("Available"))
96 function avail.cfgvalue(self, section)
97 return luci.tools.webadmin.byte_format(
98 ( tonumber(mounts[section].available) or 0 ) * 1024
99 ) .. " / " .. luci.tools.webadmin.byte_format(
100 ( tonumber(mounts[section].blocks) or 0 ) * 1024
104 used = v:option(DummyValue, "used", translate("Used"))
105 function used.cfgvalue(self, section)
106 return ( mounts[section].percent or "0%" ) .. " (" ..
107 luci.tools.webadmin.byte_format(
108 ( tonumber(mounts[section].used) or 0 ) * 1024
112 unmount = v:option(Button, "unmount", translate("Unmount"))
113 unmount.render = function(self, section, scope)
114 if non_system_mounts[section].umount then
115 self.title = translate("Unmount")
116 self.inputstyle = "remove"
117 Button.render(self, section, scope)
121 unmount.write = function(self, section)
122 if non_system_mounts[section].umount then
123 luci.sys.call("/bin/umount '%s'" % luci.util.shellstartsqescape(non_system_mounts[section].mountpoint))
124 return luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
128 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
129 mount.anonymous = true
130 mount.addremove = true
131 mount.template = "cbi/tblsection"
132 mount.extedit = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
134 mount.create = function(...)
135 local sid = TypedSection.create(...)
137 luci.http.redirect(mount.extedit % sid)
143 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
145 dev = mount:option(DummyValue, "device", translate("Device"))
147 dev.cfgvalue = function(self, section)
150 v = m.uci:get("fstab", section, "uuid")
151 e = v and devices[v:lower()]
152 if v and e and e.size then
153 return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
155 return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
157 return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
160 v = m.uci:get("fstab", section, "label")
162 if v and e and e.size then
163 return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
165 return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
167 return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
170 v = Value.cfgvalue(self, section) or "?"
172 if v and e and e.size then
173 return "%s (%d MB)" %{ tp.pcdata(v), e.size }
177 return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
181 mp = mount:option(DummyValue, "target", translate("Mount Point"))
182 mp.cfgvalue = function(self, section)
183 if m.uci:get("fstab", section, "is_rootfs") == "1" then
186 return Value.cfgvalue(self, section) or "?"
190 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
191 fs.cfgvalue = function(self, section)
194 v = m.uci:get("fstab", section, "uuid")
195 v = v and v:lower() or m.uci:get("fstab", section, "label")
196 v = v or m.uci:get("fstab", section, "device")
200 return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
203 op = mount:option(DummyValue, "options", translate("Options"))
204 op.cfgvalue = function(self, section)
205 return Value.cfgvalue(self, section) or "defaults"
208 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
209 rf.cfgvalue = function(self, section)
210 local target = m.uci:get("fstab", section, "target")
211 if target == "/" then
212 return translate("yes")
213 elseif target == "/overlay" then
214 return translate("overlay")
216 return translate("no")
220 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
221 ck.cfgvalue = function(self, section)
222 return Value.cfgvalue(self, section) == "1"
223 and translate("yes") or translate("no")
227 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>."))
228 swap.anonymous = true
229 swap.addremove = true
230 swap.template = "cbi/tblsection"
231 swap.extedit = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
233 swap.create = function(...)
234 local sid = TypedSection.create(...)
236 luci.http.redirect(swap.extedit % sid)
242 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
244 dev = swap:option(DummyValue, "device", translate("Device"))
245 dev.cfgvalue = function(self, section)
248 v = m.uci:get("fstab", section, "uuid")
249 if v then return "UUID: %s" % v end
251 v = m.uci:get("fstab", section, "label")
252 if v then return "Label: %s" % v end
254 v = Value.cfgvalue(self, section) or "?"
256 if v and e and e.size then
257 return "%s (%s MB)" % {v, e.size}