Download pdf - Namespaces in Linux

Transcript
Page 1: Namespaces in Linux

Namespaces in Linux

Ľubomír RintelGoodData Q1 off-site Harrachov 2014

Page 2: Namespaces in Linux

UNIX processes● Virtualization

– Virtual CPU and memory– Consistently accessible devices

● Shared resources– Runtime configuration– Communication channels– Filesystem– Privileges, credentials

pid_t pid = fork ();if (pid) { <parent>} else { <child>}

Page 3: Namespaces in Linux

What about threads?

Page 4: Namespaces in Linux

Sharing more

● Sharing resources and state– Address space– Signal handlers– Open file handles– CWD, umask(), ...

Page 5: Namespaces in Linux

Linux processes

● Threads are processes● Process: own resources & state● Thread: shared resources & state

pid_t pid = clone (<what_to_share>);CLONE_VM Address spaceCLONE_FILES Open filesCLONE_FS CWD, umask(), ...

...SEE ALSO: unshare(2)

Page 6: Namespaces in Linux

...and what about containers?

Page 7: Namespaces in Linux

Containers

● Virtualization● Less sharing● More separation

Sharing is not caring.Your mother was wrong!

Page 8: Namespaces in Linux

Namespaces

● Containers are to processes what processes are to threads

pid_t pid = clone (<what_to_share>);CLONE_NEWUTS Hostname, domainnameCLONE_NEWIPC SysV IPC objectsCLONE_NEWPID Process IDsCLONE_NEWNET Network configurationCLONE_NEWNS File system mountsCLONE_NEWUSER User and Group IDs

SEE ALSO: setns(2)

Page 9: Namespaces in Linux

UTS namespace

● CLONE_NEWUTS● CONFIG_UTS_NS since Linux 2.6.19● needs CAP_SYS_ADMIN● hostname● domainname

Page 10: Namespaces in Linux

SysV IPC namespace

● CLONE_NEWIPC● CONFIG_IPC_NS since 2.6.19● Obsolete System V UNIX IPC mechanisms:● semaphores● shared memory● message queues

Page 11: Namespaces in Linux

PID namespace

● CLONE_NEWPID● CONFIG_PID_NS since Linux 2.6.24● a different PID visible from within namespace

than from outside● new PID 1

Page 12: Namespaces in Linux

Network namespace● CLONE_NEWNET● CONFIG_NET_NS since Linux 2.6.29● separate network stack

– network addresses– nftables/netfilter rules– loopback interface for namespace

● veth interface (CONFIG_VETH), ip netns

Page 13: Namespaces in Linux

Mount namespace

● CLONE_NEWNS● First namespace, since 2.4.19● /proc/<pid>/mounts instead of /proc/mounts● In Fedora, run mount --make-private /

or create new user NS

Page 14: Namespaces in Linux

User namespace● CLONE_NEWUSER● CONFIG_USER_NS since 2.6.23● Unprivileged since 3.8, still disabled by default● a different UID/GID visible from within namespace than from outside● all capabilities within namespace

– limited by capabilities in parent namespace● can be combined with other namespaces● Mapping of ranges via /proc/<pid>/uid_map /proc/<pid>/gid_map

– Unprivileged user can map theirselves

Page 15: Namespaces in Linux

LXC: Lightweight containers

● Container management toolset● Create namespaces● Configure networking● Resource management with control groups● Integrated with libvirt

Page 16: Namespaces in Linux

Docker

Page 17: Namespaces in Linux

systemd-nspawn

● Quick way to boot a container● Can be run from a service unit in a separate

cgroup

Page 18: Namespaces in Linux

Future

● CONFIG_USER_NS=y by default● Userspace for multiple UIDs (ranges) per user● Syslog namespace

Page 19: Namespaces in Linux

Questions?

Page 20: Namespaces in Linux

What else?

● Auditing & SELinux● Checkpoint & Restore in userspace● fakeroot

Page 21: Namespaces in Linux

Further reading● Configuring network namespaces with iproute2's

ip netns: http://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/

● Mike Kerrisk's LWN series on namespaces: http://lwn.net/Articles/531114/

● Rami Rosen's great Namespaces/Cgroups lecturehttp://www.haifux.org/lectures/299/netLec7.pdf


Recommended