LuPPC/src/lua/core/drive.lua

74 lines
1.8 KiB
Lua
Raw Normal View History

2021-05-30 07:18:55 +10:00
local drive = {}
function drive.register()
local list = native.fs_list("/sys/block")
for _, ent in ipairs(list) do
-- Match hard drives. floppy drives, an rewindable tape drives
if (ent:match("sd%a%d*") or ent:match("hd%a%d*") or ent:match("fd%d+") or ent:match("n?st%d+[lma]?") or ent:match("n?ht%d+[lma]?")) then
--if true then
lprint("Attempting to open: "..ent)
local ok, disk = pcall(blk.open, ent)
if ok then
2021-05-30 10:18:59 +10:00
local nh = io.open("/sys/block/"..ent.."/device/model", "r")
local name = nh:read("*a"):gsub("%s+$", "")
nh:close()
nh = io.open("/sys/block/"..ent.."/device/vendor", "r")
local vendor = nh:read("*a"):gsub("%s+$", "")
nh:close()
nh = io.open("/sys/block/"..ent.."/device/wwid", "r")
local wwid
if nh then
wwid = nh:read("*a"):gsub("%s+$", "")
nh:close()
end
2021-05-30 07:18:55 +10:00
local component = {}
function component.readByte(offset)
return blk.readbyte(disk, offset)
end
function component.writeByte(offset, value)
return blk.writebyte(disk, offset, value)
end
function component.getLabel()
2021-05-30 10:18:59 +10:00
return name
2021-05-30 07:18:55 +10:00
end
function component.setLabel(str)
-- stub
2021-05-30 10:18:59 +10:00
return name
2021-05-30 07:18:55 +10:00
end
function component.readSector(sec)
return blk.readsector(disk, sec)
end
function component.writeSector(sec, val)
return blk.writesector(disk, sec, val)
end
function component.getPlatterCount()
return blk.platcount(disk)
end
function component.getSectorSize()
return blk.secsize(disk)
end
function component.getCapacity()
return blk.size(disk)
end
2021-05-30 10:18:59 +10:00
local gid = wwid or (vendor.." "..name.." "..ent)
modules.component.api.register(modules.uuidmgr.lookup("drive", gid), "drive", component)
2021-05-30 07:18:55 +10:00
else
lprint("Can't open blkdev: "..ent..": "..disk)
end
end
end
end
return drive