platform/test: unshare the netns namespace so that root tests don't mess with the system

Mount a private sysfs instance. Otherwise gudev sees the devices from the
parent netns as opposed to our netns.

We do, however need a writable /sys/devices subtree for testing the bridge
code. There doesn't seem to be any other way to get a writable subtree of a
read-only filesystem than remounting it with no parameters after the initial
mount. We use this to get a writable sysfs instance and then bindmount it so
that it fits properly in the sysfs hierarchy.

Co-Authored-By: Thomas Haller <thaller@redhat.com>
(cherry picked from commit d6aef9c188)
This commit is contained in:
Lubomir Rintel 2015-04-19 09:12:37 +02:00 committed by Thomas Haller
parent 4609138e3e
commit 2ba6845a6e

View file

@ -1,5 +1,8 @@
#include "config.h"
#include <sys/mount.h>
#include <sched.h>
#include "test-common.h"
#include "nm-test-utils.h"
@ -296,6 +299,41 @@ main (int argc, char **argv)
#endif
}
if (nmtst_platform_is_root_test () && !g_getenv ("NMTST_NO_UNSHARE")) {
int errsv;
if (unshare (CLONE_NEWNET | CLONE_NEWNS) != 0) {
errsv = errno;
g_error ("unshare(CLONE_NEWNET|CLONE_NEWNS) failed with %s (%d)", strerror (errsv), errsv);
}
/* Mount our /sys instance, so that gudev sees only our devices.
* Needs to be read-only, because we don't run udev. */
if (mount (NULL, "/sys", NULL, MS_SLAVE, NULL) != 0) {
errsv = errno;
g_error ("mount(\"/\", MS_SLAVE) failed with %s (%d)", strerror (errsv), errsv);
}
if (mount ("sys", "/sys", "sysfs", MS_RDONLY, NULL) != 0) {
errsv = errno;
g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv);
}
/* Create a writable /sys/devices tree. This makes it possible to run tests
* that modify values via sysfs (such as bridge forward delay). */
if (mount ("sys", "/sys/devices", "sysfs", 0, NULL) != 0) {
errsv = errno;
g_error ("mount(\"/sys/devices\") failed with %s (%d)", strerror (errsv), errsv);
}
if (mount (NULL, "/sys/devices", "sysfs", MS_REMOUNT, NULL) != 0) {
errsv = errno;
g_error ("remount(\"/sys/devices\") failed with %s (%d)", strerror (errsv), errsv);
}
if (mount ("/sys/devices/devices", "/sys/devices", NULL, MS_BIND, NULL) != 0) {
errsv = errno;
g_error ("mount(\"/sys\") failed with %s (%d)", strerror (errsv), errsv);
}
}
SETUP ();
setup_tests ();