aboutsummaryrefslogtreecommitdiffstats
path: root/iurt_root_command
diff options
context:
space:
mode:
Diffstat (limited to 'iurt_root_command')
-rwxr-xr-xiurt_root_command30
1 files changed, 29 insertions, 1 deletions
diff --git a/iurt_root_command b/iurt_root_command
index 3104062..8c1b265 100755
--- a/iurt_root_command
+++ b/iurt_root_command
@@ -30,6 +30,7 @@ use Cwd 'realpath';
use File::Path qw(make_path);
use File::Slurp;
use String::Escape;
+use File::Basename;
my $arg = @ARGV;
my (@params, %run);
@@ -161,6 +162,14 @@ $run{todo} = [];
"create a btrfs snapshot",
\&btrfs_snapshot, "btrfs snapshot" ],
+ [ "", "netns_create", 1, "<directory>",
+ "create a network namespace",
+ \&netfs_create, "Create network namespace for given chroot" ],
+
+ [ "", "netns_delete", 1, "<directory>",
+ "delete a network namespace",
+ \&netfs_delete, "Delete network namespace for given chroot, killing all processes" ],
+
[ "", "useradd", 3, "<directory> <username> [uid]",
"Add user in given chroot",
\&useradd, "Useradd" ],
@@ -404,6 +413,20 @@ sub btrfs_snapshot {
return !system("btrfs", "subvolume", "snapshot", $source, $dest);
}
+sub netfs_create {
+ my ($_run, $dest) = @_;
+ check_path_authorized($dest) or return;
+ return !system("ip", "netns", "add", basename($dest));
+}
+
+sub netfs_delete {
+ my ($_run, $dest) = @_;
+ check_path_authorized($dest) or return;
+ my $nsname = basename($dest);
+ system("ip netns pids $nsname | xargs -r kill -9");
+ return !system("ip", "netns", "del", $nsname);
+}
+
sub bindmount {
my ($_run, $source, $dest) = @_;
check_path_authorized($dest) or return;
@@ -484,5 +507,10 @@ sub run_chroot {
}
check_path_authorized($dir) or return;
- return !system("chroot", $dir, @options);
+ my $nsname = basename($dir);
+ if (!system("ip netns list | grep -q '^$nsname\$'")) {
+ return !system("ip", "netns", "exec", $nsname, "chroot", $dir, @options);
+ } else {
+ return !system("chroot", $dir, @options);
+ }
}