143 lines
4.5 KiB

#!/bin/bash
##
## btrfs install
##
#BTRFS_DEVICE=
BTRFS_MOUNT_ROOT=${BTRFS_MOUNT_ROOT:-"/mnt/btrfs-root"}
if [ -z "$BTRFS_DEVICE" ]; then
echo "You must set a BTRFS_DEVICE environment variable prior to executing this hook."
exit 1
fi
if [ "$FORCE" != "yes" ]; then
echo "the following is dangerous code. Please execute with FORCE=yes."
echo "it DELETES directory /var/lib/docker if you have one."
exit 1
fi
## Install latest kernel
if [ "$UPDATE_KERNEL" ]; then
case "$UPDATE_KERNEL" in
ovh)
apt-get install -y lftp </dev/null
(
OVH_KERNELS_FTP=ftp://ftp.ovh.net/made-in-ovh/bzImage/${OVH_KERNEL_VERSION:-latest-experimental}
OVH_KERNEL_REGEX='.*-xxxx-grs-ipv6-64$'
lftp -e "lcd /boot/; mirror --only-newer -i '$OVH_KERNEL_REGEX'; bye" "$OVH_KERNELS_FTP" &&
cd /boot && update-grub
) || exit 1
;;
ubuntu)
## check: http://askubuntu.com/a/257624/21888
#http://kernel.ubuntu.com/~kernel-ppa/mainline/
echo "ubuntu not supported yet" >&2
exit 1
;;
*)
echo "Unknown \$UPDATE_KERNEL method '$UPDATE_KERNEL'." >&2
exit 1
esac
fi
if [ "$UPDATE_BTRFS_TOOLS" ]; then
## Install latests ``btrfs-tools``:
(
apt-get remove -y btrfs-tools </dev/null || true
mkdir /root/dev/c -p &&
cd /root/dev/c &&
git clone git://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git &&
cd btrfs-progs &&
apt-get install -y asciidoc xmlto --no-install-recommends </dev/null &&
apt-get install -y build-essential autoconf pkg-config uuid-dev libattr1-dev \
zlib1g-dev libacl1-dev e2fslibs-dev libblkid-dev liblzo2-dev </dev/null &&
./autogen.sh &&
./configure --prefix=/opt/apps/btrfs-tools &&
make &&
make install &&
ln -sf /opt/apps/btrfs-tools/bin/* /usr/local/bin/
) || exit 1
else
apt-get install -y btrfs-tools </dev/null
fi
## "$BTRFS_DEVICE" device should not be mounted
if mount | egrep ^"$BTRFS_DEVICE\s+" >/dev/null 2>&1; then
umount "$BTRFS_DEVICE" || {
echo "Can't umount $BTRFS_DEVICE. Aborting script."
exit 1
}
echo "Unmounted $BTRFS_DEVICE."
fi
if egrep ^"$BTRFS_DEVICE\s+" /etc/fstab >/dev/null 2>&1; then
sed -r -i "\%^$BTRFS_DEVICE\s+%d" /etc/fstab || {
echo "Couldn't remove device $BTRFS_DEVICE from fstab."
exit 1
}
echo "Removed device $BTRFS_DEVICE from fstab."
fi
## Format the device and add entry in fstab
mkfs.btrfs -f "$BTRFS_DEVICE"
## No need of UID it seems:
# UUID="$(blkid -s UUID $BTRFS_DEVICE -o value)"
# echo "UUID=$UUID $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
echo "$BTRFS_DEVICE $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
## Mount point and mount device
mkdir "$BTRFS_MOUNT_ROOT" -p
mount "$BTRFS_MOUNT_ROOT"
if [ -d /var/lib/docker ] ; then
RESTART_DOCKER=yes
service docker stop
## XXXvlab: moving doesn't work and is not desirable, as we want docker
## to setup and detect new underlying btrfs system.
# mv "/var/lib/docker/"* "$BTRFS_MOUNT_ROOT/var/lib/docker"
rm -rf /var/lib/docker/*
fi
## Build subvolume structure
for d in /home /var{/{lib,cache,backups}/lxc,/lib/docker} \
/var/backups/snapshot \
/srv/datastore{,/config,/data}; do
mkdir -p "$(dirname "$BTRFS_MOUNT_ROOT$d")" ## creates parent directory of subvolume
btrfs subvolume create "$BTRFS_MOUNT_ROOT$d"
mkdir -p "$d"
binds=$(cat /etc/fstab | egrep '\s+none\s+' | grep bind | grep -v '^\s+#' | sed -r 's/^\s*([^ ]+).*$/\1/g')
for b in $binds; do
if [[ "$BTRFS_MOUNT_ROOT$d/" == "$b/"* ]]; then
echo "Directory '$d' is already available via bind '$b'."
continue 2
fi
done
## Add bind to /etc/fstab
echo "$BTRFS_MOUNT_ROOT$d $d none bind,defaults,auto 0 0" >> /etc/fstab
done
## Mount all binds
mount -a
[ -z "$RESTART_DOCKER" ] || service docker start
## Prevent mlocate from fetching unwanted informations
if [ -e "/etc/updatedb.conf" ]; then
## Without this, especially backup host can have 18Go locate database.
sed -ri 's/^\s*#\s*PRUNENAMES=/PRUNENAMES=/g;s%^(\s*PRUNEPATHS=".*)("\s*$)+%\1 /mnt/btrfs-root /var/backup/lxc /var/backup/snapshot\2%g' /etc/updatedb.conf
fi