Cloning a hard drive is useful, but what if you have to make several copies, or you just want to make a complete backup of a hard drive? Drive images let you put everything, and we mean everything, from your hard drive in one big file.
With an Ubuntu Live CD, this is a simple process – the versatile tool dd can do this for us right out of the box.
We’ve used dd to clone a hard drive before. Making a drive image is very similar, except instead of copying data from one hard drive to another, we copy from a hard drive to a file. Drive images are more flexible, as you can do what you please with the data once you’ve pulled it off the source drive.
Your drive image is going to be a big file, depending on the size of your source drive – dd will copy every bit of it, even if there’s only one tiny file stored on the whole hard drive. So, to start, make sure you have a device connected to your computer that will be large enough to hold the drive image.
Some ideas for places to store the drive image, and how to connect to them in an Ubuntu Live CD, can be found at this previous Live CD article. In this article, we’re going to make an image of a 1GB drive, and store it on another hard drive in the same PC.
Note: always be cautious when using dd, as it’s very easy to completely wipe out a drive, as we will show later in this article.
Creating a Drive Image
Boot up into the Ubuntu Live CD environment.
Since we’re going to store the drive image on a local hard drive, we first have to mount it. Click on Places and then the location that you want to store the image on – in our case, a 136GB internal drive.
Open a terminal window (Applications > Accessories > Terminal) and navigate to the newly mounted drive. All mounted drives should be in /media, so we’ll use the command
and then type the first few letters of our difficult-to-type drive, press tab to auto-complete the name, and switch to that directory.
If you wish to place the drive image in a specific folder, then navigate to it now. We’ll just place our drive image in the root of our mounted drive.
The next step is to determine the identifier for the drive you want to make an image of. In the terminal window, type in the command
sudo fdisk -l
Our 1GB drive is /dev/sda, so we make a note of that.
Now we’ll use dd to make the image. The invocation is
sudo dd if=/dev/sda of=./OldHD.img
This means that we want to copy from the input file (“if”) /dev/sda (our source drive) to the output file (“of”) OldHD.img, which is located in the current working directory (that’s the “.” portion of the “of” string).
It takes some time, but our image has been created…Let’s test to make sure it works.
Drive Image Testing: Wiping the Drive
Another interesting thing that dd can do is totally wipe out the data on a drive (a process we’ve covered before). The command for that is
sudo dd if=/dev/urandom of=/dev/sda
This takes some random data as input, and outputs it to our drive, /dev/sda.
If we examine the drive now using sudo fdisk –l, we can see that the drive is, indeed, wiped.
Drive Image Testing: Restoring the Drive Image
We can restore our drive image with a call to dd that’s very similar to how we created the image. The only difference is that the image is going to be out input file, and the drive now our output file.
The exact invocation is
sudo dd if=./OldHD.img of=/dev/sda
It takes a while, but when it’s finished, we can confirm with sudo fdisk –l that our drive is back to the way it used to be!
There are a lots of reasons to create a drive image, with backup being the most obvious. Fortunately, with dd creating a drive image only takes one line in a terminal window – if you’ve got an Ubuntu Live CD handy!
- Published 06/14/10