143 lines
4.5 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. if [ "$FORCE" != "yes" ]; then
  12. echo "the following is dangerous code. Please execute with FORCE=yes."
  13. echo "it DELETES directory /var/lib/docker if you have one."
  14. exit 1
  15. fi
  16. ## Install latest kernel
  17. if [ "$UPDATE_KERNEL" ]; then
  18. case "$UPDATE_KERNEL" in
  19. ovh)
  20. apt-get install -y lftp </dev/null
  21. (
  22. OVH_KERNELS_FTP=ftp://ftp.ovh.net/made-in-ovh/bzImage/${OVH_KERNEL_VERSION:-latest-experimental}
  23. OVH_KERNEL_REGEX='.*-xxxx-grs-ipv6-64$'
  24. lftp -e "lcd /boot/; mirror --only-newer -i '$OVH_KERNEL_REGEX'; bye" "$OVH_KERNELS_FTP" &&
  25. cd /boot && update-grub
  26. ) || exit 1
  27. ;;
  28. ubuntu)
  29. ## check: http://askubuntu.com/a/257624/21888
  30. #http://kernel.ubuntu.com/~kernel-ppa/mainline/
  31. echo "ubuntu not supported yet" >&2
  32. exit 1
  33. ;;
  34. *)
  35. echo "Unknown \$UPDATE_KERNEL method '$UPDATE_KERNEL'." >&2
  36. exit 1
  37. esac
  38. fi
  39. if [ "$UPDATE_BTRFS_TOOLS" ]; then
  40. ## Install latests ``btrfs-tools``:
  41. (
  42. apt-get remove -y btrfs-tools </dev/null || true
  43. mkdir /root/dev/c -p &&
  44. cd /root/dev/c &&
  45. git clone git://git.kernel.org/pub/scm/linux/kernel/git/kdave/btrfs-progs.git &&
  46. cd btrfs-progs &&
  47. apt-get install -y asciidoc xmlto --no-install-recommends </dev/null &&
  48. apt-get install -y build-essential autoconf pkg-config uuid-dev libattr1-dev \
  49. zlib1g-dev libacl1-dev e2fslibs-dev libblkid-dev liblzo2-dev </dev/null &&
  50. ./autogen.sh &&
  51. ./configure --prefix=/opt/apps/btrfs-tools &&
  52. make &&
  53. make install &&
  54. ln -sf /opt/apps/btrfs-tools/bin/* /usr/local/bin/
  55. ) || exit 1
  56. else
  57. apt-get install -y btrfs-tools </dev/null
  58. fi
  59. ## "$BTRFS_DEVICE" device should not be mounted
  60. if mount | egrep ^"$BTRFS_DEVICE\s+" >/dev/null 2>&1; then
  61. umount "$BTRFS_DEVICE" || {
  62. echo "Can't umount $BTRFS_DEVICE. Aborting script."
  63. exit 1
  64. }
  65. echo "Unmounted $BTRFS_DEVICE."
  66. fi
  67. if egrep ^"$BTRFS_DEVICE\s+" /etc/fstab >/dev/null 2>&1; then
  68. sed -r -i "\%^$BTRFS_DEVICE\s+%d" /etc/fstab || {
  69. echo "Couldn't remove device $BTRFS_DEVICE from fstab."
  70. exit 1
  71. }
  72. echo "Removed device $BTRFS_DEVICE from fstab."
  73. fi
  74. ## Format the device and add entry in fstab
  75. mkfs.btrfs -f "$BTRFS_DEVICE"
  76. ## No need of UID it seems:
  77. # UUID="$(blkid -s UUID $BTRFS_DEVICE -o value)"
  78. # echo "UUID=$UUID $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  79. echo "$BTRFS_DEVICE $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  80. ## Mount point and mount device
  81. mkdir "$BTRFS_MOUNT_ROOT" -p
  82. mount "$BTRFS_MOUNT_ROOT"
  83. if [ -d /var/lib/docker ] ; then
  84. RESTART_DOCKER=yes
  85. service docker stop
  86. ## XXXvlab: moving doesn't work and is not desirable, as we want docker
  87. ## to setup and detect new underlying btrfs system.
  88. # mv "/var/lib/docker/"* "$BTRFS_MOUNT_ROOT/var/lib/docker"
  89. rm -rf /var/lib/docker/*
  90. fi
  91. ## Build subvolume structure
  92. for d in /home /var{/{lib,cache,backups}/lxc,/lib/docker} \
  93. /var/backups/snapshot \
  94. /srv/datastore{,/config,/data}; do
  95. mkdir -p "$(dirname "$BTRFS_MOUNT_ROOT$d")" ## creates parent directory of subvolume
  96. btrfs subvolume create "$BTRFS_MOUNT_ROOT$d"
  97. mkdir -p "$d"
  98. binds=$(cat /etc/fstab | egrep '\s+none\s+' | grep bind | grep -v '^\s+#' | sed -r 's/^\s*([^ ]+).*$/\1/g')
  99. for b in $binds; do
  100. if [[ "$BTRFS_MOUNT_ROOT$d/" == "$b/"* ]]; then
  101. echo "Directory '$d' is already available via bind '$b'."
  102. continue 2
  103. fi
  104. done
  105. ## Add bind to /etc/fstab
  106. echo "$BTRFS_MOUNT_ROOT$d $d none bind,defaults,auto 0 0" >> /etc/fstab
  107. done
  108. ## Mount all binds
  109. mount -a
  110. [ -z "$RESTART_DOCKER" ] || service docker start
  111. ## Prevent mlocate from fetching unwanted informations
  112. if [ -e "/etc/updatedb.conf" ]; then
  113. ## Without this, especially backup host can have 18Go locate database.
  114. 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
  115. fi