Beruflich Dokumente
Kultur Dokumente
http://unix.stackexchange.com/questions/97143/ut...
sign up
log in
tour
Sign up
help
Anybody can
answer
1 ranges
00 01 00 01 00 00 00 00 00 00 40 06 00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
00
/ sd-card
/ trim
Jason C
644
17
If you map the unpartitioned space with one or more partition(s) (temporarily), you could use blkdiscard on them
which trims the entire device. frostschutz Oct 22 '13 at 19:03
Thanks! That looks like the right tool, but blkdiscard does not seem to be available in my standard package
repositories (Ubuntu 12.04, doesn't seem to be present in util-linux ). Jason C Oct 22 '13 at 20:04
I was never able to get hdparm working for me for some reason, but I grabbed the util-linux source from
github.com/karelzak/util-linux/tree/stable/v2.23, built it ( ./autogen.sh ; ./configure ; make blkdiscard )
and it's working great. Still mystified about hdparm but blkdiscard does exactly what I want. Bonus: There's no
need to make a temporary filesystem first, you can use sfdisk -l to figure out the space between partitions and
have blkdiscard trim it. Jason C Oct 30 '13 at 22:14
1 of 3
04/06/2016 06:08 AM
http://unix.stackexchange.com/questions/97143/ut...
Oh, I wasn't even aware of blkdiscard being able to work with offset/length options. So the temporary partitions I
suggested aren't even necessary. Nice! frostschutz Oct 31 '13 at 10:45
2 Answers
If you have a recent enough version of util-linux , it contains the tool blkdiscard which is
able to TRIM entire devices, or ranges within a device using --offset and --length options.
Please note:
gone!
blkdiscard
So you can figure out the unpartitioned (free) regions of your partition table and then TRIM
them using this tool. For msdos and gpt partitions, parted provides the free regions like so:
# parted -m /dev/sda unit b print free | grep ':free;'
1:17408B:1048575B:1031168B:free;
1:64022904832B:64023240191B:335360B:free;
which prints
blkdiscard --offset 17408 --length 1031168 /dev/sda
blkdiscard --offset 64022904832 --length 335360 /dev/sda
Verify that this output is correct for you, add additional options if you like (verbose?), and finally
remove the echo so it will be actually executed, and you should be set.
The second command of that example actually fails because the length is too small - it may be
worth checking inside the loop, ignore regions smaller than 1MB as they're unlikely to be
successfully trimmed.
If you are using LVM instead of partitions, you can create a LV for the unoccupied space and
trim that:
lvcreate -l100%FREE -n blkdiscard SSD-VG
blkdiscard -v /dev/SSD-VG/blkdiscard
lvremove SSD-VG/blkdiscard
blkdiscard
call as LVM
frostschutz
14.9k
20
32
Thanks! I have remarked this as the answer because it's working for me, even though Michael's answer is also valid.
Also I will add what I said in the question comments: If you don't have blkdiscard available; you can grab
util-linux source from github.com/karelzak/util-linux/tree/stable/v2.23 then build blkdiscard (run
./autogen.sh ; ./configure ; make blkdiscard ) -- it built cleanly (although you may have to install
some dependencies and tweak configure options) and as long as you don't install it, it does not conflict with
existing util-linux. Jason C Oct 31 '13 at 21:34
I was observing failures with certain values passed to blkdiscard as well, but noticed the values were based on
the offset, not the length. It's possible there is an issue in blkdiscard , I am in the process of investigating. See
unix.stackexchange.com/questions/98473/ Jason C Nov 1 '13 at 22:49
I have created a script that does discard free space on all mounted file systems as well as free LVM VGs:
github.com/stefanct/discard-everything stefanct Jan 10 '14 at 15:37
hdparm --trim-sector-ranges can trim a range. The man page warns to use it, so you better
be sure you got the right range and syntax.
I think sending a trim for all data outside a partition would be dangerous, as there is some
hidden data there sometimes like bootloader code or second partition tables. You'd need to
know exaclty, which areas outside of partitions are really unused.
answered Oct 22 '13 at 19:26
Michael Suelmann
795
Perfect, thanks! I can grab the device size with hdparm , unused ranges from sfdisk , and trim accordingly. I
know what's on the drive. If there is a partioning scheme that hides data in accessible but unallocated areas, that
would break any partition editor's basic functionality. I presume (hope) that any schemes like this have long been
abandoned (I know of none off the top of my head though). All schemes I know of store the MBR in well-known
2 of 3
04/06/2016 06:08 AM
http://unix.stackexchange.com/questions/97143/ut...
unallocated space at the start of the device, and VBRs/EBRs within allocated partitions. There may be old obscure
schemes that are different. :) Jason C Oct 22 '13 at 20:18
I am having some difficulty with hdparm , I edited my question with details; wondering if you have any insight.
Jason C Oct 22 '13 at 20:35
Seems like at least some of hdparm doesn't work on /dev/mmcblk0. Sorry, I have no further idea.
Michael Suelmann Oct 22 '13 at 21:03
Ok, thanks for your help! Jason C Oct 22 '13 at 21:04
3 of 3
04/06/2016 06:08 AM