Friday, October 2, 2009

Resizing a partition in linux with parted

Resizing a partition in linux with parted

To resize an ext3 partition comes handy LVM, but i will explain how i did it without. I did this in a Fedora 6, booting with a Fedora 7 or Knoppix disk and entering into rescue mode, sorry but i can not take any resposibility if something goes wrong while following this procedures>

After getting the prompt in Fedora 7 or any other distro in rescue mode, unmount all the partitions of the filesystem you want to resize, in my case it is the disk /dev/sda, but it could be /dev/hda

Fedora 7 rescue disk has parted installed and i get , so we are going to use it.
#umount /dev/{sda1,sda2,sda3}
Check the Filesystem to resize, i am going to shrink sda2 first
#e2fsck -f /dev/sda2
Resizing the partition:
#resize2fs -p /dev/sda2 2600M
The size parameter may be suffixed by one of the following the units designators: 's', 'K', 'M', or 'G', for 512 byte sectors, kilobytes, megabytes, or gigabytes, respectively. The -p argument is to display a nice status bar.
The message on screen is:
Resizing the filesystem on /dev/sda2 to 665600 (4k) blocks
So i do the calculation of how many bytes this is to use it later in fdisk:
665600 blocks * 4k = 2662400 Bytes.
My partition had 4 G before, now, after resizing, the partition will still have 4G, but "resize2fs" has prepared for a technical surgical procedure ;-) we are going to delete the partition and recreate it

I entered fdisk and deleted the /dev/sda2 partition, then i recreated it with the same starting cylinder and +2662400K as size (this is imperative and may loose all your information if you do not do this right, again, i take no responsability, the results may depend on filesystems). Then i wrote the partition with "w" and went back to the shell.

Now i execute again:
#resize2fs -p /dev/sda2 2600M
And this command to recreate some information of the partition table.
#e2fsck -f /dev/sda2
Time to reboot and see if all is ok, yes it is all fine and the partition has now 2662400 K.

Now it is time to resize the next partition, /dev/sda3, to take the space left by /dev/sda2, another surgical procedure is required.

Enter the rescue mode again, umount the partitions that are going to be resized and lets delete /dev/sda4 to begin at the end of /dev/sda2, this is done with "fdisk".
From the resize2fs man page:
If you wish to shrink an ext2 partition, first use resize2fs to shrink the size of filesystem. Then you may use fdisk(8) to shrink the size of the partition. When shrinking the size of the partition, make sure you do not make it smaller than the new size of the ext2 filesystem!

My partition distribution looks like this now:
start end
/dev/sda1 1 13
/dev/sda2 14 345
/dev/sda3 1012 1043
/dev/sda4 744 1011

Start - End
1 - 32,3 kb - 107 MB
2 - 107 MB - 2838 MB
4 - 6111 MB - 8316 MB
3 - 8316 MB - 8579 MB

The beginning of /dev/sda4 should be 346, so i used parted to move the partition. Be careful, it is not possible to delete the partition and recreate it with fdisk (well, it is possible but it would corrupt the partition because the partition table would be in the middle of the disk).

#parted
(parted) move
Start? 3000MB
End? 5204MB
Something curious happened here, i had to specify "MB" at the end of the numbers, else it gave me an error about "Can't have overlapping partitions"

1 comment:

  1. Please mention that this article comes from serverlinux.blogspot.com with a link back almost, because i am the original author.

    ReplyDelete