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"))
39 local mounts = luci.sys.mounts()
40 local non_system_mounts = {}
41 for rawmount, val in pairs(mounts) do
42 if (string.find(val.mountpoint, "/tmp/.jail") == nil) then
45 if (val.mountpoint == "/") then
47 elseif (val.mountpoint == "/overlay") then
49 elseif (val.mountpoint == "/rom") then
51 elseif (val.mountpoint == "/tmp") then
53 elseif (val.mountpoint == "/tmp/shm") then
55 elseif (val.mountpoint == "/tmp/upgrade") then
57 elseif (val.mountpoint == "/dev") then
62 non_system_mounts[rawmount] = val
66 v = m:section(Table, non_system_mounts, translate("Mounted file systems"))
68 fs = v:option(DummyValue, "fs", translate("Filesystem"))
70 mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
72 avail = v:option(DummyValue, "avail", translate("Available"))
73 function avail.cfgvalue(self, section)
74 return luci.tools.webadmin.byte_format(
75 ( tonumber(mounts[section].available) or 0 ) * 1024
76 ) .. " / " .. luci.tools.webadmin.byte_format(
77 ( tonumber(mounts[section].blocks) or 0 ) * 1024
81 used = v:option(DummyValue, "used", translate("Used"))
82 function used.cfgvalue(self, section)
83 return ( mounts[section].percent or "0%" ) .. " (" ..
84 luci.tools.webadmin.byte_format(
85 ( tonumber(mounts[section].used) or 0 ) * 1024
89 unmount = v:option(Button, "unmount", translate("Unmount"))
90 unmount.render = function(self, section, scope)
91 if non_system_mounts[section].umount then
92 self.title = translate("Unmount")
93 self.inputstyle = "remove"
94 Button.render(self, section, scope)
98 unmount.write = function(self, section)
99 if non_system_mounts[section].umount then
100 luci.sys.call("/bin/umount '%s'" % luci.util.shellstartsqescape(non_system_mounts[section].mountpoint))
101 return luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
105 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
106 mount.anonymous = true
107 mount.addremove = true
108 mount.template = "cbi/tblsection"
109 mount.extedit = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
111 mount.create = function(...)
112 local sid = TypedSection.create(...)
114 luci.http.redirect(mount.extedit % sid)
120 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
122 dev = mount:option(DummyValue, "device", translate("Device"))
124 dev.cfgvalue = function(self, section)
127 v = m.uci:get("fstab", section, "uuid")
128 e = v and devices[v:lower()]
129 if v and e and e.size then
130 return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
132 return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
134 return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
137 v = m.uci:get("fstab", section, "label")
139 if v and e and e.size then
140 return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
142 return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
144 return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
147 v = Value.cfgvalue(self, section) or "?"
149 if v and e and e.size then
150 return "%s (%d MB)" %{ tp.pcdata(v), e.size }
154 return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
158 mp = mount:option(DummyValue, "target", translate("Mount Point"))
159 mp.cfgvalue = function(self, section)
160 if m.uci:get("fstab", section, "is_rootfs") == "1" then
163 return Value.cfgvalue(self, section) or "?"
167 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
168 fs.cfgvalue = function(self, section)
171 v = m.uci:get("fstab", section, "uuid")
172 v = v and v:lower() or m.uci:get("fstab", section, "label")
173 v = v or m.uci:get("fstab", section, "device")
177 return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
180 op = mount:option(DummyValue, "options", translate("Options"))
181 op.cfgvalue = function(self, section)
182 return Value.cfgvalue(self, section) or "defaults"
185 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
186 rf.cfgvalue = function(self, section)
187 local target = m.uci:get("fstab", section, "target")
188 if target == "/" then
189 return translate("yes")
190 elseif target == "/overlay" then
191 return translate("overlay")
193 return translate("no")
197 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
198 ck.cfgvalue = function(self, section)
199 return Value.cfgvalue(self, section) == "1"
200 and translate("yes") or translate("no")
204 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>."))
205 swap.anonymous = true
206 swap.addremove = true
207 swap.template = "cbi/tblsection"
208 swap.extedit = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
210 swap.create = function(...)
211 local sid = TypedSection.create(...)
213 luci.http.redirect(swap.extedit % sid)
219 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
221 dev = swap:option(DummyValue, "device", translate("Device"))
222 dev.cfgvalue = function(self, section)
225 v = m.uci:get("fstab", section, "uuid")
226 if v then return "UUID: %s" % v end
228 v = m.uci:get("fstab", section, "label")
229 if v then return "Label: %s" % v end
231 v = Value.cfgvalue(self, section) or "?"
233 if v and e and e.size then
234 return "%s (%s MB)" % {v, e.size}