NETGEAR is aware of a growing number of phone and online scams. To learn how to stay safe click here.
Forum Discussion
Scouser
Jan 19, 2012Aspirant
Write-cache problems
Is there a way to disable disk write-cache permanently until such time that I might want to turn it back on again. I need to do this manually via some configuration file or command line utility and N...
Scouser
Jan 20, 2012Aspirant
I wrote this script to turn manipulate the write-cache for anyone interested.
Examples:
Query the write cache status for all disks:
Query the write cache for a specified disk:
Turn the write cache off for all disks quietly:
Turn the write cache on for /dev/sdc:
#!/bin/bash
# Disk write-cache utility for ReadyNAS
# By B Goodheart Jan 2012
# V 1.0
# Check we're running as a privileged user
if [ $(id -u) -ne 0 ]
then
echo "$0: insufficent permissions"
exit 2
fi
usage () {
echo "Usage: $0 [-qh] [0|1|on|off|status] [/dev/??]"
cat << USAGE
A utility for manipulating and querying the write-cache feature for attached disk drives.
Arguments are as follows:
-q quite mode
-h prints help and usage information
0 or on turn write-cache off for all disks or a specified disk
1 or off turn write-cache on for all disks or a specified disk
status prints current status of all disks or a specified disk
/dev/?? execute this command for the specified disk only
CONDITIONS:
1) No arguments given is the same as specifying argument "status"
2) User must have root privileges to execute this command
3) If no disk argument is specified, this command will process execution for ALL disks
4) This command does not process USB attached disks
USAGE
}
checkdev() {
if [ ! -e $1 ]
then
echo "$1: no such device"
exit 1
fi
}
status () {
disk=$1
# read the write-cache status for all attached disks in the ReadyNAS disk enclosure
if [ "$disk" = "all" ]
then
for i in /sys/block/[hs]d?; do
# ignore USB connected disks
readlink $i | grep -q usb && continue
dev="/dev/`basename $i`"
echo "${dev}: `/sbin/hdparm -W ${dev} | grep caching`"
done
else
readlink $disk | grep -q usb && return
checkdev ${disk}
echo "${disk}: `/sbin/hdparm -W ${disk} | grep caching`"
fi
}
process () {
state=$1
disk=$2
qflag=$3
if [ "$disk" = "all" ]
then
for i in /sys/block/[hs]d?; do
# ignore USB connected disks
readlink $i | grep -q usb && continue
dev="/dev/`basename $i`"
/sbin/hdparm -${qflag}W${state} ${dev}
#echo "/sbin/hdparm -${qflag}W${state} ${dev}"
done
else
readlink $disk | grep -q usb && return
checkdev ${disk}
/sbin/hdparm -${qflag}W${state} ${disk}
#echo "/sbin/hdparm -${qflag}W${state} ${disk}"
fi
/usr/bin/update_disk_info_cache
}
device="all"
if [ $# = 0 ]
then
status $device
exit 0
fi
quiet=""
while getopts qh opt
do
case "$opt" in
q)
quiet="q"
shift
;;
h|\?)
usage
exit 0
;;
esac
done
#if [ "$1" = "-q" ]
#then
# quiet="q"
# shift
#fi
if [ $# = 2 ]
then
device=$2
fi
case "$1" in
0|1)
process $1 $device $quiet
;;
on)
process 1 $device $quiet
;;
off)
process 0 $device $quiet
;;
status)
status $device
;;
*)
usage
exit 1
;;
esac
exit 0
Examples:
Query the write cache status for all disks:
MainStore:~# hdwritecache
/dev/sda: write-caching = 1 (on)
/dev/sdb: write-caching = 1 (on)
/dev/sdc: write-caching = 1 (on)
/dev/sdd: write-caching = 0 (off)
/dev/sde: write-caching = 0 (off)
Query the write cache for a specified disk:
MainStore:~# hdwritecache status /dev/sdd
/dev/sdd: write-caching = 0 (off)
Turn the write cache off for all disks quietly:
MainStore:~# hdwritecache -q 0
MainStore:~# hdwritecache
/dev/sda: write-caching = 0 (off)
/dev/sdb: write-caching = 0 (off)
/dev/sdc: write-caching = 0 (off)
/dev/sdd: write-caching = 0 (off)
Turn the write cache on for /dev/sdc:
MainStore:~# hdwritecache 1 /dev/sdc
/dev/sdc:
setting drive write-caching to 1 (on)
write-caching = 1 (on)
MainStore:~# hdwritecache
/dev/sda: write-caching = 0 (off)
/dev/sdb: write-caching = 0 (off)
/dev/sdc: write-caching = 1 (on)
/dev/sdd: write-caching = 0 (off)
Related Content
NETGEAR Academy

Boost your skills with the Netgear Academy - Get trained, certified and stay ahead with the latest Netgear technology!
Join Us!