blob: b0d9de66edd15e6aa9a12f598e3909ebe790a13a [file] [log] [blame]
Charles Chan38240872019-10-23 02:05:05 -07001#!/bin/bash
2
3# Attach to a Mininet host and run a command
4
5if [ -z $1 ]; then
6 echo "usage: $0 host cmd [args...]"
7 exit 1
8else
9 host=$1
10fi
11
12pid=`ps ax | grep "mininet:$host$" | grep bash | grep -v mnexec | awk '{print $1};'`
13
14if echo $pid | grep -q ' '; then
15 echo "Error: found multiple mininet:$host processes"
16 exit 2
17fi
18
19if [ "$pid" == "" ]; then
20 echo "Could not find Mininet host $host"
21 exit 3
22fi
23
24if [ -z $2 ]; then
25 cmd='bash'
26else
27 shift
28 cmd=$*
29fi
30
31cgroup=/sys/fs/cgroup/cpu/$host
32if [ -d "$cgroup" ]; then
33 cg="-g $host"
34fi
35
36# Check whether host should be running in a chroot dir
37rootdir="/var/run/mn/$host/root"
38if [ -d $rootdir -a -x $rootdir/bin/bash ]; then
39 cmd="'cd `pwd`; exec $cmd'"
40 cmd="chroot $rootdir /bin/bash -c $cmd"
41fi
42
43cmd="exec mnexec $cg -a $pid $cmd"
44eval $cmd