LDP/LDP/howto/docbook/Partition-Rescue.xml

49 lines
27 KiB
XML
Raw Permalink Normal View History

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.docbook.org/xml/4.2/docbookx.dtd"><article id="Partition-Rescue"><articleinfo><title>Partition-Rescue</title><revhistory id="revhistory"><revision><revnumber>1</revnumber><date>2008-11-24 09:27:50</date><authorinitials>jdd</authorinitials><revremark>mainly title change in the wiki</revremark></revision></revhistory></articleinfo><para><emphasis role='strong'>"Partition-Rescue HOWTO" - Jean-Daniel Dodin</emphasis> </para><section><title>Revision History</title><para>Revision V4.1 - 2008-11-24 - Revised by <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Title fix (the wiki have to get the exact name of the HOWTO)</para><para> Revision v4 - 2008-08-29 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Major update - First complete revised wiki version</para><para> Revision v3.7 - 2008-05-30 - Revised by: <ulink url='http://wiki.tldp.org/rm#'>rm</ulink></para><para> Minor update - lots of little punctuation, spelling, usage, and grammar nits :-)</para><para> Revision v3.6 - 2008-05-25 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Major update - LDP wiki - addition about logical partitions</para><para> Revision v3.5 - 2003-10-31 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Major update - new licence - some fixes in addresses - vi use :-)</para><para> Revision v3.4 - 2002-08-22 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Minor update related only with docbook</para><para> Revision v3.3 - 2001-11-17 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Minor update - docbook &amp; revision history - emacs use.</para><para> Revision v3.2 - 2001-09-25 - Revised by: <ulink url='http://wiki.tldp.org/jdd#'>jdd</ulink></para><para> Major update. </para><para>Whow! My disk is empty! My Linux is gone! If you have or fear to have one day or another such a problem, read this... </para></section><section><title>Beginning</title><section><title>What's in</title><para>This HOWTO addresses only the "lost partition table" problem. This can be when: </para><itemizedlist><listitem><para>you have no more access to your computer, with the "no operating system" message; </para></listitem><listitem><para>you have installed a new system (i.e., MS Windows) and you see no more Linux, and MS Windows takes up all the capacity of the disk; </para></listitem><listitem><para>you have just partitioned the wrong drive with fdisk (for example, in the process 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 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, aren't you? </para><para>It can also be done by the use of MS-DOS/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 other guys' computers, and restored Linux most of the time and MS Windows sometimes. I wish you luck! </para></section><section><title>What to do right now?</title><para>If you don't have any problem yet, if you read this by curiosity or are just seeking information, and you are on a running Linux system, do immediately the following : </para><itemizedlist><listitem><para>open a root terminal or xterm; </para></listitem><listitem><para>key in <command>/sbin/fdisk -l</command> (that last character being l for Lima). Then do <command>fdisk -u -l</command>; </para></listitem></itemizedlist><para>You will be gratified to see a list
2008-11-24 16:00:42 +00:00
Units = cylinders of 16065 * 512 bytes
Device Boot Start End Blocks Id System
/dev/hdb1 1 153 1228941 83 Linux
/dev/hdb2 154 166 104422+ 82 Linux swap
/dev/hdb3 * 167 291 1004062+ 83 Linux
/dev/hdb4 295 523 1839442+ 5 Extended
/dev/hdb5 295 422 1028128+ 83 Linux
/dev/hdb6 423 523 811251 6 FAT16]]></screen><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 such primary partitions. If one wants more than 4, one of them must be repurposed as an "extended" partition (not necessarily the fourth), and all other partitions are "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. Logical partitions' numbering always begins at 5, even if there are only 2 primary partitions. </para><para>Here's the <command>fdisk -u -l</command> listing of an other disk: </para><screen><![CDATA[Disque /dev/hda : 240 têtes, 63 secteurs, 2584
2008-11-24 16:00:42 +00:00
cylindres Unités = secteurs sur 1 * 512 octets
Périphérique Amorce Début Fin Blocs Id Système
/dev/hda1 * 63 10357199 5178568+ c Win95 FAT32 (LBA)
/dev/hda2 15452640 39070079 11808720 83 Linux
/dev/hda3 10357200 15150239 2396520 f Win95 Etdue (LBA)
/dev/hda4 15150240 15452639 151200 84 Lecteur C: caché OS/2
/dev/hda5 10357263 10463039 52888+ 83 Linux
/dev/hda6 10463103 10780559 158728+ 82 Echange Linux
/dev/hda7 10780623 15150239 2184808+ 6 FAT16
Les entrées de la table de partitions ne suivent pas l'ordre du disque.]]></screen><para>Don't worry about the French part, I'm French ... look at your own disk listing. Of course, numbers are bigger. </para></section><section><title>Why is there a problem?</title><para>The problem is that all installed operating systems must share the disks, and, since at start, the BIOS only scans the first one, there must be a so called "partition table" at 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, a "yes" answer at a question like "automatic partitioning?" is likely to give problems... This is specially true with MS Windows, and especially with custom MS Windows installations made by special makes' PCs (when no true "Windows" CD is included, as with many laptops). But it's also true with some "smart" (not so smart!) Linux installation programs included with some distributions (hopefully this is not more the case in 2008). </para></section></section><section><title>Solving the problem</title><para>Please, beware! Following the explanations given here will lead you to revert back to a previous system, losing all your recent changes, if any! You must choose... </para><section><title>The simpler case</title><para>All is simple if you have at hand: </para><itemizedlist><listitem><para>A disk (floppy, usb key 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 <command>fdisk -l</command> and <command>fdisk -u -l</command> content written down. </para></listitem></itemizedlist><para>It's enough to: </para><orderedlist numeration='arabic'><listitem><para>Start Linux; </para></listitem><listitem><para>Start <command>fdisk /dev/hda</command> (or whatever is the disk to rescue); </para></listitem><listitem><para>Use fdisk to delete (d option) all the existing partitions on the damaged disk; </para></listitem><listitem><para>Use fdisk to create all the primary (1 - 4) partitions mentioned on the paper; </para></listitem><listitem><para>Give them the appropriate tag (t option) : 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 FAT32, and f is MS Windows extended when 6 is MS Windows FAT16. </para></listitem><listitem><para>Create any logical partition. </para></listitem></orderedlist><para>On my SUSE installation and any time I had to do this for other people, this has produced good results. </para><para>However, I said that some fdisks may cut partitions on a sector basis, not cylinder. So the <command>fdisk -u -l</command> version of the paper. </para><para>For using the <command>fdisk -u -l</command> listing one must start <command>fdisk -u</command> :-). In my opinion, using sector limit is a very bad idea, but it may have a real use I'm not aware of. The problem is that, with cylinder limit, it's easy to guess even if you don't have paper. With sector one, there are 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 bones 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. If you fear a mistake, hit q (quit) or Ctrl C (^C) to quit safe without saving changes. </para><para>When your new partition table is written, start your Linux. It's possible you might not be able to do that as usual: lilo/grub may have been damaged also, and you thus may need a boot floppy or CD. Choose the option "booting the installed partition". </para><para>If you are accustomed to booting with lilo, as soon as you are logged in as root, key in "lilo" and hit return to reinstall your favourite boot loader. Right now, I'm not sure that the same thing is as easy with GRUB, but it
2008-11-24 16:00:42 +00:00
df -T]]></screen><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 "-n" and "-r" flags, to treat the system as read-only!!! </para></section><section><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><screen><![CDATA[SuSE: /var/lib/YaST/install.inf]]></screen><para>(if you are aware of others, please e-mail the maintainer of this document) </para></section><section><title>gpart</title><para>But there is a better way if you can still access the Net or have "gpart" on hand. gpart is available in most distribution, at <ulink url='http://freshmeat.net/'/> or directly from <ulink url='http://www.stud.uni-hannover.de/user/76201/gpart'/>. </para><para>Please note that gpart is <emphasis role='strong'>not</emphasis> gparted - the GNOME partition editor. </para><para>"gpart - guess PC-type hard disk partitions" as the first line of the it's man page states (man gpart). And goes on to say: </para><para>"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." </para><para>This is exactly what we need. gpart is a very useful tool. </para><para>The problem is the following: the first block of any partition is marked. But it's never "unmarked" if not overwritten. So, many "first partition block" exist on an old disk, and gpart tries to do its 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><screen><![CDATA[root@charles:/home/jdd > gpart /dev/hdb
Begin scan...
Possible partition(Linux ext2), size(1200Mb), offset(0Mb)
Possible partition(Windows NTFS), size(1200Mb), offset(1200Mb)
Possible partition(Linux ext2), size(1004Mb), offset(2402Mb)
Possible partition(Windows NTFS), size(1600Mb), offset(4102Mb)
End scan.
Checking partitions...
* Warning: partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX) ends beyond disk end .
Partition(Linux ext2 filesystem): primary
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): primary
Partition(Linux ext2 filesystem): primary
Partition(OS/2 HPFS, NTFS, QNX or Advanced UNIX): invalid primary
Ok.
Guessed primary partition table:
Primary partition(1)
type: 131(0x83)(Linux ext2 filesystem)
size: 1200mb #s(2457880) s(63-2457942)
chs: (0/1/1)-(152/254/61)d (0/1/1)-(152/254/61)r
Primary partition(2)
type: 007(0x07)(OS/2 HPFS, NTFS, QNX or Advanced UNIX)
size: 1200mb #s(2457880) s(2457944-4915823)
chs: (152/254/63)-(305/253/60)d (152/254/63)-(305/253/60)r
Primary partition(3)
type: 131(0x83)(Linux ext2 filesystem)
size: 1004mb #s(2056256) s(4919781-6976036)
chs: (306/61/49)-(434/60/47)d (306/61/49)-(434/60/47)r
Primary partition(4)
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]]></screen><para>As you see, primary partition can be recovered, but, for extended ones, it's still to be done. </para><para>DOS partitions are labelled "Windows NTFS" because they were created while trying to install MS Windows 2000 (a very awful experience in year 2000!). The "invalid" 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 <inlinemediaobject><imageobject><imagedata width='16' fileref='http://wiki.tldp.org/moin_static172/gugiel/img/smile.png' depth='16'/></imageobject><textobject><phrase>:-)</phrase></textobject></inlinemediaobject> and so new versions may be more powerful than I know. </para></section><section><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 work. The process is: </para><orderedlist numeration='arabic'><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 "<command>e2fsk -n</command>", "<command>mount -r</command>", and "<command>df</command>" 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></section><section><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><screen><![CDATA[dd if=/dev/hd?? of=/tmp/recover_hd?? bs=512 skip=XXXX count=YYY]]></screen><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><screen><![CDATA[mount -r -t ext2 -o loop /tmp/recover_hd?? /mnt/recover]]></screen><para>Use <command>dd_rescue</command> if the disk is really badly damaged. </para></section></section></section><section><title>The rich man's case</title><para>Partition Magic is a commercial (and proprietary) software 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>Ralf's original partition-rescue mini HOWTO was essentially based around the use of Partition Magic, so I assume it's a very good solution, if you have valuable data on your Linux partition and little Linux capability. However, there are now much more recent releases of Partition Magic and I think it's better for you to read the manual. </para></section><section><title>PMagic</title><para><ulink url='http://partedmagic.com/wiki/PartedMagic.php'>PartedMagic</ulink> if the tool of choice for any partition work, including recovery. Extremely good produ