System configuration for OpenStack Infrastructure
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.

78 lines
3.3KB

  1. #!/bin/bash
  2. # Copyright 2013 OpenStack Foundation
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # 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, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. # If we're running on a cloud server with no swap, fix that:
  16. if [ `grep SwapTotal /proc/meminfo | awk '{ print $2; }'` -eq 0 ]; then
  17. if [ -b /dev/vdb ]; then
  18. DEV='/dev/vdb'
  19. elif [ -b /dev/xvde ]; then
  20. DEV='/dev/xvde'
  21. fi
  22. SWAPFILE=/swapfile
  23. MEMKB=`grep MemTotal /proc/meminfo | awk '{print $2; }'`
  24. # Use the nearest power of two in MB as the swap size.
  25. # This ensures that the partitions below are aligned properly.
  26. MEM=`python3 -c "import math ; print(2**int(round(math.log($MEMKB/1024, 2))))"`
  27. # Avoid using config drive device for swap
  28. if [ -n "$DEV" ] && ! blkid | grep $DEV | grep TYPE ; then
  29. if mount | grep ${DEV} > /dev/null; then
  30. echo "*** ${DEV} appears to already be mounted"
  31. echo "*** ${DEV} unmounting and reformating"
  32. umount ${DEV}
  33. fi
  34. parted ${DEV} --script -- \
  35. mklabel msdos \
  36. mkpart primary linux-swap 1 ${MEM} \
  37. mkpart primary ext2 ${MEM} -1
  38. sync
  39. # We are only interested in scanning $DEV, not all block devices
  40. sudo partprobe ${DEV}
  41. # The device partitions might not show up immediately, make sure
  42. # they are ready and available for use
  43. udevadm settle --timeout=0 || echo "Block device not ready yet. Waiting for up to 10 seconds for it to be ready"
  44. udevadm settle --timeout=10 --exit-if-exists=${DEV}1
  45. udevadm settle --timeout=10 --exit-if-exists=${DEV}2
  46. mkswap ${DEV}1
  47. # The default ratio is 16384 bytes per inode or so. Reduce that to 8192
  48. # bytes per inode so that we get roughly twice the number of inodes as
  49. # by default. This should still be well above the block size of 4096.
  50. # We do this because we have found in at least a couple locations that
  51. # more inodes is useful and is painful to fix after the fact.
  52. mkfs.ext4 -i 8192 ${DEV}2
  53. swapon ${DEV}1
  54. mount ${DEV}2 /mnt
  55. rsync -a /opt/ /mnt/
  56. umount /mnt
  57. perl -nle "m,${DEV}, || print" -i /etc/fstab
  58. echo "${DEV}1 none swap sw 0 0" >> /etc/fstab
  59. echo "${DEV}2 /opt ext4 errors=remount-ro,barrier=0 0 2" >> /etc/fstab
  60. elif [ ! -f "$SWAPFILE" ] ; then
  61. # We don't have real devices to use so we make a swap file instead.
  62. # Note you can skip this by precreating /swapfile.
  63. # bs here is 1Mb
  64. sudo dd if=/dev/zero of=${SWAPFILE} bs=1048576 count=${MEM}
  65. sudo chown root:root $SWAPFILE
  66. sudo chmod 600 $SWAPFILE
  67. sudo mkswap $SWAPFILE
  68. echo "${SWAPFILE} none swap sw 0 0" >> /etc/fstab
  69. fi
  70. swapon -a
  71. mount -a
  72. fi