Merge pull request #1524 from dibdot/travelmate
[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 m = Map("fstab", translate("Mount Points"))
37 s = m:section(TypedSection, "global", translate("Global Settings"))
38 s.addremove = false
39 s.anonymous = true
40
41 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"))
42 detect.inputstyle = "reload"
43
44 detect.write = function(self, section)
45 luci.sys.call("block detect >/etc/config/fstab")
46 luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
47 end
48
49 o = s:option(Flag, "anon_swap", translate("Anonymous Swap"), translate("Mount swap not specifically configured"))
50 o.default = o.disabled
51 o.rmempty = false
52
53 o = s:option(Flag, "anon_mount", translate("Anonymous Mount"), translate("Mount filesystems not specifically configured"))
54 o.default = o.disabled
55 o.rmempty = false
56
57 o = s:option(Flag, "auto_swap", translate("Automount Swap"), translate("Automatically mount swap on hotplug"))
58 o.default = o.enabled
59 o.rmempty = false
60
61 o = s:option(Flag, "auto_mount", translate("Automount Filesystem"), translate("Automatically mount filesystems on hotplug"))
62 o.default = o.enabled
63 o.rmempty = false
64
65 o = s:option(Flag, "check_fs", translate("Check filesystems before mount"), translate("Automatically check filesystem for errors before mounting"))
66 o.default = o.disabled
67 o.rmempty = false
68
69 local mounts = luci.sys.mounts()
70 local non_system_mounts = {}
71 for rawmount, val in pairs(mounts) do
72 if (string.find(val.mountpoint, "/tmp/.jail") == nil) then
73 repeat
74 val.umount = false
75 if (val.mountpoint == "/") then
76 break
77 elseif (val.mountpoint == "/overlay") then
78 break
79 elseif (val.mountpoint == "/rom") then
80 break
81 elseif (val.mountpoint == "/tmp") then
82 break
83 elseif (val.mountpoint == "/tmp/shm") then
84 break
85 elseif (val.mountpoint == "/tmp/upgrade") then
86 break
87 elseif (val.mountpoint == "/dev") then
88 break
89 end
90 val.umount = true
91 until true
92 non_system_mounts[rawmount] = val
93 end
94 end
95
96 v = m:section(Table, non_system_mounts, translate("Mounted file systems"))
97
98 fs = v:option(DummyValue, "fs", translate("Filesystem"))
99
100 mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
101
102 avail = v:option(DummyValue, "avail", translate("Available"))
103 function avail.cfgvalue(self, section)
104 return luci.tools.webadmin.byte_format(
105 ( tonumber(mounts[section].available) or 0 ) * 1024
106 ) .. " / " .. luci.tools.webadmin.byte_format(
107 ( tonumber(mounts[section].blocks) or 0 ) * 1024
108 )
109 end
110
111 used = v:option(DummyValue, "used", translate("Used"))
112 function used.cfgvalue(self, section)
113 return ( mounts[section].percent or "0%" ) .. " (" ..
114 luci.tools.webadmin.byte_format(
115 ( tonumber(mounts[section].used) or 0 ) * 1024
116 ) .. ")"
117 end
118
119 unmount = v:option(Button, "unmount", translate("Unmount"))
120 unmount.render = function(self, section, scope)
121 if non_system_mounts[section].umount then
122 self.title = translate("Unmount")
123 self.inputstyle = "remove"
124 Button.render(self, section, scope)
125 end
126 end
127
128 unmount.write = function(self, section)
129 if non_system_mounts[section].umount then
130 luci.sys.call("/bin/umount '%s'" % luci.util.shellstartsqescape(non_system_mounts[section].mountpoint))
131 return luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
132 end
133 end
134
135 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
136 mount.anonymous = true
137 mount.addremove = true
138 mount.template = "cbi/tblsection"
139 mount.extedit = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
140
141 mount.create = function(...)
142 local sid = TypedSection.create(...)
143 if sid then
144 luci.http.redirect(mount.extedit % sid)
145 return
146 end
147 end
148
149
150 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
151
152 dev = mount:option(DummyValue, "device", translate("Device"))
153 dev.rawhtml = true
154 dev.cfgvalue = function(self, section)
155 local v, e
156
157 v = m.uci:get("fstab", section, "uuid")
158 e = v and devices[v:lower()]
159 if v and e and e.size then
160 return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
161 elseif v and e then
162 return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
163 elseif v then
164 return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
165 end
166
167 v = m.uci:get("fstab", section, "label")
168 e = v and devices[v]
169 if v and e and e.size then
170 return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
171 elseif v and e then
172 return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
173 elseif v then
174 return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
175 end
176
177 v = Value.cfgvalue(self, section) or "?"
178 e = v and devices[v]
179 if v and e and e.size then
180 return "%s (%d MB)" %{ tp.pcdata(v), e.size }
181 elseif v and e then
182 return tp.pcdata(v)
183 elseif v then
184 return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
185 end
186 end
187
188 mp = mount:option(DummyValue, "target", translate("Mount Point"))
189 mp.cfgvalue = function(self, section)
190 if m.uci:get("fstab", section, "is_rootfs") == "1" then
191 return "/overlay"
192 else
193 return Value.cfgvalue(self, section) or "?"
194 end
195 end
196
197 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
198 fs.cfgvalue = function(self, section)
199 local v, e
200
201 v = m.uci:get("fstab", section, "uuid")
202 v = v and v:lower() or m.uci:get("fstab", section, "label")
203 v = v or m.uci:get("fstab", section, "device")
204
205 e = v and devices[v]
206
207 return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
208 end
209
210 op = mount:option(DummyValue, "options", translate("Options"))
211 op.cfgvalue = function(self, section)
212 return Value.cfgvalue(self, section) or "defaults"
213 end
214
215 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
216 rf.cfgvalue = function(self, section)
217 local target = m.uci:get("fstab", section, "target")
218 if target == "/" then
219 return translate("yes")
220 elseif target == "/overlay" then
221 return translate("overlay")
222 else
223 return translate("no")
224 end
225 end
226
227 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
228 ck.cfgvalue = function(self, section)
229 return Value.cfgvalue(self, section) == "1"
230 and translate("yes") or translate("no")
231 end
232
233
234 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>."))
235 swap.anonymous = true
236 swap.addremove = true
237 swap.template = "cbi/tblsection"
238 swap.extedit = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
239
240 swap.create = function(...)
241 local sid = TypedSection.create(...)
242 if sid then
243 luci.http.redirect(swap.extedit % sid)
244 return
245 end
246 end
247
248
249 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
250
251 dev = swap:option(DummyValue, "device", translate("Device"))
252 dev.cfgvalue = function(self, section)
253 local v
254
255 v = m.uci:get("fstab", section, "uuid")
256 if v then return "UUID: %s" % v end
257
258 v = m.uci:get("fstab", section, "label")
259 if v then return "Label: %s" % v end
260
261 v = Value.cfgvalue(self, section) or "?"
262 e = v and devices[v]
263 if v and e and e.size then
264 return "%s (%s MB)" % {v, e.size}
265 else
266 return v
267 end
268 end
269
270 return m