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.

144 lines
4.6 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 sphinx --no-install-recommends </dev/null &&
  48. apt-get install -y build-essential autoconf pkg-config uuid-dev libattr1-dev \
  49. zlib1g-dev python3-dev python3-setuptools libacl1-dev e2fslibs-dev \
  50. libblkid-dev liblzo2-dev libzstd-dev libudev-dev </dev/null &&
  51. ./autogen.sh &&
  52. ./configure --prefix=/opt/apps/btrfs-tools --disable-zoned &&
  53. make &&
  54. make install &&
  55. ln -sf /opt/apps/btrfs-tools/bin/* /usr/local/bin/
  56. ) || exit 1
  57. else
  58. apt-get install -y btrfs-tools </dev/null
  59. fi
  60. ## "$BTRFS_DEVICE" device should not be mounted
  61. if mount | egrep ^"$BTRFS_DEVICE\s+" >/dev/null 2>&1; then
  62. umount "$BTRFS_DEVICE" || {
  63. echo "Can't umount $BTRFS_DEVICE. Aborting script."
  64. exit 1
  65. }
  66. echo "Unmounted $BTRFS_DEVICE."
  67. fi
  68. if egrep ^"$BTRFS_DEVICE\s+" /etc/fstab >/dev/null 2>&1; then
  69. sed -r -i "\%^$BTRFS_DEVICE\s+%d" /etc/fstab || {
  70. echo "Couldn't remove device $BTRFS_DEVICE from fstab."
  71. exit 1
  72. }
  73. echo "Removed device $BTRFS_DEVICE from fstab."
  74. fi
  75. ## Format the device and add entry in fstab
  76. mkfs.btrfs -f "$BTRFS_DEVICE"
  77. ## No need of UID it seems:
  78. # UUID="$(blkid -s UUID $BTRFS_DEVICE -o value)"
  79. # echo "UUID=$UUID $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  80. echo "$BTRFS_DEVICE $BTRFS_MOUNT_ROOT btrfs defaults,relatime,compress=lzo,space_cache,auto 0 0" >> /etc/fstab
  81. ## Mount point and mount device
  82. mkdir "$BTRFS_MOUNT_ROOT" -p
  83. mount "$BTRFS_MOUNT_ROOT"
  84. if [ -d /var/lib/docker ] ; then
  85. RESTART_DOCKER=yes
  86. service docker stop &&
  87. ## XXXvlab: moving doesn't work and is not desirable, as we want docker
  88. ## to setup and detect new underlying btrfs system.
  89. # mv "/var/lib/docker/"* "$BTRFS_MOUNT_ROOT/var/lib/docker"
  90. rm -rf /var/lib/docker/*
  91. fi
  92. ## Build subvolume structure
  93. for d in /home /var{/{lib,cache,backups}/lxc,/lib/docker} \
  94. /var/backups/snapshot \
  95. /srv/datastore{,/config,/data}; do
  96. mkdir -p "$(dirname "$BTRFS_MOUNT_ROOT$d")" ## creates parent directory of subvolume
  97. btrfs subvolume create "$BTRFS_MOUNT_ROOT$d"
  98. mkdir -p "$d"
  99. binds=$(cat /etc/fstab | egrep '\s+none\s+' | grep bind | grep -v '^\s+#' | sed -r 's/^\s*([^ ]+).*$/\1/g')
  100. for b in $binds; do
  101. if [[ "$BTRFS_MOUNT_ROOT$d/" == "$b/"* ]]; then
  102. echo "Directory '$d' is already available via bind '$b'."
  103. continue 2
  104. fi
  105. done
  106. ## Add bind to /etc/fstab
  107. echo "$BTRFS_MOUNT_ROOT$d $d none bind,defaults,auto 0 0" >> /etc/fstab
  108. done
  109. ## Mount all binds
  110. mount -a
  111. [ -z "$RESTART_DOCKER" ] || service docker start
  112. ## Prevent mlocate from fetching unwanted informations
  113. if [ -e "/etc/updatedb.conf" ]; then
  114. ## Without this, especially backup host can have 18Go locate database.
  115. 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
  116. fi