From: Jo-Philipp Wich Date: Sat, 21 Sep 2019 16:30:34 +0000 (+0200) Subject: luci-base: add rpc methods for mount management X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=54cab2f26be6c925cab8651c254380877474a130;p=project%2Fluci.git luci-base: add rpc methods for mount management This commit introduces the new methods luci/getBlockDevices, luci/setBlockDetect, luci/getMountPoints, luci/setUmount in preparation for the reworked mount point management. Signed-off-by: Jo-Philipp Wich --- diff --git a/modules/luci-base/root/usr/libexec/rpcd/luci b/modules/luci-base/root/usr/libexec/rpcd/luci index fb15dab6a5..8215fb95dd 100755 --- a/modules/luci-base/root/usr/libexec/rpcd/luci +++ b/modules/luci-base/root/usr/libexec/rpcd/luci @@ -590,6 +590,101 @@ local methods = { }) == 0) } end + }, + + getBlockDevices = { + call = function() + local fs = require "nixio.fs" + + local block = io.popen("/sbin/block info", "r") + if block then + local rv = {} + + while true do + local ln = block:read("*l") + if not ln then + break + end + + local dev = ln:match("^/dev/(.-):") + if dev then + local s = tonumber((fs.readfile("/sys/class/block/" .. dev .."/size"))) + local e = { + dev = "/dev/" .. dev, + size = s and s * 512 + } + + local key, val = { } + for key, val in ln:gmatch([[(%w+)="(.-)"]]) do + e[key:lower()] = val + end + + rv[dev] = e + end + end + + block:close() + + return rv + else + return { error = "Unable to execute block utility" } + end + end + }, + + setBlockDetect = { + call = function() + return { result = (os.execute("/sbin/block detect > /etc/config/fstab") == 0) } + end + }, + + getMountPoints = { + call = function() + local fs = require "nixio.fs" + + local fd, err = io.open("/proc/mounts", "r") + if fd then + local rv = {} + + while true do + local ln = fd:read("*l") + if not ln then + break + end + + local device, mount, fstype, options, freq, pass = ln:match("^(%S*) (%S*) (%S*) (%S*) (%d+) (%d+)$") + if device and mount then + device = device:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end) + mount = mount:gsub("\\(%d+)", function(n) return string.char(tonumber(n, 8)) end) + + local stat = fs.statvfs(mount) + if stat and stat.blocks > 0 then + rv[#rv+1] = { + device = device, + mount = mount, + size = stat.bsize * stat.blocks, + avail = stat.bsize * stat.bavail, + free = stat.bsize * stat.bfree + } + end + end + end + + fd:close() + + return { result = rv } + else + return { error = err } + end + end + }, + + setUmount = { + args = { path = "/mnt" }, + call = function(args) + local util = require "luci.util" + return { result = (os.execute(string.format("/bin/umount %s", util.shellquote(args.path))) == 0) } + end } }