Rework LuCI build system
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / fstab.lua
1 --[[
2 LuCI - Lua Configuration Interface
3
4 Copyright 2008 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]--
14
15 require("luci.tools.webadmin")
16
17 local fs   = require "nixio.fs"
18 local util = require "nixio.util"
19
20 local devices = {}
21 util.consume((fs.glob("/dev/sd*")), devices)
22 util.consume((fs.glob("/dev/hd*")), devices)
23 util.consume((fs.glob("/dev/scd*")), devices)
24 util.consume((fs.glob("/dev/mmc*")), devices)
25
26 local size = {}
27 for i, dev in ipairs(devices) do
28         local s = tonumber((fs.readfile("/sys/class/block/%s/size" % dev:sub(6))))
29         size[dev] = s and math.floor(s / 2048)
30 end
31
32
33 m = Map("fstab", translate("Mount Points"))
34
35 local mounts = luci.sys.mounts()
36
37 v = m:section(Table, mounts, translate("Mounted file systems"))
38
39 fs = v:option(DummyValue, "fs", translate("Filesystem"))
40
41 mp = v:option(DummyValue, "mountpoint", translate("Mount Point"))
42
43 avail = v:option(DummyValue, "avail", translate("Available"))
44 function avail.cfgvalue(self, section)
45         return luci.tools.webadmin.byte_format(
46                 ( tonumber(mounts[section].available) or 0 ) * 1024
47         ) .. " / " .. luci.tools.webadmin.byte_format(
48                 ( tonumber(mounts[section].blocks) or 0 ) * 1024
49         )
50 end
51
52 used = v:option(DummyValue, "used", translate("Used"))
53 function used.cfgvalue(self, section)
54         return ( mounts[section].percent or "0%" ) .. " (" ..
55         luci.tools.webadmin.byte_format(
56                 ( tonumber(mounts[section].used) or 0 ) * 1024
57         ) .. ")"
58 end
59
60
61
62 mount = m:section(TypedSection, "mount", translate("Mount Points"), translate("Mount Points define at which point a memory device will be attached to the filesystem"))
63 mount.anonymous = true
64 mount.addremove = true
65 mount.template = "cbi/tblsection"
66 mount.extedit  = luci.dispatcher.build_url("admin/system/fstab/mount/%s")
67
68 mount.create = function(...)
69         local sid = TypedSection.create(...)
70         if sid then
71                 luci.http.redirect(mount.extedit % sid)
72                 return
73         end
74 end
75
76
77 mount:option(Flag, "enabled", translate("Enabled")).rmempty = false
78
79 dev = mount:option(DummyValue, "device", translate("Device"))
80 dev.cfgvalue = function(self, section)
81         local v
82
83         v = m.uci:get("fstab", section, "uuid")
84         if v then return "UUID: %s" % v end
85
86         v = m.uci:get("fstab", section, "label")
87         if v then return "Label: %s" % v end
88
89         v = Value.cfgvalue(self, section) or "?"
90         return size[v] and "%s (%s MB)" % {v, size[v]} or v
91 end
92
93 mp = mount:option(DummyValue, "target", translate("Mount Point"))
94 mp.cfgvalue = function(self, section)
95         if m.uci:get("fstab", section, "is_rootfs") == "1" then
96                 return "/overlay"
97         else
98                 return Value.cfgvalue(self, section) or "?"
99         end
100 end
101
102 fs = mount:option(DummyValue, "fstype", translate("Filesystem"))
103 fs.cfgvalue = function(self, section)
104         return Value.cfgvalue(self, section) or "?"
105 end
106
107 op = mount:option(DummyValue, "options", translate("Options"))
108 op.cfgvalue = function(self, section)
109         return Value.cfgvalue(self, section) or "defaults"
110 end
111
112 rf = mount:option(DummyValue, "is_rootfs", translate("Root"))
113 rf.cfgvalue = function(self, section)
114         return Value.cfgvalue(self, section) == "1"
115                 and translate("yes") or translate("no")
116 end
117
118 ck = mount:option(DummyValue, "enabled_fsck", translate("Check"))
119 ck.cfgvalue = function(self, section)
120         return Value.cfgvalue(self, section) == "1"
121                 and translate("yes") or translate("no")
122 end
123
124
125 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>."))
126 swap.anonymous = true
127 swap.addremove = true
128 swap.template = "cbi/tblsection"
129 swap.extedit  = luci.dispatcher.build_url("admin/system/fstab/swap/%s")
130
131 swap.create = function(...)
132         local sid = TypedSection.create(...)
133         if sid then
134                 luci.http.redirect(swap.extedit % sid)
135                 return
136         end
137 end
138
139
140 swap:option(Flag, "enabled", translate("Enabled")).rmempty = false
141
142 dev = swap:option(DummyValue, "device", translate("Device"))
143 dev.cfgvalue = function(self, section)
144         local v
145
146         v = m.uci:get("fstab", section, "uuid")
147         if v then return "UUID: %s" % v end
148
149         v = m.uci:get("fstab", section, "label")
150         if v then return "Label: %s" % v end
151
152         v = Value.cfgvalue(self, section) or "?"
153         return size[v] and "%s (%s MB)" % {v, size[v]} or v
154 end
155
156 return m