1

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:~$ 
  • Could you please post text files, dialogue messages, and program output listings as text, not as images? To achieve the latter two you can either 1) select, copy & paste the dialogue text or terminal content or 2) save the program output to a file and use that. Longer listings (the editor will tell you what's too long) should be uploaded to a pastie service and linked to in the question. Thanks. – David Foerster Jul 24 '17 at 8:23
  • On another note, what you should or need to do depends on what you want to achieve. What do you want to achieve? Please edit your question to clarify that. – David Foerster Jul 24 '17 at 8:25
1

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 a start and a size file with the starting location and size of the partition, in sectors. For instance:

$ cat /sys/block/sda/sda2/start
1126440
$ cat /sys/block/sda/sda2/size
1536000

This indicates that, on my system, /dev/sda2 starts at sector 1126440 and is 1536000 sectors in size. You can extract this information and, using gdisk, 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 the sudo efibootmgr -v output -- it will be embedded in the string that's part of the ubuntu line. You can use gdisk to change this value after you've re-created the ESP -- use the c 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 a b option on the main menu to create such a backup. If you'd done so, you could restore the backup with the l (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.

| improve this answer | |
  • Unfortunately my system was not working anymore when you answered. – manu at Kubuntu Oct 2 '17 at 14:09
  • So I reinstalled the system :) (most comfortable way, personal data was backed up anyway) From now on I use sudo dd if=/dev/sdx | pv | gzip > image.dump.gz for backup and gzip -dc image.dump.gz | pv | sudo dd of=/dev/sdx for recover :) – manu at Kubuntu Oct 2 '17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.