Yesterday I accidentally created a new (boot) partition with gdisk
on my main drive instead of my external drive. However this created a new partition table, but since I am running Kubuntu on this drive the Kernel is not updated yet.
What's the fastest way to get bootable Windows & Kubuntu partitions with all my data and settings?
- I did a
dd
backup, but I think this will only contain the new partition? - I have still access to all the files, so I also zipped them together.
- Do I have to reinstall kubuntu & Windows or is there a way to undo the
gdisk
operation? - Shall I shut the Computer down and use testdisk on a live system?
gdisk -l
output:
manu@manus-Laptop:~$ sudo gdisk -l /dev/sda
[sudo] password for manu:
GPT fdisk (gdisk) version 1.0.1
Partition table scan:
MBR: protective
BSD: not present
APM: not present
GPT: present
Found valid GPT with protective MBR; using GPT.
Disk /dev/sda: 468862128 sectors, 223.6 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): 62FADE32-E715-4AC6-BA77-307107A2454F
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 468862094
Partitions will be aligned on 2048-sector boundaries
Total free space is 468861108 sectors (223.6 GiB)
Number ___ Start (sector) _ _ End (sector) _ _ _ Size _ _ _ Code _ _ _ Name
______1____________2048___________ 3000 _ _ _ 476.5 KiB _ EF02 _ _ BIOS boot partition
manu@manus-Laptop:~$
I know of three ways to do what you want, given your current situation:
Method 1: Easy but Risky and Slow
The first method is to use TestDisk, which can scan a disk for filesystem data and generate new partition table entries to match what it finds. This approach is relatively automated, and hence easy; but the tools must scan much of the disk, which can take a while. Worse, TestDisk may be thrown by stray data from old partitions you've deleted, thus producing bad results. Thus, there's a chance that TestDisk will generate a completely wrong partition table; or it might give you a number of choices about what to recover, and you may not be able to decide which is correct.
Although TestDisk can recover partition start and end points, and is likely to set the partition type code correctly, it will not be able to restore the partition's original GUID. This can be important for booting, as noted in the next section.
Note that TestDisk (or some similar tool; it's not the only one in this category) will become your only choice once you shut down or reboot the computer.
Method 2: Harder but Safer and Faster
Because your kernel is still using the old partition table, the data on your partitions can still be retrieved from memory. The question is how to do so, and the answer is to look in the
/sys/block/sda
directory tree. That directory should have subdirectories for each partition (sda1
,sda2
, and so on), and each of these directories has astart
and asize
file with the starting location and size of the partition, in sectors. For instance:This indicates that, on my system,
/dev/sda2
starts at sector 1126440 and is 1536000 sectors in size. You can extract this information and, usinggdisk
, re-create the partition table -- or at least, create partitions with the correct sizes. You'll need to use your knowledge of what's in each partition to set their type codes. AFAIK, there's no way to recover GUID values (the same is true of the previous approach). The GUID value of the ESP may be important, since it's likely to be used by the EFI as part of its boot process. You should be able to extract this from thesudo efibootmgr -v
output -- it will be embedded in the string that's part of theubuntu
line. You can usegdisk
to change this value after you've re-created the ESP -- use thec
option on the experts' menu.Method 3: Best and Easiest But You Probably Can't Do It
The best and easiest way to do what you want is to restore the partition table from a backup. The
gdisk
tool offers ab
option on the main menu to create such a backup. If you'd done so, you could restore the backup with thel
(lowercase "L") option on the recovery & transformation menu.Restoring a backup in this way will restore all the partition table data to the way it had been when the backup was made. This includes GUID values, partition names, and of course partition sizes. It's conceivable that you've made such a backup but have forgotten it. If so, use it. If not, though, this won't do you any good, except insofar as you now know about this option, and may be motivated to make a backup for use in the future.