Created
December 21, 2017 21:21
-
-
Save brianmhess/f9958990cc8df685d37b61b00606e645 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
setup-disks.sh | |
============== | |
#!/bin/bash | |
set -vx | |
# Execute this file dirctly (don't source it) if you want to use the `$(dirname $0)` construct | |
function maybe_skip_drive { | |
local drive=$1 | |
if ! [ -b $drive ]; then | |
echo "Skipping, this block device was not found: $drive" | |
continue | |
fi | |
} | |
function is_rpm_system { | |
if which rpm >& /dev/null; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
function is_deb_system { | |
if which dpkg >& /dev/null; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
function format_disk { | |
local drive=$1 | |
local fstype=$2 | |
echo "Wiping $drive a little." | |
sudo dd if=/dev/zero of=$drive bs=1M count=50 | |
echo "Zapping and Partitioning $drive" | |
sudo sgdisk -Z $drive # Zap (destroy) the GPT and MBR data structures | |
sudo sgdisk -n 1:0:0 -t 1:8300 $drive # create new partition using the full disk, partition id 1 with GUID 8300 | |
echo "Creating $fstype filesystem on $partition" | |
if [[ $fstype =~ "xfs" ]]; then | |
sudo mkfs.$fstype $partition | |
elif [[ $fstype =~ "ext4" ]]; then | |
sudo mkfs.$fstype -m 0 $partition # create an filesystem, 0% percentage of the filesystem blocks reserved for the super-user | |
else | |
echo "$fstype filesystem setup not supported yet, add it if you want it." | |
exit | |
fi | |
} | |
# optional param to specify the filesystem | |
fstype=$1 | |
if [[ "${fstype}x" == "x" ]]; then | |
fstype="ext4" | |
fi | |
is_deb_system | |
if [[ $? == 1 ]]; then | |
sudo apt-get install -y --force-yes xfsprogs | |
fi | |
is_rpm_system | |
if [[ $? == 1 ]]; then | |
sudo yum install -y gdisk | |
fi | |
# first turn off write barriers for the root drive | |
sudo sed -i '1!b;s/defaults/defaults,nobarrier/g' /etc/fstab && sudo mount -o remount / | |
# on ironic we have blade-13 instance types which have 2 1TB SSD available, so we'll setup for that | |
drives=(${DRIVES:="/dev/xvdba /dev/xvdbb /dev/xvdbc /dev/xvdbd /dev/xvdbe /dev/xvdbf /dev/xvdbg /dev/xvdbh /dev/xvdbi /dev/xvdbj /dev/xvdbk /dev/xvdbl /dev/xvdbm /dev/xvdbn /dev/xvdbo /dev/xvdbp /dev/xvdbq /dev/xvdbr /dev/xvdbs /dev/xvdbt /dev/xvdbu /dev/xvdbv /dev/xvdbw /dev/xvdbx"}) | |
data_dir_prefix="/mnt/data_disks/data" | |
drive_count=1 | |
for ((i = 0; i < ${#drives[@]}; i++)); do | |
drive=${drives[i]} | |
partition=${drive}1 | |
maybe_skip_drive $drive | |
data_dir=${data_dir_prefix}${drive_count} | |
if $(df -h | grep "$drive" >& /dev/null); then | |
sudo umount -f $drive | |
fi | |
sleep 5 | |
format_disk $drive $fstype | |
sleep 5 | |
sudo mkdir -p $data_dir | |
sleep 5 | |
echo "Mounting $partition to $data_dir" | |
sudo mount -o discard,nobarrier $partition $data_dir | |
sleep 5 | |
drive_count=$((drive_count+1)) | |
done | |
sudo chown -R automaton:automaton /mnt/cass_data_disks | |
echo "Wiping all the data dirs with 'rm -rf'" | |
for ((i = 1; i < $drive_count; i++)); do rm -rf ${data_dir_prefix}${i}/* ; done | |
echo "Done setting up drives, here is the current file system setup." | |
df -Th |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment