#!/bin/bash -x # Description : Demo scripts for ubuntu # Writer : jaeminj@gmail.com # Refer the follwoings. # https://www.kernel.org/doc/Documentation/cgroups/cpusets.txt # http://lxc.sourceforge.net/old/index.php/about/kernel-namespaces/network/configuration/ # http://blog.scottlowe.org/2013/09/04/introducing-linux-network-namespaces/ # https://doc.opensuse.org/documentation/html/openSUSE_121/opensuse-tuning/cha.tuning.cgroups.html # http://www.axeman.in/blog/2014/12/09/build-your-own-lxc-contain-it-yourself/ # http://www.linux-kongress.org/2010/slides/seyfried-cgroups-linux-kongress-2010-presentation.pdf if [ -f global.conf.sh ] ; then source global.conf.sh else UUID=$(cat /proc/sys/kernel/random/uuid) cat > global.conf.sh < cpuset.cpus echo 0 > cpuset.mems echo $PPID > tasks popd } setCgroupMemory() { #mount -t cgroup sys/fs/cgroup mkdir -p /sys/fs/cgroup/memory/$UUID pushd /sys/fs/cgroup/memory/$UUID cat memory.usage_in_bytes echo cat memory.limit_in_bytes cat memory.limit_in_bytes echo $PPID > tasks popd pushd /sys/fs/cgroup/memory/$UUID echo $1 > memory.limit_in_bytes cat memory.limit_in_bytes popd } DisableByRoute() { ip link delete veth0 if [ "$(ip netns pids $UUID)" != "" ] ; then ip netns pids $UUID | xargs kill fi ip netns del $UUID iptables -t nat -I POSTROUTING -s 10.1.1.1 ! -d 10.1.1.1 -j MASQUERADE } EnableByBridge() { ip link add type veth ifconfig veth0 up brctl addbr br0 ifconfig br0 192.168.0.1/24 up brctl addif br0 eth0 ifconig eth0 0.0.0.0 brctl addif br0 veth0 ip link set veth1 netns $UUID ip netns exec $UUID ifconfig veth1 192.168.0.102/24 up ip netns exec $UUID ifconfig lo up } DisableByBridge() { ip link delete veth0 ip link delete veth1 ifconfig br0 down brctl delbr br0 ip netns pids | xargs kill ip netns del $UUID } case "$1" in mkrootfs) mkRootfs ;; ifup) EnableByRoute ;; ifdown) DisableByRoute ;; mount) mountGuest ;; status) cat /proc/self/cgroup ip link show ;; umount) umountGuest ;; cpu0) setCgroupCpuset 0 ;; mem0) setCgroupMemory 128M ;; boot) EnableByRoute setCgroupCpuset 0 setCgroupMemory 128M mountGuest ;; shutdown) umountGuest DisableByRoute ;; *) cat <