[Novalug] cloning a partition

Peter Larsen peter@peterlarsen.org
Fri Oct 17 02:07:37 EDT 2014


On 10/16/2014 10:10 PM, Bonnie Dalzell via Novalug wrote:
> got a new drive since one of my old drives is getting a little noisy.
> (it is an old PATA drive, new drive is a SATA.  System has some other
> drives in it already.
>
> if you want to exactly copy from a partition on an existing drive to a
> partition on a new - not yet partitioned drive using dd, do you need
> to create the partitions on the new drive before using dd? Can the
> partitions be larger than the ones on the old drive so that you clone
> from a smaller partition into a larger into a larger partition?
>

If your old drive is a PATA I'm guessing it's much smaller than your new
SATA drive? You have to ask yourself if replicating the partition table
as it looks on the old drive makes sense on the new drive, but you can
easily copy a small drive 100% as it looks to a new drive as long as the
new drive is the same size or larger. But the usable space on the new
drive will be limited to that of the old. If that's what you want, dd is
all you need:

old=/dev/sda
new=/dev/sdb
dd if=$old of=$new bs=1024

Of course you should set your old and new to what makes sense for you.
The best result will happen if the old drive is not in use while you
copy - but any semi modern file system like ext3 and above will have
journalling and as long as you're not doing something like a system
upgrade while you copy, you should be fine if the drive is in use. But
better safe than sorry.

Getting a new drive is usually the best excuse to make things right. Ie.
you have more space, so maybe you should give more space to a space that
could use it. Also, maybe your partition table is full, or you should
use gpt instead of dos partition tables. Or maybe you should go all out
and use LVM. Same criteria as before - as long as the destination is of
the same size or larger than the original you can do a raw copy with dd
from the old partition/volume to the new. Ie. you would create the new
layout on the new drive, and then "dd" the first partition, then the
next etc.  As before, doing this while you're NOT using the original
disk is best but it will usually work. If you created the new volumes
larger than the old, use "resize2fs" to increase the file system to fit
the new volume after the dd has finished executing.

If you are REALLY out to improve, maybe using a new file system like
brtfs or xfs would be helpful. In that case, you need to create the file
system on the new volumes first, and then use a logical copy. "cp" can
do it, as long as you pass a set of parameters:

$ cp -vax /old/mnt /new/mnt

As you can see, you'll have to mount the new empty volume before you can
copy it logically. Be aware, if you make ANY changes, you may have to
alter your /etc/fstab before booting on the new drive. You also may need
to resetup grub. Using the initial dd would avoid that IF your new drive
gets the same bios ID and you refer to your devices using /dev/<name> in
/etc/fstab. If you're using uuid or disk labels you're set after doing
the dd and unplugging the old drive.

Finally, if your old drive is LVM already, all you need is "pvmove" for
the LVM section. The rest follow the stuff above.

-- 
Regards
  Peter Larsen






More information about the Novalug mailing list