|
| 1 | +# Licensed under the Amazon Software License (the "License"). You may not use this file except in compliance with the |
| 2 | +# License. A copy of the License is located at |
| 3 | +# |
| 4 | +# http://aws.amazon.com/asl/ |
| 5 | +# |
| 6 | +# or in the "LICENSE.txt" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES |
| 7 | +# OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions and |
| 8 | +# limitations under the License. |
| 9 | + |
| 10 | +# Error exit function |
| 11 | +function error_exit () { |
| 12 | + script=`basename $0` |
| 13 | + echo "cfncluster: $script - $1" |
| 14 | + logger -t cfncluster "$script - $1" |
| 15 | + exit 1 |
| 16 | +} |
| 17 | + |
| 18 | +# Check and run preinstall |
| 19 | +function run_preinstall () { |
| 20 | + RC=0 |
| 21 | + if [ "${cfn_preinstall}" != "NONE" ]; then |
| 22 | + tmpfile=$(mktemp) |
| 23 | + wget -qO- ${cfn_preinstall} > $tmpfile || RC=1 |
| 24 | + if [ "${cfn_preinstall_args}" != "NONE" ]; then |
| 25 | + args=${cfn_preinstall_args} |
| 26 | + fi |
| 27 | + /bin/sh $tmpfile $args || RC=1 |
| 28 | + /bin/rm $tmpfile |
| 29 | + fi |
| 30 | + if [ $RC -ne 0 ]; then |
| 31 | + error_exit "Failed to run boot_as_master preinstall" |
| 32 | + fi |
| 33 | +} |
| 34 | + |
| 35 | +# LVM stripe, format, mount ephemeral drives |
| 36 | +function setup_ephemeral_drives () { |
| 37 | + RC=0 |
| 38 | + mkdir -p ${cfn_ephemeral_dir} || RC=1 |
| 39 | + chmod 1777 ${cfn_ephemeral_dir} || RC=1 |
| 40 | + MAPPING=$(/usr/bin/ec2-metadata -b | grep ephemeral | awk '{print $2}' | sed 's/sd/xvd/') |
| 41 | + NUM_DEVS=0 |
| 42 | + for m in $MAPPING; do |
| 43 | + stat -t /dev/${m} >/dev/null 2>&1 |
| 44 | + check=$? |
| 45 | + if [ ${check} -eq 0 ]; then |
| 46 | + DEVS="${m} $DEVS" |
| 47 | + let NUM_DEVS++ |
| 48 | + fi |
| 49 | + done |
| 50 | + if [ $NUM_DEVS -gt 0 ]; then |
| 51 | + for d in $DEVS; do |
| 52 | + d=/dev/${d} |
| 53 | + dd if=/dev/zero of=${d} bs=32k count=1 || RC=1 |
| 54 | + parted -s ${d} mklabel msdos || RC=1 |
| 55 | + parted -s ${d} || RC=1 |
| 56 | + parted -s -a optimal ${d} mkpart primary 1MB 100% || RC=1 |
| 57 | + parted -s ${d} set 1 lvm on || RC=1 |
| 58 | + PARTITIONS="${d}1 $PARTITIONS" |
| 59 | + done |
| 60 | + if [ $RC -ne 0 ]; then |
| 61 | + error_exit "Failed to detect and/or partition ephemeral devices." |
| 62 | + fi |
| 63 | + |
| 64 | + # sleep 10 seconds to let partitions settle (bug?) |
| 65 | + sleep 10 |
| 66 | + |
| 67 | + # Setup LVM |
| 68 | + RC=0 |
| 69 | + pvcreate $PARTITIONS || RC=1 |
| 70 | + vgcreate vg.01 $PARTITIONS || RC=1 |
| 71 | + lvcreate -i $NUM_DEVS -I 64 -l 100%FREE -n lv_ephemeral vg.01 || RC=1 |
| 72 | + if [ "$cfn_encrypted_ephemeral" == "true" ]; then |
| 73 | + mkfs -q /dev/ram1 1024 || RC=1 |
| 74 | + mkdir -p /root/keystore || RC=1 |
| 75 | + mount /dev/ram1 /root/keystore || RC=1 |
| 76 | + dd if=/dev/urandom of=/root/keystore/keyfile bs=1024 count=4 || RC=1 |
| 77 | + chmod 0400 /root/keystore/keyfile || RC=1 |
| 78 | + cryptsetup -q luksFormat /dev/vg.01/lv_ephemeral /root/keystore/keyfile || RC=1 |
| 79 | + cryptsetup -d /root/keystore/keyfile luksOpen /dev/vg.01/lv_ephemeral ephemeral_luks || RC=1 |
| 80 | + mkfs.xfs /dev/mapper/ephemeral_luks || RC=1 |
| 81 | + mount -v -t xfs -o noatime,nodiratime /dev/mapper/ephemeral_luks ${cfn_ephemeral_dir} || RC=1 |
| 82 | + else |
| 83 | + mkfs.xfs /dev/vg.01/lv_ephemeral || RC=1 |
| 84 | + echo "/dev/vg.01/lv_ephemeral ${cfn_ephemeral_dir} xfs noatime,nodiratime 0 0" >> /etc/fstab || RC=1 |
| 85 | + mount -v ${cfn_ephemeral_dir} || RC=1 |
| 86 | + fi |
| 87 | + fi |
| 88 | + chmod 1777 ${cfn_ephemeral_dir} || RC=1 |
| 89 | + if [ $RC -ne 0 ]; then |
| 90 | + error_exit "Failed to create LVM stripe and/or format ephemeral volume." |
| 91 | + fi |
| 92 | +} |
| 93 | + |
| 94 | +# Run post install |
| 95 | +function run_postinstall () { |
| 96 | + RC=0 |
| 97 | + if [ "${cfn_postinstall}" != "NONE" ]; then |
| 98 | + tmpfile=$(mktemp) |
| 99 | + wget -qO- ${cfn_postinstall} > $tmpfile || RC=1 |
| 100 | + if [ "${cfn_postinstall_args}" != "NONE" ]; then |
| 101 | + args=${cfn_postinstall_args} |
| 102 | + fi |
| 103 | + /bin/sh $tmpfile $args || RC=1 |
| 104 | + /bin/rm $tmpfile |
| 105 | + fi |
| 106 | + if [ $RC -ne 0 ]; then |
| 107 | + error_exit "Failed to run boot_as_master postinstall" |
| 108 | + fi |
| 109 | +} |
0 commit comments