SEARCH

How-To Geek

Make a Drive Image Using an Ubuntu Live CD

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.

sshot-9

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.

sshot-1

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

cd /media

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.

sshot-2

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

sshot-3

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).

sshot-4

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.

sshot-5

If we examine the drive now using sudo fdisk –l, we can see that the drive is, indeed, wiped.

sshot-6

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

sshot-7

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!

sshot-8

Conclusion

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!

Trevor is our resident Linux geek, but always keeps his eyes open for neat Windows tricks too.

  • Published 06/14/10

Comments (16)

  1. rroberto18

    (1) Will this work on an XP OS without a Ubuntu OS installed in a separate partition?
    Screenshots shown look like Win 7.

    (2) Is the image size the same as the source?

    (3) How do you set Safe Mode to recognize the drive needed for the Ubuntu Live CD?

  2. realmrealm

    @rroberto18

    1. All screen shots are from within an Ubuntu Live CD. If you do not know about live cd’s you will want to read up on it a little. To put it quickly a “Live CD” is an operating system contained on a cd. So in this case the person demonstrating may have Windows 7, or Windows XP it doesn’t matter. He created a live cd of ubuntu and booted his computer FROM THE CD, and not from the hard drive like normal. Booting from the cd which has a live version of ubuntu on it in essence booted him into the ubuntu operating system even though he does not have it installed. This is why people like live cd’s – it changes nothing on your system (unless you change something on your hard drive while in the live cd) but allows you to either try out other operating systems, or use the tools (in this case dd) from the live cd operating system on your computer without needing to install anything. (gosh I hope that made sense)

    2. From his screen shots it looks like the image is as big as the drive itself (and we can assume that he is not utilizing the whole drive). So Image size will be equal to drive capacity.

    3. I think the answer to number one shows that question 3 is irrelevant.

    Good Luck! Go Linux!

  3. yochai

    great article.
    I would recommend using GNU ddrescue, however (gddrescue in the repos) as it is resumable AND works around failing drives.

  4. Cabanur

    Hello!

    I have an Acer Aspire One D250, and when i installed linux it rewrote my mbr putting grub on it. The thing is I now want to sell this netbook, and i’d like to give it away “as when out of the box”, but if i install XP from the rescue partition it won’t rewrite my MBR, so the pc will uselessly search for GRUB. My question is: Can I use this tool you just posted to return my harddrive to how it was before?

    Thank you for your time!

    Cabanur

  5. rroberto18

    @realmrealm

    Thanks for your previous answer. But at my level of PC experience (average at best), your answers lead to other questions I hope you have time to address.

    I will read up on Live CDs then re-read the above to make an image. But in real life terms:

    (i)I have 2 internal hard drives (a C boot 80G + a D data 40G), so it appears I would need to make two separate images ( C + D) to store on to my destination drive (external G: 1T).
    BUT
    (ii) I also use my G drive as the destination for a traditional backup created by Win XP (so I can restore single files which an image backup cannot do), So I will have more on G than just the C + D images produced by the Ubuntu Live CD.
    SO
    (iii) The “wiping test” is where I now have a question. Even if I didn’t HAVE to perform it, your explanation leads me to pose the following Qs:

    (Qa) After leaving all the above (C+ D images via Live CD + traditional back-up via Win XP), won’t wiping the drive remove MY ENTIRE G DRIVE ? In which case I’d need to have 3 separate externals to make sure each wipe affected only the image I was testing? This would mean I’d have to buy 2 additional externals, to keep each back-up separate or a total of 3 externals?

    OR

    (Qb) Would the Ubuntu Live CD ONLY recognize the C + D images it created and ignore my traditional backups even though they also resides on the G drive…so I could retain my traditional backups even after doing a wipe via the Live Ubuntu CD?

    The answer may be obvious to you, but not to me, out of my own inexperience in these waters.

    Thanks in advance for your time.

  6. PreLand

    If you create an image of Windows, I recommend using the ntfsclone tool (from the ntfsprogs package, it is already present in Ubuntu Live CD). For instance, command

    ntfsclone –save-image -o – /dev/hda1 | gzip -c > backup.img.gz

    will create the image for you effectively. (See man ntfsclone.)

    If you want to backup a bootable system, I usually create two images.
    First is about 10MB of hard drive from the very beginning (i.e., device sda or hda),
    image is created using the dd utility with parameter count=20000 (i.e. imaging will stop after 10 Megabytes), and the second is created by the above example. (Device sda1 or hda1).
    When restoring, I first restore the 10Mb-sized dd image (thus ensuring that MBR & Co will be present),
    then I restore the ntfsclone image. Works well and quickly!

  7. wyah

    @Cabanur.

    Read other sites about how to unistall grub mbr. There are better solutions than you think

  8. Sam Horstman

    I Downloaded your instructions and followed them to the letter but in ubuntu 10.04 desktop, the command (cd/media) returns the comment (command not found) even if I use ( sudo cd/media) I get the same comment. Am I missing something? I will tune in here next wee to view your comment.

  9. Trevor Bekolay

    @ Sam Horstman

    There’s a space between cd and /media. The “cd” portion of the command means “change directory”; “/media” is the actual directory you’re changing to.

  10. Sam Horstman

    Trevor, I tried the “cd” space “/media” and it still does not work. I think I know what I am doing I have been using ubuntu on a daily basis since May of ’09, And have installed it on 75 Computers. I have tried it with both the Live CD and the installed program and it will not work there either. Suggestion?

  11. Dave R

    Is there another way to use the drive image file created other than writing it back to the original drive?

    For example mount it and be able to access the files and folders like the original drive (either in Ubuntu or Win7)

    I don’t want to wipe out the original drive until I know the backup works reliably.

  12. Trevor Bekolay

    @Dave R

    Great question! That makes perfect sense.

    If you’re in Linux, you can mount an image file created by dd using the built-in “mount” command; you just have to make sure to use the “-o loop” option.

    So, if you wanted to mount an image file (hdd.img) to /media/old-hdd, you can try this:

    mount ./hdd.img /media/old-hdd -o loop

    mount should try to figure out the file system, but if you know it ahead of time, you can specify it; e.g.

    mount -t ext3 ./hdd.img /media/old-hdd -o loop

  13. Greg

    I’m trying this with an HDD source in an external enclosure writing to an image file on internal HDD. Do I have to boot a LiveCD for this?

    Thanks!

  14. al

    I created the image as you showed but it is just 4 gig , the drive was 40 gig.

    When I try to put it back I get “dd: opening ./oldimage’ : no such file or directory”

    thanks

  15. Chris

    First, thanks for the great instructions! I do have a bit of a problem though:
    I had difficulty mounting my drives, (one seems to be corrupted), but I managed to mount my external drive in /media/windows. I then tried to mount my internal drives, but they wouldn’t show.

    I went ahead and continued with the following:

    Ubuntu@ubuntu:/media/windows/backupEmer$ sudo dd if=/dev/sda of=./HDbackup.img

    Nothing happened for a while, then the screen went black. Pressing any key didn’t help (though the CD drive started making noise again for a short while), and I couldn’t get my visuals to come back. I restarted the computer, disabled the screensaver and tried again, but this time the entire system just seemed to be inactive. Will there be any feedback to show that an image is being created?

    Ubuntu Live also seems to keep hanging on me, especially if I use the GUI to execute any commands.. Is there any particular reason why this might happen?

    Many thanks in advance for your help!

  16. mike

    This dealership is a DealerRater.com Certified Dealer and is committed to providing quality customer service. One of which is the offer of our Wednesdays no credit check program.

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!