Centos – How to resize / shifting partitions

centosmdadmraidsoftware-raid

I am running out of space on my root partition and would like to steal some from a different partition. The drives are 2x120GB with mdadm software RAID.
I am using CentOS 6.5 64-bit. I used the centos installer guided RAID setup.
It seems like most of the HowTos are gearing me towards not having the underlying partitions. So others would have just /dev/md0. They would perform a resize2fs /dev/md0 25G (reducing from 50G for example) and then use mdadm to resize it, etc.

The layout is:

Filesystem            Size  Used Avail Use% Mounted on
/dev/md0p5            9.7G  7.1G  2.1G  78% /
tmpfs                  16G     0   16G   0% /dev/shm
/dev/md0p1            194M   99M   86M  54% /boot
/dev/md0p2             68G  7.2G   57G  12% /var/www

cat /proc/mdstat:

Personalities : [raid1] 
md0 : active raid1 sdb[1] sda[0]
      117220736 blocks [2/2] [UU]

unused devices: 

Here is the fdisk:

fdisk -l

Disk /dev/sda: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00035afc

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        8950    71680000   83  Linux
/dev/sda3            8950       11039    16777216   82  Linux swap / Solaris
/dev/sda4           11039       14594    28557312    5  Extended
/dev/sda5           11039       12314    10240000   83  Linux

Disk /dev/sdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00035afc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sdb2              26        8950    71680000   83  Linux
/dev/sdb3            8950       11039    16777216   82  Linux swap / Solaris
/dev/sdb4           11039       14594    28557312    5  Extended
/dev/sdb5           11039       12314    10240000   83  Linux

Disk /dev/md0: 120.0 GB, 120034033664 bytes
2 heads, 4 sectors/track, 29305184 cylinders
Units = cylinders of 8 * 512 = 4096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00035afc

    Device Boot      Start         End      Blocks   Id  System
/dev/md0p1   *         257       51456      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/md0p2           51457    17971456    71680000   83  Linux
Partition 2 does not end on cylinder boundary.
/dev/md0p3        17971457    22165760    16777216   82  Linux swap / Solaris
Partition 3 does not end on cylinder boundary.
/dev/md0p4        22165761    29305088    28557312    5  Extended
Partition 4 does not end on cylinder boundary.
/dev/md0p5        22166273    24726272    10240000   83  Linux

So what can I do to grow the root partition and shrink the /var/www partition?

Best Answer

Since you've partitioned your RAID as if it was a single disk, you can ignore the RAID altogether in this case. So it's merely a problem of resizing / shifting partitions.

So for example, you could shrink the www partition, delete the swap and then shift the root partition to the left in order to grow it.

Or, if that seems to complicated and you don't strictly need separate partitions, you could merge the root partition into your www partition since that's already large enough to hold both root and www. That's kind of what I would do.

# mount stuff
mkdir /mnt/root /mnt/www
mount /dev/md0p5 /mnt/root
mount /dev/md0p2 /mnt/www

# since /mnt/www will be the new root, move www files to /var/www
mkdir -p /mnt/www/var/www
mv /mnt/www/* /mnt/var/www/

# copy the root files
rsync -avAHSX /mnt/root/. /mnt/www/.

# comment out old root partition in fstab
# change /var/www to / in fstab

# update bootloader and reboot

This approach also has the advantage that if anything goes wrong, the original root partition is still intact, so you can revert the operation.

Once everything is working fine with the merged root+www partition, you can delete the old root partition and grow it to the full disk size.

Or you could decide that you want to stick with separate partitions after all and move the www files to the old root partition, if you think that's going to be large enough for your www in the foreseeable future.

Or you could shrink the www partition to make room for a new one.

Endless possibilities...

Related Question