luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / modules / luci-mod-system / 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 function unmount.cfgvalue(self, section)
121         return non_system_mounts[section].umount
122 end
123
124 unmount.render = function(self, section, scope)
125         self.title = translate("Unmount")
126         self.inputstyle = "remove"
127         Button.render(self, section, scope)
128 end
129
130 unmount.write = function(self, section)
131         if non_system_mounts[section].umount then
132                 luci.sys.call("/bin/umount '%s'" % luci.util.shellstartsqescape(non_system_mounts[section].mountpoint))
133                 return luci.http.redirect(luci.dispatcher.build_url("admin/system", "fstab"))
134         end
135 end
136
137 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
138 mount.anonymous = true
139 mount.addremove = true
140 mount.template = "cbi/tblsection"
141 mount.extedit  = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
142
143 mount.create = function(...)
144         local sid = TypedSection.create(...)
145         if sid then
146                 luci.http.redirect(mount.extedit % sid)
147                 return
148         end
149 end
150
151
152 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
153
154 dev = mount:option(DummyValue, "device", translate("Device"))
155 dev.rawhtml = true
156 dev.cfgvalue = function(self, section)
157         local v, e
158
159         v = m.uci:get("fstab", section, "uuid")
160         e = v and devices[v:lower()]
161         if v and e and e.size then
162                 return "UUID: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
163         elseif v and e then
164                 return "UUID: %s (%s)" %{ tp.pcdata(v), e.dev }
165         elseif v then
166                 return "UUID: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
167         end
168
169         v = m.uci:get("fstab", section, "label")
170         e = v and devices[v]
171         if v and e and e.size then
172                 return "Label: %s (%s, %d MB)" %{ tp.pcdata(v), e.dev, e.size }
173         elseif v and e then
174                 return "Label: %s (%s)" %{ tp.pcdata(v), e.dev }
175         elseif v then
176                 return "Label: %s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
177         end
178
179         v = Value.cfgvalue(self, section) or "?"
180         e = v and devices[v]
181         if v and e and e.size then
182                 return "%s (%d MB)" %{ tp.pcdata(v), e.size }
183         elseif v and e then
184                 return tp.pcdata(v)
185         elseif v then
186                 return "%s (<em>%s</em>)" %{ tp.pcdata(v), translate("not present") }
187         end
188 end
189
190 mp = mount:option(DummyValue, "target", translate("Mount Point"))
191 mp.cfgvalue = function(self, section)
192         if m.uci:get("fstab", section, "is_rootfs") == "1" then
193                 return "/overlay"
194         else
195                 return Value.cfgvalue(self, section) or "?"
196         end
197 end
198
199 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
200 fs.cfgvalue = function(self, section)
201         local v, e
202
203         v = m.uci:get("fstab", section, "uuid")
204         v = v and v:lower() or m.uci:get("fstab", section, "label")
205         v = v or m.uci:get("fstab", section, "device")
206
207         e = v and devices[v]
208
209         return e and e.type or m.uci:get("fstab", section, "fstype") or "?"
210 end
211
212 op = mount:option(DummyValue, "options", translate("Options"))
213 op.cfgvalue = function(self, section)
214         return Value.cfgvalue(self, section) or "defaults"
215 end
216
217 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
218 rf.cfgvalue = function(self, section)
219         local target = m.uci:get("fstab", section, "target")
220         if target == "/" then
221                 return translate("yes")
222         elseif target == "/overlay" then
223                 return translate("overlay")
224         else
225                 return translate("no")
226         end
227 end
228
229 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
230 ck.cfgvalue = function(self, section)
231         return Value.cfgvalue(self, section) == "1"
232                 and translate("yes") or translate("no")
233 end
234
235
236 swap = m:section(TypedSection, "swap", translate("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>."))
237 swap.anonymous = true
238 swap.addremove = true
239 swap.template = "cbi/tblsection"
240 swap.extedit  = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
241
242 swap.create = function(...)
243         local sid = TypedSection.create(...)
244         if sid then
245                 luci.http.redirect(swap.extedit % sid)
246                 return
247         end
248 end
249
250
251 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
252
253 dev = swap:option(DummyValue, "device", translate("Device"))
254 dev.cfgvalue = function(self, section)
255         local v
256
257         v = m.uci:get("fstab", section, "uuid")
258         if v then return "UUID: %s" % v end
259
260         v = m.uci:get("fstab", section, "label")
261         if v then return "Label: %s" % v end
262
263         v = Value.cfgvalue(self, section) or "?"
264         e = v and devices[v]
265         if v and e and e.size then
266                 return "%s (%s MB)" % {v, e.size}
267         else
268                 return v
269         end
270 end
271
272 return m