PsychOSPackages/partman/service/partman.lua

60 lines
1.5 KiB
Lua

local diskpart = require "diskpart"
local vcomponent = require "vcomponent"
local partman = {}
partman.fstypes = {"rtfs"}
local run = true
local function attachPartition(addr)
syslog("Finding filesystem that will mount "..addr)
for _,n in ipairs(partman.fstypes) do
local w,l = pcall(require,string.format("fs.%s",n))
if w then
syslog("Trying "..n)
local w, f = pcall(l.mount,addr)
if w then
syslog(n.." successful!")
vcomponent.register(string.format("%s/%s",addr,n), "filesystem",f)
break
end
end
end
end
local function attachDrive(addr)
for k,v in ipairs(diskpart.getPartitions(addr)) do
if v[4] > 0 then
syslog(string.format("Registering vcomponent for partition %s/%i",addr,k))
vcomponent.register(string.format("%s/%i",addr,k), "partition", diskpart.proxyPartition(addr, k))
syslog("Attempting to mount a filesystem...")
attachPartition(string.format("%s/%i",addr,k))
end
end
end
function partman.start()
run = true
os.spawn(function()
for a,_ in component.list("drive") do
attachDrive(a)
end
while run do
local tE = {coroutine.yield()}
if tE[1] == "component_added" and tE[3] == "drive" then
attachDrive(tE[2])
elseif tE[1] == "component_removed" and tE[3] == "drive" then
for a,t in component.list() do
if (t == "partition" or t == "filesystem") and a:sub(1,tE[2]:len()) == tE[2] then
syslog("Removing "..t.." vcomponent "..a)
vcomponent.unregister(a)
end
end
end
end
end, "partman")
end
function partman.stop()
run = false
end
return partman