Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use dd command to clone hard drive

Status
Not open for further replies.

alan12

Programmer
Oct 8, 2002
3
US
I use dd command to copy all system from hda1 to hdb1:
dd if=/dev/hda1 of=/dev/hdb1

it works fine when I remove hda1 and reboot hdb1, the only question is: hda1 has only 2GB and hdb1 has 30GB, but after I use dd command, I found hdb1 also has only 2GB recogonized by linux system, how can I have all the rest of space when I use dd to clone system from small HD to large HD? Thanks.
 
Although you can use dd to copy from disk to disk or even from disk to partition, I would recommend against it when the drive sizes are different.

Normally the best option is either dump/restore or tar. It takes a bit more work but is well worth it.

Below is an outline of the steps needed to do what you ask as well as an optional step #4 to use dump and restore in place of dd. I have not tested the code below as written. I believe it will do substantially what you are asking but THERE ARE NO WARRENTIES OF ANY KIND. I would recommend that you test and or double check each of the steps before comming yourself.

1.
Code:
dd if=/dev/zero of=/dev/hdb count=2 bs=1024
# Clear the boot record of the new drive
2.
Code:
fdisk -l /dev/hda
# Gets a list of partitions to create
3.
Code:
fdisk /dev/hdb
# Create partitions on the new drive.
4. The dd example is first
Code:
for each in `fdisk -l | grep Linux | cut -c9-10|    awk '{printf($1" ")}'
    do 
    dd if=/dev/hda${each} of=/dev/hdb${each}
    done

OR

Code:
for each in `fdisk -l | grep Linux | cut -c9-10|                                  awk '{printf($1" ")}'
    do 
    mkdir /mnt/${each}
    mke2fs /dev/hdb${each}
    mount /dev/hdb${each} /mnt/${each}
    dump -0f - /dev/hda${each} | (cd /mnt/${each};                                 restore -rf -)
    umount /mnt/${each} ; rmdir  /mnt/${each} 
    done

5. Mount the partition with the boot kernel.

6. Copy /etc/lilo.conf to /tmp/new_lilo.conf file on the
first drive and change the boot line from hda to hdb
and the location of the image file (and initrd) to
its location under the mountpoint from #5.
7. Run lilo -C /tmp/new_lilo.conf
Cheers

man(1) is your friend
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top