>_ IzzyLab

Hands-on task

This task is step 2 of the course “Containers from the inside: namespaces and cgroups”

Isolation of your own in one command

easy~10 min

Before a release the report export script is run "in quarantine". It picks its config by hostname, so it needs the name report-sandbox, and it must have no network: if the code still calls an external API, in quarantine the script fails instead of sending the data out. So two kinds of isolation are needed at once. You don't need Docker for that — the kernel gives the same isolation to any command.

Start a long-lived process (sleep infinity will do) in two new namespaces at once:

  • UTS — with its own hostname report-sandbox;
  • net — with its own empty network: no interface up except lo, and not a single route.

Leave the process running, and the server's own hostname must stay the same. Once it's running, compare the process's /proc/<PID>/ns/ with your shell's: which numbers differ and which are still shared.

What you'll practise:
  • namespaces
  • unshare
  • nsenter
  • /proc

Topic breakdown

A namespace is a kernel mechanism that gives a group of processes its own view of some resource. There are several kinds: pid (process numbers), mnt (the filesystem tree), net (interfaces, addresses, routes), uts (the hostname), ipc (shared memory and queues), user (users), cgroup (the view of the cgroup tree), time. Every process belongs to exactly one namespace of each kind. You can see that in /proc/<PID>/ns/: there's a link for every kind, and the number in it is the namespace's number (an inode). The same number for two processes means they share that namespace; different numbers mean they're isolated. unshare creates new namespaces of the chosen kinds and runs a command in them; the process inherits the other kinds from its parent. A new network namespace is empty: it has only a lo that is down, no addresses and no routes — until someone adds an interface to it. nsenter runs a command in the namespaces of a process that's already running. A Docker container is a process that got almost every kind of namespace at once (except user and time), with a virtual interface and a route to the outside placed into its network namespace.

What's checked

  1. The server's own hostname hasn't changed
  2. A process in its own UTS namespace with the name report-sandbox is running
  3. That process has its own empty network

What it looks like