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