How to Recover Data After Running Fdisk on the Wrong Drive

cfdiskdata-recoveryfdisk

I ran fdisk on the wrong HD. I basically cleared partition table, pressed w and quited fdisk and only then realized that that was my main harddrive…

I got this message:

WARNING: Re-reading the partition table failed with error 16: Device
or resource busy. The kernel still uses the old table. The new table
will be used at the next reboot or after you run partprobe(8) or
kpartx(8) Syncing disks.

I looked through /sys/class/block/sdb/sdb* start and size files – I knew that that's were kernel kept old partition offsets and sizes until restart… And here's my table reconstruction.

name  |  startsector  | size, sectors  | type?                
------------------------------------------------------------     
sdb1  |  2048         | 2048           |   
sdb2  |  195510270    | 2              |   
sdb3  |  4096         | 195504128      | /  
sdb5  |  195510272    | 761716736      | /home  
sdb6  |  957229056    | 19542016       | swap partition   

Here's testing for what type of partition there were:

cr1tter@cr1tterhost:~$ sudo file -s /dev/sdb{1,2,3,5,6}
/dev/sdb1: data
/dev/sdb2: x86 boot sector; partition 1: ID=0x83, starthead 254, startsector 2, 761716736 sectors; partition 2: ID=0x5, starthead 254, startsector 761716738, 19544064 sectors, code offset 0x97
/dev/sdb3: Linux rev 1.0 ext4 filesystem data, UUID=b6d939b9-c197-4513-a1fe-9c614574afb0 (needs journal recovery) (extents) (large files) (huge files)
/dev/sdb5: Linux rev 1.0 ext4 filesystem data, UUID=96f97705-f292-49db-b31d-249fab87d3d7 (needs journal recovery) (extents) (large files) (huge files)
/dev/sdb6: Linux/i386 swap file (new style), version 1 (4K pages), size 2442751 pages, no label, UUID=a5744b72-248c-4ceb-8009-db6351cdc231

I guess I'm confused what sdb1 and sdb2 are? I honestly cannot recall what they are…. When I was installing os – I had to specify paths, as in /home, or root path: / – fdisk doesn't seem to allow to do that…

P.S> RESOLVED. Thank you Jim Paris! HERE's the transcript of what I've done. I've already rebooted and system is working just as well..!

sudo fdisk /dev/sdb
Command (m for help): p

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cd649

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-976773167, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-976773167, default 976773167): +1M

Command (m for help): p

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cd649

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048        4095        1024   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2): 3
First sector (4096-976773167, default 4096): 
Using default value 4096
Last sector, +sectors or +size{K,M,G} (4096-976773167, default 976773167): +95461M

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 83

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e
Partition number (1-4, default 2): 
Using default value 2
First sector (195508224-976773167, default 195508224): 
Using default value 195508224
Last sector, +sectors or +size{K,M,G} (195508224-976773167, default 976773167): 
Using default value 976773167

Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (195510272-976773167, default 195510272): 
Using default value 195510272
Last sector, +sectors or +size{K,M,G} (195510272-976773167, default 976773167): +371932M

Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 83

Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (957229056-976773167, default 957229056): 
Using default value 957229056
Last sector, +sectors or +size{K,M,G} (957229056-976773167, default 976773167): +9542M

Command (m for help): t
Partition number (1-6): 6
Hex code (type L to list codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list codes): 82 
Changed system type of partition 6 to 82 (Linux swap / Solaris)


Command (m for help): a
Partition number (1-6): 1


Command (m for help): p

Disk /dev/sdb: 500.1 GB, 500107862016 bytes
255 heads, 63 sectors/track, 60801 cylinders, total 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000cd649

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1   *        2048        4095        1024   83  Linux
/dev/sdb2       195508224   976773167   390632472    5  Extended
/dev/sdb3            4096   195508223    97752064   83  Linux
/dev/sdb5       195510272   957227007   380858368   83  Linux
/dev/sdb6       957229056   976771071     9771008   82  Linux swap / Solaris

Partition table entries are not in disk order

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

Best Answer

fdisk just makes partitions; it's up to other things like your kernel command line and the contents of /etc/fstab to decide how to use them. When you told the installer that e.g. /dev/sdb5 was to be used as /home, it did a bunch of things to make that happen (set the partition table, format the partition, adjust fstab, etc).

To answer your specific question:

  • It looks like sdb1 is your boot partition, used to hold the kernel in a location where grub can use BIOS calls to retrieve it. Probably ext2, but you can use file -s /dev/sdb1 to try to figure out the format.

  • sdb2 looks like an extended partition. Normal MS-DOS type partition tables can only have 4 primary partitions, numbered 1 through 4. If you create one of them with type 0x05, then you can create further logical partitions with higher numbers.

  • sdb3 looks like your root partition.

It's a little weird that your sdb3 and sdb2 are out of order, but that's possible. My guess is you need to do something like this in fdisk:

  • new partition table
  • new primary partition 1, start 2048, size +2047
  • change ID of partition 1 to 0x83 (Linux)
  • new extended partition 2, start 195504128, size (maximum)
  • new primary partition 3, start 4096, size +195504127
  • change ID of partition 3 to 0x83 (Linux)
  • new logical partition 5, start 195510272, size +761716735
  • change ID of partition 5 to 0x83 (Linux)
  • new logical partition 6, start 957229056, size +19542015
  • change ID of partition 6 to 0x82 (Linux Swap)
Related Question