Last active
October 7, 2016 16:12
-
-
Save caglar10ur/8429502 to your computer and use it in GitHub Desktop.
unprivileged.sh
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
#!/bin/bash | |
uid=$(grep $USER /etc/subuid | cut -d : -f 2) | |
gid=$(grep $USER /etc/subgid | cut -d : -f 2) | |
range=$(grep $USER /etc/subuid | cut -d : -f 3) | |
NOCOLOR="\033[0m" | |
OKCOLOR="\033[0;32m" | |
UNPRIVILEGED="unpriv" | |
LXCPATH="$HOME/.local/share/lxc/" | |
CONFIGPATH="$HOME/.config/lxc/" | |
lxc_conf() { | |
mkdir -p $CONFIGPATH | |
cat > $CONFIGPATH/default.conf << EOF | |
lxc.network.type = veth | |
lxc.network.link = lxcbr0 | |
lxc.network.flags = up | |
lxc.id_map = u 0 $uid $range | |
lxc.id_map = g 0 $gid $range | |
EOF | |
} | |
start_attach_stop() { | |
CONTAINER=$1 | |
echo -e "$OKCOLOR\tStarting/Attaching/Stopping to $CONTAINER $NOCOLOR" | |
echo 1 | sudo tee -a /sys/fs/cgroup/memory/memory.use_hierarchy > /dev/null | |
for entry in /sys/fs/cgroup/*/cgroup.clone_children; do | |
echo 1 | sudo tee -a $entry > /dev/null | |
done | |
for controller in /sys/fs/cgroup/*; do | |
sudo mkdir -p $controller/$USER | |
sudo chown -R $USER $controller/$USER | |
echo $$ > $controller/$USER/tasks | |
done | |
lxc-start -d -n $CONTAINER -l debug -o $CONTAINER.log | |
lxc-attach -n $CONTAINER -P $LXCPATH -- hostname | |
lxc-stop -n $CONTAINER | |
} | |
destroy() { | |
echo -e "$OKCOLOR\tDestroying $UNPRIVILEGED $NOCOLOR" | |
lxc-destroy -n $UNPRIVILEGED | |
rm -f $UNPRIVILEGED.log | |
} | |
unprivileged() { | |
BACKEND=$1 | |
echo -e "$OKCOLOR\tCreating $UNPRIVILEGED with $BACKEND $NOCOLOR" | |
lxc_conf | |
lxc-create -B $BACKEND -t download -n $UNPRIVILEGED -- -d ubuntu -r saucy -a amd64 --force-cache | |
if [ $? -eq 0 ]; then | |
start_attach_stop $UNPRIVILEGED | |
fi | |
} | |
unprivileged dir | |
destroy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment