Resizing ext3 or ext4 partition for virtual image

        

Note: please backup and make sure that you have a copy of your data and virtual images, this can go wrong and I won’t be held responsible. This worked for me, and might not work for you. For ext4 please use the latest GParted.

As I migrated from raw or qcow images, some of the partition sizes that I have to work with are not as big as they could be. So I need to resize.

I generally give some of my hosts a 24Gb LVM virtual group each. But the image files can be as small as 2Gb – A lot of wasted space which the filesystem on the image might as well have. These instructions assume that there are only two partitions, a main partition, and a second linux swap partition (either on an extended partition or a primary).

First taken the image offline, then get the name of image that you wish to resize, then the disk storage items:

[cc lang=“bash”]

ls /etc/vm/

lvdisplay

[/cc]

In my case it was “/dev/vdisk05/vm-102-disk-1″. Then you can check to see how large the volume group is set to (vgdisplay) resize:

[cc lang=“bash”]

lvresize -L 23G /dev/vdisk05/vm-102-disk-1

[/cc]

Next I mount a SystemRescueCD on the CDRom, and make sure its the first item in the boot sequence. Then boot into the system, go for the first choice in SystemRescueCD, and it will give you a rootprompt (I am VNC’ing into the system so that I can access the console as if using a physical machine):

[cc lang=“bash”]

#Change the passwd so that you can ssh in if you need to.

passwd

#Start the GUI so you can run Gparted (First choice again).

wizard

[/cc]

From a command prompt do (check file system, repair, remove journal):

  1. fsck -n /dev/sda1
  2. e2fsck -f /dev/sda1
  3. tune2fs -O ^has_journal /dev/sda1

Then do the following in the GUI:

  1. Choose CD > System > GParted
  2. Delete the swap partition as it will be the last
  3. Resize partition, leaving some space for a new swap partition
  4. Create a swap (type linux-swap) partition twice the size of the amount of RAM you allocate to the image
  5. Click Edit > Apply all operations
  6. Close GParted

Back on the command prompt do (repair, check file system, journal, shutdown):

  1. e2fsck -f /dev/sda1
  2. fsck -n /dev/sda1
  3. tune2fs -j /dev/sda1
  4. shutdown -h now

Remove the CDRom device, and change the boot order back then start the Virtual machine. Issue a df -h to see if the partition has changed. It should have done!

comments powered by Disqus