LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   FAQ: Lost filesystem after re-partitioning (https://www.linuxquestions.org/questions/linux-general-1/faq-lost-filesystem-after-re-partitioning-913410/)

pedward 11-14-2011 01:44 AM

FAQ: Lost filesystem after re-partitioning
 
I thought others may find this info useful, because it was hard won and not documented anywhere else.

I had a 250G /home partition I created with Fedora 10 and wanted to shrink this partition to set aside space for an LVM volume group. I used resize2fs to shrink the filesystem to 150G, which worked fine.

Next I went into fdisk (fedora 15) and deleted the /home partition and recreated it as 150G instead of 250G. Up to this point it is all fairly simple, non-destructive changes.

I rebooted to a /home bad magic number in superblock. The first suspicion was that the new partition didn't start at the same place, block wise, as the old partition. I suspected this because the new default units in fdisk are sectors, in fedora 10 they were cylinders.

To determine the correct starting sector I used hexdump -v /dev/sda6 to find the magic number and offset in the filesystem. The magic number for EXT2/3/4 is EF53, in hexdump this appears on a line with xxxxxx30 at the beginning.

On a normal filesystem you will see EF53 on line 00000430. The EXT filesystem keeps backup superblocks at various block offsets of the filesystem. I got a starting list of possible offests by doing mkfs.ext3 -n /dev/sda6 and noted the block offsets for the backup superblocks.

The default block size for the EXT filesystem is 4096 bytes.

Next I ran hexdump -v /dev/sda6 | grep EF53 and listed all of the offsets where the magic number appeared. I had several hundred superblocks near the beginning of the FS, likely due to the resize, then I had superblocks spread out, 179d9030, 279d9030, 379d9030. The pattern was pretty obviously repeating, so I looked back at 079d9030 and found a superblock. This pattern offset is 256MB apart, which is 65536 blocks apart. The mkfs command said 32768 would have been the first backup superblock, but that isn't true!

So, knowing that the superblocks are spaced 65536 blocks apart and the first backup superblock is at location 79d9000, I know that the partition is actually pointing *too far* into the filesystem.

If you do 1000000 - 79d9000 you get 8627000, which is 140668928 decimal. This is 34343 blocks off.

The fdisk in fedora 15 spaces logical partitions 2048 sectors apart by default. To calculate the new partition start, I did an fdisk -l to get the current start, then subtracted (140668928 / 512) = 274744 sectors from the starting offset.

fdisk would not permit me to enter the correct start sector, which was 64 sectors greater than the ending sector of the previous partition. You can get around this by going into expert mode, then choosing 'b' which moves the start of a partition to a specific sector. I entered the correct starting sector in this and rebooted. Success!

To recap:
  • Use hexdump to find the repeating interval of the EF53 magic number in the EXT filesystem.
  • Once the interval is determined, calculate the difference from zero by subtracting the lowest offset backup superblock from the interval eg: 1000000hex - 79d9000hex
  • Divide the difference in bytes by 512 to find the sector difference
  • Use fdisk -l to list the current partition starting sector and add or subtract the difference to get the correct starting value
  • Use expert mode command 'b' of fdisk to set the correct partition starting sector
  • Reboot into single user mode and run fsck on the filesystem

GlennsPref 11-14-2011 05:47 AM

Quote:

Hi, Welcome to LQ!

LQ has a fantastic search function that may save you time waiting for an answer to a popular question.

With over 4 million posts to search it's possible the answer has been given.
:)

That's really beautiful,
thank you for your efforts.

regards Glenn


All times are GMT -5. The time now is 04:37 AM.