Welcome to the Linux Foundation Forum!

Lab 34.4. Recovering from GPT

Posts: 51
edited November 2024 in LFS207 Class Forum

Hi,

My Ubuntu VM is setup with a GPT partition table and LVM. This is what my system partitions look like:

  1. alu@ubuntu-dt-vm:~$ lsblk | grep -v loop
  2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
  3. sr0 11:0 1 5.8G 0 rom /media/alu/Ubuntu 24.04.1 LTS amd64
  4. vda 253:0 0 128G 0 disk
  5. \u251c\u2500vda1 253:1 0 1M 0 part
  6. \u251c\u2500vda2 253:2 0 2G 0 part /boot
  7. \u2514\u2500vda3 253:3 0 126G 0 part
  8. \u2514\u2500ubuntu--vg-ubuntu--lv 252:0 0 126G 0 lvm /

So I made backups form the GPT configuration from either fdisk and also gdisk. I saved the configs to a USB stick, as I could not access the backups anymore after destroying the GPT patition table.

So I destroyed the GPT by creating a new table in gdisk without any partitions. I'm not sure that was the right way to go though. After rebooting into the install media, I was able to recover the GPT partition table with the backups, but the system did not boot anyway. However, the partitions are back and I can mount and access the original root filesystem again.

Any Idea how to proceed with recovering GPT?

Thanks,
Urs

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Comments

  • Hi Urs,

    Can you put here all the commands you ran for items 1, 2 and 4, please? I think you used 'vda' instead 'sda', as shown in the guide.

    Regards,
    Luis.

  • Hi Luis,

    The vda is the main paravirualized disk of this VM which runs on KVM. It was automatically created like this during the installation of Ubuntu. So there is no sda disk, only this single vda. But the disk type should not matter I guess.

    I did not run either of the commands from step 1, 2 or 4 as these commands are related to the MBR partition (dump of the first couple of bytes on the disk with dd). So I only mad backups of the GPT partition table and destroyed it afterwards. This is what I meant when I mentioned that I don't know if that is the right way to go. Do I have to follow the steps 1, 2 and 4 and dump/destroy/recover the MBR partition with dd even if the VM's disk is configured with GPT?

    Regards,
    Urs

  • Sorry, this was the wrong topic. Can you please delete my last post?
    Thanks,
    Urs

  • Wrong post deleted.

  • Hi Luis,

    Thanks for deleting the post. Would be nice, if you could follow up on my last post from November 25.

    Regards,
    Urs

  • Hi Urs, I see it now.

    So, if you have a GPT partition you need to use the 'sgdisk' tool, as you can see in the man sgdisk(8) page:

    -b, --backup=file
    Save partition data to a backup file. You can back up your current in-memory partition table to a disk file using this option. The resulting file is a binary file consisting of the protective MBR, the main GPT header, the backup GPT header, and one copy of the partition table, in that order. Note that the backup is of the current in-memory data structures, so if you launch the program, make changes, and then use this option, the backup will reflect your changes. If the GPT data structures are damaged, the backup may not accurately reflect the damaged state; instead, they will reflect GPT fdisk's first-pass interpretation of the GPT.

    And then:

    -l, --load-backup=file
    Load partition data from a backup file. This option is the reverse of the -b option. Note that restoring partition data from anything but the original disk is not recommended. This option will work even if the disk's original partition table is bad; however, most other options on the same command line will be ignored.

    From this:

    So I made backups form the GPT configuration from either fdisk and also gdisk.

    I see you already ran gdisk and made a backup, perhaps with something like this, right?

    sgdisk --backup=gptsave

    So, that file generated before by the sgdisk command is the one that you should load with 'sgdisk --load-backup=gspsave' '.

    Regards,
    Luis.

  • Hi Luis,

    Thanks, now I got it. Not sure what the issue was the last time. I guess it was just a typo or I forgot to save the restored GPT tables in interactive mode. However, with sgdisk it worked like a charm. I'd suggest to update the according chapter and Lab to cover also the process of backup and recovery of GPT. In case someone will have difficultis too, here are all the steps which I went through:

    1. Backup GPT to a USB thumb drive with sgdisk:
    1. root@ubuntu-dt-vm:~# lsblk
    2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
    3. loop0 7:0 0 4K 1 loop /snap/bare/5
    4. loop1 7:1 0 74.3M 1 loop /snap/core22/1564
    5. loop2 7:2 0 73.9M 1 loop /snap/core22/1663
    6. loop3 7:3 0 269.8M 1 loop /snap/firefox/4793
    7. loop4 7:4 0 273M 1 loop /snap/firefox/5273
    8. loop5 7:5 0 10.7M 1 loop /snap/firmware-updater/127
    9. loop6 7:6 0 11.1M 1 loop /snap/firmware-updater/147
    10. loop7 7:7 0 505.1M 1 loop /snap/gnome-42-2204/176
    11. loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
    12. loop9 7:9 0 10.5M 1 loop /snap/snap-store/1173
    13. loop10 7:10 0 38.8M 1 loop /snap/snapd/21759
    14. loop11 7:11 0 500K 1 loop /snap/snapd-desktop-integration/178
    15. loop12 7:12 0 568K 1 loop /snap/snapd-desktop-integration/253
    16. loop13 7:13 0 10.7M 1 loop /snap/snap-store/1218
    17. loop14 7:14 0 150.3M 1 loop /snap/thunderbird/546
    18. loop15 7:15 0 150.3M 1 loop /snap/thunderbird/562
    19. loop16 7:16 0 44.3M 1 loop /snap/snapd/23258
    20. sda 8:0 1 57.3G 0 disk
    21. \u2514\u2500sda1 8:1 1 57.3G 0 part /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792
    22. sr0 11:0 1 5.8G 0 rom /media/alu/Ubuntu 24.04.1 LTS amd64
    23. vda 253:0 0 128G 0 disk
    24. \u251c\u2500vda1 253:1 0 1M 0 part
    25. \u251c\u2500vda2 253:2 0 2G 0 part /boot
    26. \u2514\u2500vda3 253:3 0 126G 0 part
    27. \u2514\u2500ubuntu--vg-ubuntu--lv 252:0 0 126G 0 lvm /
    28. root@ubuntu-dt-vm:~#
    29. root@ubuntu-dt-vm:~#
    30. root@ubuntu-dt-vm:~# sgdisk -b /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak /dev/vda
    31. The operation has completed successfully.
    32. root@ubuntu-dt-vm:~# ls -l /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak
    33. -rw-r--r-- 1 root root 17920 Dec 1 11:47 /media/alu/ed845a58-3e7c-4652-a256-261e29f1e792/sgdisk_gpt.bak
    34. root@ubuntu-dt-vm:~#
    35. root@ubuntu-dt-vm:~#
    36. root@ubuntu-dt-vm:~# umount /dev/sda1
    37. root@ubuntu-dt-vm:~#
    1. Destroy GPT partition table and reboot
    1. root@ubuntu-dt-vm:~# sgdisk --print-mbr /dev/vda
    2.  
    3. Disk size is 268435456 sectors (128.0 GiB)
    4. MBR disk identifier: 0x00000000
    5. MBR partitions:
    6.  
    7. Number Boot Start Sector End Sector Status Code
    8. 1 1 268435455 primary 0xEE
    9. root@ubuntu-dt-vm:~#
    10. root@ubuntu-dt-vm:~#
    11. root@ubuntu-dt-vm:~# sgdisk --print /dev/vda
    12. Disk /dev/vda: 268435456 sectors, 128.0 GiB
    13. Sector size (logical/physical): 512/512 bytes
    14. Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    15. Partition table holds up to 128 entries
    16. Main partition table begins at sector 2 and ends at sector 33
    17. First usable sector is 2048, last usable sector is 268435422
    18. Partitions will be aligned on 2048-sector boundaries
    19. Total free space is 2015 sectors (1007.5 KiB)
    20.  
    21. Number Start (sector) End (sector) Size Code Name
    22. 1 2048 4095 1024.0 KiB EF02
    23. 2 4096 4198399 2.0 GiB 8300
    24. 3 4198400 268433407 126.0 GiB 8300
    25. root@ubuntu-dt-vm:~#
    26. root@ubuntu-dt-vm:~#
    27. root@ubuntu-dt-vm:~# sgdisk -d 1 /dev/vda
    28. Warning: The kernel is still using the old partition table.
    29. The new table will be used at the next reboot or after you
    30. run partprobe(8) or kpartx(8)
    31. The operation has completed successfully.
    32. root@ubuntu-dt-vm:~# sgdisk -d 2 /dev/vda
    33. Warning: The kernel is still using the old partition table.
    34. The new table will be used at the next reboot or after you
    35. run partprobe(8) or kpartx(8)
    36. The operation has completed successfully.
    37. root@ubuntu-dt-vm:~# sgdisk -d 3 /dev/vda
    38. Warning: The kernel is still using the old partition table.
    39. The new table will be used at the next reboot or after you
    40. run partprobe(8) or kpartx(8)
    41. The operation has completed successfully.
    42. root@ubuntu-dt-vm:~#
    43. root@ubuntu-dt-vm:~#
    44. root@ubuntu-dt-vm:~# sgdisk --print /dev/vda
    45. Disk /dev/vda: 268435456 sectors, 128.0 GiB
    46. Sector size (logical/physical): 512/512 bytes
    47. Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    48. Partition table holds up to 128 entries
    49. Main partition table begins at sector 2 and ends at sector 33
    50. First usable sector is 2048, last usable sector is 268435422
    51. Partitions will be aligned on 2048-sector boundaries
    52. Total free space is 268433375 sectors (128.0 GiB)
    53.  
    54. Number Start (sector) End (sector) Size Code Name
    55. root@ubuntu-dt-vm:~#
    56. root@ubuntu-dt-vm:~#
    57. root@ubuntu-dt-vm:~# reboot
    1. Normal boot fails. Hence, boot from rescue media and restore GPT with sgdisk:
    1. root@ubuntu:~# lsblk
    2. NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
    3. loop0 7:0 0 1.6G 1 loop /rofs
    4. loop1 7:1 0 471M 1 loop
    5. loop2 7:2 0 874.4M 1 loop
    6. loop3 7:3 0 4K 1 loop /snap/bare/5
    7. loop4 7:4 0 74.3M 1 loop /snap/core22/1564
    8. loop5 7:5 0 269.8M 1 loop /snap/firefox/4793
    9. loop6 7:6 0 10.7M 1 loop /snap/firmware-updater/127
    10. loop7 7:7 0 505.1M 1 loop /snap/gnome-42-2204/176
    11. loop8 7:8 0 91.7M 1 loop /snap/gtk-common-themes/1535
    12. loop9 7:9 0 10.5M 1 loop /snap/snap-store/1173
    13. loop10 7:10 0 38.8M 1 loop /snap/snapd/21759
    14. loop11 7:11 0 500K 1 loop /snap/snapd-desktop-integration/178
    15. loop12 7:12 0 149.6M 1 loop /snap/thunderbird/507
    16. loop13 7:13 0 116.8M 1 loop /snap/ubuntu-desktop-bootstrap/237
    17. sda 8:0 1 57.3G 0 disk
    18. └─sda1 8:1 1 57.3G 0 part
    19. sr0 11:0 1 5.8G 0 rom /cdrom
    20. vda 253:0 0 128G 0 disk
    21. root@ubuntu:~#
    22. root@ubuntu:~#
    23. root@ubuntu:~# mount /dev/sda1 /mnt
    24. root@ubuntu:~# cd /mnt
    25. root@ubuntu:/mnt# ls -l sgdisk_gpt.bak
    26. -rw-r--r-- 1 root root 17920 Dec 1 10:47 sgdisk_gpt.bak
    27. root@ubuntu:/mnt#
    28. root@ubuntu:/mnt#
    29. root@ubuntu:/mnt# sgdisk -l /mnt/sgdisk_gpt.bak /dev/vda
    30. Warning: The kernel is still using the old partition table.
    31. The new table will be used at the next reboot or after you
    32. run partprobe(8) or kpartx(8)
    33. The operation has completed successfully.
    34. root@ubuntu:/mnt#
    35. root@ubuntu:/mnt#
    36. root@ubuntu:/mnt# sgdisk -p /dev/vda
    37. Disk /dev/vda: 268435456 sectors, 128.0 GiB
    38. Sector size (logical/physical): 512/512 bytes
    39. Disk identifier (GUID): 4472920E-CC02-4ACE-894D-ADBED8562A6E
    40. Partition table holds up to 128 entries
    41. Main partition table begins at sector 2 and ends at sector 33
    42. First usable sector is 2048, last usable sector is 268435422
    43. Partitions will be aligned on 2048-sector boundaries
    44. Total free space is 2015 sectors (1007.5 KiB)
    45.  
    46. Number Start (sector) End (sector) Size Code Name
    47. 1 2048 4095 1024.0 KiB EF02
    48. 2 4096 4198399 2.0 GiB 8300
    49. 3 4198400 268433407 126.0 GiB 8300
    50. root@ubuntu:/mnt#
    51. root@ubuntu:/mnt#
    52. root@ubuntu:/mnt# reboot

    So, that's it.

    Thanks again Louis for your support.

    Regards,
    Urs

  • Hi Urs,

    I'd suggest to update the according chapter and Lab to cover also the process of backup and recovery of GPT.

    Ok, it's a good suggestion, thanks.

    Thanks again Louis for your support.

    It's a pleasure! I'm glad that you are progressing in the course.

    Many regards,
    Luis.

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Welcome!

It looks like you're new here. Sign in or register to get started.
Sign In

Categories

Upcoming Training