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