How to Disable or Enable Disk Write Caching in Linux
ATA-Based Disks
- Check out whether disk caching is enabled on your disk or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# sudo hdparm -i /dev/sdX Note: replace the “/dev/sdX” with the correct address of your target device. The expected output will be similar below: Model=WDC WD3200BPVT-22JJ5T0, FwRev=01.01A01, SerialNo=WD-WX61EC1KZK99 Config={ HardSect NotMFM HdSw>15uSec SpinMotCtl Fixed DTR>5Mbs FmtGapReq } RawCHS=16383/16/63, TrkSize=0, SectSize=0, ECCbytes=50 BuffType=unknown, BuffSize=8192kB, MaxMultSect=16, MultSect=off CurCHS=16383/16/63, CurSects=16514064, LBA=yes, LBAsects=625142448 IORDY=on/off, tPIO={min:120,w/IORDY:120}, tDMA={min:120,rec:120} PIO modes: pio0 pio3 pio4 DMA modes: mdma0 mdma1 mdma2 UDMA modes: udma0 udma1 udma2 udma3 udma4 udma5 *udma6 AdvancedPM=yes: unknown setting WriteCache=enabled Drive conforms to: Unspecified: ATA/ATAPI-1,2,3,4,5,6,7 The words in red, WriteCache=enabled, means caching is enabled! |
- To disable write caching, you need to edit hdparm.conf using your favorite editor (e.g. gedit, vi).
1 2 3 4 5 |
# sudo gedit /etc/hdparm.conf Uncomment the line “#write_cache = off” (without quotes) by removing the # at its beginning. So it looks like: # -W Disable/enable the IDE drive’s write-caching feature write_cache = off |
- After making the changes, restart the server/computer and check out the write caching status again to make sure it’s disabled.
If in case hdparm.conf does not exist in /etc folder, an alternative approach is to use the command below.
1 2 3 4 5 6 |
# sudo hdparm -W 0 /dev/sdX --> disable write caching # sudo hdparm -W 1 /dev/sdX --> enable write caching To verify write caching if enabled or disabled, use the command below. # sudo hdparm -i /dev/sdX Note: Replace /dev/sdX with the correct address of the target device |
- To know more about “hdparm”, invoke the manual by typing the following command.
1 2 3 |
# sudo man hdparm | more OR # sudo man hdparm | less |
SCSI-Based Disks
-
Check out whether disk caching is enabled on your disk or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# sudo sdparm /dev/sdX --- replace /dev/sdX to a target device Expected Output: /dev/sda: COMPAQ BF03697B5A HPB2 Read write error recovery mode page: AWRE 1 [cha: y, def: 1, sav: 1] ARRE 1 [cha: y, def: 1, sav: 1] PER 1 [cha: y, def: 1, sav: 1] Caching (SBC) mode page: WCE 0 [cha: y, def: 1, sav: 0] --> write caching is disabled RCD 0 [cha: y, def: 0, sav: 0] Control mode page: SWP 0 [cha: y, def: 0, sav: 0] Informational exceptions control mode page: EWASC 1 [cha: y, def: 1, sav: 1] DEXCPT 0 [cha: y, def: 0, sav: 0] MRIE 4 [cha: y, def: 4, sav: 4] |
-
Sending an SCSI inquiry command using “sdparm”.
1 2 3 4 5 6 7 8 9 10 |
# sudo sdparm --inquiry /dev/sdX Expected Output: /dev/sda: COMPAQ BF03697B5A HPB2 Device identification VPD page: Addressed logical unit: id_type: EUI-64 based, code_set: Binary [0x0010b9fd080c1129] id_type: T10 vendor identification, code_set: ASCII vendor id: COMPAQ vendor specific: E20C9MKK |
-
Getting all the parameters for the Caching Mode Page. Refer to the details of this mode page from the SCSI Interface Specification.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# sudo sdparm --page=ca /dev/sdX Expected Output: /dev/sda: COMPAQ BF03697B5A HPB2 Caching (SBC) mode page: IC 0 [cha: y, def: 0, sav: 0] ABPF 0 [cha: n, def: 0, sav: 0] CAP 0 [cha: n, def: 0, sav: 0] DISC 0 [cha: n, def: 0, sav: 0] SIZE 0 [cha: y, def: 0, sav: 0] WCE 0 [cha: y, def: 1, sav: 0] --> write caching is disabled MF 0 [cha: y, def: 0, sav: 0] RCD 0 [cha: y, def: 0, sav: 0] DRRP 0 [cha: y, def: 0, sav: 0] WRP 0 [cha: y, def: 0, sav: 0] DPTL 512 [cha: y, def:512, sav:512] MIPF 256 [cha: y, def:256, sav:256] MAPF 512 [cha: y, def:512, sav:512] MAPFC 512 [cha: y, def:512, sav:512] FSW 0 [cha: n, def: 0, sav: 0] LBCSS 0 [cha: y, def: 0, sav: 0] DRA 1 [cha: y, def: 1, sav: 1] NV_DIS 0 [cha: n, def: 0, sav: 0] NCS 226 [cha: y, def:226, sav:226] CSS 0 [cha: y, def: 0, sav: 0] |
-
Setting write caching or disk caching to enable by running below command.
1 |
# sudo sdparm --set=WCE /dev/sdX --> this enables write caching |
-
To get the WCE current value:
1 |
# sdparm –g WCE /dev/sdX (just input the target DUT) |
-
To set the WCE current value (WCE=1)
1 |
# sdparm –s WCE /dev/sdX |
-
To set the WCE current and saved values (WCE=1)
1 |
# sdparm –s WCE –S /dev/sdX |
-
To clear (disable) the WCE current value (WCE=0)
1 |
# sdparm –c WCE /dev/sdX |
-
To clear (disable) the WCE current and saved values (WCE=0)
1 |
# sdparm –c WCE –S /dev/sdb |
-
To know more about “sdparm”, invoke the manual by typing the following command.
1 2 3 |
# man sdparm | more OR # man sdparm | less |