LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 05-19-2007, 06:00 PM   #1
Tommo
Member
 
Registered: May 2006
Posts: 80

Rep: Reputation: 15
Using dd to clone my hard drive


Hello there.

Here is the scenario: I have a SATA HD (80G /dev/sda) and an IDE HD (120G /dev/hdb). I have Debian Etch installed on the latter. I wish to clone the IDE HD so that the SATA HD contains my Linux volume. I have used approx 25G of the 120G drive at present. Here is the output of fdisk:

Code:
root@2[knoppix]# fdisk -l

Disk /dev/hdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1   *           1       14219   114214086   83  Linux
/dev/hdb2           14220       14593     3004155    f  W95 Ext'd (LBA)
/dev/hdb5           14220       14593     3004123+  82  Linux swap / Solaris

Disk /dev/sda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Disk /dev/sda doesn't contain a valid partition table
I am currently doing a..
Code:
dd if=/dev/zero of=/dev/sda conv=notrunc
to erase /dev/sda at the moment.

So once this is finished how would I go about doing this using dd? Because the 'if' (input file) is bigger than the output file, I cannot simply do..
Code:
dd if=/dev/hdb of=/dev/sda conv=notrunc,noerror
Can I? Will I have to resize the main Linux partition and create one the exact same size as it on the SATA hd?

Also I noticed I cannot seem to resize the linux partition from my knoppix cd, as it says it is 'active'. But I have unmounted it so I cannot see how it is active.

Thanks for taking the time to read this, any help will be greatly appreciated.

Last edited by Tommo; 05-19-2007 at 06:08 PM.
 
Old 05-19-2007, 06:39 PM   #2
lambchops468
Member
 
Registered: Mar 2007
Location: New Jersey, USA
Distribution: Archlinux
Posts: 165

Rep: Reputation: 30
Two things:

1.
Quote:
So once this is finished how would I go about doing this using dd? Because the 'if' (input file) is bigger than the output file, I cannot simply do..
Yes, you're right about that. What I'd do is format the 120G drive to have all the partitions at the beginning of the drive and have them go up to the 80G boundary (its probably less because OSes see 1 K as 1024 and hard drive amounts are 1K=1000)

2. in order to reformat your drive, I would first get something other than knoppix because it doesn't support the resizing of some linux partitions. Go find a live Cd with gparted (like sysrescuecd, www.sysresccd.org), and resize from there. You can find where you should use the 120G up (the boundary) and stop by looking at the 80 G from gparted.(I would back off at least .5 G from that amount to avoid errors or whatever)

Last edited by lambchops468; 05-19-2007 at 07:28 PM.
 
Old 05-19-2007, 09:49 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,207

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
dd is a bad option IMHO - setup the partitions on the new disk, and simply "cp -a ..." (from a liveCD). Will handle links, everything.
rsynch is another popular option.
 
Old 05-20-2007, 10:21 AM   #4
Tommo
Member
 
Registered: May 2006
Posts: 80

Original Poster
Rep: Reputation: 15
OK, thanks for the replies guys.

Will `cp -a ..` copy over filesystem information though?

I will give what lambchops suggested a go, and let you know how it goes. Thanks again
 
Old 05-20-2007, 10:58 AM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Quote:
Originally Posted by Tommo
OK, thanks for the replies guys.

Will `cp -a ..` copy over filesystem information though?
You have to set up a filesystem (mkfs) after formatting sda. When you copy over using cp, you also will have to reinstall grub. You should skip /dev, /tmp, and /proc when you copy things over.

Quote:
I will give what lambchops suggested a go, and let you know how it goes. Thanks again
I wouldn't do this if I had more than one partition on the source drive. gparted won't move partitions backward on a disk, only forward (unless a recent version has remove that limitation).
 
Old 05-20-2007, 12:10 PM   #6
Tommo
Member
 
Registered: May 2006
Posts: 80

Original Poster
Rep: Reputation: 15
OK Thanks. What I have done is used Gparted to shrink the main linux partition in the 120g hd to around 70g. I then moved the extended partition infront of the subsequent unallocated space.

So it is something like:

/dev/hdb1 - ext3 (70g)
/dev/hdb2 - extd (2g)
/dev/hdb5 - linux-swap (2g)
UNALLOCATED SPACE (remainder)

I then used gparted to create a partition at the start of sda to the exact size of the main linux one in hdb (hdb1).

Code:
Disk /dev/hdb: 120.0 GB, 120034123776 bytes
255 heads, 63 sectors/track, 14593 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/hdb1   *           1        9115    73216206   83  Linux
/dev/hdb2            9116        9489     3004155    f  W95 Ext'd (LBA)
/dev/hdb5            9116        9489     3004123+  82  Linux swap / Solaris

Disk /dev/sda: 80.0 GB, 80000000000 bytes
255 heads, 63 sectors/track, 9726 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        9115    73216206   83  Linux
/dev/sda2            9353        9726     3004155    5  Extended
/dev/sda5            9353        9726     3004123+  82  Linux swap / Solaris
I am now in the process of doing:
Code:
dd if=/dev/hdb1 of=/dev/sda1 bs=4096 conv=notrunc,noerror
But one thing I havn't taken into account is things in my system which are configured for hdb such as grub and fstab.

Quote:
When you copy over using cp, you also will have to reinstall grub. You should skip /dev, /tmp, and /proc when you copy things over.
For grub, could I not just edit grub.conf (menu.lst) ? What other things should I take into consideration?

Thanks for your help.

Edit: I tried booting up in the SATA HD and nothing, no grub, no error message. Is anything wrong with the MBR or do I just need to (re)install grub?

Last edited by Tommo; 05-20-2007 at 12:37 PM.
 
Old 05-20-2007, 12:44 PM   #7
lambchops468
Member
 
Registered: Mar 2007
Location: New Jersey, USA
Distribution: Archlinux
Posts: 165

Rep: Reputation: 30
Quote:
Edit: I tried booting up in the SATA HD and nothing, no grub, no error message. Is anything wrong with the MBR or do I just need to (re)install grub?
Considering the fact that when you did

Quote:
dd if=/dev/hdb1 of=/dev/sda1 bs=4096 conv=notrunc,noerror
dd copied the File System (and whatever inside it), not the MBR, you can't expect to boot from it.
 
Old 05-20-2007, 01:09 PM   #8
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
The command of
Code:
dd if=/dev/hdb1 of=/dev/sda1 bs=4096
in post #6 will produce a mirror image of sda1 into sdb2 because the number sectors matches each other. The cloning of the swap is unnecessary because it does not contain usable information. The Linux will not boot because of the following

(1) As far as Grub is concerned The Linux was installed in the 1st disk (hd0) so its root still points to (hd0,0) and the kernel statement will have a parameter indicating "root=/dev/hdb1"

(2) The Boot loader Grub is residing in the MBR of hdb and there is no entry to boot the newly located system in sda. To replicate the boot loader in hdb into sda the following commands are needed in Debian terminal
Code:
sudo grub
root (hd1,0)
setup (hd1)
quir
(3) The fstab has entries for the root being /dev/hdb1 and the swap being /dev/hdb5. If the Debian in sda1 is booted the kernel will go back to hdb1 to load the file and continue to use hdb5 as swap.

Thus what follows depends entirely on what the OP wants. Does he want to boot two systems side by side? In such a case he will have to correct the above 3 items.

Or does he want Debian to run on the sdb only? In such a case all he needs is to pull the hdb temporarily out and let sda be the only disk to become (hd0) to run Debian as a clone, by editing the hdbx refernces to sdax. After the boot loader has been replicated in Item (2) the cloned Debian will boot normally as the original.

Last edited by saikee; 05-20-2007 at 01:26 PM.
 
Old 05-20-2007, 01:18 PM   #9
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
Quote:
dd if=/dev/hdb1 of=/dev/sda1 bs=4096 conv=notrunc,noerror
In the past I have never been able to make this work as expected. Apparently something (not sure what) isn't picked up when I do it this way. Instead, to copy a partition, I use the output of sfdisk -l -sf to get the start block and the size of the partition, then I copy like this:

dd if=/dev/hdb of=/dev/sda bs=512 skip=startblocknumber seek=startblocknumber count=partitionsize

That'll accurately clone the partition.

However, this isn't what you want to do. You want to clone the entire HD including the master boot record. So for that you'd do this:

dd if=/dev/hdb of=/dev/sda bs=whatever

This will clone every byte of the source to the destination and will give an error when it tries to run off the end of the destination drive - which will be fine since the copy will have already completed.

I take it that the most recent versions of gparted DO permit a partition to be moved forward on a drive?
 
Old 05-20-2007, 01:31 PM   #10
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
jiml8,

I think the OP now wants to clone only the partition from hdb1 to sda1. Since both partitions have been arranged to have identically 9115 sectors the cloning by dd will work.

The latest Gparted can move a partition forward and backward as well as dragging the partition boundary to resize it.
 
Old 05-20-2007, 01:41 PM   #11
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 115Reputation: 115
I have in the past cloned partitions by partition name in dev and had it fail. I can't tell you why because it does seem like the entire partition should be picked up.

You can test it by cloning the partition to a file, mounting the file, and trying to read the contents. I haven't successfully done it, though it has been several years since I tried. I have never failed when specifying absolute addresses on the HD.
 
Old 05-20-2007, 01:48 PM   #12
Tommo
Member
 
Registered: May 2006
Posts: 80

Original Poster
Rep: Reputation: 15
Thank you all very much for your help, it has worked out nicely

So what I did was (from my linux volume in hdb1):
Code:
# mkdir /mnt/sda1
# mount /dev/sda1 /mnt/sda1
# chroot /mnt/sda1
# grub

grub > root (hd1,0)
grub > setup (hd1)
grub > quit
I then rebooted my system, changed the boot order in my BIOS to boot the SATA HD before the IDE HD, and viola! It has worked out quite nicely. I am glad I have had not problems with the dd command, it has all seemed to work fine.

Note: For anyone reading this in the future with similar problems, although I used root (hd1,0) for the above, I used (hd0,0) for the /boot/grub/menu.lst file situated in sda1

I have changed fstab corresponding to my new partition, will I have to do anything further?

Last edited by Tommo; 05-20-2007 at 01:52 PM.
 
Old 05-20-2007, 02:23 PM   #13
saikee
Senior Member
 
Registered: Sep 2005
Location: Newcastle upon Tyne UK
Distribution: Any free distro.
Posts: 3,398
Blog Entries: 1

Rep: Reputation: 112Reputation: 112
Tommo,

Do get excited yet. The testing of the pudding is to have hdb electrically disconnected. This is because if you made a mistake the Linux can still fetch the information from hdb disk to make up for it. You need to be sure everything is coming from sda so disconnecting hdb is the final check.
 
Old 05-20-2007, 04:02 PM   #14
Tommo
Member
 
Registered: May 2006
Posts: 80

Original Poster
Rep: Reputation: 15
OK, I have d/c the hard drive. Everything seems to be ok.
 
Old 05-20-2007, 04:20 PM   #15
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,207

Rep: Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298Reputation: 2298
Another satisfied customer - even if I was ignored ...

I gave up on dd because of the messing with partitions. Had lots of trouble resizing ext2/3; although I haven't tried in a while.
Also occasionally I like to try different filesystem types.

As for general "cloning", I see clonezilla on sf; haven't tried it, but would be worth consideration in future.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying files from internal Hard drive to USB 2.0 Hard Drive is NOT Behaving tubatodd Ubuntu 4 02-19-2007 05:32 PM
Bigger Hard Drive - Clone? Snump Mandriva 14 11-21-2006 09:37 AM
I need to clone a laptop hard drive mike160 Linux - Laptop and Netbook 9 04-23-2006 06:23 PM
Clone a hard drive twantrd Solaris / OpenSolaris 10 02-05-2005 03:15 AM
Easy way to clone linux installed hard drive mobassir Linux - Software 6 01-12-2004 06:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

All times are GMT -5. The time now is 05:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration