LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   SSD Trim simple bash script program (http://www.linuxquestions.org/questions/linux-newbie-8/ssd-trim-simple-bash-script-program-890794/)

ITPhoenix 07-09-2011 12:17 PM

SSD Trim simple bash script program
 
Hi folks, I wrote this script which works but it should run automatically about once per week.

I hunted and experimented with KDE Task Scheduler (no dice and no help anywhere) and cron (confusing instructions and cannot edit crontab -e with vim, and cannot enter cron folders/files).

I would settle for a desktop shortcut to run the script but found no help for that.

Script:
Code:

#!/bin/bash
xterm -hold -e fstrim -v /

Machine:

OS: openSUSE 11.4 x86_64
Kernel: Linux 2.6.37.6-0.5-desktop
Desktop: KDE 4.6.00 rel 6
Machine: HP xw9400 AMD 64 Opteron
Chipset: nVidia nForce Pro 3600 and 3050 (proprietary Tyan Thunder)
Drive: OCZ Vertex 60 GB dedicated system--single boot
RAM: 4 GB ECC

Video: nVidia GT200 (GeForce 210) 512 MB
2D Driver: nouveau
3D Driver: swarst (no 3D acceleration) (7.10)

Audio: Creative Titanium Fatal1ty Pro
Driver: ALSA (No-frills. Must use open source controls)

I am not asking anyone to write code, I could use some pointers, though.

Thanks.

weibullguy 07-09-2011 01:20 PM

cron would be the easiest. You should add your average, everyday user to the cron group to gain access to the necessary directories. Set EDITOR to vim and you will be able to edit crontabs with vim.

TobiSGD 07-09-2011 01:23 PM

I have some questions:
1. Which file-system do you use and what mount-options do you have for the partitions on your SSD?
2. Does the SSD have native TRIM support in its firmware?

ITPhoenix 07-09-2011 06:10 PM

Quote:

cron would be the easiest. You should add your average, everyday user to the cron group to gain access to the necessary directories. Set EDITOR to vim and you will be able to edit crontabs with vim.
Yes, this was not done and I suspect is why I cannot open some of the cron folders. I still do not understand how to edit in vim. There is a ghost entry from testing with KDE's Task Scheduler which I prefer to remove. The entry is not active--I grepped it and checked the cron folders for the entry. It was set for every minute to see if it worked, and it is apparent nothing is happening (at least to me).

Quote:

I have some questions:
1. Which file-system do you use and what mount-options do you have for the partitions on your SSD?
2. Does the SSD have native TRIM support in its firmware?
1. Ext4

Mount options: Default Mount by Device ID

2. Yes the drive has trim support. Here is the output of my script:

Code:

/: 16767717376 bytes was trimmed
I think I know where you are going. I saw something that erases useless data but I did not understand it.

Thanks for the input everyone.

TobiSGD 07-09-2011 06:17 PM

If you use ext4 and your device has TRIM support you don't need your script to do the trimming. Just add the mount-option discard in your /etc/fstab to the partitions on the SSD. Now every-time a sync-command is launched (which is at least every shutdown/reboot) your partitions will automatically be trimmed.

You don't need to use vim for editing the crontab, you can use your favorite editor for that. Have a look at the crontab-command for learning how to make use of cron.

ITPhoenix 07-09-2011 07:00 PM

Okay. This is what openSUSE recommends:
Quote:

As of 11.4, fstrim is part of the linux-util package and is the recommended choice for invoking trim for most users......To use the kernel realtime discard feature, you must mount with the "mount -o discard" option. openSUSE will not automatically set this option, so you must either mount the partition manually or update your /etc/fstab file to do so.

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 2.6.39 trim is only invoked by the kernel with a single discard / trim range and with current late 2010 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.0.0 kernels. It is not known when the kernels discard functionality will be optimized to work beneficially with current generation SSDs.
The OCZ Vertex model I have is not a 2010 model but I plan on obtaining a RevoDrive when I can afford it. So I can use mount -0 discard now and appears to be the easiest solution. I saw mention that trimming the swap partition is not advised. This is irrelevant to my situation because of the 4GB RAM present (at least for now).

Here is the fstab file:
Code:

/dev/disk/by-id/ata-OCZ-VERTEX_MG05KW6J9686Z397B4P1-part1 swap                swap      defaults              0 0
/dev/disk/by-id/ata-OCZ-VERTEX_MG05KW6J9686Z397B4P1-part2 /                    ext4      acl,user_xattr        1 1
/dev/disk/by-id/ata-OCZ-VERTEX_MG05KW6J9686Z397B4P1-part3 /home                ext4      defaults              1 2
/dev/disk/by-id/ata-WDC_WD1500HLFS-01G6U1_WD-WXD0CB984225-part1 /windows/C          ntfs-3g    users,gid=users,fmask=133,dmask=022,locale=en_US.UTF-8 0 0
proc                /proc                proc      defaults              0 0
sysfs                /sys                sysfs      noauto                0 0
debugfs              /sys/kernel/debug    debugfs    noauto                0 0
usbfs                /proc/bus/usb        usbfs      noauto                0 0
devpts              /dev/pts            devpts    mode=0620,gid=5      0 0
/dev/disk/by-id/ata-WDC_WD1500HLFS-01G6U1_WD-WXD0CB984225-part2 /local              ext4      defaults              1 2

Any pointers on how to add the mount -0 discard option?

TobiSGD 07-09-2011 07:19 PM

Simply add it after the other options, in your case that it looks like
Code:

/dev/disk/by-id/ata-OCZ-VERTEX_MG05KW6J9686Z397B4P1-part2 /                    ext4      acl,user_xattr,discard        1 1
/dev/disk/by-id/ata-OCZ-VERTEX_MG05KW6J9686Z397B4P1-part3 /home                ext4      defaults,discard              1 2


ITPhoenix 07-09-2011 08:19 PM

This is good. I was just in the man page for fstab (as I should have been) and it gives:
Quote:

use default options: rw, suid, dev, exec, auto, nouser, and async
I see that you want to modify the options for /. I am wondering why the defaults are kept for home/ and not /

man mount (8) gives:
Quote:

discard/nodiscard
Controls whether ext4 should issue discard/TRIM commands to the underlying block device
when blocks are freed. This is useful for SSD devices and sparse/thinly-provisioned
LUNs, but it is off by default until sufficient testing has been done.
I will test it!

TobiSGD 07-09-2011 08:32 PM

Quote:

Originally Posted by ITPhoenix (Post 4410234)
I see that you want to modify the options for /. I am wondering why the defaults are kept for home/ and not /

Sorry, but I don't understand that sentence. I simply have added the discard options to all partitions on that SSD, except the swap-partition. All other options are as given by you, I didn't modify them.

ITPhoenix 07-09-2011 09:00 PM

Sorry, I got confused. Ok, I will run with this. No news is good news; otherwise I will report back.

Thank you.

ITPhoenix 07-10-2011 12:03 PM

There was a false alarm with the last IFL backup. The backup image failure was a fluke (the disk is a little beat up).

So it appears adding

Code:

,discard
to fastab mount options is a workable way to go.

If an application would benefit from more frequent trims, then cron is the way to go.

One question, however. The script command I have is

Code:

xterm -hold -e fstrim -v /
Obviously the -v should go as well as the call for x-term in my original script. But will just the

Code:

/
argument trim all the partitions?

There are two in fstab (disregarding swap, which should researched further).

In fact, the question which partition needs trimming has not been definitively answered AFAIK.

TobiSGD 07-10-2011 12:47 PM

All partitions on that SSD should need trimming, so I would do it for /home also.
If you do trimming with your script in crontab I wouldn't use xterm for that, unless you want that this window pops up every time cron trims the SSD.

chrism01 07-10-2011 07:16 PM

Here's a reasonable guide to crontabs http://adminschoice.com/crontab-quic...Crontab%20file

ITPhoenix 07-11-2011 01:05 AM

Thanks. The more the better.


All times are GMT -5. The time now is 08:42 AM.