1LTP namespaces helper tools 2=========================== 3 4 51. Introduction 6--------------- 7 8LTP provides helper tools for creating and working with namespaces. These are 9located in ltp/testcases/kernel/containers/share directory and include: 10 11* ns_create 12** creates a child process in the new specified namespace(s) 13** child is then daemonized and is running in the background 14** PID of the daemonized child process is printed on the stdout 15** the new namespace(s) is(are) maintained by the daemonized child process 16** namespace(s) can be removed by killing the daemonized process 17* setns_check 18** check for setns() availability, should be called before using ns_exec 19* ns_exec 20** enters the namespace(s) of a process specified by a PID 21** then executes the indicated program inside that namespace(s) 22* ns_ifmove 23** moves a network interface to the namespace of a process specified by a PID 24 25Purpose of these helper tools is the ability to execute test cases utilizing 26namespaces even on older kernels which do not provide tooling (i.e. unshare(1) 27or nsenter(1) from util-linux) required for working with namespaces. The only 28requirement from kernel side is the support of "setns" syscall. 29 302. Example usage 31---------------- 32 33The following code shows how test cases can use the namespaces helper tools: 34 35[source,sh] 36------------------------------------------------------------------------------- 37# Creates a new network and ipc namespace and stores the PID of the daemonized 38# process inside that namespace into variable myns 39myns=$(ns_create net,ipc) 40 41ip link add veth0 type veth peer name veth1 42 43# Executes command 'ip a' inside the namespace specified by PID in myns variable 44ns_exec $myns net,ipc ip a 451: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN 46 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 47 48# Moves interface veth1 into the namespace specified by PID in myns variable 49ns_ifmove veth1 $myns 50ns_exec $myns net,ipc ip a 511: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN 52 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 536: veth1: <BROADCAST> mtu 1500 qdisc noop state DOWN qlen 1000 54 link/ether 6a:0a:45:ed:6e:d0 brd ff:ff:ff:ff:ff:ff 55 56# cleanup 57ip link del veth0 58# By killing the daemonized process we also delete the namespace 59kill -9 $myns 60------------------------------------------------------------------------------- 61