define-alpine/modules/data.lua
2022-06-18 21:29:00 -03:00

70 lines
1.8 KiB
Lua

local utils = require "../utils"
local t = require "../utils/templater"
local dirs = {}
local links = {}
local function generate_mount_data()
local string = "#!/bin/sh\n"
for i=1,#dirs do
local dir = dirs[i]
if dir.type == "file" then
touch(dir.mountpoint)
string = string .. t([[
mkdir -p '{{dirname}}' || exit 1
touch -a '{{path}}' || exit 1
mount '{{path}}' '{{mountpoint}}' -o bind,umask=100,uid={{uid}},gid={{gid}} || exit 1
]],
{ dirname = utils.dirname(dir.path),
path = dir.path, mountpoint = dir.mountpoint, uid = dir.uid, gid = dir.gid }
)
elseif dir.type == "dir" then
mkdir(dir.mountpoint)
string = string .. t([[
mkdir -p '{{path}}' || exit 1
mount '{{path}}' '{{mountpoint}}' -o bind,umask=100,uid={{uid}},gid={{gid}} || exit 1
]],
dir
)
end
end
for i=1,#links do
local link = links[i]
string = string .. t([[
mkdir -p '{{dirname}}' || exit 1
ln -s '{{target}}' '{{path}}' || exit 1
]],
{ dirname = utils.dirname(link.path),
target = link.target, path = link.path }
)
end
add_file("/usr/local/bin/mount-data", string)
chmod("/usr/local/bin/mount-data", 700)
end
local function add_data_dir(path, mountpoint, uid, gid)
table.insert(dirs, {
type = "dir",
path = path, mountpoint = mountpoint, uid = uid, gid = gid
})
generate_mount_data()
end
local function add_data_file(path, mountpoint, uid, gid)
table.insert(dirs, {
type = "file",
path = path, mountpoint = mountpoint, uid = uid, gid = gid
})
generate_mount_data()
end
local function add_data_link(target, path)
table.insert(links, { target = target, path = path })
generate_mount_data()
end
modules.data = {
add_data_dir = add_data_dir,
add_data_file = add_data_file,
add_data_link = add_data_link,
}
mkdir("/data")
-- XXX: hardcodeado
modules.fstab.add_mount("/dev/sdb /data ext4 defaults 0 0")