Home | History | Annotate | Download | only in doc
      1 LTP namespaces helper tools
      2 ===========================
      3 
      4 
      5 1. Introduction
      6 ---------------
      7 
      8 LTP provides helper tools for creating and working with namespaces. These are
      9 located 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 
     25 Purpose of these helper tools is the ability to execute test cases utilizing
     26 namespaces even on older kernels which do not provide tooling (i.e. unshare(1)
     27 or nsenter(1) from util-linux) required for working with namespaces. The only
     28 requirement from kernel side is the support of "setns" syscall.
     29 
     30 2. Example usage
     31 ----------------
     32 
     33 The 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
     39 myns=$(ns_create net,ipc)
     40 
     41 ip link add veth0 type veth peer name veth1
     42 
     43 # Executes command 'ip a' inside the namespace specified by PID in myns variable
     44 ns_exec $myns net,ipc ip a
     45 1: 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
     49 ns_ifmove veth1 $myns
     50 ns_exec $myns net,ipc ip a
     51 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN
     52     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     53 6: 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
     57 ip link del veth0
     58 # By killing the daemonized process we also delete the namespace
     59 kill -9 $myns
     60 -------------------------------------------------------------------------------
     61