51 lines
1.2 KiB
Ruby
Executable File
51 lines
1.2 KiB
Ruby
Executable File
#!/usr/bin/env ruby
|
|
|
|
require 'pathname'
|
|
require_relative "lib/lxc"
|
|
|
|
LXC_ROOT = Pathname.new("/data/containers")
|
|
FSTAB_D = Pathname.new("/etc/fstab.d")
|
|
SYS_MOUNT = 165
|
|
MS_BIND = 4096
|
|
|
|
def mount(src_path, dest_path, mounts)
|
|
unless src_path.exist?
|
|
abort "container directory for shared mount does not exists #{dest_path}"
|
|
end
|
|
unless src_path.exist?
|
|
abort "container directory for shared mount does not exists #{src_path}"
|
|
end
|
|
entries = mounts.map do |src, dest|
|
|
src = src.gsub(/^\//, "")
|
|
|
|
src_mount = dest_path.join(src)
|
|
if dest == true
|
|
dest_mount = src_path.join(src)
|
|
else
|
|
dest_mount = src_path.join(dest.gsub(/^\//, ""))
|
|
end
|
|
puts ("mkdir -p #{dest_mount}")
|
|
FileUtils.mkdir_p(dest_mount)
|
|
syscall(SYS_MOUNT, src_mount.to_s, dest_mount.to_s, "none", MS_BIND, 0)
|
|
end
|
|
|
|
entries
|
|
end
|
|
|
|
def main
|
|
registry = Lxc::Registry.new
|
|
network = registry.data["network"] || {}
|
|
network.each do |name, container|
|
|
src_path = LXC_ROOT.join(name, "rootfs")
|
|
containers = container["mounts"]
|
|
next if containers.nil?
|
|
|
|
containers.each do |dest_container, mounts|
|
|
dest_path = LXC_ROOT.join(dest_container, "rootfs")
|
|
mount(src_path, dest_path, mounts)
|
|
end
|
|
end
|
|
end
|
|
|
|
main
|