#LyX 1.1 created this file. For more info see http://www.lyx.org/
\lyxformat 218
\textclass linuxdoc
\language english
\inputencoding default
\fontscheme default
\graphics default
\paperfontsize 10
\spacing single 
\papersize Default
\paperpackage a4
\use_geometry 0
\use_amsmath 0
\paperorientation portrait
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\defskip medskip
\quotes_language english
\quotes_times 2
\papercolumns 1
\papersides 1
\paperpagestyle default

\layout Title
\added_space_top vfill \added_space_bottom vfill 
Partition Rescue mini HOWTO
\layout Author

Jean-Daniel Dodin
\layout Date

Revised June 1, 2001
\layout Abstract

Whow\SpecialChar ~
! my disk is empty\SpecialChar ~
! My Linux is gone\SpecialChar ~
! If you have or fear to have one
 day or an other such a problem, read this...
\layout Standard


\begin_inset LatexCommand \tableofcontents{}

\end_inset 


\layout Section

What's in
\layout Standard

This mini-HOWTO addresses only the 
\begin_inset Quotes eld
\end_inset 

lost partition table
\begin_inset Quotes erd
\end_inset 

 problem.
 This can be when\SpecialChar ~
:
\layout Itemize

you have no more access to your computer, with the 
\begin_inset Quotes eld
\end_inset 

no operating system
\begin_inset Quotes erd
\end_inset 

 message,
\layout Itemize

you have installed a new system (i.e.
 windows) and you see no more Linux, and windows take up all the capacity
 of the disk.
\layout Standard

Here you will learn that if you know the right thing and do it, Linux comes
 usually safe from such things.
 Windows can, but it's luckier.
\layout Standard

We will first see what you can do 
\emph on 
before
\emph default 
 the problem to ease the future recovery and what you must do 
\emph on 
after
\emph default 
 to recover.
 There is little to do to prevent from erasing a disk, usually this is done
 by automatic windows or Linux-install ill behaved programs or users mistakes
 - nothing can be done to prevent this except care, but you are already
 careful, isn't it\SpecialChar ~
?
\layout Standard

It can also be done by the use of MSDOS/Windows fdisk.
 avoid it as most as you can, but you probably can't.
\layout Standard

I have done this many times, on my computer and on others' guy computers
 and restored Linux most of the time and windows sometimes.
 I wish you luck\SpecialChar ~
!
\layout Section

What to do right now\SpecialChar ~
?
\layout Standard

If you read this by curiosity or just seeking information and you are on
 a running Linux system, do immediately the following\SpecialChar ~
:
\layout Itemize

open a root terminal or xterm,
\layout Itemize

key in 
\begin_inset Quotes eld
\end_inset 

/sbin/fdisk -l
\begin_inset Quotes erd
\end_inset 

 (that last character being l for Lima).
 
\layout Standard

you will be gratified by a list of all current partitions on all disks presents
 on your computer.
\layout Itemize

Write this back on paper (or do 
\begin_inset Quotes eld
\end_inset 

/sbin/fdisk -l | lpr
\begin_inset Quotes erd
\end_inset 

 to print it) and save it in a safe place for future use.
\layout Standard

If you are not the system administrator, you should not be concerned by
 the problem and can stop reading this.
\layout Section

Legal stuff
\layout Standard

This mini-HOWTO is Copyright (c) 2000-2001 by Jean-Daniel Dodin.
 All rights reserved.
 You may do (almost) whatever you want with it.
 I don't care.
 Just be sure to keep my name intact.
 I just hope it becomes useful for any person in the Linux community.
 
\layout Standard

I am not responsible of any damage on any computer as a result of anyone
 reading this HOWTO.
 If you do any damage, _it is YOUR fault, NOT MINE\SpecialChar ~
! Be careful when partitioning
 disks, and don't make any mistakes, because it can be fatal\SpecialChar ~
! Backup all
 your important data and check that everything you do is correct\SpecialChar ~
! What is
 described here worked on my computer, but it may or may not work on your
 computer.
 Although it should work for everyone, I can't guarantee anything.
 This is the last warning you get\SpecialChar ~
: BACKUP IMPORTANT DATA\SpecialChar ~
! Or, to put it
 short: Use at your own risk\SpecialChar ~
!
\layout Section

What do I must know right now\SpecialChar ~
?
\layout Standard

You 
\emph on 
must
\emph default 
 know that in case of any major problem with your hard disk, you 
\emph on 
must
\emph default 
 stop using it at all in 
\emph on 
write
\emph default 
 mode, at least the time necessary to understand what happens.
 Information there is very volatile...
\layout Standard

If ever, one morning, awaking, you computer say 
\begin_inset Quotes eld
\end_inset 

can't load, no system installed
\begin_inset Quotes erd
\end_inset 

, you 
\emph on 
must not begin reinstalling all the stuff
\emph default 
.
\layout Standard

If you have windows installed, I can't promise you can recover your data,
 but it's likely you will recover all your Linux stuff, provided it's not
 located too low in the disk structure.
 This is because some windows viruses erases the very first disk cylinder,
 whatever is on.
 However I didn't ever experiment such virus and can't say for sure.
 Try recovering anyway.
\layout Standard

You must also know that I give you all this information only for this -informati
on purpose.
 Neither I nor any other people but you can be held responsible for any
 problem your data can have using this info.
 There are too many different systems on the world for anybody being able
 to promise anything.
 I can only wish you luck and hope you, like me, will be happy recovering
 data.
\layout Section

Disks
\layout Standard

A hard disk is made of sectors numbered from 0 to the max.
\layout Standard

dmesg gives, for example\SpecialChar ~
:
\layout Standard

hdb: ST34321A, 4103MB w/128kB Cache, CHS=523/255/63
\layout Standard

CHS means Cylinders, Heads, Sectors.
\layout Standard

523*255*63=8401995 sectors of 512 bytes, thus the 4103\SpecialChar ~
Mbytes.
 This is only a logical map, it's not necessarily what is written on the
 disk cover (except for the total size).
\layout Standard

The true size of the sectors is of no interest for us given we don't want
 to modify anything but restore a previous state.
 For us, the default size given by fdisk is alright.
\layout Standard

The size seen by the system is directly dependent of the work of the BIOS
 (Basic Input/Output System - the PC's ROM).
 The mode of the hard disk indicated in the BIOS is essential.
 On a new disk, it's better to use BIOS automatic hard disk recognition
 and say 
\begin_inset Quotes eld
\end_inset 

yes
\begin_inset Quotes erd
\end_inset 

.
 Anyway, any modification at this level may destroy all the data of the
 disk, so don't play with this without essential reason.
 
\layout Standard

This is probably what your disk use, so don't be afraid.
\layout Section

Partitions
\layout Standard

Disk are now huge, 40 Gb are not rare, so that it's not really handy to
 have all this stuff packed in only one part.
 Only Windows do so, and if you use Linux, may be it's because you are aware
 of how inefficient the other is.
\layout Standard

So a hard disk is usually cut in some pieces called 
\begin_inset Quotes eld
\end_inset 

partitions
\begin_inset Quotes erd
\end_inset 

 (see the 
\begin_inset Quotes eld
\end_inset 

partition mini HOWTO
\begin_inset Quotes erd
\end_inset 

 for details, also read /usr/doc/package/util/README.fdisk)
\layout Standard

Let's get a look at (part of) my own print of fdisk -l\SpecialChar ~
:
\layout Standard

Disk /dev/hdb: 255 heads, 63 sectors, 523 cylinders Units = cylinders of
 16065 * 512 bytes 
\layout Standard

Device Boot Start End Blocks Id System 
\layout Standard

/dev/hdb1 1 153 1228941 83 Linux 
\layout Standard

/dev/hdb2 154 166 104422+ 82 Linux swap 
\layout Standard

/dev/hdb3 * 167 291 1004062+ 83 Linux 
\layout Standard

/dev/hdb4 295 523 1839442+ 5 Extended 
\layout Standard

/dev/hdb5 295 422 1028128+ 83 Linux 
\layout Standard

/dev/hdb6 423 523 811251 6 FAT16 
\layout Standard

This is my second hard disk, tied to guesses and tries (the first is too
 simple to be interesting).
\layout Standard

/dev/hdb is my second ide disk (slave on the primary interface),
\layout Standard

/dev/hdb1 is the first primary partition, running from the first (1) block
 to the block 153.
\layout Standard

There can be four of such primary partitions.
 If one wants more than 4, one of them must be an 
\begin_inset Quotes eld
\end_inset 

extended
\begin_inset Quotes erd
\end_inset 

 one (not necessarily the fourth) and all other partitions are named 
\begin_inset Quotes eld
\end_inset 

logical
\begin_inset Quotes erd
\end_inset 

 and are located 
\emph on 
inside
\emph default 
 the extended one.
 Notice that partition number 5 and partition number 4 have the same beginning.
 Number five is logical, number 4 extended.
 Logicals begin always at 5, even if there are not 4 primary ones.
\layout Section

Why is there a problem\SpecialChar ~
?
\layout Standard

Problem is all the installed Operating Systems must share the disks and
 as at start the bios only scan the first one, there must be a so called
 
\begin_inset Quotes eld
\end_inset 

partition table
\begin_inset Quotes erd
\end_inset 

 in the very beginning of this disk.
 This partition table is located in the Master Boot Record (MBR) side by
 side with the boot loader.
\layout Standard

Any misuse of the MBR by any of the OS's leads to problems.
 When trying to install any system, yes answer at a question like 
\begin_inset Quotes eld
\end_inset 

automatic partitioning\SpecialChar ~
?
\begin_inset Quotes erd
\end_inset 

 is likely to give problems...
 This is specially true with windows, especially with custom windows installatio
n made by special makes PC's (when no true 
\begin_inset Quotes eld
\end_inset 

windows
\begin_inset Quotes erd
\end_inset 

 cd is included).
 But it's also true with new 
\begin_inset Quotes eld
\end_inset 

smart
\begin_inset Quotes erd
\end_inset 

 (not so smart\SpecialChar ~
!) Linux installation programs given with some recent distribution
s.
\layout Section

Solving the problem
\layout Standard

Please, beware\SpecialChar ~
! following the explanations given here will lead you to turn
 back to a previous system, loosing all your recent installed one, if any\SpecialChar ~
!
 You must choose...
\layout Subsection

The simpler case
\layout Standard

All is simple if you have at hand\SpecialChar ~
:
\layout Itemize

a disk (floppy or cd) able to start Linux by itself with fdisk available
 - most rescue disks of any distribution can do that,
\layout Itemize

a paper with the fdisk -l content written down.
\layout Standard

It's enough to
\layout Enumerate

start Linux,
\layout Enumerate

start fdisk /dev/hda (or whatever is the disk to rescue),
\layout Enumerate

use fdisk to delete (d option) all the existing partitions on the damaged
 disk,
\layout Enumerate

use fdisk to create all the primary (1-4) partition mentioned on the paper,
\layout Enumerate

give them the appropriate tag (t option)\SpecialChar ~
: 82 is for Linux swap, 83 for Linux
 main (L gives you the list), 5 is extended and must be done before creating
 logical partitions.
\layout Enumerate

create any logical partition.
\layout Standard

fdisk is a small and very smart programs.
 There are many other makes of fdisk, but I always prefer the bare bone
 one (I speak of Linux ones, of course, not the others...).
\layout Standard

Be aware that fdisk doesn't write anything to disk before you hit w and
 return.
 In case you fear a mistake, hit q (quit) or Ctrl C (\i \^{ }
C) to quit safe.
\layout Standard

When your new partition table is written, start your Linux.
 Chance is you can't do that as usual\SpecialChar ~
: lilo can have been damaged also and
 you will need a boot floppy or booting from a cd (choose the option 
\begin_inset Quotes eld
\end_inset 

booting the installed partition
\begin_inset Quotes erd
\end_inset 

).
\layout Standard

If you use to boot with lilo, as soon as you are logged in as root, key
 in 
\begin_inset Quotes eld
\end_inset 

lilo
\begin_inset Quotes erd
\end_inset 

 and hit return to reinstall you favourite boot loader.
\layout Standard

Your Linux should be all here, test it.
 Try also to start windows if applicable.
 If you can't, there is a (very little) chance you can read your data from
 Linux, may be with a raw sector by sector read.
 If you can identify the disk sectors you data is on, using dd can copy
 them on a file.
 This is wise for text only.
 This recovery is NOT in the scope of this mini-HOWTO.
\layout Subsection

A not so simple case
\layout Subsubsection

By hand
\layout Standard

This is when the previous case can't be used, for lack of fdisk paper or
 if it won't run for use of an out of date one.
\layout Standard

First, be aware that as soon as you don't write to the disk (except with
 fdisk), you can't erase your data, so that you can use a block by block
 try.
 That is you need to know the beginning of the partition to start it.
 If, say a 153 don't fit, try a 154, and so on.
\layout Standard

This can be tiresome, but if you remember approximately the size of the
 Linux partition, there is a chance to win.
\layout Subsubsection

gpart
\layout Standard

But there is a better way if you can still access the net or have 
\begin_inset Quotes eld
\end_inset 

gpart
\begin_inset Quotes erd
\end_inset 

 at hand.
\layout Standard


\begin_inset Quotes eld
\end_inset 

gpart - guess PC-type hard disk partitions
\begin_inset Quotes erd
\end_inset 

 is the first line of the man page of gpart (man gpart).
\layout Standard


\begin_inset Quotes eld
\end_inset 

gpart tries to guess which partitions are on a hard disk.
 If the primary partition table has been lost, overwritten or destroyed
 the partitions still exist on the disk but the operating system cannot
 access them.
\begin_inset Quotes eld
\end_inset 

.
 This is exactly what we need.
\layout Standard

gpart is a very good tool.
\layout Standard

The problem is the following\SpecialChar ~
: the first block of any partition is marked.
 But it's never 
\begin_inset Quotes eld
\end_inset 

unmarked
\begin_inset Quotes erd
\end_inset 

 if not overwritten.
 So many 
\begin_inset Quotes eld
\end_inset 

first partition block
\begin_inset Quotes erd
\end_inset 

 are existing on an old disk and gpart tries to do it's best guessing what
 is the good one.
 In fact it's not too difficult to try, nothing is written on the disk by
 gpart.
\layout Standard

Here is the result of gpart on the previously seen disk hdb\SpecialChar ~
:
\layout Standard

root@charles:/home/jdd > gpart /dev/hdb
\layout Standard

Begin scan...
 
\layout Standard

Possible partition(Linux ext2), size(1200Mb), offset(0Mb) 
\layout Standard

Possible partition(Windows NTFS), size(1200Mb), offset(1200Mb) 
\layout Standard

Possible partition(Linux ext2), size(1004Mb), offset(2402Mb) 
\layout Standard

Possible partition(Windows NTFS), size(1600Mb), offset(4102Mb) 
\layout Standard

End scan.
\layout Standard

Checking partitions...
\layout Standard

* Warning: partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX) ends beyond
 disk end .
 
\layout Standard

Partition(Linux ext2 filesystem): primary 
\layout Standard

Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): primary 
\layout Standard

Partition(Linux ext2 filesystem): primary 
\layout Standard

Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): invalid primary 
\layout Standard

Ok.
\layout Standard

Guessed primary partition table:
\layout Standard

Primary partition(1) 
\layout Standard

type: 131(0x83)(Linux ext2 filesystem) 
\layout Standard

size: 1200mb #s(2457880) s(63-2457942) 
\layout Standard

chs: (0/1/1)-(152/254/61)d (0/1/1)-(152/254/61)r 
\layout Standard

Primary partition(2) 
\layout Standard

type: 007(0x07)(OS/2 HPFS, NTFS, QNX or Advanced UNIX) 
\layout Standard

size: 1200mb #s(2457880) s(2457944-4915823) 
\layout Standard

chs: (152/254/63)-(305/253/60)d (152/254/63)-(305/253/60)r 
\layout Standard

Primary partition(3) 
\layout Standard

type: 131(0x83)(Linux ext2 filesystem) 
\layout Standard

size: 1004mb #s(2056256) s(4919781-6976036) 
\layout Standard

chs: (306/61/49)-(434/60/47)d (306/61/49)-(434/60/47)r 
\layout Standard

Primary partition(4) 
\layout Standard

type: 000(0x00)(unused) size: 0mb #s(0) s(0-0) chs: (0/0/0)-(0/0/0)d (0/0/0)-(0/
0/0)r 
\layout Standard

As you see, primary partition can be recovered, but for extended ones it's
 still to be done.
\layout Standard

Dos partitions are labelled 
\begin_inset Quotes eld
\end_inset 

windows NTFS
\begin_inset Quotes erd
\end_inset 

 because they were created while trying to install Windows 2000 (a very
 awful experience\SpecialChar ~
!).
 The 
\begin_inset Quotes eld
\end_inset 

invalid
\begin_inset Quotes erd
\end_inset 

 one is, in fact the extended partition.
\layout Standard

With this, one can use fdisk and try re-creating the partition table (remember,
 this is risk-free given the original one is already lost).
\layout Subsection

The rich man case
\layout Standard

Partition Magic is a commercial product, not so cheap given the little use
 one can have (approx a hundred bucks in France) but with a very high reputation
 all around there.
 However I never use it and will not rate it.
 It's said to be able to do anything with partitions, including restoring
 them.
\layout Standard

Original Ralf partition-rescue mini HOWTO was essentially based around the
 use of Partition Magic, so I presume it's a very good solution if you have
 valuable data on your Linux partition and little Linux capability.
 However there are now very recent makes of Partition Magic and I think
 it's better for you to read the manual.
\layout Section

Authors
\layout Standard

The author of this HOWTO is Jean-Daniel Dodin.
 I can be joined at jdanield@dodin.net or at jdanield@linux-france.org.
\layout Standard

My web site is at http://www.dodin.net.
 Linux pages, who are of most interest for you are found at www.linux-france.fr/ar
ticle/jdanield.
\layout Standard

I want to thank Rolf Klausen (E-mail: rolfk@romsdal.vgs.no http://www2.romsdal.vgs.no
/~rolfk/) who write the previous partition-rescue mini HOWTO even if I rewrite
 it almost entirely, he had first the good idea.
 In 2000 He is no more responding to his e-mail.
 If anybody knows how to contact him, please let me know.
\layout Standard

Every other member of the Linux community and everybody who supports Linux
 and writes documentation and programs for Linux and all the authors of
 the LDP and virtually any person involved in anything which has to do with
 Linux.
 Particularly Linus B.
 Torvalds - he is _The King_\SpecialChar ~
!!! 
\layout Standard

I want also to thank Michail Brzitwa <mb@ichabod.han.de> for writing gpart\SpecialChar ~
!
\layout Section

Most recent version
\layout Standard

The most recent version of this mini-HOWTO shall be found on my Linux web
 site, at www.linux-france.fr/article/jdanield/
\the_end
