Bill Jonas on Sat, 15 Dec 2001 21:50:20 +0100


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Question on 'dd' - Saving a system disk II...


On Sat, Dec 15, 2001 at 12:30:07PM -0400, Guillermo Moyna wrote:
> How can I use dd to make an image of 'sda' on 'hda' that I could then
> 'dd' onto the new SCSI drive I will get next week?

To answer your question as you asked it[1]:

# dd if=/dev/sda of=drive-image.bin

When you get the new drive:

# dd if=drive-image.bin of=/dev/sda

You can substitute the filename of your choice for "drive-image.bin".
Also, to make the process run faster, you can specify a block size to
use; one to ten megs might be a reasonable value.  Specify it by adding
"bs=1M" or "bs=10M" (or whatever value you choose) to the end of the
command line.  This is just my personal preference; if you don't specify
this, dd will use the native block size of the device, which is 512
bytes (I think) for a hard disk.  This means it will read 512 bytes from
the disk, then write 512 bytes to the file, then read 512 bytes, etc.
Nothing wrong with that, but I get impatient easily. :)

If you get errors when the output file hits two gigabytes, use this pair
of commands instead:

# dd if=/dev/sda >drive-image.bin

# dd of=/dev/sda <drive-image.bin 

If you need to save space (although it doesn't appear you do), you can
gzip the image on the fly by doing this instead (and you can use the
block size parameter, just put it before the '|' character):

# dd if=/dev/sda |gzip -9 >drive-image.bin.gz

And write it to the new disk like this:

# gunzip -c <drive-image.bin.gz |dd of=/dev/sda

Compression is optional, though, if there's enough space otherwise.  I
just thought I'd throw that in.  :)

[1]Another solution would be to use tar:

# tar -cvpf fs-image.tar /path/to/mounted/partitions

With compression:

# tar -cvpf - /path/to/mounted/partitions |gzip -9 >fs-image.tar.gz

Restore:

# tar -xvpf fs-image.tar /path/to/mounted/partitions

With compression:

# tar -xzvpf fs-image.tar.gz /path/to/mounted/partitions

Two gigabyte problem?

# tar -cvpf - /path/to/mounted/partitions >fs-image.tar

# tar -xvpf - /path/to/mounted/partitions <fs-image.tar

(And adjust appropriately if you want to compress the image.)

The advantage of using dd is that you don't have to do any partitioning
or install any boot loader and therefore is less hassle and more
straightforward.  However, you *can't* use dd and *must* use tar if the
new drive differs in *any* *way* from the old one.

The two gigabyte problem can bite you with some distributions.  A bit of
background if you need it: Basically, the problem is that file sizes are
represented internally with signed long ints, which on a 32-bit platform
like x86 is 32 bits.  One bit of that is used for the sign (positive or
negative), so you're left with 31 bits.  2^31 = 2 gigabytes.  (On a
64-bit architecture, the problem goes away because 2^63 is obscenely
large.)  If I'm not mistaken, glibc has support for >2 gigabyte files on
32-bit architectures, but the user programs have to be updated to take
advantage of this.  I experience no problems with Debian (woody), but
when I was playing around with Mandrake 8.1 for a few days, for example,
dd would dump core if the input or output files were (or grew greater
than) two gigs.  Using input and output redirection can help you get
around that, if needed.

Hope that helps.

-- 
Bill Jonas    *    bill@billjonas.com    *    http://www.billjonas.com/

Developer/SysAdmin for hire!   See http://www.billjonas.com/resume.html

Attachment: pgpXwJ6NtcFxs.pgp
Description: PGP signature