FreedomBoxBlog

sourcecode of ps_network

Rob van der Hoeven
Sun Dec 29 2013

This sourcecode is part of the Program Space Construction Set (PSCS) which you can download at the Downloads page.

#!/bin/bash
# This script is part of the PSCS version 0.7
# Copyright (C) 2013 Rob van der Hoeven
# Support at http://freedomboxblog.nl
# License: GPL version 3 or later.

init_process=$(ps -p 1 -o comm=)
ps_name=$1

if [ "$(id -u)" != "0" ]
then
    echo "ERROR: this script must be run by root"
    exit 1
fi

if [ $init_process != "init" ]
then
    echo "ERROR: this script must run from user space"
    exit 1
fi

if [ -z "$ps_name" ]
then
    echo "ERROR: please give the name of the program space as a parameter: e.g. ps_network test1"
    exit 1
fi

# create veth pair, attach one end to br0 bridge

ps_pid=$(psc $ps_name --pid)
us_veth=us_veth$ps_pid
ps_veth=ps_veth$ps_pid

ip link add name $us_veth type veth peer name $ps_veth
ip link set $us_veth up
brctl addif br0 $us_veth

# unshare network, move ps_veth to program space

psc $ps_name --net
ip link set $ps_veth netns $ps_pid

# configure programspace interfaces

hex=$(echo "$ps_name" | md5sum)
ps_mac=00:${hex:0:2}:${hex:2:2}:${hex:4:2}:${hex:6:2}:${hex:8:2}

psc $ps_name --run bash<<PROGRAMSPACE
ip link set lo up

ip link set $ps_veth name eth1
ip link set eth1 address $ps_mac
ip link set eth1 up
dhclient eth1
exit
PROGRAMSPACE