Skip to content

Instantly share code, notes, and snippets.

@caglar10ur
Last active October 7, 2016 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caglar10ur/8429502 to your computer and use it in GitHub Desktop.
Save caglar10ur/8429502 to your computer and use it in GitHub Desktop.
unprivileged.sh
#!/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