luci-mod-admin-full: handle missing size for block devices
[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 and d.size then
61                 o:value(d.uuid, "%s (%s, %d MB)" %{ d.uuid, d.dev, d.size })
62         elseif d.uuid then
63                 o:value(d.uuid, "%s (%s)" %{ d.uuid, d.dev })
64         end
65 end
66
67 o:value("", translate("-- match by label --"))
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:depends("uuid", "")
74
75 for i, d in ipairs(devices) do
76         if d.label and d.size then
77                 o:value(d.label, "%s (%s, %d MB)" %{ d.label, d.dev, d.size })
78         elseif d.label then
79                 o:value(d.label, "%s (%s)" %{ d.label, d.dev })
80         end
81 end
82
83 o:value("", translate("-- match by device --"))
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:depends({ uuid = "", label = "" })
90
91 for i, d in ipairs(devices) do
92         if d.size then
93                 o:value(d.dev, "%s (%d MB)" %{ d.dev, d.size })
94         else
95                 o:value(d.dev)
96         end
97 end
98
99
100 o = mount:taboption("general", Value, "target", translate("Mount point"),
101         translate("Specifies the directory the device is attached to"))
102
103 o:value("/", translate("Use as root filesystem (/)"))
104 o:value("/overlay", translate("Use as external overlay (/overlay)"))
105
106
107 o = mount:taboption("general", DummyValue, "__notice", translate("Root preparation"))
108 o:depends("target", "/")
109 o.rawhtml = true
110 o.default = [[
111 <p>%s</p><pre>mkdir -p /tmp/introot
112 mkdir -p /tmp/extroot
113 mount --bind / /tmp/introot
114 mount /dev/sda1 /tmp/extroot
115 tar -C /tmp/intproot -cvf - . | tar -C /tmp/extroot -xf -
116 umount /tmp/introot
117 umount /tmp/extroot</pre>
118 ]] %{
119         translate("Make sure to clone the root filesystem using something like the commands below:"),
120
121 }
122
123
124 o = mount:taboption("advanced", Value, "fstype", translate("Filesystem"),
125         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>)"))
126
127 o:value("", "auto")
128
129 local fs
130 for fs in io.lines("/proc/filesystems") do
131         fs = fs:match("%S+")
132         if fs ~= "nodev" then
133                 o:value(fs)
134         end
135 end
136
137
138 o = mount:taboption("advanced", Value, "options", translate("Mount options"),
139         translate("See \"mount\" manpage for details"))
140
141 o.placeholder = "defaults"
142
143
144 if has_fscheck then
145         o = mount:taboption("advanced", Flag, "enabled_fsck", translate("Run filesystem check"),
146                 translate("Run a filesystem check before mounting the device"))
147 end
148
149 return m