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.

starfry
  • 7,442

1 Answers1

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.

rudimeier
  • 10,315