4c806dbc43da8f55de0d363aaf8e2be3f35e9724
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / fstab.lua
1 -- Copyright 2008 Steven Barth <steven@midlink.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 require("luci.tools.webadmin")
5
6 local fs   = require "nixio.fs"
7 local util = require "nixio.util"
8 local tp   = require "luci.template.parser"
9
10 local block = io.popen("block info", "r")
11 local ln, dev, devices = nil, nil, {}
12
13 repeat
14         ln = block:read("*l")
15         dev = ln and ln:match("^/dev/(.-):")
16
17         if dev then
18                 local e, s, key, val = { }
19
20                 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
21                         e[key:lower()] = val
22                         devices[val] = e
23                 end
24
25                 s = tonumber((fs.readfile("/sys/class/block/%s/size" % dev)))
26
27                 e.dev  = "/dev/%s" % dev
28                 e.size = s and math.floor(s / 2048)
29
30                 devices[e.dev] = e
31         end
32 until not ln
33
34 block:close()
35
36
37 m = Map("fstab", translate("Mount Points"))
38
39 local mounts = luci.sys.mounts()
40
41 v = m:section(Table, mounts, translate("Mounted file systems"))
42
43 fs = v:option(DummyValue, "fs", translate("Filesystem"))
44
45 mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
46
47 avail = v:option(DummyValue, "avail", translate("Available"))
48 function avail.cfgvalue(self, section)
49         return luci.tools.webadmin.byte_format(
50                 ( tonumber(mounts[section].available) or 0 ) * 1024
51         ) .. " / " .. luci.tools.webadmin.byte_format(
52                 ( tonumber(mounts[section].blocks) or 0 ) * 1024
53         )
54 end
55
56 used = v:option(DummyValue, "used", translate("Used"))
57 function used.cfgvalue(self, section)
58         return ( mounts[section].percent or "0%" ) .. " (" ..
59         luci.tools.webadmin.byte_format(
60                 ( tonumber(mounts[section].used) or 0 ) * 1024
61         ) .. ")"
62 end
63
64
65
66 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
67 mount.anonymous = true
68 mount.addremove = true
69 mount.template = "cbi/tblsection"
70 mount.extedit  = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
71
72 mount.create = function(...)
73         local sid = TypedSection.create(...)
74         if sid then
75                 luci.http.redirect(mount.extedit % sid)
76                 return
77         end
78 end
79
80
81 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
82
83 dev = mount:option(DummyValue, "device", translate("Device"))
84 dev.rawhtml = true
85 dev.cfgvalue = function(self, section)
86         local v, e
87
88         v = m.uci:get("fstab", section, "uuid")
89         e = v and devices[v:lower()]
90         if v and e then
91                 return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
92         elseif v then
93                 return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
94         end
95
96         v = m.uci:get("fstab", section, "label")
97         e = v and devices[v]
98         if v and e then
99                 return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
100         elseif v then
101                 return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
102         end
103
104         v = Value.cfgvalue(self, section) or "?"
105         e = v and devices[v]
106         if v and e then
107                 return "%s (%d MB)" %{ tp.pcdata(v), e.size }
108         elseif v then
109                 return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
110         end
111 end
112
113 mp = mount:option(DummyValue, "target", translate("Mount Point"))
114 mp.cfgvalue = function(self, section)
115         if m.uci:get("fstab", section, "is_rootfs") == "1" then
116                 return "/overlay"
117         else
118                 return Value.cfgvalue(self, section) or "?"
119         end
120 end
121
122 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
123 fs.cfgvalue = function(self, section)
124         local v, e
125
126         v = m.uci:get("fstab", section, "uuid")
127         v = v and v:lower() or m.uci:get("fstab", section, "label")
128         v = v or m.uci:get("fstab", section, "device")
129
130         e = v and devices[v]
131
132         return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
133 end
134
135 op = mount:option(DummyValue, "options", translate("Options"))
136 op.cfgvalue = function(self, section)
137         return Value.cfgvalue(self, section) or "defaults"
138 end
139
140 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
141 rf.cfgvalue = function(self, section)
142         local target = m.uci:get("fstab", section, "target")
143         if target == "/" then
144                 return translate("yes")
145         elseif target == "/overlay" then
146                 return translate("overlay")
147         else
148                 return translate("no")
149         end
150 end
151
152 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
153 ck.cfgvalue = function(self, section)
154         return Value.cfgvalue(self, section) == "1"
155                 and translate("yes") or translate("no")
156 end
157
158
159 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>."))
160 swap.anonymous = true
161 swap.addremove = true
162 swap.template = "cbi/tblsection"
163 swap.extedit  = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
164
165 swap.create = function(...)
166         local sid = TypedSection.create(...)
167         if sid then
168                 luci.http.redirect(swap.extedit % sid)
169                 return
170         end
171 end
172
173
174 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
175
176 dev = swap:option(DummyValue, "device", translate("Device"))
177 dev.cfgvalue = function(self, section)
178         local v
179
180         v = m.uci:get("fstab", section, "uuid")
181         if v then return "UUID: %s" % v end
182
183         v = m.uci:get("fstab", section, "label")
184         if v then return "Label: %s" % v end
185
186         v = Value.cfgvalue(self, section) or "?"
187         return size[v] and "%s (%s MB)" % {v, size[v]} or v
188 end
189
190 return m