Re: Automatic SSD trim script

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 Hi Sten,

On Thu, Dec 05, 2013 at 08:20:07PM +0100, Sten Heinze wrote:
> Having experienced the drop in speed when using a SSD with online
> discard, I wrote a small perl script to run fstrim using batched
> discard on partitions located on a SSD through cron. Do you think
> util-linux would be a good place for such a script, given that it is
> a helper to fstrim? What would be the best way to include one in
> util-linux?

I have doubts we want to add a perl script to the package as a regular
util, util-linux is very basic package and dependence on Perl is
unexpected here. Maybe it would be possible to write a simple shell
script for this task and add it to the Documentation/example.files/
directory.


IMHO the best solution would be to improve fstrim to trim all
filesystems where it makes sense.

All we need is to link fstrim with libmount and lib/sysfs.c, add a new
option --all and check non-zero /sys/block/<name>/queue/discard_granularity
(or so).

I guess that implement something like this will be ~30 lines in C :-)  

[CC: to Lukas who is fstrim author]

    Karel


Anyway, a few comments to your script:

> # list all mounted drives; blkid doesn't provide mount points; fstab does ans is another possible source.

 don't use mount(8) to list info about mountpoints, findmnt(8) is better, for example:

    findmnt -clo TARGET -O nodiscard

> # maybe only include fixed/internal drives? /sys/block/sdX/removable doesn't help for deciding if a dev is fixed.
> sub get_devs_from_mount {
>   my %devs = (); # empty hash
> 
>   my $output = `$mount`; 
>   my @lines = split( '\n', $output); # split the output into lines
>   foreach my $line ( @lines ) {
>     if( $line =~ m$(\S+) on (/\S*) type \S+ (.*)$) { # eg. /dev/sda8 on /home type ext4 (rw,relatime,data=ordered)
>       my $path = $1;
>       my $mount_point = $2;
>       my $mount_options = $3;
>       next if( index( $mount_options, $discard_option ) != -1 ); # not -1 means found discard, i.e. skip this line
>       next if( ! -e $path ); # skip lines that are virtual fs
>       $path = realpath( $path ); # get absolute path for those mount points that are uuid symlinks
>       $devs{ $path } = $mount_point; # add to hash
>     }
>   }
>   return %devs;
> }
> 
> # check if device is ssd using hdparm
> sub is_ssd_hdparm {
>   my $dev = shift;
>   $dev = substr( $dev, 5, 3 ); # short to 3 chars: /dev/xxxN to xxx
> 
>   return 0 if( ! -X $hdparm || ! -X $grep ); # return no ssd if no hdparm or no grep command available
> 
>   `$hdparm -I /dev/$dev 2>&1 | $grep 'TRIM supported' 2>/dev/null`; # perl calls bash, use bash redirect


 it would be possible to use lsblk to list devices with non-zero DISC-GRAN column,
 the util also provides mounpoints.

> # check if device is ssd using /sys/block/sdX/queue/rotational: 0=SSD, 1=likely HDD, but could be USB memory etc.

 again, use lsblk

-- 
 Karel Zak  <kzak@xxxxxxxxxx>
 http://karelzak.blogspot.com
--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux