This commit is contained in:
gferg 2003-11-03 20:14:53 +00:00
parent 0b3f742729
commit 34a6cc6d3e
3 changed files with 353 additions and 37 deletions

View File

@ -3380,7 +3380,7 @@ Teaches you how to plan and layout disk space for your Linux system. </Para>
Partition-Rescue</ULink>,
<CiteTitle>Partition Rescue mini HOWTO</CiteTitle>
</Para><Para>
<CiteTitle>Updated: Aug 2002</CiteTitle>.
<CiteTitle>Updated: Oct 2002</CiteTitle>.
Describes how to rescue your Linux partition if it has
been deleted. </Para>
</ListItem>

View File

@ -921,7 +921,7 @@ Partition-Rescue</ULink>, <CiteTitle>
Partition Rescue mini HOWTO</CiteTitle>
</Para><Para>
<CiteTitle>
Updated: Aug 2002</CiteTitle>.
Updated: Oct 2002</CiteTitle>.
Describes how to rescue your Linux partition if it has
been deleted. </Para>
</ListItem>

View File

@ -1,15 +1,28 @@
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
<article lang="en">
<articleinfo>
<title>Partition-Rescue HOWTO</title>
<author>
<firstname>Jean-Daniel</firstname>
<surname>Dodin</surname>
</author>
<date>2002-08-22</date>
<date>2003-10-31</date>
<abstract>
<para>
Whow&nbsp;! my disk is empty&nbsp;! My Linux is gone&nbsp;! If you have
or fear to have one day or an other such a problem, read this...
</para>
</abstract>
<revhistory>
<revision>
<revnumber>v3.5</revnumber>
<date>2003-10-31</date>
<authorinitials>jdd</authorinitials>
<revremark>Major update - new licence - some fixes in addresses - vi use :-)</revremark>
</revision>
<revision>
<revnumber>v3.4</revnumber>
<date>2002-08-22</date>
<authorinitials>jdd</authorinitials>
@ -28,21 +41,104 @@
<revremark>Major update.</revremark>
</revision>
</revhistory>
<abstract>
<para>
Whow&nbsp;! my disk is empty&nbsp;! My Linux is gone&nbsp;! If you have
or fear to have one day or an other such a problem, read this...
</para>
</abstract>
</articleinfo>
<sect1><title>Beginning</title><sect2><title>What's in</title><para>This mini-HOWTO addresses only the &ldquo;lost partition table&rdquo; problem. This can be when&nbsp;:</para><itemizedlist><listitem><para>you have no more access to your computer, with the &ldquo;no operating system&rdquo; message&nbsp;;</para></listitem><listitem><para>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&nbsp;;</para></listitem><listitem><para>you have just partitioned the wrong drive with fdisk (for example in the way of changing your hard drive).</para></listitem></itemizedlist><para>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.</para><para>We will first see what you can do <emphasis>before</emphasis> the problem to ease the future recovery and what you must do <emphasis>after</emphasis> 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&nbsp;?</para><para>It can also be done by the use of MSDOS/Windows fdisk. Avoid it as most as you can, but you probably can't.</para><para>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&nbsp;!</para></sect2>
<sect2><title>What to do right now&nbsp;?</title><para>If you don't have a problem yet, if you read this by curiosity or just seeking information and you are on a running Linux system, do immediately the following&nbsp;:</para><itemizedlist><listitem><para>open a root terminal or xterm&nbsp;;</para></listitem><listitem><para>key in &ldquo;/sbin/fdisk -l&rdquo; (that last character being l for Lima). Then do fdisk -u -l&nbsp;;</para></listitem></itemizedlist><para>You will be gratified by a list of all current partitions on all disks presents on your computer. The second one gives the listing in sectors units in place of cylinder and this is sometime necessary.</para><itemizedlist><listitem><para>Write this back on paper (or do &ldquo;/sbin/fdisk -l | lpr&rdquo; and &ldquo;/sbin/fdisk -u -l | lpr&rdquo; to print it) and save it in a safe place for future use.</para></listitem></itemizedlist><para>If you are not the system administrator, you should not be concerned by the problem and can stop reading this.</para></sect2>
<sect2><title>Legal stuff</title><para>This 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.</para><para>In fact I would like to use a more standard LGPL or like licence, but I'm bound by the initial author license and I can't join him. If you know him or any way to contact him, let me know.</para><para>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_! Be careful when partitioning disks, and don't make any mistakes, because it can be fatal! Backup all your important data and check that everything you do is correct! 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: BACKUP IMPORTANT DATA! Or, to put it short: Use at your own risk!</para></sect2>
<sect2><title>What do I must know right now?</title><para>You <emphasis>must</emphasis> know that in case of any major problem with your hard disk, you <emphasis>must</emphasis> stop using it at all in <emphasis>write</emphasis> mode, at least the time necessary to understand what happens. Information there is very volatile...</para><para>If ever, one morning, awaking, you computer say &ldquo;can't load, no system installed&rdquo;, you <emphasis>must not begin reinstalling all the stuff</emphasis>.</para><para>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.</para><para>You must also know that I give you all this information only for this -information 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.</para></sect2>
<sect1> <title>Beginning</title>
<sect2><title>What's in</title>
<para>This HOWTO addresses only the "lost partition table" problem. This can be when&nbsp;:</para>
<itemizedlist>
<listitem><para>you have no more access to your computer, with the "no operating system" message&nbsp;;</para></listitem>
<listitem><para>you have installed a new system (i.e. MS Windows) and you see no more Linux, and MS Windows take up all the capacity of the disk&nbsp;;</para></listitem>
<listitem><para>you have just partitioned the wrong drive with fdisk (for example in the way of changing your hard drive).</para></listitem>
</itemizedlist>
<para>Here you will learn that if you know the right thing and do it, Linux comes usually safe from such things. MS Windows can, but it's luckier.</para>
<para>We will first see what you can do <emphasis>before</emphasis> the problem to ease the future recovery and what you must do <emphasis>after</emphasis> to recover. There is little to do to prevent from erasing a disk, usually this is done by automatic MS 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&nbsp;?</para>
<para>It can also be done by the use of MSDOS/Windows fdisk. Avoid it as most as you can, but you probably can't.</para>
<para>I have done this many times, on my computer and on others guy computers and restored Linux most of the time and MS Windows sometimes. I wish you luck&nbsp;!</para>
</sect2>
<sect2><title>What to do right now&nbsp;?</title>
<para>
If you don't have any problem yet, if you read this by curiosity or just seeking information and you are on a running Linux system, do immediately the following&nbsp;:</para>
<itemizedlist>
<listitem><para>open a root terminal or xterm&nbsp;;</para></listitem>
<listitem><para>key in &ldquo;/sbin/fdisk -l&rdquo; (that last character being l for Lima). Then do fdisk -u -l&nbsp;;</para></listitem>
</itemizedlist>
<para>You will be gratified by a list of all current partitions on all disks presents on your computer. The second one gives the listing in sectors units in place of cylinder and this is sometime necessary.</para>
<itemizedlist>
<listitem><para>Write this back on paper (or do "/sbin/fdisk -l | lpr" and "/sbin/fdisk -u -l | lpr" to print it) and save it in a safe place for future use.</para></listitem>
</itemizedlist><para>If you are not the system administrator, you should not be concerned by the problem and can stop reading this.</para>
</sect2>
<sect2><title>Legal stuff</title>
<para>This HOWTO is Copyright (c) 2000-2001 by Jean-Daniel Dodin.</para>
<para>As of November 2003, the licence is LGPL.</para>
<para>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_! Be careful when partitioning disks, and don't make any mistakes, because it can be fatal&nbsp;! Backup all your important data and check that everything you do is correct&nbsp;! 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: BACKUP IMPORTANT DATA&nbsp;! Or, to put it short: Use at your own risk&nbsp;!</para>
</sect2>
<sect2><title>What do I must know right now?</title>
<para>You <emphasis>must</emphasis> know that in case of any major problem with your hard disk, you <emphasis>must</emphasis> stop using it at all in <emphasis>write</emphasis> mode, at least the time necessary to understand what happens. Information there is very volatile...</para>
<para>If ever, one morning, awaking, you computer say "can't load, no system installed", you <emphasis>must not begin reinstalling all the stuff</emphasis>.</para>
<para>If you have MS 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 (near the beginning of the disk) in the disk structure. This is because some MS 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.</para>
<para>You must also know that I give you all this information only for this - information 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.</para>
</sect2>
</sect1>
<sect1><title>Technical info</title><sect2><title>Disks</title><para>A hard disk is made of sectors numbered from 0 to the max.</para><para>dmesg gives, for example:</para><programlisting><![CDATA[hdb: ST34321A, 4103MB w/128kB Cache, CHS=523/255/63
]]></programlisting><para>CHS means Cylinders, Heads, Sectors.</para><para>523*255*63=8401995 sectors of 512 bytes, thus the 4103&nbsp;Mbytes. This is only a logical map, it's not necessarily what is written on the disk cover (except for the total size).</para><para>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.</para><para>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 &ldquo;yes&rdquo;. Anyway, any modification at this level may destroy all the data of the disk, so don't play with this without essential reason. </para><para>This is probably what your disk use, so don't be afraid.</para></sect2>
<sect2><title>Partitions</title><para>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.</para><para>So a hard disk is usually cut in some pieces called &ldquo;partitions&rdquo; (see the &ldquo;partition HOWTO&rdquo; for details, also read /usr/share/doc/package/util/README.fdisk)</para><para>Let's get a look at (part of) my own print of fdisk -l&nbsp;:</para><programlisting><![CDATA[Disk /dev/hdb: 255 heads, 63 sectors, 523 cylinders
<sect1><title>Technical info</title>
<sect2><title>Disks</title>
<para>
A hard disk is made of sectors numbered from 0 to the max.</para><para>dmesg gives, for example:</para>
<programlisting>
<![CDATA[hdb: ST34321A, 4103MB w/128kB Cache, CHS=523/255/63
]]>
</programlisting>
<para>
CHS means Cylinders, Heads, Sectors.
</para>
<para>523*255*63=8401995 sectors of 512 bytes, thus the 4103&nbsp;Mbytes. This is only a logical map, it's not necessarily what is written on the disk cover (except for the total size).</para>
<para>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.</para>
<para>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 "yes". Anyway, any modification at this level may destroy all the data of the disk, so don't play with this without essential reason. </para>
<para>This is probably what your disk use, so don't be afraid.</para>
</sect2>
<sect2><title>Partitions</title>
<para>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 MS Windows do so, and if you use Linux, may be it's because you are aware of how inefficient the other is.</para>
<para>So a hard disk is usually cut in some pieces called "partitions" (see the "partition HOWTO" for details, also read /usr/share/doc/package/util/README.fdisk)</para>
<para>Let's get a look at (part of) my own print of fdisk -l&nbsp;:</para>
<programlisting>
<![CDATA[Disk /dev/hdb: 255 heads, 63 sectors, 523 cylinders
]]><![CDATA[Units = cylinders of 16065 * 512 bytes
]]><![CDATA[Device Boot Start End Blocks Id System
]]><![CDATA[/dev/hdb1 1 153 1228941 83 Linux
@ -51,7 +147,21 @@
]]><![CDATA[/dev/hdb4 295 523 1839442+ 5 Extended
]]><![CDATA[/dev/hdb5 295 422 1028128+ 83 Linux
]]><![CDATA[/dev/hdb6 423 523 811251 6 FAT16
]]></programlisting><para>This is my second hard disk, tied to guesses and tries (the first is too simple to be interesting).</para><para>/dev/hdb is my second ide disk (slave on the primary interface),</para><para>/dev/hdb1 is the first primary partition, running from the first (1) block to the block 153.</para><para>There can be four of such primary partitions. If one wants more than 4, one of them must be an &ldquo;extended&rdquo; one (not necessarily the fourth) and all other partitions are named &ldquo;logical&rdquo; and are located <emphasis>inside</emphasis> 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.</para><para>Here the fdisk -u -l listing of an other disk:</para><programlisting><![CDATA[Disque /dev/hda : 240 têtes, 63 secteurs, 2584
]]>
</programlisting>
<para>This is my second hard disk, tied to guesses and tries (the first is too simple to be interesting).</para>
<para>/dev/hdb is my second ide disk (slave on the primary interface),</para>
<para>/dev/hdb1 is the first primary partition, running from the first (1) block to the block 153.</para>
<para>There can be four of such primary partitions. If one wants more than 4, one of them must be an "extended" one (not necessarily the fourth) and all other partitions are named "logical" and are located <emphasis>inside</emphasis> 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.</para>
<para>Here the fdisk -u -l listing of an other disk:</para>
<programlisting>
<![CDATA[Disque /dev/hda : 240 têtes, 63 secteurs, 2584
]]><![CDATA[cylindres Unités = secteurs sur 1 * 512 octets
]]><![CDATA[Périphérique Amorce Début Fin Blocs Id Système
]]><![CDATA[/dev/hda1 * 63 10357199 5178568+ c Win95 FAT32 (LBA)
@ -62,21 +172,153 @@
]]><![CDATA[/dev/hda6 10463103 10780559 158728+ 82 Echange Linux
]]><![CDATA[/dev/hda7 10780623 15150239 2184808+ 6 FAT16
]]><![CDATA[Les entrées de la table de partitions ne suivent pas l'ordre du disque.
]]></programlisting><para>Don't worry for the french part, I'm french... look at your own disk listing. Of course numbers are bigger.</para></sect2>
<sect2><title>Why is there a problem&nbsp;?</title><para>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 &ldquo;partition table&rdquo; 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.</para><para>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 &ldquo;automatic partitioning?&rdquo; is likely to give problems... This is specially true with Windows, especially with custom windows installation made by special makes PC's (when no true &ldquo;Windows&rdquo; CD is included, as with many laptops). But it's also true with new &ldquo;smart&rdquo; (not so smart!) Linux installation programs given with some recent distributions.</para></sect2>
]]>
</programlisting>
<para>Don't worry for the french part, I'm french... look at your own disk listing. Of course numbers are bigger.</para>
</sect2>
<sect2><title>Why is there a problem&nbsp;?</title>
<para>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 "partition table" 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.</para>
<para>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 &ldquo;automatic partitioning?&rdquo; is likely to give problems... This is specially true with MS Windows, especially with custom MS Windows installation made by special makes PC's (when no true "Windows" CD is included, as with many laptops). But it's also true with new &ldquo;smart&rdquo; (not so smart!) Linux installation programs given with some recent distributions.</para>
</sect2>
</sect1>
<sect1><title>Solving the problem</title><para>Please, beware! following the explanations given here will lead you to turn back to a previous system, loosing all your recent installed one, if any! You must choose...</para><sect2><title>The simpler case</title><para>All is simple if you have at hand&nbsp;:</para><itemizedlist><listitem><para>a disk (floppy or CD) able to start Linux by itself with fdisk available - most rescue disks of any distribution can do that,</para></listitem><listitem><para>a paper with the fdisk -l and fdisk -u -l content written down.</para></listitem></itemizedlist><para>It's enough to</para><orderedlist><listitem><para>start Linux&nbsp;;</para></listitem><listitem><para>start fdisk /dev/hda (or whatever is the disk to rescue)&nbsp;;</para></listitem><listitem><para>use fdisk to delete (d option) all the existing partitions on the damaged disk&nbsp;;</para></listitem><listitem><para>use fdisk to create all the primary (1-4) partition mentioned on the paper&nbsp;;</para></listitem><listitem><para>give them the appropriate tag (t option)&nbsp;: 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, c is Windows Fat 32 and f Windows extended when 6 is Windows Fat 16.</para></listitem><listitem><para>create any logical partition.</para></listitem></orderedlist><para>On my SuSE installation and anytime I had to do this for other peoples, this gives a good result.</para><para>However I was said that some fdisk may cut partitions on a sector basis, not cylinder. So the fdisk -u -l version of the paper.</para><para>For using the fdisk -u -l listing one must start fdisk -u :-). On my opinion, using sector limit is a very bad idea, but it may have a real use I'm not aware of. Problem is that with cylinder limit it's easy to guess even if you don't have paper. With sector one, there a many more guesses...</para><para>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...).</para><para>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 (^C) to quit safe.</para><para>When your new partition table is written, start your Linux. Chance is you can't do that as usual: lilo can have been damaged also and you will need a boot floppy or booting from a CD (choose the option &ldquo;booting the installed partition&rdquo;).</para><para>If you use to boot with lilo, as soon as you are logged in as root, key in &ldquo;lilo&rdquo; and hit return to reinstall you favorite boot loader.</para><para>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.</para></sect2>
<sect2><title>A not so simple case</title><sect3><title>By hand</title><para>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.</para><para>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.</para><para>This can be tiresome, but if you remember approximately the size of the Linux partition, there is a chance to win.</para></sect3>
<sect3><title>Linux own info and other hacks</title><sect4><title>Kernel</title><para>If you just destroyed your own partition table, but have not rebooted Linux: Don't reboot! You can still retrieve the partition information stored in the Kernel:</para><para>cat /proc/partitions gives</para><literallayout>major minor&nbsp; #blocks&nbsp; name&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 0&nbsp;&nbsp; 19535040 hda&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 1&nbsp;&nbsp;&nbsp; 2096451 hda1&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 2&nbsp;&nbsp;&nbsp; 4980150 hda2&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 hda3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <----- this marks an extended partition&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 5&nbsp;&nbsp;&nbsp; 4980118 hda5&nbsp;&nbsp;&nbsp; 3&nbsp;&nbsp;&nbsp;&nbsp; 6&nbsp;&nbsp;&nbsp; 4972086 hda6</literallayout></sect4>
<sect4><title>hdparm</title><literallayout>hdparm -g /dev/hda1/dev/hda1: geometry&nbsp;&nbsp;&nbsp;&nbsp; = 2432/255/63, sectors = 4192902, start = 63</literallayout><para>You'll need to do a few unit conversions. &quot;&num;blocks&quot; are usually 1K in length. &quot;Sectors&quot; are disk sectors, often 512 bytes. But usually the disk partitioning tools work in units of cylinders (here 255*63=16065 sectors). Using this information you can build a new partition table.</para></sect4>
<sect4><title>I know the start of the partition, but not the end.</title><para>If you know the start of a Linux partition, but not the end, you can still mount it, and learn about the structure. Set the partition table start correctly, and set the end to something very large. See if you guessed correctly with:</para><programlisting><![CDATA[e2fsck -n /dev/hd??
]]></programlisting><para>You can even mount the partition and check the size:</para><programlisting><![CDATA[mount -r /dev/hd?? /mnt
]]><![CDATA[df -T
]]></programlisting><para>This won't directly tell you where the next partition starts, because of rounding issues. But it can help you get close. Be sure to use the &quot;-n&quot; and &quot;-r&quot; flags to treat the system as read-only!!!</para></sect4>
<sect4><title>Other places partition information is stored</title><para>Some distributions record partition information in a file. Of course you probably won't be able to get to this file when you need it. But just in case:</para><programlisting><![CDATA[SuSE: /var/lib/YaST/install.inf
]]></programlisting><para>(if you are aware of others, please email the maintainer of this document)</para></sect4>
<sect1><title>Solving the problem</title>
<para>Please, beware&nbsp;! following the explanations given here will lead you to turn back to a previous system, loosing all your recent installed one, if any&nbsp;! You must choose...</para>
<sect2><title>The simpler case</title>
<para>All is simple if you have at hand&nbsp;:</para>
<itemizedlist>
<listitem>
<para>A disk (floppy or CD) able to start Linux by itself with fdisk available - most rescue disks of any distribution can do that&nbsp;;</para>
</listitem>
<listitem><para>A paper with the fdisk -l and fdisk -u -l content written down.</para></listitem>
</itemizedlist>
<para>It's enough to</para>
<orderedlist>
<listitem><para>Start Linux&nbsp;;</para></listitem>
<listitem><para>Start fdisk /dev/hda (or whatever is the disk to rescue)&nbsp;;</para></listitem>
<listitem><para>Use fdisk to delete (d option) all the existing partitions on the damaged disk&nbsp;;</para></listitem>
<listitem><para>Use fdisk to create all the primary (1&nbsp;-&nbsp;4) partition mentioned on the paper&nbsp;;</para></listitem>
<listitem><para>Give them the appropriate tag (t option)&nbsp;: 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, c is MS Windows Fat 32 and f MS Windows extended when 6 is MS Windows Fat 16.</para></listitem>
<listitem><para>Create any logical partition.</para></listitem>
</orderedlist>
<para>On my SuSE installation and anytime I had to do this for other peoples, this gives a good result.</para>
<para>However I was said that some fdisk may cut partitions on a sector basis, not cylinder. So the fdisk -u -l version of the paper.</para>
<para>For using the fdisk -u -l listing one must start fdisk -u :-). On my opinion, using sector limit is a very bad idea, but it may have a real use I'm not aware of. Problem is that with cylinder limit it's easy to guess even if you don't have paper. With sector one, there a many more guesses...</para>
<para>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...).</para>
<para>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 (^C) to quit safe.</para>
<para>When your new partition table is written, start your Linux. Chance is you can't do that as usual&nbsp;: lilo can have been damaged also and you will need a boot floppy or booting from a CD (choose the option "booting the installed partition").</para>
<para>If you use to boot with lilo, as soon as you are logged in as root, key in "lilo" and hit return to reinstall you favorite boot loader. Right now I'm not sure that the same thing is as easy with Grub, but it should not either be so difficult.</para>
<para>Your Linux should be all here, test it. Try also to start MS 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.</para>
</sect2>
<sect2><title>A not so simple case</title>
<sect3><title>By hand</title>
<para>This is when the previous case can't be used, for lack of fdisk paper or if it won't work for use of an out of date one.</para>
<para>First, be aware that as soon as you don't write to the disk (except with fdisk), you don'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.</para>
<para>This can be tiresome, but if you remember approximately the size of the Linux partition, there is a chance to win.</para>
</sect3>
<sect3><title>gpart</title><para>But there is a better way if you can still access the net or have &ldquo;gpart&rdquo; at hand. gpart is available in most distribution, by freshmeat.net or directly at <ulink url="http://www.stud.uni-hannover.de/user/76201/gpart/">http://www.stud.uni-hannover.de/user/76201/gpart</ulink>.</para><para>&ldquo;gpart - guess PC-type hard disk partitions&rdquo; is the first line of the man page of gpart (man gpart).</para><para>&ldquo;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.&rdquo; This is exactly what we need.</para><para>gpart is a very good tool.</para><para>The problem is the following: the first block of any partition is marked. But it's never &ldquo;unmarked&rdquo; if not overwritten. So many &ldquo;first partition block&rdquo; 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.</para><para>Here is the result of gpart on the previously seen disk hdb:</para><programlisting><![CDATA[root@charles:/home/jdd > gpart /dev/hdb
<sect3><title>Linux own info and other hacks</title>
<sect4><title>Kernel</title>
<para>If you just destroyed your own partition table, but have not rebooted Linux&nbsp;: Don't reboot! You can still retrieve the partition information stored in the Kernel:</para>
<para>cat /proc/partitions gives</para>
<literallayout>
major minor #blocks name
3 0 19535040 hda
3 1 2096451 hda1
3 2 4980150 hda2
3 3 1 hda3 &lt;----- this marks an extended partition
3 5 4980118 hda5
3 6 4972086 hda6
</literallayout>
</sect4>
<sect4><title>hdparm</title>
<literallayout>
hdparm -g /dev/hda1/dev/hda1&nbsp;:
geometry = 2432/255/63, sectors = 4192902, start = 63
</literallayout>
<para>You'll need to do a few unit conversions. &quot;blocks&quot; are usually 1K in length. &quot;Sectors&quot; are disk sectors, often 512 bytes. But usually the disk partitioning tools work in units of cylinders (here 255*63=16065 sectors). Using this information you can build a new partition table.</para>
</sect4>
<sect4><title>I know the start of the partition, but not the end.</title>
<para>If you know the start of a Linux partition, but not the end, you can still mount it, and learn about the structure. Set the partition table start correctly, and set the end to something very large. See if you guessed correctly with&nbsp;:</para>
<programlisting>
<![CDATA[e2fsck -n /dev/hd??
]]>
</programlisting>
<para>You can even mount the partition and check the size:</para>
<programlisting>
<![CDATA[mount -r /dev/hd?? /mnt
]]><![CDATA[df -T
]]></programlisting><para>This won't directly tell you where the next partition starts, because of rounding issues. But it can help you get close. Be sure to use the &quot;-n&quot; and &quot;-r&quot; flags to treat the system as read-only!!!</para>
</sect4>
<sect4><title>Other places partition information is stored</title>
<para>Some distributions record partition information in a file. Of course you probably won't be able to get to this file when you need it. But just in case:</para>
<programlisting>
<![CDATA[SuSE: /var/lib/YaST/install.inf
]]>
</programlisting>
<para>(if you are aware of others, please email the maintainer of this document)</para>
</sect4>
</sect3>
<sect3><title>gpart</title>
<para>But there is a better way if you can still access the net or have &ldquo;gpart&rdquo; at hand. gpart is available in most distribution, by freshmeat.net or directly at <ulink url="http://www.stud.uni-hannover.de/user/76201/gpart/">http://www.stud.uni-hannover.de/user/76201/gpart</ulink>.</para>
<para>&ldquo;gpart - guess PC-type hard disk partitions&rdquo; is the first line of the man page of gpart (man gpart).</para>
<para>&ldquo;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.&rdquo; This is exactly what we need.</para>
<para>gpart is a very good tool.</para>
<para>The problem is the following&nbsp;: the first block of any partition is marked. But it's never &ldquo;unmarked&rdquo; if not overwritten. So many &ldquo;first partition block&rdquo; 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.</para>
<para>Here is the result of gpart on the previously seen disk hdb&nbsp;:</para>
<programlisting>
<![CDATA[root@charles:/home/jdd > gpart /dev/hdb
]]><![CDATA[Begin scan...
]]><![CDATA[Possible partition(Linux ext2), size(1200Mb), offset(0Mb)
]]><![CDATA[Possible partition(Windows NTFS), size(1200Mb), offset(1200Mb)
@ -105,16 +347,90 @@
]]><![CDATA[chs: (306/61/49)-(434/60/47)d (306/61/49)-(434/60/47)r
]]><![CDATA[Primary partition(4)
]]><![CDATA[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
]]></programlisting><para>As you see, primary partition can be recovered, but for extended ones it's still to be done.</para><para>Dos partitions are labeled &ldquo;windows NTFS&rdquo; because they were created while trying to install Windows 2000 (a very awful experience!). The &ldquo;invalid&rdquo; one is, in fact the extended partition.</para><para>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).</para><para>gpart is updated on a weekly basis :-) and so new makes may be more powerful than I know.</para><sect4><title>Recovering partitions inside an extended partition</title><para>Extended partition information is scattered on the disk, not stored with the primary partition. To recover these often requires more works. The process is:</para><orderedlist><listitem><para>Scan for the start of the first partition (using gpart's -k option);</para></listitem><listitem><para>Create a temporary primary partition entry with the true start position and a fake end position (this may drive you to delete an actual primary partition if no one is available - this is risk free if you don't reuse the sectors of the deleted partition);</para></listitem><listitem><para>Use &ldquo;e2fsk -n&rdquo;, &ldquo;mount -r&rdquo;, and &ldquo;df&rdquo; to determine the true end point. Write this value down (warning:read the man page for each program mentioned, and use the read-only options, you do not want to write to your disk until all partitions are in the correct place);</para></listitem><listitem><para>Repeat this process for each partition to be recovered;</para></listitem><listitem><para>Build a complete new correct partition table.</para></listitem></orderedlist></sect4>
<sect4><title>If your hard drive has errors</title><para>If your hard drive has errors, you may have real trouble mounting, checking or using data (the drive read errors get in the way). Gpart may not even find it. But if you know the start of the partition, you can easily copy the data to a temporary file stored on a different drive. Sectors with read errors will usually be set to zero by this process:</para><itemizedlist><listitem><para>Copy the partition data to a file. You must know the start block of the partition;</para></listitem></itemizedlist><programlisting><![CDATA[dd if=/dev/hd?? of=/tmp/recover_hd?? bs=512 skip=XXXX count=YYY
]]></programlisting><para>XXX is the sector start and YYY the sector count (can be guessed).</para><itemizedlist><listitem><para>Mount the file as a loop file system.</para></listitem></itemizedlist><programlisting><![CDATA[mount -r -t ext2 -o loop /tmp/recover_hd?? /mnt/recover
]]></programlisting>
<para>As you see, primary partition can be recovered, but for extended ones it's still to be done.</para>
<para>Dos partitions are labeled &ldquo;windows NTFS&rdquo; because they were created while trying to install MS Windows 2000 (a very awful experience&nbsp;!). The &ldquo;invalid&rdquo; one is, in fact the extended partition.</para>
<para>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).</para>
<para>gpart is updated on a weekly basis :-) and so new makes may be more powerful than I know.</para>
<sect4><title>Recovering partitions inside an extended partition</title>
<para>Extended partition information is scattered on the disk, not stored with the primary partition. To recover these often requires more works. The process is&nbsp;:</para>
<orderedlist>
<listitem><para>Scan for the start of the first partition (using gpart's -k option)&nbsp;;</para></listitem>
<listitem><para>Create a temporary primary partition entry with the true start position and a fake end position (this may drive you to delete an actual primary partition if no one is available - this is risk free if you don't reuse the sectors of the deleted partition)&nbsp;;</para></listitem>
<listitem><para>Use &ldquo;e2fsk -n&rdquo;, &ldquo;mount -r&rdquo;, and &ldquo;df&rdquo; to determine the true end point. Write this value down (warning&nbsp;: read the man page for each program mentioned, and use the read-only options, you do not want to write to your disk until all partitions are in the correct place)&nbsp;;</para></listitem>
<listitem><para>Repeat this process for each partition to be recovered&nbsp;;</para></listitem>
<listitem><para>Build a complete new correct partition table.</para></listitem>
</orderedlist>
</sect4>
<sect4><title>If your hard drive has errors</title>
<para>If your hard drive has errors, you may have real trouble mounting, checking or using data (the drive read errors get in the way). Gpart may not even find it. But if you know the start of the partition, you can easily copy the data to a temporary file stored on a different drive. Sectors with read errors will usually be set to zero by this process:</para>
<itemizedlist>
<listitem><para>Copy the partition data to a file. You must know the start block of the partition;</para></listitem>
</itemizedlist>
<programlisting>
<![CDATA[dd if=/dev/hd?? of=/tmp/recover_hd?? bs=512 skip=XXXX count=YYY
]]>
</programlisting>
<para>XXX is the sector start and YYY the sector count (can be guessed).</para>
<itemizedlist>
<listitem><para>Mount the file as a loop file system.</para></listitem>
</itemizedlist>
<programlisting>
<![CDATA[mount -r -t ext2 -o loop /tmp/recover_hd?? /mnt/recover
]]></programlisting></sect4>
</sect3>
</sect2>
<sect2><title>The rich man case</title><para>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.</para><para>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.</para></sect2>
<sect2><title>The rich man case</title>
<para>
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.</para>
<para>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.</para>
</sect2>
</sect1>
<sect1><title>References</title><sect2><title>Authors</title><para>The author of this HOWTO is Jean-Daniel Dodin. I can be joined at <ulink url="mailto:jdanield@dodin.net">jdanield@dodin.net</ulink> or at <ulink url="mailto:jdanield@linux-france.org">jdanield@linux-france.org</ulink>.</para><para>My web site is at http://www.dodin.net. Linux pages, who are of most interest for you are found at www.linux-france.org/article/jdanield.</para><para>I want to thank Rolf Klausen (- not responding - E-mail: rolfk@romsdal.vgs.no http://www2.romsdal.vgs.no/&tilde;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.</para><para>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_&nbsp;!!! </para><para>I want also to thank Michail Brzitwa &lt;mb@ichabod.han.de&gt; for writing gpart&nbsp;!</para><para>Bryce Nesbitt &lt;bryce at obviously dot com&gt; did a very good job, &ldquo;Linux own info&rdquo; is from him as are some minor enhancements.</para></sect2>
<sect2><title>Most recent version</title><para>The most recent version of this mini-HOWTO shall be found on my Linux web site, at www.linux-france.org/article/jdanield/</para></sect2>
<sect1><title>References</title>
<sect2><title>Authors</title>
<para>The author of this HOWTO is Jean-Daniel Dodin. I can be joined at <ulink url="http://www.dodin.net/e-mail_codée.html">My masqued e-mail</ulink>.</para>
<para>My web site is at <ulink url="http://www.dodin.net">here</ulink>. Linux pages, who are of most interest for you are found at <ulink url="http://www.linux-france.org/article/jdanield">Linux-France</ulink>.</para>
<para>I want to thank Rolf Klausen who write the previous partition-rescue mini HOWTO even if I rewrite it almost entirely, he had first the good idea.</para>
<para>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_&nbsp;!!! </para>
<para>I want also to thank Michail Brzitwa (see web site in the text) for writing gpart&nbsp;!</para>
<para>Bryce Nesbitt &lt;bryce at obviously dot com&gt; did a very good job, "Linux own info" is from him as are some minor enhancements.</para>
</sect2>
<sect2><title>Most recent version</title>
<para>The most recent version of this mini-HOWTO shall be found on my Linux web site (see previous section).</para>
</sect2>
</sect1>