[GUFSC] Utilizando o dd para ``clonar'' partições

franciscogick em free.fr franciscogick em free.fr
Quinta Julho 15 00:17:41 BRT 2004


E aí,

Lembro que rolou há algum tempo uma série de discuções sobre clonagem de
partições, este artigo me pareceu interessante...

Desculpem se o artigo já rolou por aqui, pelo menos eu não lembro.../

Falou
Francisco

<colado>

The Scenario
Anybody who's ever had the ... errr... pleasure... of installing Windows NT
knows that it takes a long time to simply install the basic OS, vendor-specific
drivers, service packs, etc. I once counted the number of reboots necessary for
a full installation, and it was upwards of 6. While perhaps some Microsoft
masochists take a sick pride in wasting a perfectly good hour and a half, our
production folks soon grew tired of the process -- since every single machine we
ship has to have NT installed from the ground up.

    Permit me a short tangent: While typing this article, I asked my wife how to
spell "masochist". She was in the middle of an attempted spelling when the phone
rang. I answered, and after a typical 2 second delay, the quiet voice of one of
those annoying telemarketers could be heard. The conversation went something
like this:

    Ken: Hello?
    Lady: *Pause* ... Hi, may I speak to Mr. or Mrs...
    Ken (interrupting): No, but I have a question for you. Do you know how to
spell "masochist"? M-A-S-O-C-H-I-S-T, does that sound correct?
    Lady: I really don't have any idea, sir.
    Ken: Okay, thank you. Goodbye. *Click!*

    And now, back to your regularly scheduled article.


In the face of this installer boredom, somebody suggested that we consider using
Norton Ghost to do the installations. The promise was intriguing. Do one of
these painstaking procedures, then duplicate the raw contents of the drive off
to an image on some external medium, and then use that image to create
duplicated drives in a matter of minutes.

So it was requested that Tom look into this. He did, and found that Ghost works
well; it does exactly what we wanted it to. You boot off of a floppy (while the
image medium is in another drive), and Ghost does the rest.

The problem lies in Ghost's licensing. If you want to install in a situation
like ours, you have to purchase a Value-Added Reseller (VAR) license from
Symantec. And, every time you create a drive, you have to pay them about 17
dollars. When you also figure in the time needed to keep track of those
licenses, that adds up in a hurry.

Software licensing issues always come as a bit of a shock to me, since I've been
living in an open-source world for some time now. And I've taken to using this
moment of surprise as an opportunity to say, "how could I use Linux to do the
same task?" Sometimes, a dim flickering of a light comes on.

It finally occurred to me that we could use Linux and a couple of simple tools
(dd, gzip, and a shell script) to do the same thing as Ghost -- at least as far
as our purposes go. Here's what we did:

Install Linux on a Removable Disk
You could use whatever you want, but we have an abundance of Fujitsu 230MB
magneto-optical drives at our company. It's easy to install on these, because
you can treat them like any other fixed disk. We skipped the swap partition,
because this little Linux system wouldn't have very heavy demands put on its VM
system.

We installed Red Hat simply because we had it on hand. Slackware would have been
a much better choice for a tiny system like this, since the "no frills" Red Hat
install was still over 100MB. Whatever you use, trim it down as much as you can.
I found a number of RPMs that I was able to remove from the Red Hat install, and
I got it down to a somewhat more reasonable 70MB.

There's no reason why you need to start any of the common services, either. Just
boot it up and give me a prompt, please.

Prepare the System
If you've just finished a fresh installation, you should be good to go. If, on
the other hand, you're making an image of a hard disk that's been in use for a
while, you'll want to use something like DOS's SCANDISK to move everything to
the front of the partition. SCANDISK calls this a "full optimization".

Copy off the Master Boot Record (MBR)
Boot up your Linux-on-Removable system and execute the following command:

dd if=/dev/hda of=hda.mbr bs=512 count=1



Replace the "hda" with the device you're trying to make an image of. This
command rips the first 512 bytes off of the hard disk, which gives you the
partition table and the entry point for the bootloader.

Create a Compressed Disk Image
You should have a good idea of how much data is on this partition. If not, mount
it and use df -h to figure it out. Then, execute the following command:

dd if=/dev/hda1 bs=1M count=150 | gzip > hda1-image.gz

Again, replace the "hda1" with the partition that you're duplicating. Also,
replace the "150" with the number of Used megabytes on the partition. This
process will take a while; it copies all of the data off of the partition and
puts it in a compressed image. When we were done, the compressed image was about
a 55 MB in size.

Write a Restoration Script
We wrote a quick little script to make life easier for the folks who will be
using our utility. It needs to do a couple of things: copy the MBR to the new
disk, format the DOS partition, and then uncompress the image as it copies it to
the partition.

Our script looks like this:


    #!/bin/sh

    echo && echo
    echo "This program will copy the system disk image to the first hard"
    echo "drive attached to the system. This will DESTROY ALL DATA on the drive."
    echo -n "Do you want to continue (Y/N)? "
    read ans

    if test "$ans" != "Y" -a "$ans" != "y";
    then exit 0;
    fi

    echo -n "Writing disk image... "

    dd if=hda.mbr of=/dev/hda >& /dev/null
    mkdosfs /dev/hda1 >& /dev/null
    zcat hda1-image.gz | dd of=/dev/hda1 >& /dev/null

    echo "done!"
    echo
    echo "The computer will now reboot".
    reboot



You get the idea. It is left as a exercise to the reader to get this to run
automatically at boot.

The Results?

We showed our little program to management, and they were impressed. We were
able to create disk images almost as quickly as Norton Ghost, and we did it all
in an afternoon using entirely free software. The rest is history.

-- Ken Treis

</colado>


Mais detalhes sobre a lista de discussão GUFSC