Escolar Documentos
Profissional Documentos
Cultura Documentos
Projects
Aboutme
synaptic fault
Contact
minddumped
Tweet
HowtoproperlyactivateTRIMforyourSSDonLinux:
fstrim,lvmanddmcrypt
PostedonSaturday,23rdFebruary2013
27comments
Language
English
Espaol
Unlikeharddiskdrives(HDDs),NANDflashmemorythatmakeSSDcannotoverwriteexistingdata.This
means that you first have to delete the old data before writing new one. Flash memory is divided into
blocks,whichisfurtherdividedinpages.Theminimumwriteunitisapage,butthesmallesteraseunitisa
block.
Tags
apache Applet debian email fpga game Git GNOME
warwebkitwireless
Categories
http://planet.igalia.com
http://planet.webkit.org
http://planet.webkitgtk.org
http://planetsecurity.org
Shareit!
Datacanbewrittendirectlyintoanemptypage,butonlywholeblockscanbeerased.Therefore,toreclaim
thespacetakenupbyinvaliddata,allthevaliddatafromoneblockmustbefirstcopiedandwrittenintothe
emptypagesofanewblock.Onlythencantheinvaliddataintheoriginalblockbeerased,makingitready
fornewvaliddatatobewritten.
Do you see the problem? This means that as time goes on, the SSD will internally fragment the blocks
among the different pages, until that it reaches a point where there wont be available any empty page.
Theneverytimethedriveneedstowriteablockintoanyofthesemifullpages,itfirstneedstocopythe
currentblocksfromthepagetoabuffer,thenithastodeletethewholepagetofinallyrewritetheoldblocks
alongwiththenewone.ThismeansthatastimegoesontheSSDperformancedegradesmoreandmore,
because for every write it has to go through a cycle of readerasemodifywrite. This is known as write
amplification.
RecentPosts
WebKitGTK+PerformanceBot!
How to connect to .onion Tor domains with
FirefoxonLinux
How to properly activate TRIM for your SSD on
Linux:fstrim,lvmanddmcrypt
Git: Copy a file or directory from another
repositorypreservingthehistory
IKEAHackers:LackRack
WithoutTRIMthediskisunabletoknowwhichblocksareinusebyafileorwhichonesaremarkedasfree
space.Thisisbecausewhenafileisdeleted,theonlythingtheOSdoesistomarktheblocksthatwere
usedbythefileasfreeinsidethefilesystemindex.ButtheOSwonttellthediskaboutthis.Thismeans
thatovertimetheperformanceoftheSSDdiskwilldegrademoreandmore,anditdontmattershowmuch
spaceyoufree,becausetheSSDwontknowaboutit.
What is TRIM? TRIM was invented for solving this problem. TRIM is the name of a command that the
operatingsystemcansendtotelltheSSDwhichblocksarefreeinthefilesystem.
The SSD uses this information to internally defragment the blocks and keep free pages available to be
writtenquicklyandefficiently.
HowtoactiveTRIMonLinux?ThefirstthingtoknowisthatTRIMshouldbeenabledonallI/Oabstraction
layers.Thismeansthatifyouhaveanext4partitionontopofLVM,whichinturnisontopofanencrypted
volumewithLUKS/dmcrypt,thenyoumustenablesupportforTRIMinthesethreelayers:Thefilesystem,
LVManddmcrypt.Thereisnopointinenablingitatthefilesystemlevelifyoudontenableitalsoonthe
otherlayers.TheTRIMcommandshouldbetranslatedfromonelayertoanotheruntilreachingtheSSD.
remotelyviaSSH
OpenRISC:Makingthestack100%free
DontejectyourUSBstorage:stopit!
RFKillAppletforGNOME
Auth
Tweets
RT
@News_Executive:
Update:
RT
@aszy:
Improving
Compiler
Microsoftisgoingtocelebratethenext
Debian
release!.
Not
joke:
http://t.co/1NO3dZCjaR@debian
1monthago
1monthago
1. EnablingTRIMsupportondmcrypt
Wesimplyhavetoaddtheoptiondiscardinsideourcrypttab
$cat/etc/crypttab
#<targetname><sourcedevice><keyfile><options>
sda2_crypt/dev/sda2noneluks,discard
Note:TheusageofTRIMondmcryptcouldcausesomesecurityissuesliketherevelationof
whichsectorsofyourdiskareunused.
2. EnablingTRIMsupportonLVM
Wehavetoenabletheoptionissue_discardsintheLVMconfiguration.
$cat/etc/lvm/lvm.conf
#[...]
devices{
#[...]
issue_discards=1
#[...]
}
#[...]
3. EnablingTRIMsupportonthefilesystem
This is the most interesting part. Most people simply add the option discard in the mounting
options at /etc/fstab. However, this means that every time you delete a file, the OS will be
reporting in realtime to the SSD which blocks were occupied by that file and are not longer in
use, and then the SSD will have to perform a defragmentation and deletion of those internal
blocks,operationwhichwilltakeanamountoftimehigherthandesired.
InordertooptimizetheperformanceoftheSSD,IstronglyadviseyoutoavoiddoingtheTRIM
operationinrealtime(wheneverafileisdeleted)becauseyouwouldbeputtinganunnecessary
extraamountofworkovertheSSD.Inotherwords:Youshouldnotenablethediscardoptionin
fstab.
Instead,whatIrecommendistorunascriptperiodicallytotelltheSSDwhichblocksarefreewith
thecommandfstrim.Doingthisoperationdailyorweeklyismorethanenough.Thiswaywedo
notloseanyperformanceduetoTRIMwhendeletingfilesandweperiodicallykeepinformedthe
SSDaboutthefreeblocks.
Otheradvantagesofthefstrimwayare:
IfyoudidntenabledcorrectlytheTRIMsupportintheabovelayersofyoursetup,you
willreceiveanerrorwhenexecutingfstrim. On the other hand, if you were using the
discard option at fstab you wouldnt have received any error and you would end
thinkingthatyoumanagedtogetTRIMworkingproperlywhenyoudidnt.
Ifyoudeleteafilebymistake(youknowithappens),youcanrecoveritbeforeanacron
runsyourscriptfstrim.Ontheotherhand,ifyouwereusingthediscardatfstaboption
youwouldnthaveanychanceofrecoveringthefile,becausetheOSwouldhavetold
the SSD to TRIM that blocks as soon as you deleted the file, and consequently the
SSDhasirreversiblydestroyedsuchblocks.
Hereyouhavesimplescripttorunfstrim on the /,/bootand/home partitions, which can be
programmedtobeexecutedperiodicallybyanacron
$cat/etc/cron.weekly/dofstrim
#!/bin/sh
formountin//boot/homedo
fstrim$mount
done
Note: On most setups you will have to rebuild your initramfs with updateinitramfsu (Debian and
derivatives) or dracutf (Redhat and derivatives) and reboot the machine after touching the
configurationoptionsofLVMordmcrypt.
Update25Feb2013:SeemsthatFedora users with a dmcrypt volume will be affected by this problem:
https://bugzilla.redhat.com/890533
Update14Aug2014: The following script can be used to automatically detect and fstrim all filesystems
thathaveTRIMsupportenabled.
dwhichFSsupporttrim,wecheckthatDISCMAX(discardmaxbytes)
atthanzero.Checkdiscard_max_bytesdocumentationat
//www.kernel.org/doc/Documentation/block/queuesysfs.txt
n$(lsblkoMOUNTPOINT,DISCMAX,FSTYPE|grepE'^/.*[19]+.*'|awk'{print$1}')do
linux,ssd,trim
Howtoconnectto.onionTordomainswithFirefoxon
preservingthehistory
Linux
Leaveacomment?
27Comments.
charlieFriday,12thApril2013at15:50UTC
Reply
thedefaultinstallwithencryptionsets/bootasext2.andiguessthatdoesntsupportTrim.
whatwilltheramificationsbetoleave/bootasext2andnotbeabletoTrim?
clopezFriday,12thApril2013at19:14UTC
Reply
/bootisusuallyaverysmallpartition(<500MBusually).SoIthinkisnotabigdealtohavethis
withouttrimsupport.Ifyouareworriedbythis,youcanchangethedefaulttouseext3for/boot
FlaburganThursday,31stOctober2013at7:04UTC
Reply
Whatabouttheswappartition?ShouldIrunfstrimonittoo?
charlieFriday,12thApril2013at21:37UTC
Reply
thankyou!excellentblogyouhave!
ExonThursday,9thMay2013at15:30UTC
Reply
Thanksforyourgreatinformation.
I used to add discard in fstab before. When I switched to LVM, I needed to implement
another way to set the Trim in my SSD. I have followed the blog information to setup the
Trim,isthereanymethodtocheckmysetupiscorrect?Justlike
https://sites.google.com/site/lightrush/random1/checkiftrimonext4isenabledandworking
or
http://howto.unixdev.net/Test_LVM_Trim_Ext4.html
clopezThursday,9thMay2013at16:22UTC
Reply
Ifyoudontgetanyerrorwhenyourunfstrimthenallshouldbeworkingcorrectly.fstrimwill
giveyouanerrorifTRIMsupportisnotenabledcorrectly
ExonSaturday,11thMay2013at2:27UTC
Reply
Thanksclopez!
ChristianTuesday,4thJune2013at23:48UTC
Reply
Excellentarticlethanks!
pcworldMonday,5thAugust2013at1:51UTC
Reply
Isthisstilltrue?(fromhttp://askubuntu.com/a/191945/178920)
However, these discards will only trigger on remove or reduce operations on
LVs.DiscardsonthefilesystemontopofaLVwillnotpropagate!Fromthesame
LVM changelog: Issue discards on lvremove and lvreduce etc. if enabled and
supported.
clopezMonday,5thAugust2013at2:36UTC
Reply
Yes.NotsureaboutUbuntu12.04,butatleastonDebian6(Squeeze)istrue.
ThesupportforTRIMtoLVMwasaddedintheversion2.02.85(seechangelog)
So,ifyourLinuxdistributionshipsanolderversionofLVMyoumaygetnosupportforTRIM.
pcworldMonday,5thAugust2013at14:10UTC
Reply
But doesnt that mean the discard option in fstab is useless? Or does fstrim work
aroundLVM2?
clopezMonday,5thAugust2013at16:24UTC
Reply
IfyourfilesystemisontopofaLVMvolume,andyourversionofLVMisolderthan
2.02.85youwontgetTRIMsupport.Neitherwiththediscardoptionnorthefstrim
one.
Thegoodthingaboutfstrimisthatitwillreporttheerrors.Howeverdiscardoptionin
fstabwillfailsilently..
So the best advice to check if your FS has TRIM support enabled on all the
required layers is to run fstrim. If fstrim works, then you have TRIM enabled
correctly,otherwiseyouwillgetanerror.
JohnWednesday,21stAugust2013at12:12UTC
Reply
Notbeingmuchofabashpersonacoupleofthingsconfusemeonthis.
MymachineissetupwithsystemfilesonSSDand/tmp/varand/homeonharddrivesto
avoidwritestothessd.Thisleavesmewonderingwhattofstrimasallofthesecomeoff/.
The other aspect comes from reading about using ssd disks in redhat docs. They suggest
trimmingwhenthessdhasrunoutoffreshspace.Makessenseasthiswouldensurewear
levelling.Theredoesntseemtobeacommandavailabletocheckhowmuchfreshspaceis
leftthough.Allnothelpedbythefactthatthereseemstobelittleinfoaboutonwhatexactly
goesoninsidethesedisks.
Thanksfortheinfobytheway.BestIhavefound.
John
consatanMonday,28thOctober2013at9:10UTC
Reply
HowcanIfstrimswappartition?
clopezMonday,28thOctober2013at11:07UTC
Reply
Discardisavalidmountoption(/etc/fstab)forswap.Theotheroptionistousetheflagdwhen
addingtheswap.Checkthemanpageofswapon
ShashSaturday,21stDecember2013at21:09UTC
HiClopez,
ThanksforsharingthisinfoGracias
Ireachedthispage,bygoingthroughyouroriginalquestiononServerfault.com(
withref.toyourquestionyouhadaskedaboutdoingthissetupw/RAID0:
FS(OS)|
LVM|
RAID1|
dmcrypt|
SSD1+SSD2v
However,yourpostheredoesntcovertheRAID0setuppart.Highlyappreciateifyoucan
shareonhowsoftwareRAID1wasachieved(ifatall)inthissetup.
Reply
Manythanks.
cheers,
s#
clopezSaturday,1stFebruary2014at21:35UTC
Reply
Thisquestiononserverfault.comisnotfrommyself.
AboutTRIMoverRAID,Inevertestedit,butmyunderstandingisthatitshouldworkoutofthe
boxifyouhavearecentenoughkernelthatsupportsit.
MD RAID needs specific support to forward DISCARD requests and that hasnt even been
proposeduntil3.6https://lkml.org/lkml/2012/3/11/261
shashWednesday,19thFebruary2014at12:31UTC
Reply
HiClopez,
thanksfortip.
didrealizethatIhavegottheRAID#sallovertheplaceinmyoriginalcomment:D.but
IguessyoudidgetthedriftthatIwaslookingforRAID1:).
cheers.
S#
oxtanMonday,6thJanuary2014at18:50UTC
Reply
I have this bash snippet for the cron.weekly job that gets the mountpoints based on the
mounteddevicesinfoinsteadofhardcodingthem.Maybeitisusefultosomebodyelse:
#!/bin/sh
# this cronjob will discard unused blocks on the ssd mounted
filesystems
#getthelocallymountedblockdevicesthosestartingwith"/dev:
#rundfk,pipetheresultthroughgrepandsavethesixthfield
in
#inthemountpointarray
mountpoint=($(dfk|grep^/dev|awk'{print$6}'))
#loopthroughthearrayandrunfstrimoneverymountpoint
foriin"${mountpoint[@]}"
do
/usr/sbin/fstrimv$i
done
ryno75Monday,27thJanuary2014at23:05UTC
Reply
Fantastic writeup! Really well written and organized. One of the most useful howtos Ive
seen.ever.
FrankWangSunday,9thMarch2014at11:43UTC
Reply
Great blog! The only problem I encountered in my Ubuntu 12.04 where / is on lvm is the
fstrim/commandcanstillberunwithoutanyerrorraisedevenifIaddedissue_discard=0
in lvm.conf, then updateinitramfs u and reboot performed. This is not expected. Any
comment?Thanks!
RalfThursday,17thApril2014at15:06UTC
Reply
ItseemsthatopenSUSE13.1iscurrentlynotpreparedforproperhandlingofthediscard
optionin/etc/crypttab.Theinitrddoesnotcontainthepropercommand.
Workaround: In order to activate discard on boot time, I just hardcoded the cryptsetup
commandsin/lib/mkinitrd/boot/71luks.shtocontainluksOpenallowdiscards.Worksfine
sofar.
mr.CoconutWednesday,4thJune2014at17:58UTC
Reply
Hi,
it seems that trim is now activated by default on ubuntu 14.04 lts and its derivatives.
However,ifyouareusingLVM(notencrypted)andhavesnapshotsoflogicalvolumes,then
thesystemisunabletotrim.Icanconfirmthisbehaviour.Anyideas?
XTLSaturday,5thJuly2014at13:31UTC
Reply
OnDebian(andthelike)youcanalsodo
#!/bin/sh
formountin//boot/homedo
fstrim$mount
done
XTLSaturday,5thJuly2014at13:32UTC
Isntthatanicepasteslip
Nopreviewhere.
Reply
$sudodpkgreconfigureinitramfstools
Leave a Comment
NAME
EMAIL
WebsiteURL
CAPTCHACode*
NOTEYoucanusetheseHTMLtagsandattributes:
<ahref=""title=""><abbrtitle=""><acronymtitle=""><b><blockquote
cite=""><cite><code><deldatetime=""><em><i><qcite=""><strike>
<strong>
SUBMIT
Repliestomycomments
Notifymeoffollowupcommentsviaemail.Youcanalsosubscribe
withoutcommenting.
TrackbacksandPingbacks:
HowtoproperlyactivateTRIMforyourSSDonL...Pingbackon2014/03/10/18:06
Revisting an article on how to set up Solid State Disks with Linuxnews.siduction.org | news.siduction.org
Pingbackon2014/10/10/22:43
AllcontentonthissiteislicensedundertheCreativeCommonsAttributionShareAlike3.0License.
Top