Skip to content

Commit d3ebfca

Browse files
author
Jonathan Abrahams
committed
SERVER-29877 Mount /data on EBS volume in AWS EC2 instance
1 parent f2fc0b1 commit d3ebfca

File tree

2 files changed

+144
-1
lines changed

2 files changed

+144
-1
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
*.vcproj -crlf -diff -merge
22
*.pbxproj -crlf -diff -merge
3-
3+
*.sh text eol=lf

buildscripts/mount_ephemeral_drive.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/bin/bash
2+
# Script used to mount /data on a separate drive from root.
3+
# This script must be invoked either by a root user or with sudo.
4+
# Currently only supports Windows & Linux.
5+
# See _usage_ for how this script should be invoked.
6+
7+
set -o errexit
8+
9+
# Default options
10+
fs_type=xfs
11+
user_group=$USER:$(id -Gn $USER | cut -f1 -d ' ')
12+
13+
# _usage_: Provides usage infomation
14+
function _usage_ {
15+
cat << EOF
16+
usage: $0 options
17+
This script supports the following parameters for Windows & Linux platforms:
18+
-d <deviceNames>, REQUIRED, Space separated list of devices to mount /data on,
19+
i.e., "xvdb xvdc", more than one device indicates a RAID set.
20+
For Windows, specify the drive letter, i.e., "z".
21+
-r <raidDeviceName>, REQUIRED, if more the one device is specified in <deviceNames>,
22+
i.e., md0.
23+
Not supported on Windows.
24+
-t <fsType>, File system type, defaults to '$fs_type'.
25+
Not supported on Windows.
26+
-o <mountOptions>, File system mount options, i.e., "-m crc=0,finobt=0".
27+
Not supported on Windows.
28+
-u <user:group>, User:Group to make owner of /data. Defaults to '$user_group'.
29+
EOF
30+
}
31+
32+
33+
# Parse command line options
34+
while getopts "d:o:r:t:u:?" option
35+
do
36+
case $option in
37+
d)
38+
device_names=$OPTARG
39+
;;
40+
o)
41+
mount_options=$OPTARG
42+
;;
43+
r)
44+
raid_device_name=$OPTARG
45+
;;
46+
t)
47+
fs_type=$OPTARG
48+
;;
49+
u)
50+
user_group=$OPTARG
51+
;;
52+
\?|*)
53+
_usage_
54+
exit 0
55+
;;
56+
esac
57+
done
58+
59+
# Determine how many devices were specified
60+
num_devices=0
61+
for device_name in $device_names
62+
do
63+
devices="$devices /dev/$device_name"
64+
let "num_devices=num_devices+1"
65+
done
66+
67+
# $OS is defined in Cygwin
68+
if [ "Windows_NT" = "$OS" ]; then
69+
if [ $num_devices -ne 1 ]; then
70+
echo "Must specify only one drive"
71+
_usage_
72+
exit 1
73+
fi
74+
75+
i=0
76+
DRIVE_POLL_DELAY=1
77+
DRIVE_RETRY_MAX=240
78+
79+
drive=$device_names
80+
system_drive=c
81+
82+
while true; do
83+
sleep "$DRIVE_POLL_DELAY"
84+
echo "Looking for ephemeral drive '$drive'"
85+
if [ -d "/cygdrive/$drive" ]; then
86+
echo "Found ephemeral drive"
87+
rm -rf /data
88+
rm -rf /cygdrive/$system_drive/data
89+
mkdir -p /cygdrive/$drive/data/db
90+
cmd.exe /c mklink /J $system_drive:\\data $drive:\\data
91+
ln -s /cygdrive/$system_drive/data /data
92+
chown -R $user_group /data
93+
break
94+
fi
95+
let "i=i+1"
96+
if [ "$i" -eq "$DRIVE_RETRY_MAX" ]; then
97+
echo "TIMED OUT trying to mount ephemeral drive."
98+
exit 1
99+
fi
100+
done
101+
102+
elif [ $(uname | awk '{print tolower($0)}') = "linux" ]; then
103+
if [ $num_devices -eq 0 ]; then
104+
echo "Must specify atleast one device"
105+
_usage_
106+
exit 1
107+
elif [ $num_devices -gt 1 ]; then
108+
if [ -z "$raid_device_name" ]; then
109+
echo "Missing RAID device name"
110+
_usage_
111+
exit 1
112+
fi
113+
fi
114+
115+
# Unmount the current devices, if already mounted
116+
umount /mnt || true
117+
umount $devices || true
118+
119+
# Determine if we have a RAID set
120+
if [ ! -z "$raid_device_name" ]; then
121+
echo "Creating RAID set on '$raid_device_name' for devices '$devices'"
122+
device_name=/dev/$raid_device_name
123+
/sbin/udevadm control --stop-exec-queue
124+
yes | /sbin/mdadm --create $device_name --level=0 -c256 --raid-devices=$num_devices $devices
125+
/sbin/udevadm control --start-exec-queue
126+
/sbin/mdadm --detail --scan > /etc/mdadm.conf
127+
/sbin/blockdev --setra 32 $device_name
128+
else
129+
device_name=$devices
130+
fi
131+
132+
# Mount the ephemeral drive(s)
133+
/sbin/mkfs.$fs_type $mount_options -f $device_name
134+
mkdir -p /data/db || true
135+
echo "$device_name /data auto noatime 0 0" | tee -a /etc/fstab
136+
mount -t $fs_type $device_name /data
137+
chown -R $user_group /data
138+
mkdir /data/tmp
139+
chmod 1777 /data/tmp
140+
else
141+
echo "Unsupported OS '$(uname)'"
142+
exit 0
143+
fi

0 commit comments

Comments
 (0)