You can find plenty of info online about using dd
to clone OS drives.
Don't listen! It's MUCH faster to use dump
and restore
because you only copy the data, rather than copying all the blocks (empty or not).
Part 1: Know where your source and destination drives are in the device list
lsscsi | grep sd*
will show you a list of scsi devices and their associated letters. If you are lucky enough to be working with a hot-swapable box, then you can simply run that command before and then after you insert the drive--the newest device to show up is, of course, the drive you just inserted.
Part 2: Prepare to dump
- Safety tip: assign variables for the destination and source devices. (Also, if you are doing this more than once, variablization allows you to reuse the commands.)
SOURCE=/dev/sdx
DEST=/dev/sdy
- A note on the source drive (the one you are copying from). If you are copying the current OS drive, it will (duh) already be mounted. If you are copying a different drive, it actually doesn't need to be mounted.
Copy the partition table of your source drive out to a file:
sfdisk -d $SOURCE > part_table
Copy the part_table already stored in a file
sfdisk --force $DEST < part_table
Zero out the boot sector:
dd if=/dev/zero of=${DEST}1 bs=512 count=1
Make your filesystem (one partition at a time):
mkfs -t ext4 ${DEST}1
mkswap ${DEST}2
Take a look:
parted $DEST --script print
Copy the label of all non-swap partitions. Example:
tune2fs -L "/" /${DEST}1
Part 3: The dump | restore moment
- Make a directory to mount the destination device of the dump | restore. (As mentioned above, source device need not be mounted.)
mkdir -p /mnt/${DEST}1
Mount the destination device:
mount -t ext4 ${DEST}1 /mnt/${DEST}1
cd
into the mount point:cd /mnt/${DEST}1
Dump and restore:
dump -a0f - /dev/${SOURCE}1 | restore -rf -
(dump flags: a=autosize; 0 (zero)=start at block 0; f = file, - = stdout; restore flags: r=rebuild; f=file; - = stdout)The
dump | restore
should take just a few minutes.
Part 4: use grub to install a boot loader onto the cloned drive
Assuming you are copying an OS drive (i.e., a drive off which you boot a box), you need to install a boot loader.
Grub identifies disks as hd#, starting from 0 (NOT 1). The correspondence is easy: /dev/sda = hd0, /dev/sdb = hd1, etc.
[root@drive-toaster /]# grub grub> root (hd1,0) # use correct number for your disk! root (hd1,0) Filesystem type is ext2fs, partition type 0x83 grub> setup (hd1) # use correct number for your disk! setup (hd1) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd1)"... 27 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd1) (hd1)1+27 p (hd1,0)/boot/grub/stage2 /boot/grub/grub.conf"... succeeded Done. grub> quit