Skip to content

Commit e739c6f

Browse files
author
Dougal Ballantyne
committed
Cleanup ephemeral drive handling.
1 parent b4b686c commit e739c6f

File tree

3 files changed

+122
-170
lines changed

3 files changed

+122
-170
lines changed

bootstrap/src/scripts/boot_as_compute

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,87 +13,20 @@
1313

1414
set -x
1515

16+
# Source config
1617
. /opt/cfncluster/cfnconfig
1718

18-
function error_exit () {
19-
script=`basename $0`
20-
echo "cfncluster: $script - $1"
21-
logger -t cfncluster "$script - $1"
22-
exit 1
23-
}
19+
# Source functions
20+
. /opt/cfncluster/scripts/functions.shlib
2421

2522
# Run preinstall script if defined
26-
RC=0
27-
if [ "${cfn_preinstall}" != "NONE" ]; then
28-
tmpfile=$(mktemp)
29-
wget -qO- ${cfn_preinstall} > $tmpfile || RC=1
30-
if [ "${cfn_preinstall_args}" != "NONE" ]; then
31-
args=${cfn_preinstall_args}
32-
fi
33-
/bin/sh $tmpfile $args || RC=1
34-
/bin/rm $tmpfile
35-
fi
36-
if [ $RC -ne 0 ]; then
37-
error_exit "Failed to run boot_as_compute preinstall"
38-
fi
23+
run_preinstall
3924

4025
## Non-scheduler specific functions
4126
##
4227

4328
# LVM stripe, format, mount ephemeral drives
44-
RC=0
45-
mkdir -p ${cfn_ephemeral_dir} || RC=1
46-
chmod 1777 ${cfn_ephemeral_dir} || RC=1
47-
MAPPING=$(/usr/bin/ec2-metadata -b | grep ephemeral | awk '{print $2}' | sed 's/sd/xvd/')
48-
for m in $MAPPING; do
49-
stat -t /dev/${m} >/dev/null 2>&1 || RC=1
50-
check=$?
51-
if [ ${check} -eq 0 ]; then
52-
DEVS="${m} $DEVS"
53-
fi
54-
done
55-
NUM_DEVS=0
56-
for d in $DEVS; do
57-
d=/dev/${d}
58-
dd if=/dev/zero of=${d} bs=32k count=1 || RC=1
59-
parted -s ${d} mklabel msdos || RC=1
60-
parted -s ${d} || RC=1
61-
parted -s -a optimal ${d} mkpart primary 1MB 100% || RC=1
62-
parted -s ${d} set 1 lvm on || RC=1
63-
let NUM_DEVS++
64-
PARTITIONS="${d}1 $PARTITIONS"
65-
done
66-
if [ $RC -ne 0 ]; then
67-
error_exit "Failed to detect and/or partition ephemeral devices."
68-
fi
69-
70-
# sleep 10 seconds to let partitions settle (bug?)
71-
sleep 10
72-
73-
# Setup LVM
74-
RC=0
75-
pvcreate $PARTITIONS || RC=1
76-
vgcreate vg.01 $PARTITIONS || RC=1
77-
lvcreate -i $NUM_DEVS -I 64 -l 100%FREE -n lv_ephemeral vg.01 || RC=1
78-
if [ "$cfn_encrypted_ephemeral" == "true" ]; then
79-
mkfs -q /dev/ram1 1024 || RC=1
80-
mkdir -p /root/keystore || RC=1
81-
mount /dev/ram1 /root/keystore || RC=1
82-
dd if=/dev/urandom of=/root/keystore/keyfile bs=1024 count=4 || RC=1
83-
chmod 0400 /root/keystore/keyfile || RC=1
84-
cryptsetup -q luksFormat /dev/vg.01/lv_ephemeral /root/keystore/keyfile || RC=1
85-
cryptsetup -d /root/keystore/keyfile luksOpen /dev/vg.01/lv_ephemeral ephemeral_luks || RC=1
86-
mkfs.xfs /dev/mapper/ephemeral_luks || RC=1
87-
mount -v -t xfs -o noatime,nodiratime /dev/mapper/ephemeral_luks ${cfn_ephemeral_dir} || RC=1
88-
else
89-
mkfs.xfs /dev/vg.01/lv_ephemeral || RC=1
90-
echo "/dev/vg.01/lv_ephemeral ${cfn_ephemeral_dir} xfs noatime,nodiratime 0 0" >> /etc/fstab || RC=1
91-
mount -v ${cfn_ephemeral_dir} || RC=1
92-
fi
93-
chmod 1777 ${cfn_ephemeral_dir} || RC=1
94-
if [ $RC -ne 0 ]; then
95-
error_exit "Failed to create LVM stripe and/or format ephemeral volume."
96-
fi
29+
setup_ephemeral_drives
9730

9831
# Mount NFS exports
9932
RC=0
@@ -136,19 +69,7 @@ if [ $RC -ne 0 ]; then
13669
fi
13770

13871
# Run postinstall script if defined
139-
RC=0
140-
if [ "${cfn_postinstall}" != "NONE" ]; then
141-
tmpfile=$(mktemp)
142-
wget -qO- ${cfn_postinstall} > $tmpfile || RC=1
143-
if [ "${cfn_postinstall_args}" != "NONE" ]; then
144-
args=${cfn_postinstall_args}
145-
fi
146-
/bin/sh $tmpfile $args || RC=1
147-
/bin/rm $tmpfile
148-
fi
149-
if [ $RC -ne 0 ]; then
150-
error_exit "Failed to run boot_as_compute postinstall"
151-
fi
72+
run_postinstall
15273

15374
# Notify compute is ready
15475
instance_id_url="http://169.254.169.254/latest/meta-data/instance-id"

bootstrap/src/scripts/boot_as_master

Lines changed: 6 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,14 @@
1313

1414
set -x
1515

16+
# Source config
1617
. /opt/cfncluster/cfnconfig
1718

18-
function error_exit () {
19-
script=`basename $0`
20-
echo "cfncluster: $script - $1"
21-
logger -t cfncluster "$script - $1"
22-
exit 1
23-
}
19+
# Source functions
20+
. /opt/cfncluster/scripts/functions.shlib
2421

2522
# Run preinstall script if defined
26-
RC=0
27-
if [ "${cfn_preinstall}" != "NONE" ]; then
28-
tmpfile=$(mktemp)
29-
wget -qO- ${cfn_preinstall} > $tmpfile || RC=1
30-
if [ "${cfn_preinstall_args}" != "NONE" ]; then
31-
args=${cfn_preinstall_args}
32-
fi
33-
/bin/sh $tmpfile $args || RC=1
34-
/bin/rm $tmpfile
35-
fi
36-
if [ $RC -ne 0 ]; then
37-
error_exit "Failed to run boot_as_master preinstall"
38-
fi
23+
run_preinstall
3924

4025
## Non-scheduler specific functions
4126
##
@@ -60,59 +45,7 @@ if [ $RC -ne 0 ]; then
6045
fi
6146

6247
# LVM stripe, format, mount ephemeral drives
63-
RC=0
64-
mkdir -p ${cfn_ephemeral_dir} || RC=1
65-
chmod 1777 ${cfn_ephemeral_dir} || RC=1
66-
MAPPING=$(/usr/bin/ec2-metadata -b | grep ephemeral | awk '{print $2}' | sed 's/sd/xvd/')
67-
for m in $MAPPING; do
68-
stat -t /dev/${m} >/dev/null 2>&1 || RC=1
69-
check=$?
70-
if [ ${check} -eq 0 ]; then
71-
DEVS="${m} $DEVS"
72-
fi
73-
done
74-
NUM_DEVS=0
75-
for d in $DEVS; do
76-
d=/dev/${d}
77-
dd if=/dev/zero of=${d} bs=32k count=1 || RC=1
78-
parted -s ${d} mklabel msdos || RC=1
79-
parted -s ${d} || RC=1
80-
parted -s -a optimal ${d} mkpart primary 1MB 100% || RC=1
81-
parted -s ${d} set 1 lvm on || RC=1
82-
let NUM_DEVS++
83-
PARTITIONS="${d}1 $PARTITIONS"
84-
done
85-
if [ $RC -ne 0 ]; then
86-
error_exit "Failed to detect and/or partition ephemeral devices."
87-
fi
88-
89-
# sleep 10 seconds to let partitions settle (bug?)
90-
sleep 10
91-
92-
# Setup LVM
93-
RC=0
94-
pvcreate $PARTITIONS || RC=1
95-
vgcreate vg.01 $PARTITIONS || RC=1
96-
lvcreate -i $NUM_DEVS -I 64 -l 100%FREE -n lv_ephemeral vg.01 || RC=1
97-
if [ "$cfn_encrypted_ephemeral" == "true" ]; then
98-
mkfs -q /dev/ram1 1024 || RC=1
99-
mkdir -p /root/keystore || RC=1
100-
mount /dev/ram1 /root/keystore || RC=1
101-
dd if=/dev/urandom of=/root/keystore/keyfile bs=1024 count=4 || RC=1
102-
chmod 0400 /root/keystore/keyfile || RC=1
103-
cryptsetup -q luksFormat /dev/vg.01/lv_ephemeral /root/keystore/keyfile || RC=1
104-
cryptsetup -d /root/keystore/keyfile luksOpen /dev/vg.01/lv_ephemeral ephemeral_luks || RC=1
105-
mkfs.xfs /dev/mapper/ephemeral_luks || RC=1
106-
mount -v -t xfs -o noatime,nodiratime /dev/mapper/ephemeral_luks ${cfn_ephemeral_dir} || RC=1
107-
else
108-
mkfs.xfs /dev/vg.01/lv_ephemeral || RC=1
109-
echo "/dev/vg.01/lv_ephemeral ${cfn_ephemeral_dir} xfs noatime,nodiratime 0 0" >> /etc/fstab || RC=1
110-
mount -v ${cfn_ephemeral_dir} || RC=1
111-
fi
112-
chmod 1777 ${cfn_ephemeral_dir} || RC=1
113-
if [ $RC -ne 0 ]; then
114-
error_exit "Failed to create LVM stripe and/or format ephemeral volume."
115-
fi
48+
setup_ephemeral_drives
11649

11750
# Attach and mount /shared volume
11851
RC=0
@@ -222,16 +155,4 @@ fi
222155
cd /opt/cfncluster/sqswatcher && ./sqswatcher.py 2>&1
223156

224157
# Run postinstall script if defined
225-
RC=0
226-
if [ "${cfn_postinstall}" != "NONE" ]; then
227-
tmpfile=$(mktemp)
228-
wget -qO- ${cfn_postinstall} > $tmpfile || RC=1
229-
if [ "${cfn_postinstall_args}" != "NONE" ]; then
230-
args=${cfn_postinstall_args}
231-
fi
232-
/bin/sh $tmpfile $args || RC=1
233-
/bin/rm $tmpfile
234-
fi
235-
if [ $RC -ne 0 ]; then
236-
error_exit "Failed to run boot_as_master postinstall"
237-
fi
158+
run_postinstall
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
let NUM_DEVS++
59+
PARTITIONS="${d}1 $PARTITIONS"
60+
done
61+
if [ $RC -ne 0 ]; then
62+
error_exit "Failed to detect and/or partition ephemeral devices."
63+
fi
64+
65+
# sleep 10 seconds to let partitions settle (bug?)
66+
sleep 10
67+
68+
# Setup LVM
69+
RC=0
70+
pvcreate $PARTITIONS || RC=1
71+
vgcreate vg.01 $PARTITIONS || RC=1
72+
lvcreate -i $NUM_DEVS -I 64 -l 100%FREE -n lv_ephemeral vg.01 || RC=1
73+
if [ "$cfn_encrypted_ephemeral" == "true" ]; then
74+
mkfs -q /dev/ram1 1024 || RC=1
75+
mkdir -p /root/keystore || RC=1
76+
mount /dev/ram1 /root/keystore || RC=1
77+
dd if=/dev/urandom of=/root/keystore/keyfile bs=1024 count=4 || RC=1
78+
chmod 0400 /root/keystore/keyfile || RC=1
79+
cryptsetup -q luksFormat /dev/vg.01/lv_ephemeral /root/keystore/keyfile || RC=1
80+
cryptsetup -d /root/keystore/keyfile luksOpen /dev/vg.01/lv_ephemeral ephemeral_luks || RC=1
81+
mkfs.xfs /dev/mapper/ephemeral_luks || RC=1
82+
mount -v -t xfs -o noatime,nodiratime /dev/mapper/ephemeral_luks ${cfn_ephemeral_dir} || RC=1
83+
else
84+
mkfs.xfs /dev/vg.01/lv_ephemeral || RC=1
85+
echo "/dev/vg.01/lv_ephemeral ${cfn_ephemeral_dir} xfs noatime,nodiratime 0 0" >> /etc/fstab || RC=1
86+
mount -v ${cfn_ephemeral_dir} || RC=1
87+
fi
88+
fi
89+
chmod 1777 ${cfn_ephemeral_dir} || RC=1
90+
if [ $RC -ne 0 ]; then
91+
error_exit "Failed to create LVM stripe and/or format ephemeral volume."
92+
fi
93+
}
94+
95+
# Run post install
96+
function run_postinstall () {
97+
RC=0
98+
if [ "${cfn_postinstall}" != "NONE" ]; then
99+
tmpfile=$(mktemp)
100+
wget -qO- ${cfn_postinstall} > $tmpfile || RC=1
101+
if [ "${cfn_postinstall_args}" != "NONE" ]; then
102+
args=${cfn_postinstall_args}
103+
fi
104+
/bin/sh $tmpfile $args || RC=1
105+
/bin/rm $tmpfile
106+
fi
107+
if [ $RC -ne 0 ]; then
108+
error_exit "Failed to run boot_as_master postinstall"
109+
fi
110+
}

0 commit comments

Comments
 (0)