2015-08-05 15:33:37 +00:00
|
|
|
#!/usr/bin/env ruby
|
|
|
|
|
|
|
|
require 'pathname'
|
|
|
|
require_relative "lib/lxc"
|
|
|
|
|
|
|
|
LXC_ROOT = Pathname.new("/data/containers")
|
|
|
|
FSTAB_D = Pathname.new("/etc/fstab.d")
|
2016-02-02 22:15:17 +00:00
|
|
|
SYS_MOUNT = 165
|
|
|
|
MS_BIND = 4096
|
2015-08-05 15:33:37 +00:00
|
|
|
|
2016-02-02 22:15:17 +00:00
|
|
|
def mount(src_path, dest_path, mounts)
|
2015-08-05 15:33:37 +00:00
|
|
|
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
|
2016-02-02 22:15:17 +00:00
|
|
|
puts ("mkdir -p #{dest_mount}")
|
2015-08-05 15:33:37 +00:00
|
|
|
FileUtils.mkdir_p(dest_mount)
|
2016-02-02 22:15:17 +00:00
|
|
|
syscall(SYS_MOUNT, src_mount.to_s, dest_mount.to_s, "none", MS_BIND, 0)
|
2015-08-05 15:33:37 +00:00
|
|
|
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")
|
2016-02-02 22:15:17 +00:00
|
|
|
mount(src_path, dest_path, mounts)
|
2015-08-05 15:33:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
main
|