lxc-config/hooks/update-mounts

60 lines
1.5 KiB
Plaintext
Raw Normal View History

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")
def fstab_entries(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)
puts ("mkdir -p #{src_mount}")
if dest == true
dest_mount = src_path.join(src)
else
dest_mount = src_path.join(dest.gsub(/^\//, ""))
end
FileUtils.mkdir_p(dest_mount)
"#{src_mount} #{dest_mount} none bind,nofail,x-systemd.device-timeout=1 0 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?
fstab = []
containers.each do |dest_container, mounts|
dest_path = LXC_ROOT.join(dest_container, "rootfs")
fstab << fstab_entries(src_path, dest_path, mounts)
end
content = fstab.join("\n")
path = FSTAB_D.join("50_lxc_#{name}")
f = File.open(path, "w+")
f.write content
f.write "\n"
f.close
Lxc::Utils.sh("update-conf.d", "fstab")
Lxc::Utils.sh("mount", "-a")
end
end
main