-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #296 from mariux64/add-startup-scripts
libexec_startup: Import sbin scripts from projects
- Loading branch information
Showing
3 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#! /usr/bin/bash | ||
|
||
USER=torprj | ||
ROOT=/project/tor | ||
INTERFACE=vlan.tor0 | ||
VLAN=64 | ||
IP=141.14.220.177/31 | ||
ROUTER=141.14.220.176 | ||
|
||
set -e | ||
|
||
cd $ROOT | ||
|
||
get_guardian_pid() { | ||
pid="$(cat $ROOT/var/run/guardian.pid 2>/dev/null)" || exit 1 | ||
kill -0 "$pid" 2>/dev/null || exit 1 | ||
echo "$pid" | ||
} | ||
|
||
exec_in_ns() { | ||
pid=$1 | ||
shift | ||
HOME=$ROOT/home nsenter --net --mount --user --target $pid -- "$@" | ||
} | ||
|
||
case $1 in | ||
start) | ||
pid=$(get_guardian_pid) || true | ||
if [ -z "$pid" ]; then | ||
pid=$(sudo -i -u $USER unshare --propagation unchanged --user --map-root-user --net --mount bash -c 'sleep infinity>/dev/null&echo $!') | ||
echo $pid > $ROOT/var/run/guardian.pid | ||
fi | ||
|
||
if ! exec_in_ns $pid ip link show $INTERFACE >/dev/null 2>&1; then | ||
if ! ip link show $INTERFACE >/dev/null 2>&1; then | ||
if ! ip link add link net02 name $INTERFACE type vlan id $VLAN; then | ||
echo "link may be locked in removed namespace for some minutes. try again later" >&2 | ||
exit 1 | ||
fi | ||
fi | ||
ip link set $INTERFACE netns $pid | ||
fi | ||
|
||
exec_in_ns $pid mount -t tmpfs shm /dev/shm | ||
exec_in_ns $pid mount --bind /dev/null /etc/nsswitch.conf | ||
exec_in_ns $pid ip link set lo up | ||
exec_in_ns $pid ip address add $IP dev $INTERFACE | ||
exec_in_ns $pid ip link set $INTERFACE up | ||
exec_in_ns $pid ip route add default via $ROUTER | ||
;; | ||
stop) | ||
pid=$(get_guardian_pid) || exit 0 | ||
kill $pid | ||
rm $ROOT/var/run/guardian.pid | ||
;; | ||
*) | ||
echo "usage: $0 {start|stop}" >&2 | ||
exit 1 | ||
;; | ||
esac | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#! /usr/bin/bash | ||
|
||
USER=torbprj | ||
ROOT=/project/torb | ||
INTERFACE=vlan.torb0 | ||
VLAN=87 | ||
IP=141.14.15.217/31 | ||
ROUTER=141.14.15.216 | ||
|
||
set -e | ||
|
||
cd $ROOT | ||
|
||
get_guardian_pid() { | ||
pid="$(cat $ROOT/var/run/guardian.pid 2>/dev/null)" || exit 1 | ||
kill -0 "$pid" 2>/dev/null || exit 1 | ||
echo "$pid" | ||
} | ||
|
||
exec_in_ns() { | ||
pid=$1 | ||
shift | ||
HOME=$ROOT/home nsenter --net --mount --user --target $pid -- "$@" | ||
} | ||
|
||
case $1 in | ||
start) | ||
pid=$(get_guardian_pid) || true | ||
if [ -z "$pid" ]; then | ||
pid=$(sudo -i -u $USER unshare --propagation unchanged --user --map-root-user --net --mount bash -c 'sleep infinity>/dev/null&echo $!') | ||
echo $pid > $ROOT/var/run/guardian.pid | ||
fi | ||
|
||
if ! exec_in_ns $pid ip link show $INTERFACE >/dev/null 2>&1; then | ||
if ! ip link show $INTERFACE >/dev/null 2>&1; then | ||
if ! ip link add link net02 name $INTERFACE type vlan id $VLAN; then | ||
echo "link may be locked in removed namespace for some minutes. try again later" >&2 | ||
exit 1 | ||
fi | ||
fi | ||
ip link set $INTERFACE netns $pid | ||
fi | ||
|
||
exec_in_ns $pid mount -t tmpfs shm /dev/shm | ||
exec_in_ns $pid mount --bind /dev/null /etc/nsswitch.conf | ||
exec_in_ns $pid ip link set lo up | ||
exec_in_ns $pid ip address add $IP dev $INTERFACE | ||
exec_in_ns $pid ip link set $INTERFACE up | ||
exec_in_ns $pid ip route add default via $ROUTER | ||
;; | ||
stop) | ||
pid=$(get_guardian_pid) || exit 0 | ||
kill $pid | ||
rm $ROOT/var/run/guardian.pid | ||
;; | ||
*) | ||
echo "usage: $0 {start|stop}" >&2 | ||
exit 1 | ||
;; | ||
esac | ||
|