PsychOSPackages/partman/service/partman.lua

41 lines
993 B
Lua

local diskpart = require "diskpart"
local vcomponent = require "vcomponent"
local partman = {}
local run = true
local function attach(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))
end
end
end
function partman.start()
run = true
os.spawn(function()
for a,_ in component.list("drive") do
attach(a)
end
while run do
local tE = {coroutine.yield()}
if tE[1] == "component_added" and tE[3] == "drive" then
attach(tE[2])
elseif tE[1] == "component_removed" and tE[3] == "drive" then
for a,t in component.list("partition") do
if a:sub(1,tE[2]:len()) == tE[2] then
syslog("Removing partition vcomponent "..a)
vcomponent.unregister(a)
end
end
end
end
end, "partman")
end
function partman.stop()
run = false
end
return partman