1 (edited by vj777 2010-11-04 03:00:55)

Topic: [SOLVED] Error shrinking Ext2 partition

Hi all,

I have to report that I have been bitten by a gparted bug which looks rather like this one... http://gparted-forum.surf4.info/viewtopic.php?id=13777  As requested there, I've created this new post.

I was trying to shrink an ext2 partition to make room for a new linux install.  Having previously read about this bug, I downloaded and used the latest version as a live CD (I think it was 0.6.5-something), since later versions were recommended in that thread.

Unfortunately, at the end of shrinking the partition, gparted bombed out.  I would have liked to have saved the .htm error report but at the time, I couldn't recall how to mount a USB stick to which to save to and I had no other PC with which to access the web.  I'm going to file a feature request for either automounting USB disks or providing instructions on the desktop/save dialogue of how to mount a save disk.

The error when gparted failed was basically that the superblock did not match.  I got the same error when I tried to boot the system.  At this point it doesnt seem that gparted is much use as it just says the partition is unreadable.

Most of this thread and the solutions seem to be for NTFS partitions, which I don't have.  Soupcans solution doesn't help me as I can't boot the system to read the dmesg file (and of course it scrolls too fast to read).

I assume there must be some other way of checking the partitioning table and determining the magic numbers.

I can actually see the files on the hard disk using an Ubuntu 10.04.1 live CD, so I'm hoping I just need to fix the partition table or superblock.

I'm assuming/hoping that I just need to rewrite the partition table.  Any ideas on if this is the best course and, if so, how to calculate the new size to use? 

I don't know if this will help or not but I had backed up the partition table before using gparted.  I've listed that output at the bottom.   That seems to confirm that gparted got as far as shrinking the partition.

If I can work out the exact steps to take I'll write a mini-howto guide.  One thing that I was wondering about, even if you can get the output of dmesg, is how to convert from the dmesg block count to the sfdisk sector size.  Do you always multiply by 8 or would that change with sector/block size or filesystem type (ext2/3/4)?
(sfdisk -l says the drive is 1024 bytes per block while fdisk and parted say its 512 bytes per sector - that would give 2 sectors per block, not 8 ????  I'm confused ! )

Thanks very much,
Vic

-------------------------------------------------------
The output from sudo fdisk -l -u on /dev/sda is

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders, total 156301488 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000033f4

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1    41371647    20685823+  83  Linux
/dev/sda2       153174766   156301487     1563361    5  Extended
/dev/sda5       153174767   156301487     1563360+  83  Linux
-------------------------------------------------------

sudo parted /dev/sda unit s print  gives
Model: ATA HTS541080G9AT00 (scsi)
Disk /dev/sda: 156301488s
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start       End         Size       Type      File system     Flags
 1      1s          41371647s   41371647s  primary   ext3            boot
 2      153174766s  156301487s  3126722s   extended
 5      153174767s  156301487s  3126721s   logical   linux-swap(v1)
-------------------------------------------------------
sudo sfdisk -d /dev/sda 

# partition table of /dev/sda
unit: sectors

/dev/sda1 : start=        1, size= 41371647, Id=83, bootable
/dev/sda2 : start=153174766, size=  3126722, Id= 5
/dev/sda3 : start=        0, size=        0, Id= 0
/dev/sda4 : start=        0, size=        0, Id= 0
/dev/sda5 : start=153174767, size=  3126721, Id=83

and using -l, sfdisk gives
Disk /dev/sda: 9729 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+   2575-   2576-  20685823+  83  Linux
/dev/sda2       9534+   9729-    195-   1563361    5  Extended
/dev/sda3          0       -       0          0    0  Empty
/dev/sda4          0       -       0          0    0  Empty
/dev/sda5       9534+   9729-    195-   1563360+  83  Linux
-------------------------------------------------------

*** The following is how the disk WAS BEFORE GPARTED was run ***

sudo fdisk -l

Disk /dev/hda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1   *           1        9535    76587382+  83  Linux
/dev/hda2            9535        9730     1563361    5  Extended
/dev/hda5            9535        9730     1563360+  83  Linux


sfdisk -l
Disk /dev/hda: 9729 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/hda1   *      0+   9534-   9535-  76587382+  83  Linux
/dev/hda2       9534+   9729-    195-   1563361    5  Extended
/dev/hda3          0       -       0          0    0  Empty
/dev/hda4          0       -       0          0    0  Empty
/dev/hda5       9534+   9729-    195-   1563360+  83  Linux
-------------------------------------------------------

2

Re: [SOLVED] Error shrinking Ext2 partition

vj777 wrote:

I can actually see the files on the hard disk using an Ubuntu 10.04.1 live CD, so I'm hoping I just need to fix the partition table or superblock.

If you can read the files using the Ubuntu 10.04.1 live CD, then it would appear that the file system and the partition starting sector are correct.

Since you were shrinking the partition, my guess is that the partition table end sector, and the end according to the file system do not agree.

You might try using testdisk to fix the partition table (basically the partition ending sector).  I suggest that you only accept the solution from testdisk if it agrees with the other partitions you have documented for your partition table.  Testdisk is included on the GParted Live image.

As for the crash of GParted, it is possible that you encountered the following bug which is fixed with GParted 0.7.0:
gpartedbin crashed with signal 5 in Glib::exception_handlers_invoke()

3 (edited by vj777 2010-11-02 19:58:10)

Re: [SOLVED] Error shrinking Ext2 partition

Thanks for the really quick response. 

gedakc wrote:

As for the crash of GParted, it is possible that you encountered the following bug...

Sorry to mislead you, when I say it 'bombed out', gparted didn't actually crash - it gave up with an error message "Either the superblock or the partition table is likely to be corrupt".   I dont recall the exact text but the text I get  when I try to boot the PC is very similar...

Partition end 153174765  Superblock says 19146845

Lets' call the first *A* and the 2nd *B*.  It then goes on to say:

Checking root file system: filesystem size (according to superblock) is *A* blocks.  
The physical size of the device is *B* blocks.  
Either the superblock or the partition table is likely to be corrupt.
/dev/hda1: UNEXPECTED INCONSISTANCY: run fsck manually.

Gparted is now no longer able to do anything with hda1 (sda1 on the liveCD) - it says unable to read the contents of this filesystem.  (Incidently, I had thought the partition was ext2, but gparted is now reporting it as ext3.  I was probably just assuming ext2 as thats what I normally use.)

I hope I didn't mislead with the vagueness on the error message in my original post.

gedakc wrote:

You might try using testdisk to fix the partition table (basically the partition ending sector).  I suggest that you only accept the solution from testdisk if it agrees with the other partitions you have documented for your partition table.  Testdisk is included on the GParted Live image.

I tried running sudo testdisk.  I think that matches the partition table - its a bit hard for me to tell as it's reporting in CHS, while sfdisk and parted are in sectors and blocks.

80Gb 156301488 sectors
CHS 9729 255 63
Current partition structure
No Ext2, JFS, Reiser, cramfs or XFS marker
1 * Linux 0   0  2   2575  67  52  41371647
line repeated
2 E Extended  9534 175 32  9729 80 63  3126722
No Ext2 , JFS etc,
5 L Linux  9534  175 33  9729  80 63  3126721
line repeated

I think this matches the sfdisk sectors & the cylinders match the sfdisk -l values, so I think that's ok.  On that basis I'll try and work out how to get testdisk to rewrite the partition table tonight.

Thanks again for the help.

4

Re: [SOLVED] Error shrinking Ext2 partition

On a disk device with 512 byte sectors, the size of a cylinder is usually calculated as follows:

Cylinder size = (255 heads) x (63 sectors per track) x (512 bytes per sector) = 8,225,280 bytes

Note that as an alternative you can also use fdisk to modify the partition table since fdisk does not modify the underlying file system.  For example you could delete the sda1 partition, and then recreate it as the correct size.

5

Re: [SOLVED] Error shrinking Ext2 partition

I have the problem fixed.  It turned out not to be the partition table at all, rather it was the superblock that was corrupted.    During the shrink process I suspect that the superblock was either not written to, or written incorrectly.  I dont know if gparted writes to the superblock before or after writing the partition table - I suspect both have to agree before it will even read the contents of the filesystem.

For anyone having similar issues, here's what I did to resolve the problem.  I believe you can skip steps 1 through 3:
1) Zero'ed out the partition table with cfdisk (on the gparted cd)
2) Rewrote the partition table with sfdisk with the original cylinder values.  I couldn't get fdisk to accept sector start of less than sixty-something, which is why I used sfdisk.
3) Rebooted the live CD.

At this point gparted still refused to read the filesystem - showing the partition with an exclamation mark.  After some reading tried to check the superblock.
4) Ran e2fsck

sudo e2fsck -vnf /dev/sda

The v=verbose, f=force a check even if things look ok, n=don't make any changes to disk
This reported a bad superblock.  (Note that in retrospect I dont think using the drive 'sda' is valid - and it might always report a bad superblock, I think I should have used the partition 'sda1'). 
I wasnt sure if e2fsck was the right tool for ext3 (subsequent reading showed it will work on ext2 and 3) so I double checked...
5) I ran fsck - this seemed to just call e2fsck

sudo fsck -nv /dev/sda1

This gave the message that the superblock or the partition table are corrupt, which was the original problem.  This also noted a block where reading the inodes failed.
It seems that you have to run e2fsck (or fsck) on the partition (/dev/sda1 in this case), rather than the drive (/dev/sda).
6a) To repair the superblock, I ran sudo testdisk.  This showed the backup superblocks locations being at 32768, 98304, 163840 & several more.  According to this informative link http://www.linuxcertified.com/linux-recovery.html, the location of the backup superblocks (for ext2 anyway) depends on the filesystem blocksize.
Another way to find the location of the backup superblocks, without wading through testdisks menus, is
6b) dumpe2fs

sudo dumpe2fs /dev/sda1 | grep -i superblock

7) Verify the backup is ok

sudo e2fsck -v -n -f -b 32768 /dev/sda1

where
v=verbose,
n=don't write any changes back to disk (we're just checking at this point)
f=force (check it anyway)
b 32768 = sector of backup block to check, determined from testdisk or dumpe2fs
/dev/sda1 = partition that has the bad superblock that gparted cant read.

8) If no errors are reported you can, rerun e2fsck without the '-n' which tells it to fix the primary superblock using the info from the backup.

sudo e2fsck -v -f -b 32768 /dev/sda1

If errors were reported at step 7 you can either try the next backup superblock (eg 98304), or run it again without the '-n' then answer yes when prompted to let e2fsck try to fix the errors.   

To verify the problem is fixed, run e2fsck (or fsck) on the partition again [step 5] and see that no errors are reported.

I could then run gparted ok. big_smile

It sounds a lot worse than it is.  The steps were fairly simple - it was finding it all out that took me ages.

6

Re: [SOLVED] Error shrinking Ext2 partition

'Glad to hear that you were able to resolve the problem, and thanks for sharing your learnings.  These might help others in a similar situation.

vj777 wrote:

It seems that you have to run e2fsck (or fsck) on the partition (/dev/sda1 in this case), rather than the drive (/dev/sda).

This is correct.  If the file system is within the partition (normally the case), then run the file system check on the partition.  :-)