Skip to content

Instantly share code, notes, and snippets.

@dklassen
Created November 19, 2015 18:44
Show Gist options
  • Save dklassen/74d71774eaa9ae0d0a36 to your computer and use it in GitHub Desktop.
Save dklassen/74d71774eaa9ae0d0a36 to your computer and use it in GitHub Desktop.
format disks
#!/bin/bash
usage()
{
cat << EO
Usage: format_data_disks [options]
Find and format data disks on nodes
EO
}
OPTS=`getopt -o d: --long destructive,help -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
echo "$OPTS"
eval set -- "$OPTS"
DRY_RUN=true
while true; do
case "$1" in
-h | --help ) usage; shift ;;
-n | --destructive ) DRY_RUN=true; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
DRIVES=$(lsblk -lo NAME,TYPE | grep disk | cut -d' ' -f1 | sort)
DRIVES_WITH_PARTITIONS=$(lsblk -lo NAME,TYPE | grep part | cut -d' ' -f1 | sed 's/[0-9]*//g' | uniq | sort)
format_drive () {
if [ $DRY_RUN = true ]
then
echo "Formatting $1"
else
echo "Would format $1"
fi
}
update_fstab_with_uuid () {
if [ $DRY_RUN = true ] ; then
echo "Would replace $1 with $2"
else
echo "not working at the moment"
#sed -i 's/$1/$2/g' /etc/fstab.new
fi
}
echo "Found the following unformatted drives"
UNFORMATTED_DRIVES=`comm -2 -3 <(echo "$DRIVES") <(echo "$DRIVES_WITH_PARTITIONS")`
echo $UNFORMATTED_DRIVES
NEW_UUIDS=()
for drive_to_format in $UNFORMATTED_DRIVES; do
format_drive drive_to_format
partition="/dev/$drive_to_format1"
uuid=$(sudo blkid /dev/sdi1 | gawk 'match($0, /^.*UUID=\"([a-z0-9-]+)/, a) {print a[1]}')
NEW_UUIDS+=(uuid)
done
FSTAB_UUIDS=$(awk '/(^|#\s+)UUID/ {split($1,a,"="); print a[2]; }' /etc/fstab | sort)
CURRENT_UUIDS=$(ls /dev/disk/by-uuid | sort)
# We only assign uuids to data disks
OlD_UUIDS=$(comm -23 <(echo "$FSTAB_UUIDS") <(echo "$CURRENT_UUIDS"))
if [ ${#OLD_UUIDS[@]} -eq 0 ]; then
echo "No UUIDS to replace...all done here"
exit 0
fi
# backup the fstab
# cp /etc/fstab /etc/fstab.old
# cp /etc/fstab /etc/fstab.new
REPLACEMENT_UUIDS=( "${OLD_UUIDS[@]}" "${NEW_UUIDS[@]}" )
for index in "${!REPLACEMENT_UUIDS[@]}"
do
update_fstab_with_uuid $index ${COMBINED[$index]}
done
# cp /etc/fstab.new /etc/fstab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment