Skip to content

Instantly share code, notes, and snippets.

@007revad
Last active January 30, 2023 21:13
Show Gist options
  • Save 007revad/1ecb58660b27351c9bcebec936fd2014 to your computer and use it in GitHub Desktop.
Save 007revad/1ecb58660b27351c9bcebec936fd2014 to your computer and use it in GitHub Desktop.
Enable, disable or show current HDDs' write cache setting.
#!/usr/bin/env bash
#-----------------------------------------------------------
# Enable, disable or show current HDDs' write cache setting
# https://gist.github.com/007revad
#
# Run in a shell with:
# sudo hdd_writecache.sh
#-----------------------------------------------------------
# If Linux has more than 26 HDDs it names the extra ones sdaa, sdab, sdac etc
# https://rwmj.wordpress.com/2011/01/09/how-are-linux-drives-named-beyond-drive-26-devsdz/
set a b c d e f g h i j k l m n o p q r s t u v w x y z aa ab
echo "To enable write cache on all HDDs type: enable"
echo "To disable write cache on all HDDs type: disable"
echo "Anything else will show the current setting for each HDD."
read -r Action
case "$Action" in
disable)
# Disable write caching on all HDDs
while [[ $1 ]]
do
if [[ -e /dev/sd$1 ]]; then
#sudo hdparm -W 0 /dev/sd$1
hdparm -W 0 /dev/sd$1
fi
shift
done
;;
enable)
# Enable write caching on all HDDs
while [[ $1 ]]
do
if [[ -e /dev/sd$1 ]]; then
#hdparm -W 1 /dev/sd$1
hdparm -W 1 /dev/sd$1
fi
shift
done
;;
*)
# Show the write cache status of all HDDs
while [[ $1 ]]
do
if [[ -e /dev/sd$1 ]]; then
printf "sd$1 "
#sudo hdparm -i /dev/sd$1 | grep -Po 'WriteCache=.*abled'
hdparm -i /dev/sd$1 | grep -o 'WriteCache=.*abled'
fi
shift
done
;;
esac
echo # just to keep the shell output consistent
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment