public inbox for util-linux@vger.kernel.org
 help / color / mirror / Atom feed
From: "Lukáš Czerner" <lczerner@redhat.com>
To: Karel Zak <kzak@redhat.com>
Cc: Sten Heinze <shze@gmx.de>, util-linux@vger.kernel.org
Subject: Re: Automatic SSD trim script
Date: Mon, 9 Dec 2013 14:43:32 +0100 (CET)	[thread overview]
Message-ID: <alpine.LFD.2.00.1312091438300.2143@localhost.localdomain> (raw)
In-Reply-To: <20131206104944.GE16891@x2.net.home>

On Fri, 6 Dec 2013, Karel Zak wrote:

> Date: Fri, 6 Dec 2013 11:49:44 +0100
> From: Karel Zak <kzak@redhat.com>
> To: Sten Heinze <shze@gmx.de>
> Cc: util-linux@vger.kernel.org, Lukas Czerner <lczerner@redhat.com>
> Subject: Re: Automatic SSD trim script
> 
> 
>  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.

I very much like the idea of having a simple script to add to cron
to be able to keep SSD's at top of its performance.

> 
> 
> 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).

Yes and yes. This is the ultimate solution. Simply being able to run
fstrim --all to discard free space on all the mounted supported file
systems is great idea. In that case we do not need any scripts at
all.

Thanks!
-Lukas

> 
> 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
> 
> 

  reply	other threads:[~2013-12-09 13:43 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-05 19:20 Automatic SSD trim script Sten Heinze
2013-12-06 10:49 ` Karel Zak
2013-12-09 13:43   ` Lukáš Czerner [this message]
2013-12-10 15:44     ` Karel Zak
2013-12-11  2:07       ` Sten Heinze
2013-12-11 10:54       ` Pádraig Brady
2013-12-11 11:36         ` Karel Zak
2013-12-11 16:03         ` Karel Zak
2013-12-17 19:46       ` Marcos Mello
2013-12-17 19:58         ` Marcos Mello
2014-01-13 12:12         ` Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=alpine.LFD.2.00.1312091438300.2143@localhost.localdomain \
    --to=lczerner@redhat.com \
    --cc=kzak@redhat.com \
    --cc=shze@gmx.de \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox