11

What I am looking for is a way to clone hard drives (mainly windows partitions) while staying inside of the Ubuntu OS. I do not want to create a partition on the hard drive to boot off of and I am not wanting to use a boot cd. The only thing I can even think of that is close is the 32bit version of ghost that can run from the desktop in windows.

It doesn't have to be clonezilla, but that is the only linux cloning software I have used and have almost used it exclusively since I discovered it. If there is no way to use clonezilla from the desktop, then I will accept an alternative program. The main thing is that it clones drives and runs from within Ubuntu.

Thank you for your time in advance

2 Answers 2

6

Clonezilla uses dd behind the scenes.

About disks and partitions

A whole disk is a device like /dev/sda. This is the first disk, the second disk is /dev/sdb, the third /dev/sdc, etc. Older disks connected through an IDE cable are named like hda, hdb, ... A disk can have multiple partitions like /dev/sda1. The second partition on disk /dev/sda is /dev/sda2 and so on. An image (literal copy of bytes) can be made from both a partition and disk. Note that first 512 bytes of a disk contains the MBR (Master Boot Record).

A partition should not be mounted when creating or restoring images, otherwise data loss may occur when reading from it (creating an image) or unexpected bahavior and data corruption if you're writing to it (restoring from a image).

In the below examples, /dev/sda1 is the partition from which an image should be created.

Partitions and disk devices in /dev are only writable by the superuser (root) and users of the disk group. I dislike running everything as root, so for safety (in case you made a typo for example), I change the group temporary to myself, so I can read and write to it:

sudo chgrp my_user_name /dev/sda1

If you skip the above command, you've to prefix the below dd commands with sudo.

Basics

The basic command for creating an image from a partition is:

dd if=/dev/sda1 of=disk.img

if means "input file", of means "output file". Everything in Linux is a file, even devices.

To restore such an image, run:

dd if=disk.img of=/dev/sda1

The order does not matter, you could have written the above as dd of=/dev/sda1 if=disk.img too.

Compressed images

Since partitions are generally big, it's recommended to compress the data before writing it to the image:

dd if=/dev/sda1 | gzip > disk.img.gz

This works because if of is omitted, the output is written to "standard output" which is the pipe to the compress program gzip. The output of that is written to disk.img.

To restore such a compressed image, run:

gunzip -c disk.img.gz | dd of=/dev/sda1

Here, gunzip is the reverse command of gzip. -c causes the output be written to standard output which is the pipe to the dd command. Because if is omitted on dd, the input is read from "standard input" which is the output of gunzip.

Reading from an image without restoring it

Uncompressed images can be mounted so you can read from it. Should you've compressed your partition images, uncompress them first (disk.img.gz will be removed, disk.img will be created. Be sure to have enough space!):

gunzip disk.img.gz

Alternatively, uncompress an image without touching the image itself:

gunzip -c disk.img.gz > disk.img

Now create a directory on which the disk can be mounted and mount the image read-only (ro):

sudo mkdir /mnt/wind
sudo mount -o ro disk.img /mnt/wind

You can now view your files in /mnt/wind. When done, unmount it and remove the obsolete mount point:

sudo umount /mnt/wind
sudo rmdir /mnt/wind

Less size, more CPU usage, longer backup and restore duration

If time is not an issue and you don't have much storage space, you could use the bzip2 compression format. Simply replace gzip by bzip2 in the above. It's common to use the .bz2 extension for bzip2-compressed files, so do so.

9
  • Being that clonezilla uses dd then that is in fact what I am looking for. I'm assuming the commands in the first post are valid since I am going disk to disk. Thanks for the clarification.
    – Ctuchik
    Jul 28, 2011 at 17:23
  • The first answer was correct, but did not mention the pipe method. I've had a need to backup a 20GB disk with just 150MB data from a 4GB flash drive running tinycorelinux. Obviously, 20GB uncompressed data was not going to fit on that, so I had to use this pipe method.
    – Lekensteyn
    Jul 28, 2011 at 17:34
  • @Lekensteyn Great way to save space using pipe method. Thanks for insight
    – Amey Jah
    Jul 28, 2011 at 17:37
  • After all the writing the answer is - NO :) Additionally "A partition should not be mounted when creating or restoring images(...)" so there is no way to do an image of system partition/disk.
    – Bucic
    Jan 18, 2012 at 1:54
  • Thank you for the very educational guide. I ran these commands, with compression, but the image file turns out larger than my system (!). Do you know why? @Lekensteyn
    – Heisenberg
    Oct 22, 2013 at 17:44
4

Do you want to clone one hard disk to other? There are several ways to do that. I generally prefer dd command. Please note that to use this command, you would need sudo access.

  1. To clone Hard disk 1 into Hard disk 2. Note that Hard disk 2 must be of greater size than hard disk 1. If your source hard disk is sda and target hard disk is sdb then you can copy all contents of sda to sdb using following command.

    dd if=/dev/sda of=/dev/sdb
    
  2. To clone whole hard disk 1 into an image, use following command. Note that ~/disk1.img should be residing on different disk

    dd if=/dev/sda of=~/disk1.img
    
  3. To clone single partition into image, use following command. Note that image file should not reside on same partition.

    dd if=/dev/sda1 of=~/drive1.img
    
  4. To restore image, use following command.

    dd if=drive1.img of=/dev/sda1 - For Partition
    dd if=disk1.img of=/dev/sda   - For whole hard disk
    
  5. You can also save space by compressing image file.

    gzip disk1.img  - This will generate disk1.img.gz
    bzip2 disk1.img - This will generate disk1.img.bz2
    

Courtesy: Linux Backup: Hard Disk Clone with "dd"

5
  • I am in fact wanting to go from one drive to the other. The main thing I need to work, is the registry being maintained on windows drives after the copy. My issue with DD is I have heard stories where it can take an exceptionally long time to clone, sometimes over a day. I simply don't have that kind of time for a drive to clone. With ghost I know the average is around 2-3 hours (I use my co-workers Windows machine). I don't really need the imaging functionality, although it is nice to know that it's a possibility. If DD meets those requirements though I will definitely give it a shot.
    – Ctuchik
    Jul 28, 2011 at 16:06
  • @Ctuchik dd will simply copy from byte to byte. So IMO it should preserve the registry because it make exact copy of hard disk. Though I don't know what will the effect if window maintain's hard disk information on registry. Because I do remember that windows has boot profile where it actually remember's your bootsector and hard disk information. Give a try and let us know.
    – Amey Jah
    Jul 28, 2011 at 16:37
  • @Ctuchik I did some reading. Seems that windows is smart enough (:P) to detect new hard disk. So if you clone the hard disk, make sure you remove the old hard disk and connect latest one. Depending on your previous configuration and location of mbr, you might have to do fixmbr or fixboot using windows cd. Follow this thread for actual explanation and procedure of cloning xp (using dd) justlinux.com/forum/showthread.php?threadid=134457
    – Amey Jah
    Jul 28, 2011 at 16:45
  • Ok, it may be a couple of days before I have to do a clone for a client, but if I do, then I will update this thread with the results. Thanks for the speedy reply.
    – Ctuchik
    Jul 28, 2011 at 16:45
  • When I restored an image to a new (bigger) disk, I had to reactivate WinXP. Just keep in mind that something like that can happen.
    – Lekensteyn
    Jul 28, 2011 at 17:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .