Skip to content

Instantly share code, notes, and snippets.

@caglar10ur
Created January 15, 2014 03:25
Show Gist options
  • Save caglar10ur/8430231 to your computer and use it in GitHub Desktop.
Save caglar10ur/8430231 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [ ! -f /usr/bin/uidmapshift ]; then
wget https://bazaar.launchpad.net/~serge-hallyn/+junk/nsexec/download/head:/uidmapshift.c-20121030152620-2zy9rkac9y6htoia-8/uidmapshift.c
gcc -o uidmapshift uidmapshift.c
sudo mv uidmapshift /usr/bin/uidmapshift
rm -f uidmapshift.c
fi
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"
ORIGINAL="original"
CLONE="clone"
LXCPATH="$HOME/lxcpath"
lxc_conf() {
mkdir -p $LXCPATH
cat > $LXCPATH/lxc.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"
sudo lxc-start -d -n $CONTAINER -P $LXCPATH -l debug -o $CONTAINER.log
sudo lxc-attach -n $CONTAINER -P $LXCPATH -- hostname
sudo lxc-stop -n $CONTAINER -P $LXCPATH
}
destroy() {
echo -e "$OKCOLOR\tDestroying $ORIGINAL and $CLONE $NOCOLOR"
sudo lxc-destroy -n $CLONE -P $LXCPATH
sudo lxc-destroy -n $ORIGINAL -P $LXCPATH
sudo rm -f $ORIGINAL.log $CLONE.log
sudo rm -rf $LXCPATH
}
uidshift() {
CONTAINER=$1
DIRECTORY=$2
sudo uidmapshift -u $LXCPATH/$CONTAINER/$DIRECTORY 0 $uid $range
sudo uidmapshift -g $LXCPATH/$CONTAINER/$DIRECTORY 0 $gid $range
}
original() {
BACKEND=$1
echo -e "$OKCOLOR\tCreating $ORIGINAL with $BACKEND $NOCOLOR"
lxc_conf
sudo lxc-create -q -B $BACKEND -t ubuntu -n $ORIGINAL -P $LXCPATH -f $LXCPATH/lxc.conf
if [ $? -eq 0 ]; then
uidshift $ORIGINAL rootfs
start_attach_stop $ORIGINAL
fi
}
clone() {
BACKEND=$1
echo -e "$OKCOLOR\tCloning $CLONE with $BACKEND $NOCOLOR"
sudo lxc-clone -s -B $BACKEND -p $LXCPATH $ORIGINAL $CLONE > /dev/null
if [ $? -eq 0 ]; then
uidshift $CLONE rootfs
if [ $BACKEND == "overlayfs" ]; then
uidshift $CLONE delta0
fi
start_attach_stop $CLONE
fi
}
original dir
# this will require a patched kernel
clone overlayfs
destroy
original btrfs
clone btrfs
destroy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment