You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

100 lines
2.9 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
apt-get install -y btrfs-tools
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
## "$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