Resizing Partitions/Getting rid of Windows

Remember, there is always the chance you can lose data when making system changes like this. Therefore, it's advisable to ensure you have a backup of your data. I'm not responsible if you lose your data.

Up until the other day, I was dual-booting Windows and CentOS.  I had decided to drop Windows from this machine, since all I needed to do could be done within a virtual machine running Windows.  So, I'm now 50GB better for CentOS!

First of all, let me explain my partition setup prior to the resize.  I had my partitions as follows:

/dev/sda1 = 50GB = Windows
/dev/sda2 = 8GB = Swap
/dev/sda3 = 20GB = /
/dev/sda4 = 82GB = /home

I know, 8GB of swap technically isn't needed, but I have 4GB of RAM.  If I was to suspend my machine to disk, I would certainly need this space if all my memory was being used.  So I'm just playing on the safe side here.  Now, since I was deleting that 50GB Windows partition, I wanted to reallocate it to /home.  Since this meant reordering my partitions, I wanted to have something like this:

/dev/sda1 = 8GB = Swap
/dev/sda2 = 20GB = /
/dev/sda3 = 132GB = /home

This would mean creating interim partitions whilst moving data about.  To do all of this, you need to boot from a Linux Live CD or Rescue CD because you won't be able to change any of this while your system is active.  I then created the 8GB swap using fdisk utility and made the new swap file:

mkswap -L SWAP-sda1 /dev/sda1

If you're familiar with Red Hat/CentOS/Fedora, you'll notice that the /etc/fstab always uses Labels for mounting partitions.  Whilst I set a label on my swap, it won't actually be used.  I always ended up getting:

Unable to access resume device (LABEL=SWAP-sda2)

first after moving to the new swap, and after re-generating the initrd for the kernel, it then mentioned SWAP-sda1.  Easy way is to just alter /etc/fstab and change the swap line to actually reference the partition, so /dev/sda1 instead of the label.  Then generate the initrd after this and you'll not see the error message again.  For the rest of the partitions, I used labels to mount, as it's only swap that was having the problem.

Now that the swap was done, I had space to create another partition, 20GB to allocate as /.  So using fdisk again, I created /dev/sda2 as 20GB.  Then I created the filesystem:

mke2fs -j /dev/sda2

Then, I did the following to migrate the data from /dev/sda3 to /dev/sda2.

mkdir /mnt/oldroot
mkdir /mnt/newroot
mount /dev/sda3 /mnt/oldroot
mount /dev/sda2 /mnt/newroot
cp -dpRx /mnt/oldroot /mnt/newroot/

verify that the file-structure looks the same between /mnt/oldroot and /mnt/newroot.  If all is OK, you will successfully be able to boot your system.  But first, since we need to fix the labels, I do this next:

e2label /dev/sda2 /
e2label /dev/sda3 oldroot

that will mean that when /etc/fstab tries to mount the label for /, it will now mount /dev/sda2 instead of /dev/sda3.  Verify this after your system has booted successfully.  Now, you can reboot using the LiveCD or Rescue CD again, so that we can delete /dev/sda3 and then start to move /home about.  Luckily, I was only using 15GB of my partition, which meant that the remaining 40GB or so that would now become /dev/sda3 meant I didn't have to copy data off of my machine to another before moving partitions around again.

So, the process, is similar again.  I created /dev/sda3 using all the space between /dev/sda2 and /dev/sda4.  Then:

mke2fs -j /dev/sda3

and I'm now copying data again, so:

mkdir /mnt/oldhome
mkdir /mnt/newhome
mount /dev/sda4 /mnt/oldhome
mount /dev/sda3 /mnt/newhome
cp -dpRx /mnt/oldhome /mnt/newhome/

again, verify that all data was copied successfully to the new partition.  Then, edit the labels again:

e2label /dev/sda3 /home
e2label /dev/sda4 oldhome

I then booted my system again, to make sure that /dev/sda3 was mounted to /home, and that /dev/sda4 was not mounted.  Then, I rebooted one more final time to delete /dev/sda4 and resize /dev/sda3 to have /home set to use 132GB.  First, I check /dev/sda3 for errors, and then remove the journal so that I can resize it (you cannot resize with journaling enabled).

fsck -n /dev/sda3
tune2fs -O ^has_journal /dev/sda3
e2fsck /dev/sda3

once that has all happened, I then go into fdisk and delete /dev/sda4.  I then delete /dev/sda3, but I recreate it again using the same Start block - as this is important.  If you change the start block, you will lose all your data.  The end block was then basically the last available one on the disk, meaning I could use my full disk when I issue a resize for the /home filesystem.

resize2fs /dev/sda3
fsck -n /dev/sda3
tune2fs -j /dev/sda3
e2fsck /dev/sda3

I can then mount /dev/sda3 and check to see if it has 132GB.  It did, so all was OK.  Now I restarted my system normally, and all was accessible without problems, and I had all my disks how I wanted them.  All my disk space available for Linux :-).