Icon Fame Journal.

Juicy entertainment chatter with tabloid flavor.

news

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

By Isabella Wilson

Want to improve this question? Update the question so it’s on-topic for Ask Ubuntu.

Closed 7 years ago .

Why doesn’t ubuntu turn on discard mount option for ssd drives that support it? Or, alternatively, include a cron-job to run fstrim periodically ? The aim of this question is to understand the technical difficulties and the reasons behind that decision and not to discuss a bug or a feature request.

1 Answer 1

Ubuntu always goes for stable before cutting edge and TRIM is not perfect yet.

A couple of quotes:

The reason Ubuntu doesn’t TRIM SSDs by default is because the Linux kernel’s implementation of TRIM is slow and results in poor performance in normal use. Source

The kernel implementation of realtime trim in 11.2, 11.3, and 11.4 is not optimized. The spec. calls for trim supporting a vectorized list of trim ranges, but as of kernel 3.0 trim is only invoked by the kernel with a single discard / trim range and with current mid 2011 SSDs this has proven to cause a performance degradation instead of a performance increase. There are few reasons to use the kernels realtime discard support with pre-3.1 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs.

Ubuntu is also looking at enabling TRIM automatically by having the system regularly run fstrim. This will hopefully be part of Ubuntu 14.04 so Ubuntu users won’t be forced to deal with SSD performance degradation or run fstrim on their own. Source

Update: fstrim (fstrim-all) was added to 14.04 in the new util-linux package (2.20.1-5.1ubuntu11)

I have heard, that Ubuntu 14.04 will have Trim enabled by default. How will this be done? By adding a discards option to the ext4 mount options or by running a cron-job?

Will the default options for Trim be automatically compatible with the LVM and encryption layers of the standard Ubuntu-system encryption?

It would be great to see some details about this.

1 Answer 1

Claiming that 14.04 will enable trim by default is only 1/2 the story.

Will the default options for TRIM be automatically compatible with the LVM and encryption layers of the standard ubuntu-system encryption?

LVM is a container and is not a filesystem so this is not an issue.

Articles worth reading:

SSDs need to be TRIMed, i. e. they need to be told which blocks the OS considers as “unused” (i. e. from deleted files). Withouth this, the write speed on SSDs becomes unbearably slow over time.

(German) explains the details, but this kind of housekeeping really should happen by default. There are two approaches to this, immediately marking the blocks as unused when deleting a file (“discard” mount option), or calling fstrim regularly. discard slows down deletion while a cronjob occasionally puts some potentially unexpected IO load on the machine, so this discussion is primarily about deciding which approach we want to do by default.

I (Martin Pitt) think that a cron approach is better. If we go with this we need to discuss when and how to run this:

  • Whats a reasonable interval (weekly/monthly/etc.)
  • How to detect devices/partitions which need trimming (/proc/mounts, hdparm -I, not mounted with “discard”, etc.)

Conclusion from that link in German:

  • cron job on weekly basis since discard will lead to too much of a performance hit.

Only Intel and Samsung SSDs will have TRIM enabled by default in Ubuntu 14.04 because some cheap SSDs can even brick themselves when running TRIM. The contents of /etc/ on 14.04 beta 2:

This doesn’t mean TRIM should only be used with Samsung and Intel SSDs, but to avoid running into issues, this is the default option for now. Note that if you already know your SSD can handle TRIM, you can bypass the vendor check by editing the /etc/ file above and adding the –no-model-check option to the last line:

Alternatively, you can schedule TRIM manually a few other ways. How to do it manual.

  • BUT not all SSD’s are supported. Mine is not by the way (just checked)
  • LSI.com has a list of compatible SSDs in this PDF.

The method to do trim is with this command:

And this is also how Ubuntu does this: by creating a cronjob that does this on a weekly basis.

Executed from my own system:

Takes a bit of time and it does not show any progress until done.

Chris Hoffman is Editor-in-Chief of How-To Geek. He’s written about technology for over a decade and was a PCWorld columnist for two years. Chris has written for The New York Times, been interviewed as a technology expert on TV stations like Miami’s NBC 6, and had his work covered by news outlets like the BBC. Since 2011, Chris has written over 2,000 articles that have been read nearly one billion times—and that’s just here at How-To Geek. Read more.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu wants to enable TRIM for SSDs by default in Ubuntu 14.04. In other words, Ubuntu isn’t already using TRIM, so your SSD is slowing down over time. But why isn’t Ubuntu already using TRIM?

This news will likely come as a surprise to many people, who assumed that Ubuntu and other Linux distributions were already using TRIM. TRIM prevents SSDs from slowing down over time and is a necessary part of SSD maintenance.

Why TRIM is Important

We’ve covered why TRIM is important before. When you delete a file on an old, magnetic hard drive, the computer simply marks that file as deleted. The file’s data sticks around on the hard drive — that’s why deleted files can be recovered. The computer will eventually overwrite the deleted files when it overwrites their sectors with new data.

Solid-state drives (SSDs) work differently. Whenever you write a file to an SSD, the computer must first erase any data in the sectors it’s writing the data to. It can’t just “overwrite” the sectors in one operation — it must first clear them, then write to the empty sectors.

This means that an SSD will slow down over time. Writing to the SSD’s sectors will be quick the first time. After you delete some files and try to write to it again, it will take longer. This is a big part of the reason Google’s original Nexus 7 slowed down so much over time. Google fixed this by implementing TRIM in Android 4.3. (Android also uses the Linux kernel.)

With TRIM enabled, the operating system tells the SSD each time it deletes a file. The drive can then erase the sectors containing the file’s contents, so writing to the sectors will be quick in the future.

In other words, if you don’t use TRIM, your SSD will slow down over time. That’s why modern operating systems, including Windows 7+, Mac OS X 10.6.8+, and Android 4.3+ use TRIM. TRIM was implemented in Linux back in December 2008, but Ubuntu isn’t using it by default.

Why Doesn’t Ubuntu TRIM By Default?

The real reason Ubuntu doesn’t TRIM SSDs by default is because the Linux kernel’s implementation of TRIM is slow and results in poor performance in normal use.

On Windows 7 and 8, Windows sends a TRIM command each time it deletes a file, telling the drive to immediately delete the bits of the file. Linux supports this when file systems are mounted with the “discard” option. However, Ubuntu — and other distributions — don’t do this by default for performance reasons.

OpenSUSE’s wiki contains some detailed information from a developer who’s more familiar with the Linux kernel than we are. It’s a bit dated, but is likely still true when it comes to performance:

“The kernel implementation of realtime trim in 11.2, 11.3, and 11.4 is not optimized. The spec. calls for trim supporting a vectorized list of trim ranges, but as of kernel 3.0 trim is only invoked by the kernel with a single discard / trim range and with current mid 2011 SSDs this has proven to cause a performance degradation instead of a performance increase. There are few reasons to use the kernels realtime discard support with pre-3.1 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs.” [Source]

In other words, the Linux kernel handles such real-time TRIM commands in a slow, unoptimized way. Enabling TRIM similar to how Windows does — that is, using the “discard” option — results in the system actually becoming slower than if TRIM were not used at all. Ubuntu and other Linux distributions don’t enable “discard” by default for your file systems, and you shouldn’t either.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

There’s Another Way

Because the Linux kernel’s real-time “discard” TRIM operation doesn’t perform well, most Linux distributions — including Ubuntu — don’t use TRIM automatically. Android also didn’t use TRIM until Android 4.3.

But there’s another way to use TRIM. Rather than simply issuing the TRIM command each time a file is deleted, the FITRIM feature can be used. This happens via the fstrim command. Essentially, the fstrim command analyzes the file system and informs the drive which blocks are no longer needed, so the drive can discard them. This turns TRIM from a real-time operation into a scheduled task. In other words, fstrim can perform TRIM as a cron job. There’s no reason not to do this. It won’t slow down anything; it’s just another housecleaning task the system has to perform on a schedule.

In fact, this is the approach Google took with Android 4.3. Android simply runs a fstrim task occasionally to TRIM the file system, fixing the problem that slowed down all those original Nexus 7s.

Ubuntu is also looking at enabling TRIM automatically by having the system regularly run fstrim. This will hopefully be part of Ubuntu 14.04 so Ubuntu users won’t be forced to deal with SSD performance degradation or run fstrim on their own.

How to Enable TRIM

We don’t recommend mounting your file systems with the “discard” operation, as this will likely result in slower performance in normal use. However, you can use TRIM yourself by occasionally running the fstrim command or creating your own cronjob that runs fstrim on a schedule.

To TRIM your SSD on Ubuntu, simply open a terminal and run the following command:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

You can run the above command occasionally to prevent performance degradation on SSDs. How often you need to run it depends on how often files are deleted from your SSD. You’ll see an error if you try to run the command with a drive that doesn’t support TRIM.

If you want to run TRIM regularly, you can simply create a cronjob that runs the fstrim command for you. Here’s how to make a barebones cron job that will do this automatically.

First, run the following command to open the nano text editor with root permissions:

Type the following code into the file:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Save the file by pressing Ctrl+O and press Enter to confirm. Press Ctrl+X to close nano after saving the file.

Last, run the following command to make the script executable:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu will now run fstrim on a schedule, just as it does other system maintenance tasks.

Note that TRIM is only supported on modern file systems, so you’ll need something like ext4 and not ext3 or ext2. If you don’t know what file system you’re using, don’t worry — ext4 is selected by default.

Much of this advice also applies to other Linux distributions. While Linux did implement TRIM support in the kernel long ago, its TRIM support seems to have never been enabled by default for typical users in Linux distributions.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu ต้องการเปิดใช้งาน TRIM สำหรับ SSD ตามค่าเริ่มต้นใน Ubuntu 14.04 กล่าวอีกอย่างคืออูบุนตูยังไม่ได้ใช้ TRIM ดังนั้น SSD ของคุณจึงช้าลงตามกาลเวลา แต่ทำไม Ubuntu ถึงไม่ใช้ TRIM?

ข่าวนี้น่าประหลาดใจสำหรับหลาย ๆ คนที่คิดว่า Ubuntu และลีนุกซ์รุ่นอื่น ๆ ใช้ TRIM อยู่แล้ว TRIM ป้องกัน SSD จากการชะลอตัวเมื่อเวลาผ่านไปและเป็นส่วนที่จำเป็นของการบำรุงรักษา SSD.

ทำไม TRIM จึงสำคัญ

เราได้อธิบายถึงสาเหตุที่ TRIM สำคัญก่อน เมื่อคุณลบไฟล์ในฮาร์ดไดรฟ์แม่เหล็กเก่าคอมพิวเตอร์จะทำเครื่องหมายไฟล์นั้นว่าถูกลบ ข้อมูลของไฟล์ติดอยู่บนฮาร์ดไดรฟ์ – นั่นเป็นสาเหตุที่สามารถกู้คืนไฟล์ที่ถูกลบได้ คอมพิวเตอร์จะเขียนทับไฟล์ที่ถูกลบในที่สุดเมื่อมันเขียนทับภาคของพวกเขาด้วยข้อมูลใหม่.

โซลิดสเตทไดรฟ์ (SSD) ทำงานแตกต่างกัน เมื่อใดก็ตามที่คุณเขียนไฟล์ไปยัง SSD คอมพิวเตอร์จะต้องลบข้อมูลใด ๆ ในภาคที่กำลังเขียนข้อมูลไปก่อน ไม่เพียงแค่ “เขียนทับ” ส่วนต่าง ๆ ในการดำเนินการเดียว – ก่อนอื่นคุณต้องล้างข้อมูลเหล่านั้นแล้วเขียนไปยังส่วนที่ว่างเปล่า.

ซึ่งหมายความว่า SSD จะช้าลงเมื่อเวลาผ่านไป การเขียนไปยังภาคส่วนของ SSD จะรวดเร็วในครั้งแรก หลังจากคุณลบไฟล์บางไฟล์แล้วลองเขียนมันอีกครั้งมันจะใช้เวลานานกว่า นี่เป็นส่วนใหญ่ของเหตุผลที่ Nexus 7 ดั้งเดิมของ Google ชะลอตัวลงมากเมื่อเวลาผ่านไป Google แก้ไขสิ่งนี้โดยใช้ TRIM ใน Android 4.3 (Android ยังใช้เคอร์เนล Linux ด้วย)

เมื่อเปิดใช้งาน TRIM ระบบปฏิบัติการจะแจ้ง SSD ทุกครั้งที่ลบไฟล์ ไดรฟ์สามารถลบเซ็กเตอร์ที่มีเนื้อหาของไฟล์ดังนั้นการเขียนไปยังเซกเตอร์จะเป็นไปอย่างรวดเร็วในอนาคต.

กล่าวอีกนัยหนึ่งถ้าคุณไม่ใช้ TRIM SSD ของคุณจะช้าลงเมื่อเวลาผ่านไป นั่นเป็นเหตุผลที่ระบบปฏิบัติการสมัยใหม่รวมถึง Windows 7+, Mac OS X 10.6.8+ และ Android 4.3+ ใช้ TRIM TRIM ถูกนำมาใช้ใน Linux ในเดือนธันวาคม 2008 แต่ Ubuntu ไม่ได้ใช้งานเป็นค่าเริ่มต้น.

ทำไมจึงไม่ Ubuntu TRIM โดยค่าเริ่มต้น?

เหตุผลที่แท้จริง Ubuntu ไม่ใช่ TRIM SSDs โดยค่าเริ่มต้นเนื่องจากการใช้งาน TRIM ของเคอร์เนลของ Linux ช้าและทำให้ประสิทธิภาพในการใช้งานปกติไม่ดี.

บน Windows 7 และ 8 Windows จะส่งคำสั่ง TRIM ทุกครั้งที่ลบไฟล์โดยบอกให้ไดรฟ์ลบบิตของไฟล์ทันที Linux รองรับสิ่งนี้เมื่อติดตั้งระบบไฟล์ด้วยตัวเลือก “ทิ้ง” อย่างไรก็ตาม Ubuntu และการกระจายอื่น ๆ – อย่าทำสิ่งนี้โดยค่าเริ่มต้นด้วยเหตุผลด้านประสิทธิภาพ.

วิกิของ OpenSUSE มีข้อมูลโดยละเอียดจากนักพัฒนาที่คุ้นเคยกับเคอร์เนลลินุกซ์มากกว่าที่เราเป็น มันค่อนข้างเก่า แต่ก็ยังคงเป็นจริงเมื่อพูดถึงการแสดง:

“ การปรับใช้เคอร์เนลของการตัดแบบเรียลไทม์ใน 11.2, 11.3 และ 11.4 นั้นไม่ได้รับการปรับปรุง สเป็ค เรียกใช้สำหรับการตัดแต่งที่สนับสนุนรายการ vectorized ของช่วงการตัดแต่ง แต่ในขณะที่เคอร์เนล 3.0 การตัดแต่งถูกเรียกใช้โดยเคอร์เนลที่มีช่วงการละทิ้ง / การตัดเดี่ยวและด้วยช่วงกลางปี ​​2554 SSD นี้ได้พิสูจน์แล้วว่า มีเหตุผลบางอย่างที่จะใช้การสนับสนุนเรียลไทม์ยกเลิกเมล็ดด้วยเมล็ด 3.1 ก่อน ไม่ทราบว่าเมื่อใดที่ฟังก์ชั่นการทิ้งเมล็ดจะได้รับการปรับให้ทำงานอย่างเป็นประโยชน์กับ SSD รุ่นปัจจุบัน” [แหล่งที่มา]

กล่าวอีกนัยหนึ่งเคอร์เนล Linux จัดการคำสั่ง TRIM แบบเรียลไทม์ดังกล่าวในวิธีที่ช้าและไม่ได้เพิ่มประสิทธิภาพ การเปิดใช้งาน TRIM คล้ายกับวิธีที่ Windows ใช้ – นั่นคือการใช้ตัวเลือก “ทิ้ง” – ส่งผลให้ระบบช้าลงจริง ๆ ถ้าไม่ได้ใช้ TRIM เลย Ubuntu และ Linux รุ่นอื่น ๆ ไม่ได้เปิดใช้งาน “ทิ้ง” โดยค่าเริ่มต้นสำหรับระบบไฟล์ของคุณและคุณไม่ควรทำเช่นนั้น.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

มีวิธีอื่น

เนื่องจากการดำเนินการ TRIM แบบเรียลไทม์ “ยกเลิก” ของเคอร์เนล Linux ทำงานได้ไม่ดีการกระจาย Linux ส่วนใหญ่ – รวมถึง Ubuntu – ห้ามใช้ TRIM โดยอัตโนมัติ Android ยังไม่ได้ใช้ TRIM จนกว่า Android 4.3.

แต่มีวิธีการใช้ TRIM อีกวิธีหนึ่ง แทนที่จะใช้คำสั่ง TRIM ในแต่ละครั้งที่ไฟล์ถูกลบคุณลักษณะ FITRIM สามารถใช้ได้ สิ่งนี้เกิดขึ้นผ่านคำสั่ง fstrim โดยพื้นฐานแล้วคำสั่ง fstrim จะวิเคราะห์ระบบไฟล์และแจ้งให้ทราบถึงไดรฟ์ที่ไม่จำเป็นต้องใช้บล็อกอีกต่อไปดังนั้นไดรฟ์สามารถยกเลิกได้ สิ่งนี้เปลี่ยน TRIM จากการดำเนินการตามเวลาจริงเป็นงานที่กำหนดเวลาไว้ กล่าวอีกนัยหนึ่ง fstrim สามารถทำงาน TRIM เป็นงาน cron ได้ ไม่มีเหตุผลที่จะไม่ทำเช่นนี้ มันจะไม่ทำให้อะไรช้าลง มันเป็นเพียงภารกิจการทำความสะอาดอื่นที่ระบบต้องดำเนินการตามกำหนดเวลา.

อันที่จริงนี่เป็นวิธีการที่ Google ใช้กับ Android 4.3 Android รันงาน fstrim เป็นครั้งคราวเพื่อตัดระบบไฟล์แก้ไขปัญหาที่ชะลอตัว Nexus 7 รุ่นเดิมทั้งหมด.

Ubuntu ยังมองหาการเปิดใช้งาน TRIM โดยอัตโนมัติด้วยการให้ระบบรัน fstrim อย่างสม่ำเสมอ หวังว่านี่จะเป็นส่วนหนึ่งของ Ubuntu 14.04 ดังนั้นผู้ใช้ Ubuntu จะไม่ถูกบังคับให้จัดการกับการเสื่อมประสิทธิภาพของ SSD หรือรัน fstrim ด้วยตนเอง.

วิธีเปิดใช้งาน TRIM

เราไม่แนะนำให้ติดตั้งระบบไฟล์ของคุณด้วยการดำเนินการ “ทิ้ง” เนื่องจากอาจทำให้ประสิทธิภาพในการใช้งานปกติช้าลง อย่างไรก็ตามคุณสามารถใช้ TRIM ได้ด้วยการเรียกใช้คำสั่ง fstrim หรือสร้าง cronjob ของคุณเองที่รัน fstrim ตามกำหนดเวลา.

ในการตัด SSD ของคุณบน Ubuntu เพียงแค่เปิดเทอร์มินัลแล้วเรียกใช้คำสั่งต่อไปนี้:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

คุณสามารถเรียกใช้คำสั่งดังกล่าวเป็นครั้งคราวเพื่อป้องกันการเสื่อมประสิทธิภาพใน SSD ความถี่ที่คุณต้องเปิดใช้งานขึ้นอยู่กับความถี่ในการลบไฟล์ออกจาก SSD คุณจะเห็นข้อผิดพลาดหากคุณพยายามเรียกใช้คำสั่งด้วยไดรฟ์ที่ไม่รองรับ TRIM.

หากคุณต้องการเรียกใช้ TRIM เป็นประจำคุณสามารถสร้าง cronjob ที่รันคำสั่ง fstrim สำหรับคุณ ต่อไปนี้เป็นวิธีสร้างงาน cron เปล่าที่จะทำสิ่งนี้โดยอัตโนมัติ.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

บันทึกไฟล์โดยกด Ctrl + O แล้วกด Enter เพื่อยืนยัน กด Ctrl + X เพื่อปิด nano หลังจากบันทึกไฟล์.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

ตอนนี้ Ubuntu จะเรียกใช้ fstrim ตามกำหนดเวลาเช่นเดียวกับการทำงานบำรุงรักษาระบบอื่น ๆ.

โปรดทราบว่า TRIM ได้รับการสนับสนุนเฉพาะกับระบบไฟล์ที่ทันสมัยดังนั้นคุณต้องมีสิ่งที่ต้องการ ext4 และไม่ใช่ ext3 หรือ ext2 หากคุณไม่ทราบว่าระบบไฟล์ใดที่คุณใช้อยู่ไม่ต้องกังวล ext4 จะถูกเลือกตามค่าเริ่มต้น.

คำแนะนำนี้ส่วนใหญ่ยังใช้กับการกระจาย Linux อื่น ๆ ในขณะที่ Linux ใช้การสนับสนุน TRIM ในเคอร์เนลนานมาแล้วการรองรับ TRIM ดูเหมือนจะไม่เคยเปิดใช้งานโดยค่าเริ่มต้นสำหรับผู้ใช้ทั่วไปในการแจกแจง Linux.

เครดิตรูปภาพ: Mace Ojala on Flickr (เกรียน)

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu muốn bật TRIM cho SSD theo mặc định trong Ubuntu 14.04. Nói cách khác, Ubuntu chưa sử dụng TRIM, do đó SSD của bạn đang chậm dần theo thời gian. Nhưng tại sao Ubuntu không sử dụng TRIM?

Tin tức này có thể sẽ gây ngạc nhiên cho nhiều người, những người cho rằng Ubuntu và các bản phân phối Linux khác đã sử dụng TRIM. TRIM ngăn SSD chậm lại theo thời gian và là một phần cần thiết trong bảo trì SSD.

Tại sao TRIM lại quan trọng

Chúng tôi đã đề cập tại sao TRIM lại quan trọng trước đây. Khi bạn xóa một tệp trên ổ cứng cũ, từ tính, máy tính chỉ cần đánh dấu tệp đó là đã xóa. Dữ liệu của tệp dính xung quanh trên ổ cứng – đó là lý do tại sao các tệp bị xóa có thể được phục hồi. Cuối cùng, máy tính sẽ ghi đè lên các tệp đã bị xóa khi ghi đè lên các cung của chúng bằng dữ liệu mới.

Ổ đĩa trạng thái rắn (SSD) hoạt động khác nhau. Bất cứ khi nào bạn ghi một tệp vào ổ SSD, trước tiên máy tính phải xóa bất kỳ dữ liệu nào trong các lĩnh vực mà nó ghi dữ liệu. Nó không thể chỉ ghi đè lên các lĩnh vực trong một hoạt động – trước tiên phải xóa chúng, sau đó ghi vào các khu vực trống.

Điều này có nghĩa là một ổ SSD sẽ chậm lại theo thời gian. Viết thư cho các lĩnh vực của SSD sẽ nhanh chóng ngay lần đầu tiên. Sau khi bạn xóa một số tệp và cố gắng ghi lại nó, sẽ mất nhiều thời gian hơn. Đây là một phần lớn lý do khiến Nexus 7 gốc của Google bị chậm đi rất nhiều theo thời gian. Google đã khắc phục điều này bằng cách triển khai TRIM trong Android 4.3. (Android cũng sử dụng nhân Linux.)

Khi bật TRIM, hệ điều hành sẽ báo cho SSD mỗi lần xóa tệp. Ổ đĩa sau đó có thể xóa các thành phần chứa nội dung của tệp, vì vậy việc ghi vào các lĩnh vực sẽ nhanh chóng trong tương lai.

Nói cách khác, nếu bạn không sử dụng TRIM, SSD của bạn sẽ chậm dần theo thời gian. Đó là lý do tại sao các hệ điều hành hiện đại, bao gồm Windows 7+, Mac OS X 10.6.8+ và Android 4.3+ sử dụng TRIM. TRIM đã được triển khai trong Linux vào tháng 12 năm 2008, nhưng Ubuntu không sử dụng nó theo mặc định.

Tại sao Ubuntu không TRIM theo mặc định?

Lý do thực sự khiến Ubuntu không TRIM SSD theo mặc định là do việc triển khai TRIM của nhân Linux chậm và dẫn đến hiệu suất kém khi sử dụng bình thường.

Trên Windows 7 và 8, Windows sẽ gửi lệnh TRIM mỗi lần nó xóa một tệp, yêu cầu ổ đĩa xóa ngay các bit của tệp. Linux hỗ trợ điều này khi các hệ thống tập tin được gắn kết với tùy chọn loại bỏ từ chối. Tuy nhiên, Ubuntu – và các bản phân phối khác – không làm điều này theo mặc định vì lý do hiệu suất.

Wiki của OpenSUSE chứa một số thông tin chi tiết từ một nhà phát triển quen thuộc với nhân Linux hơn chúng ta. Đó là một chút ngày, nhưng có khả năng vẫn đúng khi nói về hiệu suất:

Phần mềm triển khai kernel của trim thời gian thực trong 11.2, 11.3 và 11.4 không được tối ưu hóa. Các thông số kỹ thuật. các yêu cầu cắt xén hỗ trợ danh sách vectơ phạm vi cắt xén, nhưng đối với kernel 3.0 trim chỉ được gọi bởi kernel với phạm vi loại bỏ / cắt duy nhất và với SSD giữa năm 2011 hiện tại, điều này đã được chứng minh là làm giảm hiệu suất thay vì tăng hiệu suất. Có một vài lý do để sử dụng hạt nhân hỗ trợ loại bỏ thời gian thực với hạt nhân trước 3,1. Người ta không biết khi nào chức năng loại bỏ hạt nhân sẽ được tối ưu hóa để hoạt động có lợi với các ổ SSD thế hệ hiện tại. [[Nguồn]

Nói cách khác, nhân Linux xử lý các lệnh TRIM thời gian thực như vậy theo cách chậm, không tối ưu hóa. Việc kích hoạt TRIM tương tự như cách Windows thực hiện – nghĩa là, bằng cách sử dụng tùy chọn của Disc Discard – kết quả là hệ thống thực sự trở nên chậm hơn so với khi TRIM không được sử dụng. Ubuntu và các bản phân phối Linux khác không cho phép loại bỏ các loại bỏ mặc định cho các hệ thống tệp của bạn, và bạn không nên.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Có một cách khác

Bởi vì thời gian thực của kernel Linux loại bỏ hoạt động TRIM của kernel không hoạt động tốt, hầu hết các bản phân phối Linux – bao gồm Ubuntu – không sử dụng TRIM tự động. Android cũng không sử dụng TRIM cho đến Android 4.3.

Nhưng có một cách khác để sử dụng TRIM. Thay vì chỉ đơn giản là ban hành lệnh TRIM mỗi khi tệp bị xóa, tính năng FITRIM có thể được sử dụng. Điều này xảy ra thông qua lệnh fstrim. Về cơ bản, lệnh fstrim phân tích hệ thống tệp và thông báo cho ổ đĩa những khối không còn cần thiết nữa, vì vậy ổ đĩa có thể loại bỏ chúng. Điều này biến TRIM từ một hoạt động thời gian thực thành một nhiệm vụ theo lịch trình. Nói cách khác, fstrim có thể thực hiện TRIM như một công việc định kỳ. Không có lý do gì để không làm điều này. Nó sẽ không làm chậm bất cứ điều gì; đó chỉ là một nhiệm vụ dọn dẹp nhà cửa khác mà hệ thống phải thực hiện theo lịch trình.

Trên thực tế, đây là cách tiếp cận Google đã thực hiện với Android 4.3. Android chỉ thỉnh thoảng chạy một tác vụ fstrim để TRIM hệ thống tệp, khắc phục sự cố làm chậm tất cả những chiếc Nexus 7 gốc đó.

Ubuntu cũng đang xem xét kích hoạt TRIM tự động bằng cách hệ thống thường xuyên chạy fstrim. Hy vọng đây sẽ là một phần của Ubuntu 14.04 vì vậy người dùng Ubuntu sẽ không bị buộc phải đối phó với sự suy giảm hiệu năng của SSD hoặc tự mình chạy fstrim.

Cách bật TRIM

Chúng tôi không khuyên bạn nên gắn các hệ thống tệp của mình với hoạt động loại bỏ từ chối, vì điều này có thể sẽ dẫn đến hiệu suất chậm hơn trong sử dụng bình thường. Tuy nhiên, bạn có thể tự sử dụng TRIM bằng cách thỉnh thoảng chạy lệnh fstrim hoặc tạo cronjob của riêng bạn chạy fstrim theo lịch trình.

Để TRIM SSD của bạn trên Ubuntu, chỉ cần mở một thiết bị đầu cuối và chạy lệnh sau:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Thỉnh thoảng bạn có thể chạy lệnh trên để ngăn chặn sự suy giảm hiệu suất trên SSD. Tần suất bạn cần chạy nó tùy thuộc vào tần suất xóa các tệp khỏi ổ SSD của bạn. Bạn sẽ thấy lỗi nếu bạn cố chạy lệnh với ổ đĩa không hỗ trợ TRIM.

Nếu bạn muốn chạy TRIM thường xuyên, bạn chỉ cần tạo một cronjob chạy lệnh fstrim cho bạn. Dưới đây là cách thực hiện công việc định kỳ barebones sẽ tự động thực hiện việc này.

Đầu tiên, chạy lệnh sau để mở trình soạn thảo văn bản nano với quyền root:

Nhập mã sau vào tệp:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Lưu tệp bằng cách nhấn Ctrl + O và nhấn Enter để xác nhận. Nhấn Ctrl + X để đóng nano sau khi lưu tệp.

Cuối cùng, chạy lệnh sau để thực thi tập lệnh:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu bây giờ sẽ chạy fstrim theo lịch trình, giống như các nhiệm vụ bảo trì hệ thống khác.

Lưu ý rằng TRIM chỉ được hỗ trợ trên các hệ thống tệp hiện đại, vì vậy bạn sẽ cần một cái gì đó như ext4 chứ không phải ext3 hoặc ext2. Nếu bạn không biết hệ thống tệp nào mình đang sử dụng, đừng lo lắng – ext4 được chọn theo mặc định.

Phần lớn lời khuyên này cũng áp dụng cho các bản phân phối Linux khác. Mặc dù Linux đã triển khai hỗ trợ TRIM trong kernel từ lâu, nhưng hỗ trợ TRIM của nó dường như chưa bao giờ được bật theo mặc định cho người dùng thông thường trong các bản phân phối Linux.

Tín dụng hình ảnh: Mace Ojala trên Flickr (đã cắt)

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Trim command (TRIM) helps keep your solid-state drive (SSD) at its full speed. If you find your SSD slowed down after a while of using, TRIM may help you make SSD always fast.

Because low-level operation of SSDs differs significantly from hard drives, the typical way in which operating systems handle operations like deletes and formats resulted in unanticipated progressive performance degradation of write operations on SSDs. Trimming enables the SSD to handle garbage collection overhead, which would otherwise significantly slow down future write operations to the involved blocks, in advance.

See more about TRIM on the Wiki page

UPDATE: Ubuntu 14.04 Trusty will have TRIM enabled by default.

To get started, press Ctrl+Alt+T on keyboard to open terminal. When it opens, follow steps below:

1. Check out if you have an SSD:

If you got 0 it’s a SSD. If the output was 1 it’s a HDD.

2. Even if you have an SSD not all of them support TRIM. To find out if yours does run:

If what you get back is this:

Data Set Management TRIM supported

Then you are all good to go. If there’s no output, your SSD doesn’t support TRIM.

You should see an output that looks something like this:

4. If everything went OK it’s time to make a CRON job so fstrim run’s once a day.

  • نصائح
  • مدونة
  • كيف
  • شبابيك
  • هاتف

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

  • رئيسي 
  • مدونة 
  • Ubuntu Doesn’t TRIM SSDs By Default: Why Not and How To Enable It yourself

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

يريد Ubuntu تمكين TRIM for SSDs بشكل افتراضي في Ubuntu 14.04. بعبارة أخرى ، لا يستخدم Ubuntu بالفعل TRIM ، لذا فإن SSD الخاص بك يتباطأ بمرور الوقت. لكن لماذا لا يستخدم Ubuntu بالفعل TRIM؟

من المحتمل أن تكون هذه الأخبار مفاجئة لكثير من الناس ، الذين افترضوا أن أوبونتو وغيرها من توزيعات لينكس كانت تستخدم بالفعل TRIM. تمنع TRIM محركات SSD من التباطؤ مع مرور الوقت وهي جزء ضروري من صيانة SSD.

لماذا تعتبر TRIM مهمة

لقد تناولنا سبب أهمية TRIM من قبل. عندما تقوم بحذف ملف على قرص صلب قديم ، فإن الكمبيوتر يقوم ببساطة بتحديد هذا الملف على أنه محذوف. تلتصق بيانات الملف على القرص الصلب – ولهذا السبب يمكن استعادة الملفات المحذوفة. سيقوم الكمبيوتر في نهاية المطاف بالكتابة فوق الملفات المحذوفة عند الكتابة فوق قطاعاتها ببيانات جديدة.

تعمل محركات الأقراص ذات الحالة الصلبة (SSD) بشكل مختلف. عندما تكتب ملفًا إلى SSD ، يجب على الكمبيوتر أولاً مسح أي بيانات في القطاعات التي يكتب البيانات إليها. لا يمكن فقط “استبدال” القطاعات في عملية واحدة – يجب أولاً مسحها ، ثم الكتابة إلى القطاعات الفارغة.

هذا يعني أن SSD سيتباطأ مع مرور الوقت. ستكون الكتابة إلى قطاعات SSD سريعة في المرة الأولى. بعد حذف بعض الملفات ومحاولة الكتابة إليها مرة أخرى ، ستستغرق وقتًا أطول. هذا جزء كبير من السبب في أن جهاز Nexus 7 الأصلي من Google قد تباطأ كثيرًا بمرور الوقت. قامت Google بإصلاح هذا عن طريق تنفيذ TRIM في Android 4.3. (يستخدم Android أيضًا نظام Linux kernel).

مع تمكين TRIM ، يخبر نظام التشغيل SSD في كل مرة يقوم فيها بحذف ملف. يمكن لمحرك الأقراص بعد ذلك مسح القطاعات التي تحتوي على محتويات الملف ، لذلك فإن الكتابة إلى القطاعات ستكون سريعة في المستقبل.

بعبارة أخرى ، إذا كنت لا تستخدم TRIM ، فسوف يتباطأ محرك أقراص SSD بمرور الوقت. لهذا السبب تستخدم أنظمة التشغيل الحديثة ، بما في ذلك نظام التشغيل Windows 7 والإصدارات الأحدث ونظام التشغيل Mac OS X 10.6.8 والإصدارات الأحدث و Android 4.3 والإصدارات الأحدث نظام TRIM. تم تنفيذ TRIM في Linux في ديسمبر 2008 ، ولكن Ubuntu لا يستخدمه بشكل افتراضي.

لماذا لا تفعل أوبونتو تريم بشكل افتراضي؟

السبب الحقيقي هو أن Ubuntu لا يتعامل مع TRIM SSD بشكل افتراضي لأن تطبيق Linux kernel لـ TRIM بطيء ويؤدي إلى أداء ضعيف في الاستخدام العادي.

في نظامي التشغيل Windows 7 و 8 ، يرسل Windows أمر TRIM في كل مرة يقوم فيها بحذف ملف ، ويخبر محرك الأقراص بحذف بتات الملف فورًا. يدعم Linux هذا عندما يتم تثبيت أنظمة الملفات مع خيار “تجاهل”. ومع ذلك ، فإن Ubuntu – والتوزيعات الأخرى – لا تفعل ذلك بشكل افتراضي لأسباب تتعلق بالأداء.

يحتوي الويكي الخاص بـ OpenSUSE على بعض المعلومات التفصيلية من مطور يكون أكثر دراية بنواة لينكس مما نحن عليه. إنه مؤرخ ، لكن من المحتمل أن يكون صحيحًا عندما يتعلق الأمر بالأداء:

“The kernel implementation of realtime trim in 11.2, 11.3, and 11.4 is not optimized. The spec. calls for trim supporting a vectorized list of trim ranges, but as of kernel 3.0 trim is only invoked by the kernel with a single discard / trim range and with current mid 2011 SSDs this has proven to cause a performance degradation instead of a performance increase. There are few reasons to use the kernels realtime discard support with pre-3.1 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs.” [Source]

بمعنى آخر ، تعالج نواة لينكس أوامر TRIM الحقيقية هذه بطريقة بطيئة وغير مجدية. إن تمكين TRIM يشبه طريقة عمل Windows – أي باستخدام خيار “تجاهل” – يؤدي إلى أن يصبح النظام أبطأ بالفعل مما لو لم يتم استخدام TRIM على الإطلاق. لا تعمل Ubuntu وتوزيعات Linux الأخرى على تمكين “تجاهل” بشكل افتراضي لأنظمة الملفات لديك ، ولا يجب عليك أيضًا.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

هناك طريق آخر

نظرًا لأن عملية TRIM في الوقت الفعلي “تجاهل” لنواة نظام التشغيل Linux لا تعمل بشكل جيد ، فإن معظم توزيعات Linux – بما في ذلك Ubuntu – لا تستخدم TRIM تلقائيًا. كما أن Android لم يستخدم TRIM حتى Android 4.3.

ولكن هناك طريقة أخرى لاستخدام TRIM. بدلاً من مجرد إصدار أمر TRIM في كل مرة يتم فيها حذف ملف ، يمكن استخدام ميزة FITRIM. يحدث هذا عن طريق الأمر fstrim. وبشكل أساسي ، يقوم الأمر fstrim بتحليل نظام الملفات وإعلام محرك الأقراص الذي لم تعد هناك حاجة إلى الكتل ، بحيث يمكن لمحرك الأقراص التخلص منها. يؤدي هذا إلى تحويل TRIM من عملية في الوقت الفعلي إلى مهمة مجدولة. وبعبارة أخرى ، يمكن لـ Fstrim إجراء TRIM كعمل كرون. لا يوجد سبب لعدم القيام بذلك. لن يبطئ أي شيء ؛ إنها مهمة تنظيف أخرى يجب على النظام تنفيذها وفقًا لجدول زمني.

في الواقع ، هذه هي الطريقة التي اتخذتها Google مع Android 4.3. يدير Android مهمة fstrim من حين لآخر لتتبع نظام الملفات ، مع إصلاح المشكلة التي أدت إلى تباطؤ جميع أجهزة Nexus 7 الأصلية.

كما تبحث Ubuntu أيضًا في تمكين TRIM تلقائيًا من خلال تشغيل النظام بشكل منتظم. من المؤمل أن يكون هذا جزءًا من Ubuntu 14.04 حتى لا يضطر مستخدمو Ubuntu إلى التعامل مع تدهور أداء SSD أو تشغيل fstrim بأنفسهم.

كيفية تمكين تريم

لا نوصي بتركيب أنظمة الملفات لديك باستخدام عملية “التراجع” ، نظرًا لأن هذا سيؤدي على الأرجح إلى أداء أبطأ في الاستخدام العادي. ومع ذلك ، يمكنك استخدام TRIM بنفسك من خلال تشغيل الأمر fstrim من وقت لآخر أو إنشاء cronjob الخاص بك الذي يدير fstrim على جدول زمني.

لتتبع SSD الخاص بك على Ubuntu ، ببساطة فتح محطة وتشغيل الأمر التالي:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

يمكنك تشغيل الأمر أعلاه من وقت لآخر لمنع تدهور الأداء على محركات الأقراص ذات الحالة الثابتة. يعتمد عدد مرات تشغيله على عدد مرات حذف الملفات من SSD. سترى خطأً إذا حاولت تشغيل الأمر باستخدام محرك أقراص لا يدعم TRIM.

إذا كنت تريد تشغيل TRIM بانتظام ، يمكنك ببساطة إنشاء cronjob الذي يدير الأمر fstrim لك.في ما يلي كيفية إنشاء مهمة cirebones cron تؤدي ذلك تلقائيًا.

أولاً ، قم بتشغيل الأمر التالي لفتح محرر نصوص nano بأذونات الجذر:

sudo nano /etc/

اكتب التعليمة البرمجية التالية في الملف:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

احفظ الملف عن طريق الضغط على Ctrl + O ثم اضغط على Enter للتأكيد. اضغط على Ctrl + X لإغلاق nano بعد حفظ الملف.

أخيرًا ، قم بتشغيل الأمر التالي لجعل البرنامج النصي قابل للتنفيذ:

sudo chmod +x /etc/

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

سيقوم Ubuntu الآن بتشغيل Fstrim وفقًا لجدول زمني ، تمامًا كما يفعل مهام صيانة النظام الأخرى.

لاحظ أن TRIM مدعوم فقط على أنظمة الملفات الحديثة ، لذلك ستحتاج إلى شيء مثل ext4 وليس ext3 أو ext2. إذا كنت لا تعرف نظام الملفات الذي تستخدمه ، فلا داعي للقلق – حيث يتم تحديد ext4 افتراضيًا.

وينطبق الكثير من هذه النصائح أيضًا على توزيعات لينكس الأخرى. في حين أن لينكس قام بتطبيق دعم TRIM في النواة منذ فترة طويلة ، يبدو أن دعم TRIM لم يتم تمكينه افتراضيًا للمستخدمين العاديين في توزيعات Linux.

Ubuntu želi omogućiti TRIM za SSD-ove po defaultu u Ubuntu 14.04. Drugim riječima, Ubuntu već ne koristi TRIM, pa se SSD s vremenom usporava. Ali zašto Ubuntu već ne koristi TRIM?

Ova će vijest vjerojatno biti iznenađenje za mnoge ljude koji su pretpostavili da Ubuntu i druge Linux distribucije već koriste TRIM. TRIM sprječava usporavanje SSD-ova tijekom vremena i nužan je dio održavanja SSD-a.

Zašto je TRIM važan

Preispitali smo zašto je TRIM prije važan. Kada izbrišete datoteku na starom magnetskom tvrdom disku, računalo jednostavno označava tu datoteku kao izbrisanu. Podatci se nalaze na tvrdom disku – zato se izbrisane datoteke mogu vratiti. Računalo će na kraju prebrisati izbrisane datoteke kada prepiše svoje sektore novim podacima.

SSD pogoni rade drugačije. Kad god pišete datoteku na SSD, računalo prvo mora izbrisati sve podatke u sektorima u koje piše podatke. Ona ne može jednostavno “prepisati” sektore u jednoj operaciji – prvo ih mora izbrisati, a zatim zapisati u prazne sektore.

To znači da će SSD s vremenom usporiti. Pisanje u sektorima SSD-a bit će brzo prvi put. Nakon što izbrišete neke datoteke i ponovno pokušate pisati, bit će potrebno dulje vrijeme. To je veliki dio razloga što je Googleov originalni Nexus 7 toliko usporio tijekom vremena. Google je to riješio implementacijom TRIM-a u Android 4.3. (Android također koristi Linux kernel.)

Kada je TRIM omogućen, operativni sustav javlja SSD-u svaki put kada izbriše datoteku. Pogon tada može izbrisati sektore koji sadrže sadržaj datoteke, tako da će pisanje u sektore biti brzo u budućnosti.

Drugim riječima, ako ne koristite TRIM, vaš SSD će se s vremenom usporiti. Zato moderni operativni sustavi, uključujući Windows 7+, Mac OS X 10.6.8+ i Android 4.3+, koriste TRIM. TRIM je implementiran u Linuxu još u prosincu 2008., ali ga Ubuntu ne koristi po defaultu.

Zašto ne Ubuntu TRIM po defaultu?

Pravi razlog zašto Ubuntu ne TRIM SSD-ove po defaultu je zato što je implementacija TRIM-a u Linux kernelu spora i rezultira lošim performansama u normalnoj upotrebi.

Na sustavima Windows 7 i 8, Windows šalje naredbu TRIM svaki put kada izbriše datoteku, govoreći pogonu da odmah izbriše bitove datoteke. Linux to podržava kada su datotečni sustavi montirani s opcijom “discard”. Međutim, Ubuntu – i druge distribucije – to ne čine prema zadanim postavkama zbog performansi.

OpenSUSEov wiki sadrži neke detaljne informacije od razvojnog programera koji je bolje upoznat s Linux kernelom nego što smo mi. Malo je datirano, ali je vjerojatno još uvijek istinito kada je riječ o izvedbi:

“Implementacija kernela stvarnog vremena u 11.2, 11.3 i 11.4 nije optimizirana. Specifikacije poziva na trim koji podržava vektorski popis raspona opreme, ali od trim kernela 3.0 kernel se poziva samo s jednim rasponom odbacivanja / uklanjanja i sa trenutnim SSD-ovima iz sredine 2011. to se pokazalo da uzrokuje smanjenje performansi umjesto povećanja performansi. Postoji nekoliko razloga za korištenje jezgre podrške u realnom vremenu s kernelima pre-3.1. Nije poznato kada će funkcionalnost odbacivanja kernela biti optimizirana za rad s trenutnim generacijskim SSD-ovima. ”[Izvor]

Drugim riječima, Linux kernel upravlja takvim TRIM naredbama u realnom vremenu na spor, neoptimiziran način. Omogućavanje TRIM-a slično načinu na koji Windows radi – to jest, upotrebom opcije “odbaci” – rezultira time da sustav zapravo postaje sporiji nego ako se TRIM uopće ne koristi. Ubuntu i druge distribucije Linuxa ne omogućuju “odbacivanje” prema zadanim postavkama za vaše datotečne sustave, a ni vi ne biste trebali.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Postoji još jedan način

Budući da TRIM operacija u realnom vremenu u Linuxu nije uspješna, većina Linux distribucija – uključujući Ubuntu – ne koristi TRIM automatski. Android također nije koristio TRIM do Androida 4.3.

Ali postoji još jedan način korištenja TRIM-a. Umjesto jednostavnog izdavanja TRIM naredbe svaki put kad se datoteka izbriše, može se koristiti značajka FITRIM. To se događa preko naredbe fstrim. Zapravo, naredba fstrim analizira sustav datoteka i informira pogon koji blokovi više nisu potrebni, tako da ih disk može odbaciti. Time se TRIM pretvara iz operacije u realnom vremenu u planirani zadatak. Drugim riječima, fstrim može izvoditi TRIM kao cron posao. Nema razloga za to. Neće ništa usporiti; to je samo još jedan zadatak čišćenja koji sustav mora obaviti po rasporedu.

Zapravo, to je pristup koji je Google uzeo s Androidom 4.3. Android jednostavno pokreće fstrim zadatak povremeno da TRIM datotečni sustav, popravljajući problem koji je usporio sve one izvorne Nexus 7s.

Ubuntu također pokušava automatski omogućiti TRIM tako da sustav redovito pokreće fstrim. Ovo će, nadamo se, biti dio Ubuntu 14.04 tako da korisnici Ubuntua neće biti prisiljeni suočiti se sa propadanjem performansi SSD-a ili pokrenuti fstrim sami.

Kako omogućiti TRIM

Ne preporučujemo montiranje sustava datoteka s operacijom “odbacivanje”, jer će to vjerojatno rezultirati sporijim performansama u normalnoj uporabi. Međutim, sami možete koristiti TRIM tako da povremeno pokrenete fstrim naredbu ili kreirate vlastiti cronjob koji pokreće fstrim po rasporedu.

TRIM svoj SSD na Ubuntu, jednostavno otvorite terminal i pokrenite sljedeću naredbu:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Gornju naredbu možete pokrenuti povremeno kako biste spriječili pogoršanje performansi na SSD-ovima. Koliko često trebate pokretati ovisi o tome koliko često se datoteke brišu s SSD-a. Vidjet ćete pogrešku ako pokušate pokrenuti naredbu s pogonom koji ne podržava TRIM.

Ako želite redovito pokretati TRIM, možete jednostavno stvoriti cronjob koji pokreće naredbu fstrim za vas. Evo kako napraviti barebones cron posao koji će to učiniti automatski.

Najprije pokrenite sljedeću naredbu za otvaranje nano teksta s root dopuštenjima:

Upišite sljedeći kôd u datoteku:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Spremite datoteku pritiskom na Ctrl + O i pritisnite Enter za potvrdu. Pritisnite Ctrl + X da biste zatvorili nano nakon spremanja datoteke.

Posljednje, pokrenite sljedeću naredbu kako bi skripta bila izvršna:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu će sada izvoditi fstrim po rasporedu, baš kao i druge zadatke održavanja sustava.

Imajte na umu da je TRIM podržan samo na modernim datotečnim sustavima, tako da ćete trebati nešto poput ext4, a ne ext3 ili ext2. Ako ne znate koji datotečni sustav koristite, ne brinite – ext4 je odabran prema zadanim postavkama.

Velik dio ovih savjeta vrijedi i za druge Linux distribucije. Dok je Linux u kernelu davno implementirao TRIM podršku, čini se da TRIM podrška nikada nije bila omogućena po defaultu za tipične korisnike u Linux distribucijama.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Geoffrey_Carr

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu vēlas, lai Ubuntu 14.04 versijā pēc noklusējuma iespējotu TRIM par SSD. Citiem vārdiem sakot, Ubuntu vēl neizmanto TRIM, tāpēc jūsu SSD laika gaitā palēnināsies. Bet kāpēc Ubuntu jau neizmanto TRIM?

Šīs ziņas, visticamāk, būs pārsteigums daudziem cilvēkiem, kuri uzskatīja, ka Ubuntu un citi Linux izplatītāji jau izmanto TRIM. TRIM neļauj SSDs palēnināties laika gaitā un ir nepieciešama SSD tehniskās apkopes daļa.

Kāpēc TRIM ir svarīgs

Mēs esam apsprieduši, kāpēc TRIM ir svarīgs pirms tam. Ja izdzēšat failu vecā, magnētiskā cietā diska, dators vienkārši atzīmē šo failu kā dzēstu. Faila dati tiek novietoti cietajā diskā – tādēļ dzēstos failus var atgūt. Dators galu galā pārrakstīs izdzēstos failus, kad tie pārraksta savas nozares ar jauniem datiem.

Cietvielu diskdziņi (SSD) strādā atšķirīgi. Ikreiz, kad rakstāt failu SSD, vispirms datoram vispirms jāizdzēš visi dati nozarēs, uz kurām dati tiek rakstīti. Tas nevar vienkārši “pārrakstīt” sektorus vienā operācijā – tai vispirms ir jāpārklāj tie, tad rakstiet tukšajos sektoros.

Tas nozīmē, ka laika gaitā SSD palēnināsies. Rakstīšana SSD nozarēs būs ātra pirmoreiz. Pēc tam, kad esat izdzēš dažus failus un mēģinās vēlreiz rakstīt, tas aizņems laiku. Šī ir liela iemesla dēļ Google sākotnējais Nexus 7 laika gaitā palēninājās. Google to fiksēja, ieviešot TRIM Android 4.3 versijā. (Android arī izmanto Linux kodolu.)

Izmantojot TRIM, operētājsistēma parāda SSD katru reizi, kad tiek izdzēsts fails. Pēc tam disks var izdzēst sektorus, kuros ir faila saturs, tādēļ nākotnē rakstīšana nozarēs būs ātra.

Citiem vārdiem sakot, ja jūs neizmantojat TRIM, jūsu SSD laika gaitā palēnināsies. Tāpēc mūsdienu operētājsistēmas, tostarp Windows 7+, Mac OS X 10.6.8+ un Android 4.3+, izmanto TRIM. TRIM tika ieviests Linux 2008. gada decembrī, bet Ubuntu to neizmanto pēc noklusējuma.

Kāpēc Ubuntu trima pēc noklusējuma?

Patiesais iemesls Ubuntu pēc noklusējuma nav TRIM SSD, jo Linux kodola TRIM ieviešana ir lēna un normālā lietošanā ir slikta veiktspēja.

Operētājsistēmās Windows 7 un Windows 8 katru reizi, kad tiek dzēsts fails, sistēma Windows nosūta komandu TRIM, paziņojot diskdzinim, lai nekavējoties izdzēstu faila biti. Linux to atbalsta, ja failu sistēmas tiek montētas ar opciju “atbrīvoties”. Tomēr Ubuntu un citiem izplatījumiem tas pēc noklusējuma netiek veikts izpildes dēļ.

OpenSUSE wiki satur detalizētu informāciju no izstrādātāja, kas ir vairāk iepazinies ar Linux kodolu nekā mēs esam. Tas ir nedaudz novecojis, taču, visticamāk, joprojām ir taisnība, kad runa ir par veiktspēju:

“The kernel implementation of realtime trim in 11.2, 11.3, and 11.4 is not optimized. The spec. calls for trim supporting a vectorized list of trim ranges, but as of kernel 3.0 trim is only invoked by the kernel with a single discard / trim range and with current mid 2011 SSDs this has proven to cause a performance degradation instead of a performance increase. There are few reasons to use the kernels realtime discard support with pre-3.1 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs.” [Source]

Citiem vārdiem sakot, Linux kodols apstrādā šādas reāllaika komandas TRIM lēni, neoptimizēti. Iespējojot TRIM līdzīgu tam, kā darbojas Windows – tas ir, izmantojot opciju “izmest” – sistēma faktiski kļūst lēnāka nekā tad, ja TRIM vispār netiek izmantots. Ubuntu un citi Linux sadalījumi pēc noklusējuma neļauj “atbrīvoties” jūsu failu sistēmām, un jums tas nevajadzētu.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Tur ir cits ceļš

Tā kā Linux kodola reāllaika “izmešanas” TRIM darbība nedarbojas labi, lielākā daļa Linux sadali, ieskaitot Ubuntu, automātiski neizmanto TRIM. Android arī neizmantoja TRIM, kamēr nav operētājsistēmas Android 4.3.

Bet tur ir vēl viens veids, kā izmantot TRIM. Nevis vienkārši izsniedzot TRIM komandu katru reizi, kad tiek dzēsts fails, var izmantot FITRIM funkciju. Tas notiek, izmantojot komandu fstrim. Būtībā komanda fstrim analizē failu sistēmu un informē disku, kādi bloki vairs nav vajadzīgi, tāpēc disks var tos atbrīvot. Tas izslēdz TRIM no reāllaika operācijas par plānotu uzdevumu. Citiem vārdiem sakot, fstrim var veikt TRIM kā cron darbu. Nav iemesla to nedarīt. Tas netiks palēnināt neko; tas ir tikai vēl viens mājsaimniecības uzdevums, kas sistēmai jāveic pēc grafika.

Patiesībā šī ir pieeja, kuru Google izmantoja, izmantojot Android 4.3. Android vienkārši laiku pa laikam pavada fstrim uzdevumu pārvietot failu sistēmu, novēršot problēmu, kas palēnināja visus sākotnējos Nexus 7s.

Ubuntu arī meklē automātiski iespējot TRIM, regulējot sistēmas darbību fstrim. Tas, cerams, būs daļa no Ubuntu 14.04, tādēļ Ubuntu lietotāji nebūs spiesti rīkoties ar SSD veiktspējas degradāciju vai palaist firstim paši.

Kā iespējot TRIM

Mēs neiesakām instalēt failu sistēmas ar “atmest” darbību, jo tas, iespējams, novedīs pie lēnākas veiktspējas parastā lietošanā. Tomēr jūs varat izmantot TRIM sevi, laiku pa laikam palaižot komandu fstrim vai izveidojot savu cronjob, kas darbojas fstrim pēc grafika.

Lai izdzēstu savu SSD uz Ubuntu, vienkārši atveriet termināli un palaidiet šo komandu:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Jūs reizēm varat palaist šo komandu, lai novērstu SSD darbības traucējumus. Cik bieži jums jāstrādā, atkarīgs no tā, cik bieži faili tiek izdzēsti no SSD. Ja mēģināsiet palaist komandu ar disku, kas neatbalsta TRIM, jūs redzēsit kļūdu.

Ja jūs vēlaties regulāri palaist TRIM, jūs varat vienkārši izveidot cronjob, kas jums izpilda komandu fstrim.Lūk, kā padarīt barebones cron darbu, kas to darīs automātiski.

Pirmkārt, palaidiet šādu komandu, lai atvērtu nano teksta redaktoru ar saknes atļaujām:

sudo nano /etc/

Ievadiet šādu kodu failā:

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Saglabājiet failu, nospiežot Ctrl + O un nospiediet Enter, lai apstiprinātu. Nospiediet Ctrl + X, lai aizvērtu nano pēc faila saglabāšanas.

Visbeidzot, izpildiet šādu skriptu:

sudo chmod +x /etc/

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Ubuntu tagad palaiž fstrim pēc grafika, tāpat kā citiem sistēmas uzturēšanas uzdevumiem.

Ņemiet vērā, ka TRIM tiek atbalstīts tikai mūsdienu failu sistēmās, tāpēc jums būs nepieciešams kaut kas līdzīgs ext4, nevis ext3 vai ext2. Ja nezināt, kādu failu sistēmu izmantojat, neuztraucieties – pēc noklusējuma tiek atlasīts ext4.

Liela daļa šī padoma attiecas arī uz citiem Linux sadalījumiem. Kamēr Linux jau sen īstenoja TRIM atbalstu kodolā, šķiet, ka tās TRIM atbalsts nekad nav bijis iespējots pēc noklusējuma tipiskiem lietotājiem Linux distribūcijās.

mind dumped

How to properly activate TRIM for your SSD on Linux: fstrim, lvm and dm-crypt

Unlike hard disk drives (HDDs), NAND flash memory that make SSD cannot overwrite existing data. This means that you first have to delete the old data before writing new one. Flash memory is divided into blocks, which is further divided in pages. The minimum write unit is a page, but the smallest erase unit is a block.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Data can be written directly into an empty page, but only whole blocks can be erased. Therefore, to reclaim the space taken up by invalid data, all the valid data from one block must be first copied and written into the empty pages of a new block. Only then can the invalid data in the original block be erased, making it ready for new valid data to be written.

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 won’t be available any empty page. Then every time the drive needs to write a block into any of the semi-full pages, it first needs to copy the current blocks from the page to a buffer, then it has to delete the whole page to finally rewrite the old blocks along with the new one. This means that as time goes on the SSD performance degrades more and more, because for every write it has to go through a cycle of read-erase-modify-write. This is known as “write amplification”.

Without TRIM the disk is unable to know which blocks are in use by a file or which ones are marked as free space. This is because when a file is deleted, the only thing the OS does is to mark the blocks that were used by the file as free inside the file system index. But the OS won’t tell the disk about this. This means that over time the performance of the SSD disk will degrade more and more, and it don’t matters how much space you free, because the SSD won’t know about it.

What is TRIM? TRIM was invented for solving this problem. TRIM is the name of a command that the operating system can send to tell the SSD which blocks are free in the filesystem.
The SSD uses this information to internally defragment the blocks and keep free pages available to be written quickly and efficiently.

How to active TRIM on Linux? The first thing to know is that TRIM should be enabled on all I/O abstraction layers. This means that if you have an ext4 partition on top of LVM, which in turn is on top of an encrypted volume with LUKS/dm-crypt, then you must enable support for TRIM in these three layers: The filesystem, LVM and dm-crypt. There is no point in enabling it at the filesystem level if you don’t enable it also on the other layers. The TRIM command should be translated from one layer to another until reaching the SSD.

Ubuntu doesn’t trim ssds by default why not and how to enable it yourself

Enabling TRIM support on dm-crypt

We simply have to add the option discard inside our crypttab

Note: The usage of TRIM on dm-crypt could cause some security issues like the revelation of which sectors of your disk are unused.

Enabling TRIM support on LVM

We have to enable the option issue_discards in the LVM configuration.

Enabling TRIM support on the file system

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 real-time 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, operation which will take an amount of time higher than desired.

In order to optimize the performance of the SSD, I strongly advise you to avoid doing the TRIM operation in real time (whenever a file is deleted) because you would be putting an unnecessary extra amount of work over the SSD. In other words: You should not enable the discard option in fstab.

Instead, what I recommend is to run a script periodically to tell the SSD which blocks are free with the command fstrim. Doing this operation daily or weekly is more than enough. This way we do not lose any performance due to TRIM when deleting files and we periodically keep informed the SSD about the free blocks.

Other advantages of the fstrim way are:

  • If you didn’t enabled correctly the TRIM support in the above layers of your setup, you will receive an error when executing fstrim. On the other hand, if you were using the discard option at fstab you wouldn’t have received any error and you would end thinking that you managed to get TRIM working properly when you didn’t.
  • If you delete a file by mistake (you know it happens), you can recover it before anacron runs your script fstrim. On the other hand, if you were using the discard-at-fstab option you wouldn’t have any chance of recovering the file, because the OS would have told the SSD to TRIM that blocks as soon as you deleted the file, and consequently the SSD has irreversibly destroyed such blocks.

Here you have simple script to run fstrim on the / , /boot and /home partitions, which can be programmed to be executed periodically by anacron

It’s worth mentioning that both fstrim and discard-at-fstab options only work on filesystems that implement the ioctl FITRIM. Today, such filesystems are:

Note: On most setups you will have to rebuild your initramfs with update-initramfs -u (Debian and derivatives) or dracut -f (Redhat and derivatives) and reboot the machine after touching the configuration options of LVM or dm-crypt.

Update 25-Feb-2013: Seems that Fedora users with a dm-crypt volume will be affected by this problem:

Update 14-Aug-2014: The following script can be used to automatically detect and fstrim all filesystems that have TRIM support enabled.