[CentOS] Turn an ext2 filesystem into a component of a mirrored RAID?

Mon Nov 27 22:07:00 UTC 2006
Aleksandar Milivojevic <alex at milivojevic.org>

Quoting Bart Schaefer <barton.schaefer at gmail.com>:

> We have a CentOS 3 server with about 300GB of data on an ext2
> filesystem that we need to mirror onto a new drive, which we're then
> going to pull out and put into a second server.  A straight
> disk-to-disk copy (with rsync, tar, or "cp -a" doesn't much matter)
> manages about 75MB per minute, which would take almost three days, and
> the system gets very sluggish while such a copy is going on, so we
> can't afford to just let it run.
>
> Is it possible, without loss of data, to convert the existing ext2
> filesystem into a mirrored software RAID, then add the new drive as a
> second device and let rebuilding the RAID take care of making the
> copy?  Even if this took more time, we've had good overall system
> performance with software RAIDs rebuilding in the background before,
> so it could run as long as necessary.  We'd then need to be able to
> remove the second device from the RAID and either convert it back into
> a plain ext2 or put it into a similar software RAID in the destination
> machine.
>
> Is this possible?  Is there another plan that would make more sense?

Yes, it is possible.  It's not too complicated, downtime should be  
minimal, but if you do any calculation mistakes, you loose your data.

I can't find an document that I used to have describing the process in  
more detail.  So, just some hints:

MD keeps metadata information at the end of partition.  Hence, MD  
device will be a bit smaller than the partition (for the size of  
metadata chunk).  Google around or check kernel docs to find out how  
much space it takes.  If you can't find it, shrik the file system 1  
GB, than after you are done with creating mirrors you can expand it  
back to use all available space (or simply leave at whatever size it  
was).

Your filesystem currently uses the entire partition.  You need to  
resize it (shrink) so that there's enough space for MD metadata at the  
end of partition.  The file system size must be multiple of file  
system block size.  Use dumpe2fs to find out block size (and current  
file system size).  Use resize2fs to shrink file system (must be done  
offline).  Check man page for resize2fs for details.

If you are using LVM, you'll also need to shrink your logical and  
physical volumes.

Once the space at the end of partition is not used for anything, you  
can use mdadm to create RAID-1 MD device.  Create it with only one  
disk (leave the second disk missing).  After that, simply attach  
second drive (again using mdadm) and let it resync.  You'll probably  
want to change partition tag to linux reaid autodetect.

Try this on some spare testing box until you get it right.  If you  
make any errors, you can kiss your data goodbye.

BTW, some popular HOWTO's on the web suggest resizing file system  
*after* mirror is created.  This is dangerous.  If there is any  
allocated blocks in the part of file system at the far end of  
partition (the space that will be used for MD metadata), you will  
loose some data or in worst (but not likely) scenario you can end up  
loosing entire file system.  Don't do that.  Do it the safe way,  
shrink file system first, than create mirror.