Skip to content

Instantly share code, notes, and snippets.

@nl5887
Last active June 17, 2018 18:47
Show Gist options
  • Select an option

  • Save nl5887/9b26ef8dfa5b7c1247bc09bb46175346 to your computer and use it in GitHub Desktop.

Select an option

Save nl5887/9b26ef8dfa5b7c1247bc09bb46175346 to your computer and use it in GitHub Desktop.
ip -all netns delete
ip link |grep LINK|awk '{print $2}'|sed s/\@.\*//g > /tmp/links.txt
while read line
do
echo "Deleting link $line"
ip link delete $line
done < /tmp/links.txt
runsc list 2>/dev/null|grep ht_|awk '{print $1}' > /tmp/containers.txt
while read line
do
echo "Deleting container $line"
runsc delete --force $line
done < /tmp/containers.txt
{
"ociVersion": "1.0.0",
"process": {
"user": {
"uid": 0,
"gid": 0
},
"args": [
"dropbear",
"-F",
"-p",
"8022",
"-R",
"-E"
],
"env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"HOSTNAME=7ff99a5a341a"
],
"terminal": true,
"consoleSize": {
"height": 80,
"width": 80
},
"cwd": "/",
"capabilities": {
},
"apparmorProfile": "docker-default",
"oomScoreAdj": 0
},
"root": {
"path": "/chroot"
},
"hostname": "7ff99a5a341a",
"mounts": [
{
"destination": "/proc",
"type": "proc",
"source": "proc",
"options": [
"nosuid",
"noexec",
"nodev"
]
},
{
"destination": "/dev",
"type": "tmpfs",
"source": "tmpfs",
"options": [
"nosuid",
"strictatime",
"mode=755",
"size=65536k"
]
},
{
"destination": "/dev/pts",
"type": "devpts",
"source": "devpts",
"options": [
"nosuid",
"noexec",
"newinstance",
"ptmxmode=0666",
"mode=0620",
"gid=5"
]
},
{
"destination": "/sys",
"type": "sysfs",
"source": "sysfs",
"options": [
"nosuid",
"noexec",
"nodev",
"ro"
]
},
{
"destination": "/sys/fs/cgroup",
"type": "cgroup",
"source": "cgroup",
"options": [
"ro",
"nosuid",
"noexec",
"nodev"
]
},
{
"destination": "/dev/mqueue",
"type": "mqueue",
"source": "mqueue",
"options": [
"nosuid",
"noexec",
"nodev"
]
},
{
"destination": "/etc/resolv.conf",
"type": "bind",
"source": "/var/lib/docker/containers/7ff99a5a341a21b6a4d76b6f049c09a3bd5d05d22bdb7e6cd47f9e8f5e61d87f/resolv.conf",
"options": [
"rbind",
"rprivate"
]
},
{
"destination": "/etc/hostname",
"type": "bind",
"source": "/var/lib/docker/containers/7ff99a5a341a21b6a4d76b6f049c09a3bd5d05d22bdb7e6cd47f9e8f5e61d87f/hostname",
"options": [
"rbind",
"rprivate"
]
},
{
"destination": "/etc/hosts",
"type": "bind",
"source": "/var/lib/docker/containers/7ff99a5a341a21b6a4d76b6f049c09a3bd5d05d22bdb7e6cd47f9e8f5e61d87f/hosts",
"options": [
"rbind",
"rprivate"
]
},
{
"destination": "/dev/shm",
"type": "bind",
"source": "/var/lib/docker/containers/7ff99a5a341a21b6a4d76b6f049c09a3bd5d05d22bdb7e6cd47f9e8f5e61d87f/shm",
"options": [
"rbind",
"rprivate"
]
}
],
"hooks": {
"prestart": []
},
"linux": {
"resources": {
"devices": [
{
"allow": false,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 1,
"minor": 5,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 1,
"minor": 3,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 1,
"minor": 9,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 1,
"minor": 8,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 5,
"minor": 0,
"access": "rwm"
},
{
"allow": true,
"type": "c",
"major": 5,
"minor": 1,
"access": "rwm"
},
{
"allow": false,
"type": "c",
"major": 10,
"minor": 229,
"access": "rwm"
}
],
"memory": {
},
"cpu": {
"shares": 0
},
"pids": {
"limit": 0
},
"blockIO": {
"weight": 0
}
},
"cgroupsPath": "/docker/7ff99a5a341a21b6a4d76b6f049c09a3bd5d05d22bdb7e6cd47f9e8f5e61d87f",
"namespaces": [
{
"type": "mount"
},
{
"type": "network",
"path": "/var/run/netns/NS1"
},
{
"type": "uts"
},
{
"type": "pid"
},
{
"type": "ipc"
}
],
"maskedPaths": [
"/proc/kcore",
"/proc/latency_stats",
"/proc/timer_list",
"/proc/timer_stats",
"/proc/sched_debug",
"/proc/scsi",
"/sys/firmware"
],
"readonlyPaths": [
"/proc/asound",
"/proc/bus",
"/proc/fs",
"/proc/irq",
"/proc/sys",
"/proc/sysrq-trigger"
]
}
}
NS_NAME="NS"
NS_NUMBER=5
NS_IPADDR_BASE="10.0.3"
NS_IPADDR_START=100
NS_IPADDR_NETWORK=${NS_IPADDR_BASE}".0"
NS_GATEWAY=${NS_IPADDR_BASE}".1"
HOST_ETH=`ip route | awk '/default/ { print $5 }'`
NS_ETH="eth0"
NETWORK_NAME="br0"
i=1
ip link delete ${NS_NAME}${i}
# Create network namespace
ip netns add ${NS_NAME}${i}
# Create a Linkl
ip link add ${NS_ETH} type veth peer name LINK_${NS_NAME}${i}
# Connect the link to the network namespace and give it a name eth0
ip link set ${NS_ETH} netns ${NS_NAME}${i}
# Set network namespace eth0 local IP address
ip netns exec ${NS_NAME}${i} ip addr add ${NS_IPADDR_BASE}.$((NS_IPADDR_START + i))/24 dev ${NS_ETH}
# Bring up network namespace eth0 interface
ip netns exec ${NS_NAME}${i} ip link set ${NS_ETH} up
# Bring up network namespace loopback interface
ip netns exec ${NS_NAME}${i} ip link set lo up
# Set network namespace default IP gateway
ip netns exec ${NS_NAME}${i} ip route add default via ${NS_GATEWAY}
# Connect the namespace link to the network bridge
ip link set LINK_${NS_NAME}${i} master ${NETWORK_NAME}
# Bring up network interface with the namespace
ip link set LINK_${NS_NAME}${i} up
rm -rf /etc/netns/${NS_NAME}${i}
# Setup namespace specific /etc
mkdir -p /etc/netns/${NS_NAME}${i}
echo 'nameserver 8.8.8.8' > /etc/netns/${NS_NAME}${i}/resolv.conf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment