Class: OsCtl::ExportFS::Operations::Server::Spawn
- Includes:
- Lib::Utils::Log, Lib::Utils::System
- Defined in:
- lib/osctl/exportfs/operations/server/spawn.rb
Overview
Start a NFS server and wait for it to terminate
Networking is realized using a pair of veth interfaces – one on the host and one in the container. IP addresses of the NFS server is routed to the container, the container routes outbound traffic through osrtr0.
The container has its own mount, network, UTS and PID namespace, only the user namespace is shared. The rootfs is a tmpfs and all required directories and files are bind-mounted from the host, i.e. the entire /nix/store, static files in /etc and so on. All other mounts are cleared.
Instance Attribute Summary collapse
-
#cfg ⇒ Object
readonly
protected
Returns the value of attribute cfg.
-
#cgroup ⇒ Object
readonly
protected
Returns the value of attribute cgroup.
-
#netif_host ⇒ Object
readonly
protected
Returns the value of attribute netif_host.
-
#netif_ns ⇒ Object
readonly
protected
Returns the value of attribute netif_ns.
-
#rand_id ⇒ Object
readonly
protected
Returns the value of attribute rand_id.
-
#server ⇒ Object
readonly
protected
Returns the value of attribute server.
-
#sys ⇒ Object
readonly
protected
Returns the value of attribute sys.
Instance Method Summary collapse
- #add_exports ⇒ Object protected
- #clear_mounts ⇒ Object protected
- #execute ⇒ Object
-
#initialize(name) ⇒ Spawn
constructor
A new instance of Spawn.
- #run_server ⇒ Object protected
Methods inherited from Base
Constructor Details
#initialize(name) ⇒ Spawn
Returns a new instance of Spawn.
22 23 24 25 26 27 28 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 22 def initialize(name) super() @server = Server.new(name) @cfg = server.open_config @cgroup = Operations::Server::CGroup.new(server) @sys = OsCtl::Lib::Sys.new end |
Instance Attribute Details
#cfg ⇒ Object (readonly, protected)
Returns the value of attribute cfg.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def cfg @cfg end |
#cgroup ⇒ Object (readonly, protected)
Returns the value of attribute cgroup.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def cgroup @cgroup end |
#netif_host ⇒ Object (readonly, protected)
Returns the value of attribute netif_host.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def netif_host @netif_host end |
#netif_ns ⇒ Object (readonly, protected)
Returns the value of attribute netif_ns.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def netif_ns @netif_ns end |
#rand_id ⇒ Object (readonly, protected)
Returns the value of attribute rand_id.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def rand_id @rand_id end |
#server ⇒ Object (readonly, protected)
Returns the value of attribute server.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def server @server end |
#sys ⇒ Object (readonly, protected)
Returns the value of attribute sys.
91 92 93 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 91 def sys @sys end |
Instance Method Details
#add_exports ⇒ Object (protected)
250 251 252 253 254 255 256 257 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 250 def add_exports cfg.exports.group_by_as.each do |dir, as, _exports| target_as = File.join(RunState::ROOTFS, as) FileUtils.mkdir_p(target_as) sys.bind_mount(dir, target_as) end end |
#clear_mounts ⇒ Object (protected)
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 216 def clear_mounts mounts = [] whitelist = %W[ / /dev /nix/store /nix/.overlay-store /run #{RunState::ROOTFS} #{RunState::ROOTFS}/** #{RunState::SERVERS}/*/shared /run/wrappers /sys /sys/fs/cgroup /sys/fs/cgroup/* ] File.open('/proc/mounts').each_line do |line| fs, mountpoint = line.split keep = whitelist.detect do |pattern| File.fnmatch?(pattern, mountpoint) end mounts << mountpoint unless keep end mounts.sort.reverse_each do |mnt| sys.unmount_lazy(mnt) rescue Errno::ENOENT next end end |
#execute ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 30 def execute server.synchronize do raise 'server is already running' if server.running? raise 'provide server address' if cfg.address.nil? end @rand_id = SecureRandom.hex(3) netns = rand_id @netif_host = cfg.netif @netif_ns = "nfsns-#{server.name}" cgroup.enter_manager # The parent remains in the host namespace, where as the child unshares # namespaces main = Process.fork do cgroup.enter_payload Process.setpgrp # Create a new network namespace and a veth pair syscmd("ip netns add #{netns}") syscmd("ip link add #{netif_host} type veth peer name #{netif_ns}") syscmd("ip link set #{netif_ns} netns #{netns}") syscmd("ip link set #{netif_host} up") syscmd("ip route add #{cfg.address}/32 dev #{netif_host}") # Remove the named network namespace from the filesystem sys.setns_path("/run/netns/#{netns}", OsCtl::Lib::Sys::CLONE_NEWNET) sys.unmount("/run/netns/#{netns}") File.unlink("/run/netns/#{netns}") # Create new namespaces sys.unshare_ns( OsCtl::Lib::Sys::CLONE_NEWNS \ | OsCtl::Lib::Sys::CLONE_NEWUTS \ | OsCtl::Lib::Sys::CLONE_NEWIPC \ | OsCtl::Lib::Sys::CLONE_NEWPID ) # Run the server in a forked process, required by PID namespace run_server end # Forward SIGTERM and SIGINT %w[TERM INT].each do |sig| Signal.trap(sig) do Process.kill(sig, main) end end # Wait for the child to terminate and then cleanup Process.wait(main) File.unlink(server.pid_file) syscmd("ip link del #{netif_host}") log(:info, 'Killing remaining processes') cgroup.clear_payload end |
#run_server ⇒ Object (protected)
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/osctl/exportfs/operations/server/spawn.rb', line 93 def run_server main = Process.fork do # Mount the new root filesystem sys.mount_tmpfs(RunState::ROOTFS) # Add exports that were configured before the server was started add_exports # Mark all mounts as slave, in case some mounts from the parent namespace # are marked as shared, because unmounting them in this namespaces # would result in them being unmounted in the parent namespace as well. sys.make_rslave('/') # Unmount all unnecessary mounts clear_mounts # Create necessary directories in the new rootfs FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'bin')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'dev')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'etc/runit')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'proc')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, RunState::DIR)) File.chmod(0o750, File.join(RunState::ROOTFS, RunState::DIR)) FileUtils.mkdir_p(File.join(RunState::ROOTFS, RunState::CURRENT_SERVER)) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'nix/store')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'sys')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'var/lib/nfs')) FileUtils.mkdir_p(File.join(RunState::ROOTFS, 'usr/bin')) # Mount /proc, /dev, /sys and /nix/store sys.mount_proc(File.join(RunState::ROOTFS, 'proc')) sys.bind_mount('/dev', File.join(RunState::ROOTFS, 'dev')) sys.bind_mount('/nix/store', File.join(RunState::ROOTFS, 'nix/store')) sys.bind_mount('/sys', File.join(RunState::ROOTFS, 'sys')) # Symlinks for $PATH and /etc File.symlink( File.readlink('/run/current-system'), File.join(RunState::ROOTFS, 'run/current-system') ) File.symlink( File.readlink('/bin/sh'), File.join(RunState::ROOTFS, 'bin/sh') ) File.symlink( File.readlink('/usr/bin/env'), File.join(RunState::ROOTFS, 'usr/bin/env') ) File.symlink( File.readlink('/etc/static'), File.join(RunState::ROOTFS, 'etc/static') ) %w[group passwd shadow].each do |v| dst = File.join(RunState::ROOTFS, 'etc', v) File.new(dst, 'w').close sys.bind_mount(File.join('/etc', v), dst) end # Mount the current server directory, we need rbind to also mount the # shared directory sys.rbind_mount( server.dir, File.join(RunState::ROOTFS, RunState::CURRENT_SERVER) ) # Switch to the new rootfs Dir.chdir(RunState::ROOTFS) Dir.mkdir('old-root') syscmd('pivot_root . old-root') sys.chroot('.') sys.unmount_lazy('/old-root') Dir.rmdir('/old-root') server.enter_ns # Mount files and directories from the server directory to the system # where they're expected sys.bind_mount(server.nfs_state, '/var/lib/nfs') File.symlink('/run', '/var/run') %w[hosts localtime services].each do |v| File.symlink("/etc/static/#{v}", "/etc/#{v}") end File.symlink(server.exports_file, '/etc/exports') # Generate runit scripts and services Operations::Runit::Generate.run(server, cfg) # Generate NFS exports Operations::Exportfs::Generate.run(server) # Configure the network interface syscmd('ip link set lo up') syscmd("ip link set #{netif_ns} name eth0") @netif_ns = 'eth0' syscmd("ip link set #{netif_ns} up") syscmd("ip addr add #{cfg.address}/32 dev #{netif_ns}") syscmd("ip route add 255.255.255.254 dev #{netif_ns}") syscmd("ip route add default via 255.255.255.254 dev #{netif_ns}") # Instruct runit to exit on SIGCONT if File.exist?('/etc/runit/stopit') File.chmod(0o100, '/etc/runit/stopit') else File.new('/etc/runit/stopit', 'w', 0o100).close end puts 'Starting nfsd...' Process.exec('runit-init') end # Save the server's PID file File.write(server.pid_file, main.to_s) # Forward SIGTERM and SIGINT %w[TERM INT].each do |sig| Signal.trap(sig) do Process.kill('CONT', main) end end Process.wait(main) end |