Merge pull request #798 from thoto/feature_lstat
[project/luci.git] / modules / luci-mod-admin-full / luasrc / model / cbi / admin_system / fstab / mount.lua
1 -- Copyright 2010 Jo-Philipp Wich <jow@openwrt.org>
2 -- Licensed to the public under the Apache License 2.0.
3
4 local fs   = require "nixio.fs"
5 local util = require "nixio.util"
6
7 local has_fscheck = fs.access("/usr/sbin/e2fsck")
8
9 local block = io.popen("block info", "r")
10 local ln, dev, devices = nil, nil, {}
11
12 repeat
13         ln = block:read("*l")
14         dev = ln and ln:match("^/dev/(.-):")
15
16         if dev then
17                 local e, s, key, val = { }
18
19                 for key, val in ln:gmatch([[(%w+)="(.-)"]]) do
20                         e[key:lower()] = val
21                 end
22
23                 s = tonumber((fs.readfile("/sys/class/block/%s/size" % dev)))
24
25                 e.dev  = "/dev/%s" % dev
26                 e.size = s and math.floor(s / 2048)
27
28                 devices[#devices+1] = e
29         end
30 until not ln
31
32 block:close()
33
34
35 m = Map("fstab", translate("Mount Points - Mount Entry"))
36 m.redirect = luci.dispatcher.build_url("admin/system/fstab")
37
38 if not arg[1] or m.uci:get("fstab", arg[1]) ~= "mount" then
39         luci.http.redirect(m.redirect)
40         return
41 end
42
43
44
45 mount = m:section(NamedSection, arg[1], "mount", translate("Mount Entry"))
46 mount.anonymous = true
47 mount.addremove = false
48
49 mount:tab("general",  translate("General Settings"))
50 mount:tab("advanced", translate("Advanced Settings"))
51
52
53 mount:taboption("general", Flag, "enabled", translate("Enable this mount")).rmempty = false
54
55
56 o = mount:taboption("general", Value, "uuid", translate("UUID"),
57         translate("If specified, mount the device by its UUID instead of a fixed device node"))
58
59 o:value("", translate("-- match by uuid --"))
60
61 for i, d in ipairs(devices) do
62         if d.uuid and d.size then
63                 o:value(d.uuid, "%s (%s, %d MB)" %{ d.uuid, d.dev, d.size })
64         elseif d.uuid then
65                 o:value(d.uuid, "%s (%s)" %{ d.uuid, d.dev })
66         end
67 end
68
69
70 o = mount:taboption("general", Value, "label", translate("Label"),
71         translate("If specified, mount the device by the partition label instead of a fixed device node"))
72
73 o:value("", translate("-- match by label --"))
74
75 o:depends("uuid", "")
76
77 for i, d in ipairs(devices) do
78         if d.label and d.size then
79                 o:value(d.label, "%s (%s, %d MB)" %{ d.label, d.dev, d.size })
80         elseif d.label then
81                 o:value(d.label, "%s (%s)" %{ d.label, d.dev })
82         end
83 end
84
85
86 o = mount:taboption("general", Value, "device", translate("Device"),
87         translate("The device file of the memory or partition (<abbr title=\"for example\">e.g.</abbr> <code>/dev/sda1</code>)"))
88
89 o:value("", translate("-- match by device --"))
90
91 o:depends({ uuid = "", label = "" })
92
93 for i, d in ipairs(devices) do
94         if d.size then
95                 o:value(d.dev, "%s (%d MB)" %{ d.dev, d.size })
96         else
97                 o:value(d.dev)
98         end
99 end
100
101
102 o = mount:taboption("general", Value, "target", translate("Mount point"),
103         translate("Specifies the directory the device is attached to"))
104
105 o:value("/", translate("Use as root filesystem (/)"))
106 o:value("/overlay", translate("Use as external overlay (/overlay)"))
107
108
109 o = mount:taboption("general", DummyValue, "__notice", translate("Root preparation"))
110 o:depends("target", "/")
111 o.rawhtml = true
112 o.default = [[
113 <p>%s</p><pre>mkdir -p /tmp/introot
114 mkdir -p /tmp/extroot
115 mount --bind / /tmp/introot
116 mount /dev/sda1 /tmp/extroot
117 tar -C /tmp/introot -cvf - . | tar -C /tmp/extroot -xf -
118 umount /tmp/introot
119 umount /tmp/extroot</pre>
120 ]] %{
121         translate("Make sure to clone the root filesystem using something like the commands below:"),
122
123 }
124
125
126 o = mount:taboption("advanced", Value, "fstype", translate("Filesystem"),
127         translate("The filesystem that was used to format the memory (<abbr title=\"for example\">e.g.</abbr> <samp><abbr title=\"Third Extended Filesystem\">ext3</abbr></samp>)"))
128
129 o:value("", "auto")
130
131 local fs
132 for fs in io.lines("/proc/filesystems") do
133         fs = fs:match("%S+")
134         if fs ~= "nodev" then
135                 o:value(fs)
136         end
137 end
138
139
140 o = mount:taboption("advanced", Value, "options", translate("Mount options"),
141         translate("See \"mount\" manpage for details"))
142
143 o.placeholder = "defaults"
144
145
146 if has_fscheck then
147         o = mount:taboption("advanced", Flag, "enabled_fsck", translate("Run filesystem check"),
148                 translate("Run a filesystem check before mounting the device"))
149 end
150
151 return m