Linux How To Recover Partition Table from Kernel and Restore Centos/Debian etc.. -

Linux How To Recover Partition Table from Kernel and Restore Centos/Debian etc..

We've all done this at some point, you work on the wrong shell window and this was my first time making this mistake but I deleted a partition table in fdisk, recreated it and saved it with "wq" and even ran partprobe!  If you haven't rebooted yet then you can still recover your partition table, otherwise you're in big trouble.

Fortunately since it was a live system and in use the kernel still had to use the old table like below:

Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (Device or resource busy).  As a result, it may not reflect all of your changes until after reboot.
Warning: Error fsyncing/closing /dev/md127: Input/output error
Warning: Error fsyncing/closing /dev/md126: Input/output error
Warning: Error fsyncing/closing /dev/md125: Input/output error

Other places on the internet don't properly explain the procedure for recovery which can be done but you need the right side of instructions and info.

Step 1 -Find your device directory in /sys/block/sda/sda1 (change the /sda and /sda1 to match your disk device name and partition number).

You have to do a cat on the 'start' and 'size' which represents the starting sector of the partition and the 'size' represents how many sectors it is. 


#take the size value and subtract 1 from it or you will not get the original or correct result
fdisk /dev/sda
#press u to change to sectors which is what /sys/block/sda goes by
 /sys/block/sda
[root@server1 sda]# cat sda1/start
2048
[root@server1 sda]# cat sda1/size
1024000

[root@server1 sda]# cat sda2/start
1026048
[root@server1 sda]# cat sda2/size
1464121344
 

 Step 2 -Use fdisk to take the above sector information and input it (remembering to subtract 1 from the size when telling fdisk the end sector)

"Press u" to switch to sector mode once you run:

fdisk /dev/sda



Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First sector (63-1465149167, default 63): 2048
Last sector, +sectors or +size{K,M,G} (2048-1465149167, default 1465149167): +1023999

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First sector (63-1465149167, default 63): 1026048
Last sector, +sectors or +size{K,M,G} (1026048-1465149167, default 1465149167): +1464121343

##########


and for those who do a normal fdisk -l /dev/sda and wonder why you can't duplicate the cylinder setup is because you need to press "u" and deal with sectors.

Using sectors shows that you can actually end and start a partition on the same cylinder and in other ways cylinders will not work for:


   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       91202   732060672   83  Linux




###########


  • bash Linux how to get first or last letters of a word
  • l2tp ipsec VPN Error Sep 12 18:16:25 vps pluto[7299]: ERROR: asynchronous network error report on eth0 (sport=500) for message to 192.5.6.2 port 20640, complainant 192.5.6.2: Connection refused [errno 111, origin ICMP type 3 code 3 (not authenticated
  • Centos 5.9 Working Vault Repo file
  • Disable SSH Password Authentication to Increase Security and Harden SSH Linux Unix Server Ubuntu Mint Centos Debian
  • ecryptfs how to mount or recover from a backup
  • Linux bash script to see what connected computers respond to ping
  • rsync specify alternate port non-standard port than 22
  • Centos 6.6/6.9 KVM VM Kernel Panic On Boot - Kernel panic - not syncing: Attempted to kill init!
  • Cannot allocate TUN/TAP dev dynamically - FreeBSD/OpenBSD OpenVPN Client error Solution
  • Linux Mint/Ubuntu/Debian apt how to downgrade a package
  • Ubuntu Linux Mint How To Exclude Stop Package from being Upgraded or Installed
  • Intel NUC Lower Power Green Computing Boxes Review/Comparison of J3160 and J3455 Models
  • How to backup entire computer Linux Mint Ubuntu with tar
  • tar how to ignore failed reads and not exit
  • kdenlive titles/text renders as white screen when using .sh script
  • [1035724.274610] [drm:intel_pipe_update_end [i915_bpo]] *ERROR* Atomic update failure on pipe A (start=62076478 end=62076479) time 102 us, min 894, max 899, scanline start 893, end 900 W: Possible missing firmware /lib/firmware/i915/kbl_dmc_ver1.bin
  • ffmpeg how to watermark and concatenate in one command
  • /usr/bin/ld: cannot find -lboost_system-mt-s /usr/bin/ld: cannot find -lboost_filesystem-mt-s /usr/bin/ld: cannot find -lboost_program_options-mt-s /usr/bin/ld: cannot find -lboost_thread-mt-s collect2: error: ld returned 1 exit status make: *** [cag
  • Wine uninstalled broken on Linux Mint
  • ffmpeg trouble concatenating similar but different files