5

I need to identify the last sector used by an ext4 filesystem so that I can move it to another device.

The filesystem has been shrunk (with resize2fs) and is smaller than the partition that contains it, so I am not asking how to find the last sector in the partition.

I have done tune2fs -l and identified that

Block count:              48934
First block:              0
Block size:               4096

From that I would postulate that the filesystem uses 48934 * 4096 / 512 = 391472 sectors and that I can move that many sectors with dd starting at the first sector of the partition (as reported by gdisk).

I am uncertain whether that block count includes any ext4 overhead or if there is additional size that needs to be considered. I read this question which implies there is additional space to be considered.

1 Answer 1

4

You are right. There shouldn't be any problem.

To avoid some calculations you could use the bs option and use the partition name of the device rather than starting at an offset.

dd count=48934 bs=4096 if=/dev/sdxN  of=... 

To be 100% sure about the size you could test it before. "Simulate" a smaller partition:

umount /dev/XYZ
losetup --offset N-BYTES --sizelimit $(( 48934 * 4096 )) /dev/loop1 /dev/XYZ

mount or fsck of /dev/loop1 should tell you if you made it too small. resize2fs would tell if the partition is still too large but there is no dry-run. You could also play around with fsadm -v --dry-run check/resize ... which I have never used yet. If paranoid you should use losetup --read-only. Don't forget losetup --detach when done.

5
  • That's great, Yes I actually used dd if=/dev/sdd2 of=/dev/sdc5 bs=512 count=395568 (It's LUKS and I also copied the LUKS header). So far, so good.
    – starfry
    Oct 5, 2016 at 9:28
  • I've updated the "testing before" part. Should be also useful somehow in LUKS case.
    – rudimeier
    Oct 5, 2016 at 9:39
  • @starfry Don't use dd. Do the loopback mount and use cat, it's the easiest way to ensure you're copying the right thing, and it's faster. Oct 7, 2016 at 6:42
  • @Gilles, why would cat be faster than dd with a lo-device?
    – Tom Hale
    Nov 7, 2016 at 9:46
  • 1
    @TomHale I don't know. It may not always be faster, but I did find that it was faster in a benchmark, even when varying the dd block size, and more so with dd's default tiny block size. Nov 7, 2016 at 11:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .