Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have done half an hour reading around to prepare to clone my hard drive. It has multiple partitions, including a Windows partition. I am going to buy a very large external hard drive for the backup. I would like to be able to use this clone to restore the whole drive in case something goes wrong (I'm about to do some OS re-shuffling). I want do learn how to do this using dd, as I like low-level tools that don't require installing anything.

I found the following useful code from the ubuntu forums (entered from a root shell using a live CD):

dd if=/dev/hda of=/dev/hdb & pid=$!
while kill -USR1 $pid; do sleep 1; done

(I know that I will have to edit the input and output locations.) However I have two questions. The first one is very noobie: this command is split across two lines. Surely when I press enter after the exclamation mark it will start the process?

Two, on other sites it recommended entering block size. Like this:

# dd if=/dev/hda conv=sync,noerror bs=64K of=/mnt/sda1/hda.img

I don't know anything about block sizes. Is 64K right? It looks like my block size is 512 bytes from the following, the output of sudo fdisk -ul:

Disk /dev/sda: 750.2 GB, 750156374016 bytes
255 heads, 63 sectors/track, 91201 cylinders, total 1465149168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0xc3ffc3ff

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *          63   143364059    71681998+   7  HPFS/NTFS/exFAT
Partition 1 does not start on physical sector boundary.
/dev/sda2       976867328  1465147391   244140032    7  HPFS/NTFS/exFAT
/dev/sda3       143364094   976867327   416751617    5  Extended
Partition 3 does not start on physical sector boundary.
/dev/sda5       143364096   162895871     9765888   82  Linux swap / Solaris
/dev/sda6       162897920   205864959    21483520   83  Linux
/dev/sda7       205867008   976867327   385500160   83  Linux

Partition table entries are not in disk order

Disk /dev/mapper/cryptswap1: 10.0 GB, 10000269312 bytes
255 heads, 63 sectors/track, 1215 cylinders, total 19531776 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk identifier: 0x433bb3a7

Disk /dev/mapper/cryptswap1 doesn't contain a valid partition table

Thank you.

share|improve this question

2 Answers 2

up vote 6 down vote accepted

Progress

The command you listed

dd if=/dev/hda of=/dev/hdb & pid=$!
while kill -USR1 $pid; do sleep 1; done

is a nice two-liner to get the the progress of dd on a regular basis. I use a very similar one too. Looks good. Found it here perhaps?

Blocksizes with dd: alignment and performance

You can add a block size in which the operations take place. It does not matter what the block size of the underlying block device is to get the operation done equally well, but for performance reasons you might want to pick one that suits your needs.

First of all, there's the alignment thing. In case your block device operates as 512KiB (like flash drives do), it would be very unfortunate to run dd with bs=512 (bytes) as this will cause 1024 writes (!) for each block from the device perspective. In practise it won't be this bad as writes are buffered and taken into one go, but during syncs it can still amplify the amount of writes a lot.

Then also consider the plain CPU usage overhead when dealing with a very large amount of small operations. It's just more efficient to take megabytes at once when copying over large amounts of data.

My best practice is to start with 1MB as that is a nice multiple of most set ups, including RAID stripe sizes, LVM extent sizes, etc. On my laptop with SSD I tend to see a slight improvement using 10MB as block size, whereas I don't see it anymore on my physical hard drive.

Last block

Don't worry about the drive/volume size not being a multiple of the block size. The last block dd will copy will be adjusted to match the last bit of data on it. You can see whether the last block had a different size by looking at the output.

18335302+0 records out

The +0 means that it was an exact match, a +1 means that it wasn't. No big deal.

See also

share|improve this answer
    
Wow, what a thorough answer. Thank you so much for taking the time to do it. I will updat the original question with a link to my source. I will go with 1 MB then. So my command will be like this, am I right? # dd if=/dev/hda conv=sync,noerror bs=1MB of=/mnt/sda1/hda.img & pid=$! while kill -USR1 $pid; do sleep 1; done –  oldmankit Jan 25 '13 at 9:36
1  
@oldmankit I'd do bs=1M as that's a power of 2, rather than bs=1MB being a power of 10. But just run some benchmarks on your system if you can to see what's the best. –  gertvdijk Jan 25 '13 at 10:04

As others have said, there is no universally correct block size; what is optimal for one situation or one piece of hardware may be terribly inefficient for another. Also, depending on the health of the disks it may be preferable to use a different block size than what is "optimal".

One thing that is pretty reliable on modern hardware is that the default block size of 512 bytes tends to be almost an order of magnitude slower than a more optimal alternative. When in doubt, I've found that 64K is a pretty solid modern default. Though 64K usually isn't THE optimal block size, in my experience it tends to be a lot more efficient than the default. 64K also has a pretty solid history of being reliably performant: You can find a message from the Eug-Lug mailing list, circa 2002, recommending a block size of 64K here: http://www.mail-archive.com/eug-lug@efn.org/msg12073.html

For determining THE optimal output block size, I've written the following script that tests writing a 128M test file with dd at a range of different block sizes, from the default of 512 bytes to a maximum of 64M. Be warned, this script uses dd internally, so use with caution.

dd_obs_test.sh:

#!/bin/bash

# Since we're dealing with dd, abort if any errors occur
set -e

TEST_FILE=${1:-dd_obs_testfile}
[ -e "$TEST_FILE" ]; TEST_FILE_EXISTS=$?
TEST_FILE_SIZE=134217728

# Header
PRINTF_FORMAT="%8s : %s\n"
printf "$PRINTF_FORMAT" 'block size' 'transfer rate'

# Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M
for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864
do
  # Calculate number of segments required to copy
  COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE))

  if [ $COUNT -le 0 ]; then
    echo "Block size of $BLOCK_SIZE estimated to require $COUNT blocks, aborting further tests."
    break
  fi

  # Create a test file with the specified block size
  DD_RESULT=$(dd if=/dev/zero of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT 2>&1 1>/dev/null)

  # Extract the transfer rate from dd's STDERR output
  TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?')

  # Clean up the test file if we created one
  [ $TEST_FILE_EXISTS -ne 0 ] && rm $TEST_FILE

  # Output the result
  printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE"
done

View on GitHub

I've only tested this script on a Debian (Ubuntu) system and on OSX Yosemite, so it will probably take some tweaking to make work on other Unix flavors.

By default the command will create a test file named dd_obs_testfile in the current directory. Alternatively, you can provide a path to a custom test file by providing a path after the script name:

$ ./dd_obs_test.sh /path/to/disk/test_file

The output of the script is a list of the tested block sizes and their respective transfer rates like so:

$ ./dd_obs_test.sh
     512 : 11.3 MB/s
    1024 : 22.1 MB/s
    2048 : 42.3 MB/s
    4096 : 75.2 MB/s
    8192 : 90.7 MB/s
   16384 : 101 MB/s
   32768 : 104 MB/s
   65536 : 108 MB/s
  131072 : 113 MB/s
  262144 : 112 MB/s
  524288 : 133 MB/s
 1048576 : 125 MB/s
 2097152 : 113 MB/s
 4194304 : 106 MB/s
 8388608 : 107 MB/s
16777216 : 110 MB/s
33554432 : 119 MB/s
67108864 : 134 MB/s

(Note: The unit of the transfer rates will vary by OS)

To test optimal read block size, you could use more or less the same process, but instead of reading from /dev/zero and writing to the disk, you'd read from the disk and write to /dev/null. A script to do this might look like so:

dd_ibs_test.sh:

#!/bin/bash

# Since we're dealing with dd, abort if any errors occur
set -e

TEST_FILE=${1:-dd_ibs_testfile}
[ -e "$TEST_FILE" ]; TEST_FILE_EXISTS=$?
TEST_FILE_SIZE=134217728

# Exit if file exists
if [ -e $TEST_FILE ]; then
  echo "Test file $TEST_FILE exists, aborting."
  exit 1
fi

# Create test file
echo 'Generating test file...'
BLOCK_SIZE=65536
COUNT=$(($TEST_FILE_SIZE / $BLOCK_SIZE))
dd if=/dev/urandom of=$TEST_FILE bs=$BLOCK_SIZE count=$COUNT > /dev/null 2>&1

# Header
PRINTF_FORMAT="%8s : %s\n"
printf "$PRINTF_FORMAT" 'block size' 'transfer rate'

# Block sizes of 512b 1K 2K 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M
for BLOCK_SIZE in 512 1024 2048 4096 8192 16384 32768 65536 131072 262144 524288 1048576 2097152 4194304 8388608 16777216 33554432 67108864
do
  # Read test file out to /dev/null with specified block size
  DD_RESULT=$(dd if=$TEST_FILE of=/dev/null bs=$BLOCK_SIZE 2>&1 1>/dev/null)

  # Extract transfer rate
  TRANSFER_RATE=$(echo $DD_RESULT | \grep --only-matching -E '[0-9.]+ ([MGk]?B|bytes)/s(ec)?')

  printf "$PRINTF_FORMAT" "$BLOCK_SIZE" "$TRANSFER_RATE"
done

# Clean up the test file if we created one
[ $TEST_FILE_EXISTS -ne 0 ] && rm $TEST_FILE

View on GitHub

An important difference in this case is that the test file is a file that is written by the script. Do not point this command at an existing file or the existing file will be overwritten with random data!

For my particular hardware I found that 128K was the most optimal input block size on a HDD and 32K was most optimal on a SSD.

Though this answer covers most of my findings, I've needed to determine an optimal dd block size enough times that I wrote a blog post about it: http://blog.tdg5.com/tuning-dd-block-size/ You can find more specifics on the tests I performed there.

This StackOverflow post may also be useful: dd: How to calculate optimal blocksize?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.