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