Programs
Savehd7 Source Code
 previous   up   next 

Savehd7 saves a potentially damaged harddisk partition to an image file. Savehd7 works for all filesystems. Calling Savehd7 with -h gives some helping explanations.

Rationale

Harddisks are not 100% reliable and over time they can acquire bad blocks (which cannot be fixed by the firmware). Reading a bad block will cause the operating system to report an error or to return garbage. Writing to a damaged partition can even cause the filesystem to become corrupt. For this reason a file system checker should not be used when there are read or write errors. To fix the filesystem it is necessary to copy the whole partition to a file. Tools like 'cp' or 'cat' are unusable to copy a damaged partition, since they will abort on a read error. With conv=noerror 'dd' will proceed, but it will just try to copy all blocks sequentially. A smarter approach which jumps over bad blocks and maintains a list of bad block areas would be helpful.

The Savehd7 program is designed to copy from a device file to a disk image file even when read errors occur. It will try to copy as much as possible and leave the unreadable blocks filled with zero bytes. The file system checker can fix the saved disk image afterwards. Finally the saved and repaired disk image file can be mounted with the loop mount feature (which can mount a file as if it is a device).

Savehd7 tries to jump over bad blocks to reach a high rate of good blocks in relative small time. In later phases it will try to shrink the areas with possible bad blocks without trying to read bad blocks too much. This way the rate of good blocks is increased without spending too much time. Just the final phase will try to read all blocks still marked as bad.

Savdhd7 maintains a list of potential bad areas and the processing state. It can be paused by pressing a key and is able to continue afterwards. This way the partition image can be mounted much earlier. When a read only mount was used the saving process can continue afterwards.

Usage

Several conditions must be fulfilled to use Savehd7:

  • Operating systems without device files are not supported.
  • To get access to the device file Savehd7 needs to be started as superuser.
  • Nothing should be mounted on the device file processed by Savehd7.
  • Programs which possibly change the device file such as filesystem checkers should not run in parallel to Savehd7.
  • There must be enough free space on the destination device to copy the whole partition.

When Savehd7 is called it asks for the device file which should be used as input:

root@penguin:~# /home/tm/seed7_5/prg/savehd7
Savehd7 Version 2.0 - Save a possibly damaged harddisk partition
Copyright (C) 2006, 2009 Thomas Mertes
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Savehd7 is written in the Seed7 programming language
Homepage: http://seed7.sourceforge.net
Use 'savehd7 -h' to get more information

Please enter the conditions to save the partition:
  Input file name:

After entering e.g. /dev/sda3 (and pressing enter) it asks for the output file:

  Output file name:

After entering e.g. sda3 (and pressing enter) it writes information about the current state and asks for confirmation:

Conditions to save the partition:
  Input file name:  /dev/sda3
  Input file size:  60620313600
  Output file name: sda3
  State:            Nothing saved

Should the save start (type 'Yes' to confirm)?

After entering Yes (and pressing enter) it starts the saving process:

Processing - Press any key to pause (may take some time to react)
        progress:      okay:  bad blks:     fixed:   bad bytes:
copy      0.0000%    0.0000%    0.0000%    0.0000%            0

The last line is updated to document the progress:

Processing - Press any key to pause (may take some time to react)
        progress:      okay:  bad blks:     fixed:   bad bytes:
copy      2.6568%    2.6568%    0.0000%    0.0000%            0

The terms of the progress description are:

copy The current saving phase. Phases are: copy, improve, examine and fix.
progress The progress of the current phase.
okay Percentage of data found okay.
bad blks Percentage of bad blocks found.
fixed Percentage of fixed potential bad blocks.
bad bytes Bytes in potential bad areas.

Operating systems usually retry to read bad blocks again and again in the hope to succeed finally. Therefore copying from a damaged harddisk can take very long (many hours even up to several days). When Savehd7 is processing it can be interrupted by pressing any key:

Processing - Press any key to pause (may take some time to react)
        progress:      okay:  bad blks:     fixed:   bad bytes:
copy     11.5099%   11.5009%    0.0089%    0.0000%      5431296

Copy paused - To continue restart the program

root@penguin:~#

Since the OS spends so much time reading bad blocks it may take some time until Savehd7 has a chance to save the processing state and exit afterwards. Savehd7 should not be interrupted with control-C or some other signal, since this prevents saving the processing state. When Savehd7 is restarted it is prepared to continue at the position where it was interrupted:

root@penguin:~# /home/tm/seed7_5/prg/savehd7
Savehd7 Version 2.0 - Save a possibly damaged harddisk partition
Copyright (C) 2006, 2009 Thomas Mertes
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Savehd7 is written in the Seed7 programming language
Homepage: http://seed7.sourceforge.net
Use 'savehd7 -h' to get more information

Conditions to save the partition:
  Input file name:  /dev/sda3
  Input file size:  60620313600
  Output file name: sda3
  Output file size: 6977368064
  State:            Copy - 11.5099% done
  Total bad bytes:  5431296

Should the bad areas be listed (Y/N/Q)?

When answering 'Y' Savehd7 writes a list of potential bad areas:

List of bad areas:
   position   size
  1610678273 983040
  1744863233 1015808
  1879117825 978944
  2147618817 913408
  2281832449 917504
  2416001025 61440
  2550255617 24576
  2818605057 110592
  2952847361 86016
  3087093761 57344
  3221270529 98304
  3355521025 65536
  3489746945 57344
  6442532865 61440

Should the save continue (type 'Yes' to confirm)?

After entering Yes Savehd7 continues the saving process.

Operation method

The file "savehd7.dat" is used to maintain the processing state. Savehd7 writes also logging information to the file "savehd7.log".

Savehd7 saves the harddisk partition in several phases:

  • During the 'copy' phase a sequential copy with chunks of 16384 Bytes (2 ** 14 Bytes) takes place. When the 'read' function of the operating system returns a partial chunk, the rest of the destination chunk is filled with zero bytes and marked as potential bad area. The processing continues 1 MiB (2 ** 20 Bytes) forward and the bytes in the gap are also filled with zero bytes and marked as potential bad area. Bad areas are maintained in the file "savehd7.dat". The big skip size of 1 MiB is used to jump over hopefully small areas with real bad blocks. This way a good deal of the data is copied in a relatively small period.
  • During the optional 'reread' phase chunks of 16384 Bytes are read sequentially and compared to the chunks read by the 'copy' phase. This comparison is done with blocks of 512 Bytes. When both elements are not identical the corresponding 512 Byte block is marked as potential bad area. When an element is only present in one of the phases ('copy' and 'reread') the existing element is assumed to be okay.
  • During the 'improve' phase potential bad areas are processed one by one. Savehd7 reads 512 Byte blocks from the end of each potential bad area towards its beginning. The processing of a potential bad block area is terminated when a read operation fails. In this case the processing moves to the next potential bad block area. The 'improve' phase reduces the size of the areas in the bad area list by a high percentage.
  • During the 'examine' phase potential bad areas are processed one by one. Savehd7 reads 512 Byte blocks from the middle of each potential bad area towards its end and its beginning. At first blocks are read forward until a read operation fails or the end is reached. Then blocks are read backward until a read operation fails or the beginning is reached. If the potential bad area was split into two both new areas are processed in the same way. If not, the processing moves to the next potential bad block area.
  • During the 'fix' phase the potential bad areas are processed one by one. Savehd7 reads 512 Byte blocks from the end of each potential bad area towards its beginning. The processing of a potential bad block area is continued even when read operations fail. This way Savehd7 attempts to read blocks of 512 Byte in all areas of the bad area list. Since this phase reads small blocks in areas with many potential bad blocks it takes extremely long.

Download

Savehd7 is written in the Seed7 programming language and can be downloaded as part of the Seed7 package. To use savehd7 it is necessary to compile the Seed7 interpreter. Afterwards Savehd7 can be started from the directory 'seed7/prg' with:

s7 savehd7

Savehd7 can be also compiled with:

s7c savehd7

This creates an executable which can be used without the 's7' interpreter.


Savehd7 called with -h

Start saving a partition

Pause and restart Savehd7

Improve phase

Complete run

 previous   up   next