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

  1. #!/bin/bash
  2. ##
  3. ## btrfs install
  4. ##
  5. #BTRFS_DEVICE=
  6. BTRFS_MOUNT_ROOT=${BTRFS_MOUNT_ROOT:-"/mnt/btrfs-root"}
  7. if [ -z "$BTRFS_DEVICE" ]; then
  8. echo "You must set a BTRFS_DEVICE environment variable prior to executing this hook."
  9. exit 1
  10. fi
  11. apt-get install -y btrfs-tools
  12. if [ "$FORCE" != "yes" ]; then
  13. echo "the following is dangerous code. Please execute with FORCE=yes."
  14. echo "it DELETES directory /var/lib/docker if you have one."
  15. exit 1
  16. fi
  17. ## "$BTRFS_DEVICE" device should not be mounted
  18. if mount | egrep ^"$BTRFS_DEVICE\s+" >/dev/null 2>&1; then
  19. umount "$BTRFS_DEVICE" || {
  20. echo "Can't umount $BTRFS_DEVICE. Aborting script."
  21. exit 1
  22. }
  23. echo "Unmounted $BTRFS_DEVICE."
  24. fi
  25. if egrep ^"$BTRFS_DEVICE\s+" /etc/fstab >/dev/null 2>&1; then
  26. sed -r -i "\%^$BTRFS_DEVICE\s+%d" /etc/fstab || {
  27. echo "Couldn't remove device $BTRFS_DEVICE from fstab."
  28. exit 1
  29. }
  30. echo "Removed device $BTRFS_DEVICE from fstab."
  31. fi
  32. ## Format the device and add entry in fstab
  33. mkfs.btrfs -f "$BTRFS_DEVICE"
  34. ## No need of UID it seems:
  35. # UUID="$(blkid -s UUID $BTRFS_DEVICE -o value)"
  36. # echo "UUID=$UUID $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  37. echo "$BTRFS_DEVICE $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  38. ## Mount point and mount device
  39. mkdir "$BTRFS_MOUNT_ROOT" -p
  40. mount "$BTRFS_MOUNT_ROOT"
  41. if [ -d /var/lib/docker ] ; then
  42. RESTART_DOCKER=yes
  43. service docker stop
  44. ## XXXvlab: moving doesn't work and is not desirable, as we want docker
  45. ## to setup and detect new underlying btrfs system.
  46. # mv "/var/lib/docker/"* "$BTRFS_MOUNT_ROOT/var/lib/docker"
  47. rm -rf /var/lib/docker/*
  48. fi
  49. ## Build subvolume structure
  50. for d in /home /var{/{lib,cache,backups}/lxc,/lib/docker} \
  51. /var/backups/snapshot \
  52. /srv/datastore{,/config,/data}; do
  53. mkdir -p "$(dirname "$BTRFS_MOUNT_ROOT$d")" ## creates parent directory of subvolume
  54. btrfs subvolume create "$BTRFS_MOUNT_ROOT$d"
  55. mkdir -p "$d"
  56. binds=$(cat /etc/fstab | egrep '\s+none\s+' | grep bind | grep -v '^\s+#' | sed -r 's/^\s*([^ ]+).*$/\1/g')
  57. for b in $binds; do
  58. if [[ "$BTRFS_MOUNT_ROOT$d/" == "$b/"* ]]; then
  59. echo "Directory '$d' is already available via bind '$b'."
  60. continue 2
  61. fi
  62. done
  63. ## Add bind to /etc/fstab
  64. echo "$BTRFS_MOUNT_ROOT$d $d none bind,defaults,auto 0 0" >> /etc/fstab
  65. done
  66. ## Mount all binds
  67. mount -a
  68. [ -z "$RESTART_DOCKER" ] || service docker start
  69. ## Prevent mlocate from fetching unwanted informations
  70. if [ -e "/etc/updatedb.conf" ]; then
  71. ## Without this, especially backup host can have 18Go locate database.
  72. 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
  73. fi