Shrinking ext4 partition: fdisk won't allow to recreate the partition at the same start block

Solution 1:

Why this behaviour from fdisk ?

Performance on some hard drive models

Would using 2048 cause data-loss ?

If it's the root partition it shouldn't be able to boot anyway.

Any work around or solution to shrink the partition ?

Use parted instead of fdisk. Be careful, unlike fdisk, parted write changes on the fly !

(parted) p                                                                
Model: ATA Hitachi HTS72501 (scsi)
Disk /dev/sda: 160041885696B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start         End            Size           Type     File system     Flags
 1      32256B        53694627839B   53694595584B   primary  ext4            boot
 2      53694627840B  57996449279B   4301821440B    primary  linux-swap(v1)
 3      57996449280B  160039272959B  102042823680B  primary  xfs

(parted) rm 1
(parted) mkpart primary 32256B 28998224639B                               
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) p                                                                
Model: ATA Hitachi HTS72501 (scsi)
Disk /dev/sda: 160041885696B
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start         End            Size           Type     File system     Flags
 1      32256B        28998224895B   28998192640B   primary  ext4
 2      53694627840B  57996449279B   4301821440B    primary  linux-swap(v1)
 3      57996449280B  160039272959B  102042823680B  primary  xfs

Warning !!

I thought that ext4 would not fragment so much (indeed it had only 0.1% of non-contiguous blocks) so I did not run resize2fs.

At first I did that and couldn't mount /dev/sda1 again. (After a good laugh) I recreated the partition at its initial size, ran e2fsck and decided to run resize2fs before trying parted again and it worked.

Remember, we're just editing the partition table with parted/fdisk, not altering any physical data.

This is a headless server, after reboot it would not ping. I decided not to reboot into rescue mode again, as this would potentially interrupt boot-time fsck, so I went to sleep. The next morning I found the server back up.

Solution 2:

I also had the problem that I couldn't keep the original start block. I used cfdisk. cfdisk could do it right and much easier then fdisk! Just select 'resize' and enter the amount of diskspace. And 'write' when you are satisfied with your configuration. That's it! And as you can see, it doesn't have the disadvantage of writing on the fly.

Solution 3:

Any […] solution to shrink the partition?

Recreate the partition with the default start sector (2048) and proper end sector. Remember that this change is pending until you write the partition table, so it doesn't matter it's not 63 yet. Next give the command x which is

x   extra functionality (experts only)

Now m prints (among other things):

b   move beginning of data in a partition

Use b, you will be allowed to input 63. Return to main menu with r. At this moment p should show you 63 as the start sector. If everything looks fine, write changes to disk with w.