54beb63b42fe8fa7d7d2f5ee181ad7587a3f1ff6
[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 for i, d in ipairs(devices) do
60         if d.uuid then
61                 o:value(d.uuid, "%s (%s, %d MB)" %{ d.uuid, d.dev, d.size })
62         end
63 end
64
65 o:value("", translate("-- match by label --"))
66
67
68 o = mount:taboption("general", Value, "label", translate("Label"),
69         translate("If specified, mount the device by the partition label instead of a fixed device node"))
70
71 o:depends("uuid", "")
72
73 for i, d in ipairs(devices) do
74         if d.label then
75                 o:value(d.label, "%s (%s, %d MB)" %{ d.label, d.dev, d.size })
76         end
77 end
78
79 o:value("", translate("-- match by device --"))
80
81
82 o = mount:taboption("general", Value, "device", translate("Device"),
83         translate("The device file of the memory or partition (<abbr title=\"for example\">e.g.</abbr> <code>/dev/sda1</code>)"))
84
85 o:depends({ uuid = "", label = "" })
86
87 for i, d in ipairs(devices) do
88         o:value(d.dev, "%s (%d MB)" %{ d.dev, d.size })
89 end
90
91
92 o = mount:taboption("general", Value, "target", translate("Mount point"),
93         translate("Specifies the directory the device is attached to"))
94
95 o:value("/", translate("Use as root filesystem (/)"))
96 o:value("/overlay", translate("Use as external overlay (/overlay)"))
97
98
99 o = mount:taboption("general", DummyValue, "__notice", translate("Root preparation"))
100 o:depends("target", "/")
101 o.rawhtml = true
102 o.default = [[
103 <p>%s</p><pre>mkdir -p /tmp/introot
104 mkdir -p /tmp/extroot
105 mount --bind / /tmp/introot
106 mount /dev/sda1 /tmp/extroot
107 tar -C /tmp/intproot -cvf - . | tar -C /tmp/extroot -xf -
108 umount /tmp/introot
109 umount /tmp/extroot</pre>
110 ]] %{
111         translate("Make sure to clone the root filesystem using something like the commands below:"),
112
113 }
114
115
116 o = mount:taboption("advanced", Value, "fstype", translate("Filesystem"),
117         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>)"))
118
119 o:value("", "auto")
120
121 local fs
122 for fs in io.lines("/proc/filesystems") do
123         fs = fs:match("%S+")
124         if fs ~= "nodev" then
125                 o:value(fs)
126         end
127 end
128
129
130 o = mount:taboption("advanced", Value, "options", translate("Mount options"),
131         translate("See \"mount\" manpage for details"))
132
133 o.placeholder = "defaults"
134
135
136 if has_fscheck then
137         o = mount:taboption("advanced", Flag, "enabled_fsck", translate("Run filesystem check"),
138                 translate("Run a filesystem check before mounting the device"))
139 end
140
141 return m