fix_disk_layout.sh 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/bin/bash
  2. # Copyright (C) 2016 OpenStack Foundation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  13. # implied.
  14. #
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. # Don't attempt to fix disk layout more than once
  18. [[ -e /etc/fixed_disk_layout ]] && return 0 || sudo touch /etc/fixed_disk_layout
  19. # Ensure virtual machines from different providers all have at least 8GB of
  20. # swap.
  21. # Use an ephemeral disk if there is one or create and use a swapfile.
  22. # Rackspace also doesn't have enough space on / for two devstack installs,
  23. # so we partition the disk and mount it on /opt, syncing the previous
  24. # contents of /opt over.
  25. SWAPSIZE=8192
  26. swapcurrent=$(( $(grep SwapTotal /proc/meminfo | awk '{ print $2; }') / 1024 ))
  27. if [[ $swapcurrent -lt $SWAPSIZE ]]; then
  28. if [ -b /dev/xvde ]; then
  29. DEV='/dev/xvde'
  30. else
  31. EPHEMERAL_DEV=$(blkid -L ephemeral0 || true)
  32. if [ -n "$EPHEMERAL_DEV" -a -b "$EPHEMERAL_DEV" ]; then
  33. DEV=$EPHEMERAL_DEV
  34. fi
  35. fi
  36. if [ -n "$DEV" ]; then
  37. # If an ephemeral device is available, use it
  38. swap=${DEV}1
  39. lvmvol=${DEV}2
  40. optdev=${DEV}3
  41. if mount | grep ${DEV} > /dev/null; then
  42. echo "*** ${DEV} appears to already be mounted"
  43. echo "*** ${DEV} unmounting and reformating"
  44. sudo umount ${DEV}
  45. fi
  46. sudo parted ${DEV} --script -- mklabel msdos
  47. sudo parted ${DEV} --script -- mkpart primary linux-swap 1 ${SWAPSIZE}
  48. sudo parted ${DEV} --script -- mkpart primary ext2 8192 -1
  49. sudo mkswap ${DEV}1
  50. sudo mkfs.ext4 ${DEV}2
  51. sudo swapon ${DEV}1
  52. sudo mount ${DEV}2 /mnt
  53. sudo find /opt/ -mindepth 1 -maxdepth 1 -exec mv {} /mnt/ \;
  54. sudo umount /mnt
  55. sudo mount ${DEV}2 /opt
  56. # Sanity check
  57. grep -q ${DEV}1 /proc/swaps || exit 1
  58. grep -q ${DEV}2 /proc/mounts || exit 1
  59. else
  60. # If no ephemeral devices are available, use root filesystem
  61. # Don't use sparse device to avoid wedging when disk space and
  62. # memory are both unavailable.
  63. swapfile='/root/swapfile'
  64. swapdiff=$(( $SWAPSIZE - $swapcurrent ))
  65. sudo dd if=/dev/zero of=${swapfile} bs=1M count=${swapdiff}
  66. sudo chmod 600 ${swapfile}
  67. sudo mkswap ${swapfile}
  68. sudo swapon ${swapfile}
  69. # Sanity check
  70. grep -q ${swapfile} /proc/swaps || exit 1
  71. fi
  72. fi
  73. # dump vm settings for reference (Ubuntu 12 era procps can get
  74. # confused with certain proc trigger nodes that are write-only and
  75. # return a EPERM; ignore this)
  76. sudo sysctl vm || true
  77. # ensure a standard level of swappiness. Some platforms
  78. # (rax+centos7) come with swappiness of 0 (presumably because the
  79. # vm doesn't come with swap setup ... but we just did that above),
  80. # which depending on the kernel version can lead to the OOM killer
  81. # kicking in on some processes despite swap being available;
  82. # particularly things like mysql which have very high ratio of
  83. # anonymous-memory to file-backed mappings.
  84. # make sure reload of sysctl doesn't reset this
  85. sudo sed -i '/vm.swappiness/d' /etc/sysctl.conf
  86. # This sets swappiness low; we really don't want to be relying on
  87. # cloud I/O based swap during our runs
  88. sudo sysctl -w vm.swappiness=10