### Create a new partition with sgdisk Source: https://www.rodsbooks.com/gdisk/sgdisk.html Example of creating a new partition on /dev/sdc using the -n option. ```bash sgdisk -n 2:2000:50000 /dev/sdc ``` -------------------------------- ### Start gdisk on a device Source: https://www.rodsbooks.com/gdisk/walkthrough.html Launch the gdisk utility on a specific device, such as /dev/sdf, to begin partition management. ```bash # **gdisk /dev/sdf** GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: MBR only BSD: not present APM: not present GPT: not present *************************************************************** Found invalid GPT and valid MBR; converting MBR to GPT format in memory. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if you don't want to convert your MBR partitions to GPT format! *************************************************************** Command (? for help): ``` -------------------------------- ### Example build failure output Source: https://www.rodsbooks.com/gdisk/download.html An example of error output indicating a missing development package, specifically popt.h. ```text g++ -Wall -D_FILE_OFFSET_BITS=64 -c -o sgdisk.o sgdisk.cc sgdisk.cc:13:18: error: popt.h: No such file or directory sgdisk.cc: In function 'int main(int, char**)': sgdisk.cc:58: error: 'poptContext' was not declared in this scope ``` -------------------------------- ### cgdisk prompts for creating a new partition Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html The four prompts cgdisk displays when creating a new partition: first sector, size in sectors or {KMGTP}, hex code or GUID for the partition type, and the partition name. The example shows entering 4G for size, 0700 for type (Microsoft basic data), and 'Shared data' for the name. ```text First sector (2048-30719966, default = 2048): Size in sectors or {KMGTP} (default = 30715904): **4G** Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): **0700** Current partition name is '' Enter new partition name, or to use the current name: **Shared data** ``` -------------------------------- ### Configure GRUB Legacy chainloading Source: https://www.rodsbooks.com/gdisk/hybrid.html Example configuration for GRUB Legacy to chainload a captured MBR image from a specific partition. ```text title OpenSolaris 2008.11 captured MBR root (hd0,1) chainloader (hd1,4)/hda-solaris-mbr.img ``` -------------------------------- ### Launch gdisk and display partition table Source: https://www.rodsbooks.com/gdisk/hybrid.html Start the gdisk utility for a specific device and use the 'r' command to enter the recovery/transformation menu, followed by 'p' to list the current partitions. ```bash # **gdisk /dev/sdc** GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: protective BSD: not present APM: not present GPT: present Found valid GPT with protective MBR; using GPT. Command (? for help): **r** recovery/transformation command (? for help): **p** Disk /dev/sdc: 15654912 sectors, 7.5 GiB Model: USB Flash Drive Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): E1F3535B-8F5B-1579-E04A-D8DEF36B8302 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 15654878 Partitions will be aligned on 8-sector boundaries Total free space is 6 sectors (3.0 KiB) Number Start (sector) End (sector) Size Code Name 1 40 3072007 1.5 GiB AF00 Apple HFS/HFS+ 2 3072008 7266311 2.0 GiB 8300 Linux filesystem 3 7266312 15654878 4.0 GiB 0700 Microsoft basic data recovery/transformation command (? for help): ``` -------------------------------- ### Create partitions with type codes using sgdisk -n and -t Source: https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html Creates two new partitions on /dev/sdf using default start points and sizes specified with +5G and +7G, then sets their type codes to 0700 (Microsoft basic data) and A503 (FreeBSD UFS). The -p option prints the resulting partition table, showing the new partitions and their attributes. ```bash # **sudo sgdisk -n 2:0:+5G -n 3:0:+7G -t 2:0700 -t 3:a503 /dev/sdf -p** Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 739CB37E-1201-47C2-9304-036EE67223F0 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 1359805 sectors (664.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 4196351 2.0 GiB 8300 2 4196352 14682111 5.0 GiB 0700 3 14682112 29362175 7.0 GiB A503 The operation has completed successfully. ``` -------------------------------- ### Launch cgdisk on /dev/sdf Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html Command to start cgdisk on a Linux device. Must be run with administrative privileges (e.g., via sudo or as root). The output shows the warning displayed when a non-GPT or damaged disk is detected, prompting the user to press any key to continue. ```bash # cgdisk /dev/sdf ``` ```text Warning! Non-GPT or damaged disk detected! This program will attempt to convert to GPT form or repair damage to GPT data structures, but may not succeed. Use gdisk or another disk repair tool if you have a damaged GPT disk. Press any key to continue.... ``` -------------------------------- ### Change a partition name with sgdisk Source: https://www.rodsbooks.com/gdisk/sgdisk.html Example of changing the GPT name of partition 1 on /dev/sdb to 'Sample Name'. ```bash sgdisk -c 1:"Sample Name" /dev/sdb ``` -------------------------------- ### Set partition attributes with sgdisk Source: https://www.rodsbooks.com/gdisk/sgdisk.html Example of setting the legacy BIOS bootable attribute (bit 2) on partition 4 of /dev/sdc. ```bash sgdisk -A 4:set:2 /dev/sdc ``` -------------------------------- ### Verify GPT Data Structures Output Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html Example output from the Verify command indicating no problems found and the availability of free sectors. ```text No problems found. 4029 free sectors (2.0 MiB) available in 2 segments, the largest of which is 2015 (1007.5 KiB) in size. ``` -------------------------------- ### gdisk: Create new partition with type code selection Source: https://www.rodsbooks.com/gdisk/walkthrough.html Interactive gdisk session using 'n' to create a new partition. Shows prompts for partition number, first and last sectors, and type code selection. The example includes listing codes with 'L', searching for 'data', and setting the type to 0700 (Microsoft basic data). ```bash Command (? for help): **n** Partition number (1-128, default 1): First sector (34-30719966, default = 2048) or {+-}size{KMGTP}: Last sector (2048-30719966, default = 30717951) or {+-}size{KMGTP}: **+8G** Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): **L** Type search string, or to show all codes: **data** 0700 Microsoft basic data 4200 Windows LDM data 4201 Windows LDM metadata a005 Android metadata a008 Android data a03a Android user data a200 Atari TOS basic data a580 Midnight BSD data c001 HP-UX data e900 Veracrypt data f101 Fuchsia durable mutable encrypted f103 Fuchsia factory ro system data f104 Fuchsia factory ro bootloader data f106 Fuchsia verified boot metadata (sl f10a Fuchsia Data f115 Fuchsia verified boot metadata (A) f116 Fuchsia verified boot metadata (B) f117 Fuchsia verified boot metadata (R) Hex code or GUID (L to show codes, Enter = 8300): **0700** Changed type of partition to 'Microsoft basic data' ``` -------------------------------- ### Run gdisk on a damaged disk Source: https://www.rodsbooks.com/gdisk/repairing.html This example shows the diagnostic output when gdisk detects a main partition table CRC mismatch and automatically loads the backup partition table. ```bash # **gdisk /dev/sdf** GPT fdisk (gdisk) version 1.0.9 Caution! After loading partitions, the CRC doesn't check out! Warning! Main partition table CRC mismatch! Loaded backup partition table instead of main partition table! Warning! One or more CRCs don't match. You should repair the disk! Main header: OK Backup header: OK Main partition table: ERROR Backup partition table: OK Partition table scan: MBR: protective BSD: not present APM: not present GPT: damaged **************************************************************************** Caution: Found protective or hybrid MBR and corrupt GPT. Using GPT, but disk verification and recovery are STRONGLY recommended. **************************************************************************** ``` -------------------------------- ### compare_gpts function Source: https://www.rodsbooks.com/gdisk/efi.c Compares primary and alternate GPT headers for discrepancies and logs warnings. It checks fields like LBA locations, GUIDs, and partition entry metadata. ```c static void compare_gpts(gpt_header *pgpt, gpt_header *agpt, u64 lastlba) { int error_found = 0; if (!pgpt || !agpt) return; if (le64_to_cpu(pgpt->my_lba) != le64_to_cpu(agpt->alternate_lba)) { printk(KERN_WARNING "GPT:Primary header LBA != Alt. header alternate_lba\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(pgpt->my_lba), (unsigned long long)le64_to_cpu(agpt->alternate_lba)); error_found++; } if (le64_to_cpu(pgpt->alternate_lba) != le64_to_cpu(agpt->my_lba)) { printk(KERN_WARNING "GPT:Primary header alternate_lba != Alt. header my_lba\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(pgpt->alternate_lba), (unsigned long long)le64_to_cpu(agpt->my_lba)); error_found++; } if (le64_to_cpu(pgpt->first_usable_lba) != le64_to_cpu(agpt->first_usable_lba)) { printk(KERN_WARNING "GPT:first_usable_lbas don't match.\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(pgpt->first_usable_lba), (unsigned long long)le64_to_cpu(agpt->first_usable_lba)); error_found++; } if (le64_to_cpu(pgpt->last_usable_lba) != le64_to_cpu(agpt->last_usable_lba)) { printk(KERN_WARNING "GPT:last_usable_lbas don't match.\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(pgpt->last_usable_lba), (unsigned long long)le64_to_cpu(agpt->last_usable_lba)); error_found++; } if (efi_guidcmp(pgpt->disk_guid, agpt->disk_guid)) { printk(KERN_WARNING "GPT:disk_guids don't match.\n"); error_found++; } if (le32_to_cpu(pgpt->num_partition_entries) != le32_to_cpu(agpt->num_partition_entries)) { printk(KERN_WARNING "GPT:num_partition_entries don't match: " "0x%x != 0x%x\n", le32_to_cpu(pgpt->num_partition_entries), le32_to_cpu(agpt->num_partition_entries)); error_found++; } if (le32_to_cpu(pgpt->sizeof_partition_entry) != le32_to_cpu(agpt->sizeof_partition_entry)) { printk(KERN_WARNING "GPT:sizeof_partition_entry values don't match: " "0x%x != 0x%x\n", le32_to_cpu(pgpt->sizeof_partition_entry), le32_to_cpu(agpt->sizeof_partition_entry)); error_found++; } if (le32_to_cpu(pgpt->partition_entry_array_crc32) != le32_to_cpu(agpt->partition_entry_array_crc32)) { printk(KERN_WARNING "GPT:partition_entry_array_crc32 values don't match: " "0x%x != 0x%x\n", le32_to_cpu(pgpt->partition_entry_array_crc32), le32_to_cpu(agpt->partition_entry_array_crc32)); error_found++; } if (le64_to_cpu(pgpt->alternate_lba) != lastlba) { printk(KERN_WARNING "GPT:Primary header thinks Alt. header is not at the end of the disk.\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(pgpt->alternate_lba), (unsigned long long)lastlba); error_found++; } if (le64_to_cpu(agpt->my_lba) != lastlba) { printk(KERN_WARNING "GPT:Alternate GPT header not at the end of the disk.\n"); printk(KERN_WARNING "GPT:%lld != %lld\n", (unsigned long long)le64_to_cpu(agpt->my_lba), (unsigned long long)lastlba); error_found++; } if (error_found) printk(KERN_WARNING "GPT: Use GNU Parted to correct GPT errors.\n"); return; } ``` -------------------------------- ### Bash script to create partitions with sgdisk Source: https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html This script creates a GPT partition table with a BIOS Boot Partition, EFI System Partition, Linux /boot, and Linux LVM. It uses sgdisk -E to get the last sector of free space for the LVM partition, allowing the script to work on disks of varying sizes. Note that sgdisk does not create filesystems, and the kernel may need a reboot or disk re-installation to recognize the new partition table. ```bash #!/bin/bash sgdisk -og "$1" sgdisk -n 1:2048:4095 -c 1:"BIOS Boot Partition" -t 1:ef02 "$1" sgdisk -n 2:4096:+550M -c 2:"EFI System Partition" -t 2:ef00 "$1" sgdisk -n 3:0:+1G -c 3:"Linux /boot" -t 3:8300 "$1" ENDSECTOR=$(sgdisk -E "$1") sgdisk -n 4:0:"$ENDSECTOR" -c 4:"Linux LVM" -t 4:8e00 "$1" sgdisk -p "$1" ``` -------------------------------- ### gdisk: Print partition table and delete partition Source: https://www.rodsbooks.com/gdisk/walkthrough.html Interactive gdisk session showing the 'p' command to display the GPT partition table and the 'd' command to delete the only partition. The output shows GPT-specific details: sector-based addressing, GUID disk identifier, and partition type code 0x0700 (Microsoft basic data). ```bash Command (? for help): **p** Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): ADBD4AAA-F174-4ADF-A74E-C5BBF0B29526 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 4029 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 30717951 14.6 GiB 0700 Microsoft basic data Command (? for help): **d** Using 1 Command (? for help): ``` -------------------------------- ### Validate PTE Entry Source: https://www.rodsbooks.com/gdisk/efi.c Tests a single Partition Table Entry (PTE) for validity by checking the GUID and LBA bounds. Returns 1 if valid, 0 otherwise. ```c /** * is_pte_valid() - tests one PTE for validity * @pte is the pte to check * @lastlba is last lba of the disk * * Description: returns 1 if valid, 0 on error. */ static inline int is_pte_valid(const gpt_entry *pte, const u64 lastlba) { if ((!efi_guidcmp(pte->partition_type_guid, NULL_GUID)) || le64_to_cpu(pte->starting_lba) > lastlba || le64_to_cpu(pte->ending_lba) > lastlba) return 0; return 1; } ``` -------------------------------- ### read_lba() Source: https://www.rodsbooks.com/gdisk/efi.c Reads a specified number of bytes from a block device starting at a given LBA. ```c static size_t read_lba(struct block_device *bdev, u64 lba, u8 * buffer, size_t count) { size_t totalreadcount = 0; sector_t n = lba * (bdev_logical_block_size(bdev) / 512); if (!bdev || !buffer || lba > last_lba(bdev)) return 0; while (count) { int copied = 512; Sector sect; unsigned char *data = read_dev_sector(bdev, n++, §); if (!data) break; if (copied > count) copied = count; memcpy(buffer, data, copied); put_dev_sector(sect); buffer += copied; totalreadcount +=copied; count -= copied; } return totalreadcount; } ``` -------------------------------- ### Create and Configure Partitions with gdisk Source: https://www.rodsbooks.com/gdisk/walkthrough.html This sequence shows creating two additional partitions, filtering for Linux and FreeBSD partition types, and assigning specific hex codes. ```text Command (? for help): **n** Partition number (2-128, default 2): First sector (34-30719966, default = 16779264) or {+-}size{KMGTP}: Last sector (16779264-30719966, default = 30717951) or {+-}size{KMGTP}: **+4G** Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): **L** Type search string, or to show all codes: **Linux** 8200 Linux swap 8300 Linux filesystem 8301 Linux reserved 8302 Linux /home 8303 Linux x86 root (/) 8304 Linux x86-64 root (/) 8305 Linux ARM64 root (/) 8306 Linux /srv 8307 Linux ARM32 root (/) 8308 Linux dm-crypt 8309 Linux LUKS 830a Linux IA-64 root (/) 830b Linux x86 root verity 830c Linux x86-64 root verity 830d Linux ARM32 root verity 830e Linux ARM64 root verity 830f Linux IA-64 root verity 8310 Linux /var 8311 Linux /var/tmp 8312 Linux user's home 8313 Linux x86 /usr 8314 Linux x86-64 /usr 8315 Linux ARM32 /usr 8316 Linux ARM64 /usr 8317 Linux IA-64 /usr 8318 Linux x86 /usr verity 8319 Linux x86-64 /usr verity 831a Linux ARM32 /usr verity 831b Linux ARM64 /usr verity 831c Linux IA-64 /usr verity 8500 Container Linux /usr 8501 Container Linux resizable rootfs 8502 Container Linux /OEM customization 8503 Container Linux root on RAID 8e00 Linux LVM fd00 Linux RAID Hex code or GUID (L to show codes, Enter = 8300): **8300** Changed type of partition to 'Linux filesystem' Command (? for help): **n** Partition number (3-128, default 3): First sector (34-30719966, default = 25167872) or {+-}size{KMGTP}: Last sector (25167872-30719966, default = 30717951) or {+-}size{KMGTP}: Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): **L** Type search string, or to show all codes: **FreeBSD** a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a506 FreeBSD nandfs Hex code or GUID (L to show codes, Enter = 8300): **a503** Changed type of partition to 'FreeBSD UFS' ``` -------------------------------- ### Create filesystems on Linux Source: https://www.rodsbooks.com/gdisk/walkthrough.html Commands to create a FAT32 filesystem and an ext4 filesystem on the specified partitions. ```bash # mkdosfs /dev/sdf1 # mkfs -t ext4 /dev/sdf2 ``` -------------------------------- ### Build software from source RPM Source: https://www.rodsbooks.com/gdisk/download.html Use the rpmbuild command to compile the software from a source RPM file. ```bash $ **rpmbuild --rebuild gptfdisk-1.0.10-1.src.rpm** ``` -------------------------------- ### Create Additional Partitions Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html Define new partitions by specifying size, type code, and name within the available free space. ```text First sector (8390656-30719966, default = 8390656): Size in sectors or {KMGTP} (default = 22327296): **6G** Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): Current partition name is '' Enter new partition name, or to use the current name: **Linux data** First sector (20973568-30719966, default = 20973568): Size in sectors or {KMGTP} (default = 9744384): Current type is 8300 (Linux filesystem) Hex code or GUID (L to show codes, Enter = 8300): **af00** Current partition name is '' Enter new partition name, or to use the current name: **HFS+ data** ``` -------------------------------- ### Writing changes to disk in gdisk Source: https://www.rodsbooks.com/gdisk/walkthrough.html Demonstrates the process of writing changes to the disk using the 'w' command and confirming the operation. ```text Expert command (? for help): **w** Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): **y** OK; writing new GUID partition table (GPT) to /dev/sdf. The operation has completed successfully. ``` -------------------------------- ### Access gdisk command help Source: https://www.rodsbooks.com/gdisk/walkthrough.html Type '?' at the command prompt to display the list of available gdisk commands. ```bash Command (? for help): **?** b back up GPT data to a file c change a partition's name d delete a partition i show detailed information on a partition l list known partition types n add a new partition o create a new empty GUID partition table (GPT) p print the partition table q quit without saving changes r recovery and transformation options (experts only) s sort partitions t change a partition's type code v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu ``` -------------------------------- ### Back up partition table with sfdisk Source: https://www.rodsbooks.com/gdisk/mbr2gpt.html Backs up the partition table to a text file using sfdisk. The backup can be restored with sfdisk. ```bash # **sfdisk -d /dev/sda > backup.txt** ``` -------------------------------- ### Convert GPT to MBR using gdisk recovery menu Source: https://www.rodsbooks.com/gdisk/mbr2gpt.html Interactive session showing the commands to enter the recovery/transformation menu, select the 'g' option, and view the current partition table with 'p'. The output displays the MBR partition table with notes about partition numbering and extended partitions. ```text Command (? for help): **r** Recovery/transformation command (? for help): **g** MBR command (? for help): **p** ** NOTE: Partition numbers do NOT indicate final primary/logical status, ** unlike in most MBR partitioning tools! ** Extended partitions are not displayed, but will be generated as required. Disk size is 3981312 sectors (1.9 GiB) MBR disk identifier: 0x00000000 MBR partitions: Can Be Can Be Number Boot Start Sector End Sector Status Logical Primary Code 1 2048 411647 primary Y 0x07 2 411648 1026047 primary Y 0xAF 3 1026056 1026255 primary Y Y 0x07 4 1026256 1435855 omitted 0x07 5 1435856 2050255 omitted Y 0x8E 6 2050264 2459863 logical Y 0xA5 7 2459872 3074271 logical Y 0x07 8 3074272 3981262 omitted 0xBF MBR command (? for help): ``` -------------------------------- ### Select partitions and configure EFI GPT placement Source: https://www.rodsbooks.com/gdisk/hybrid.html Specify the GPT partition numbers to include in the hybrid MBR and choose whether to place the EFI GPT (0xEE) partition first in the MBR table. ```bash Type from one to three GPT partition numbers, separated by spaces, to be added to the hybrid MBR, in sequence: **1 3** Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): **y** ``` -------------------------------- ### Enable gdisk write access on FreeBSD Source: https://www.rodsbooks.com/gdisk/walkthrough.html Use this command to allow gdisk to write to a disk on FreeBSD when partitions are mounted. ```bash sysctl kern.geom.debugflags=16 ``` -------------------------------- ### Accessing the gdisk experts' menu Source: https://www.rodsbooks.com/gdisk/walkthrough.html Displays the available commands in the experts' menu, accessed by typing 'x' at the main or recovery menus. ```text recovery/transformation command (? for help): **x** Expert command (? for help): ? a set attributes b byte-swap a partition's name c change partition GUID d display the sector alignment value e relocate backup data structures to the end of the disk f randomize disk and partition unique GUIDs g change disk GUID h recompute CHS values in protective/hybrid MBR i show detailed information on a partition j move the main partition table l set the sector alignment value m return to main menu n create a new protective MBR o print protective MBR data p print the partition table q quit without saving changes r recovery and transformation options (experts only) s resize partition table t transpose two partition table entries u replicate partition table on new device v verify disk w write table to disk and exit z zap (destroy) GPT data structures and exit ? print this menu ``` -------------------------------- ### Assign Partition Names with c command Source: https://www.rodsbooks.com/gdisk/walkthrough.html Use the 'c' command to rename partitions, followed by the 'p' command to verify the changes. ```text Command (? for help): **c** Partition number (1-3): **1** Enter name: **Shared (FAT) data** Command (? for help): **c** Partition number (1-3): **2** Enter name: **Linux ext4fs data** Command (? for help): **p** Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): ADBD4AAA-F174-4ADF-A74E-C5BBF0B29526 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 4029 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 16779263 8.0 GiB 0700 Shared (FAT) data 2 16779264 25167871 4.0 GiB 8300 Linux ext4fs data 3 25167872 30717951 2.6 GiB A503 FreeBSD UFS ``` -------------------------------- ### Delete and create partitions with sgdisk -d and -n Source: https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html Deletes partition 1 and creates a new partition using the -d and -n flags, while also converting the disk to GPT format with -g. ```bash # **sgdisk -d 1 -n 1:2048:4196351 -g /dev/sdf** *************************************************************** Found invalid GPT and valid MBR; converting MBR to GPT format in memory. *************************************************************** Warning! Secondary partition table overlaps the last partition by 33 blocks! You will need to delete this partition or resize it in another utility. The operation has completed successfully. ``` -------------------------------- ### Initiate hybrid MBR creation Source: https://www.rodsbooks.com/gdisk/hybrid.html Use the 'h' command in the recovery/transformation menu to begin the hybrid MBR creation process. This will trigger a warning about the risks involved. ```bash recovery/transformation command (m for help): **h** WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one, just hit the Enter key at the below prompt and your MBR partition table will be untouched. ``` -------------------------------- ### Review partitions with sgdisk -p Source: https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html Displays the partition table of the specified device. The -p option is informational and does not save changes to the disk. ```bash # **sgdisk -p /dev/sdf** *************************************************************** Found invalid GPT and valid MBR; converting MBR to GPT format in memory. *************************************************************** Warning! Secondary partition table overlaps the last partition by 33 blocks! You will need to delete this partition or resize it in another utility. Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): B29DF43E-CA6B-4525-B2A1-8A045A010F1F Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 4029 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 30717951 14.6 GiB 0700 Microsoft basic data ``` -------------------------------- ### Back up MBR with dd Source: https://www.rodsbooks.com/gdisk/hybrid.html Use the dd command to back up the entire MBR to an image file. Changing the block size (bs) to 440 allows backing up only the boot loader code. ```bash # **dd if=/dev/sda of=/boot/sda-mbr-backup-1.img bs=512 count=1** ``` -------------------------------- ### Add partition names with sgdisk -c and verify with -p Source: https://www.rodsbooks.com/gdisk/sgdisk-walkthrough.html Adds names to existing partitions using the -c option, with quotes required for names containing spaces. The subsequent -p command prints the partition table to verify the names are applied. ```bash # **sgdisk -c 1:"Linux data" -c 2:"Shared FAT" -c 3:FreeBSD /dev/sdf** The operation has completed successfully. # **sudo sgdisk -p /dev/sdf** Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): 739CB37E-1201-47C2-9304-036EE67223F0 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 1359805 sectors (664.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 4196351 2.0 GiB 8300 Linux data 2 4196352 14682111 5.0 GiB 0700 Shared FAT 3 14682112 29362175 7.0 GiB A503 FreeBSD ``` -------------------------------- ### Back up and Restore MBR Partition Data with sfdisk Source: https://www.rodsbooks.com/gdisk/wipegpt.html Use sfdisk to create a backup of the partition table to a file and restore it if necessary. Replace /dev/sdf with the target disk identifier. ```bash # **sfdisk -d /dev/sdf > parts.txt** ``` ```bash **sfdisk -f /dev/sdf < parts.txt** ``` -------------------------------- ### Inspect Partitions with p command Source: https://www.rodsbooks.com/gdisk/walkthrough.html Use the 'p' command to display the current partition table, including sector ranges, sizes, type codes, and names. ```text Command (? for help): **p** Disk /dev/sdf: 30720000 sectors, 14.6 GiB Model: STORE N GO Sector size (logical/physical): 512/512 bytes Disk identifier (GUID): ADBD4AAA-F174-4ADF-A74E-C5BBF0B29526 Partition table holds up to 128 entries Main partition table begins at sector 2 and ends at sector 33 First usable sector is 34, last usable sector is 30719966 Partitions will be aligned on 2048-sector boundaries Total free space is 4029 sectors (2.0 MiB) Number Start (sector) End (sector) Size Code Name 1 2048 16779263 8.0 GiB 0700 Microsoft basic data 2 16779264 25167871 4.0 GiB 8300 Linux filesystem 3 25167872 30717951 2.6 GiB A503 FreeBSD UFS ``` -------------------------------- ### Launch gdisk for Disk Analysis Source: https://www.rodsbooks.com/gdisk/wipegpt.html Open the disk in gdisk to inspect partition table data without the -l option. ```bash # **gdisk /dev/sdf** ``` -------------------------------- ### Back up MBR with dd Source: https://www.rodsbooks.com/gdisk/mbr2gpt.html Backs up the MBR from /dev/sda to backup.mbr. Ensure if and of parameters are correct; reversing them will wipe out the MBR. ```bash # **dd if=/dev/sda of=backup.mbr bs=512 count=1** ``` -------------------------------- ### Accessing the Recovery and Transformation Menu Source: https://www.rodsbooks.com/gdisk/repairing.html Displays the available commands within the recovery and transformation menu by entering 'r' at the main prompt and '?' at the recovery prompt. ```text Command (? for help): **r** recovery/transformation command (? for help): **?** b use backup GPT header (rebuilding main) c load backup partition table from disk (rebuilding main) d use main GPT header (rebuilding backup) e load main partition table from disk (rebuilding backup) f load MBR and build fresh GPT from it g convert GPT into MBR and exit h make hybrid MBR i show detailed information on a partition l load partition data from a backup file m return to main menu o print protective MBR data p print the partition table q quit without saving changes t transform BSD disklabel partition v verify disk w write table to disk and exit x extra functionality (experts only) ? print this menu ``` -------------------------------- ### Search for Partition Type Codes Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html Use the 'L' command at the code entry prompt to list available partition type codes, optionally filtering by a search string. ```text Hex code or GUID (L to show codes, Enter = 8300): **L** Type search string, or to show all codes: **data** 0700 Microsoft basic data 4200 Windows LDM data 4201 Windows LDM metadata a005 Android metadata a008 Android data a03a Android user data a200 Atari TOS basic data a580 Midnight BSD data c001 HP-UX data e900 Veracrypt data f101 Fuchsia durable mutable encrypted f103 Fuchsia factory ro system data f104 Fuchsia factory ro bootloader data f106 Fuchsia verified boot metadata (sl f10a Fuchsia Data f115 Fuchsia verified boot metadata (A) f116 Fuchsia verified boot metadata (B) f117 Fuchsia verified boot metadata (R) ``` -------------------------------- ### Backup disk with dd Source: https://www.rodsbooks.com/gdisk/repairing.html Creates a complete low-level backup of a disk to another device. Ensure the 'if' (input file) and 'of' (output file) parameters are correct to avoid overwriting the source disk. ```bash # **dd if=/dev/sda of=/dev/sdb status=progress** ``` -------------------------------- ### Zap GPT Data using gdisk Expert Menu Source: https://www.rodsbooks.com/gdisk/wipegpt.html Navigate to the expert menu and use the z command to destroy GPT data structures while keeping the MBR intact by answering n to the blanking prompt. ```bash Command (? for help): **x** Expert command (? for help): **z** About to wipe out GPT on /dev/sdf. Proceed? (Y/N): **y** GPT data structures destroyed! You may now partition the disk using fdisk or other utilities. Blank out MBR? (Y/N): **n** MBR is unchanged. You may need to delete an EFI GPT (0xEE) partition with fdisk or another tool. ``` -------------------------------- ### View Updated Partition List Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html The cgdisk interface displays the current partition table, including free space segments. ```text cgdisk 1.0.9 Disk Drive: /dev/sdf Size: 30720000,, 14.6 GiB Part. # Size Partition Type Partition Name ---------------------------------------------------------------- 1007.0 KiB free space 1 4.0 GiB Microsoft basic data Shared data 10.6 GiB free space [ Align ] [ Backup ] [ Help ] [ Load ] [ New ] [ Quit ] [ Verify ] [ Write ] Create new partition from free space ``` -------------------------------- ### GRUB entry for FreeBSD multi-boot Source: https://www.rodsbooks.com/gdisk/booting.html GRUB menu entry that chainloads the FreeBSD boot loader from a copied MBR file. The root (hd0,2) is the FreeBSD root partition and (hd1,5) is the Linux /boot partition containing the MBR copy. ```plaintext title FreeBSD 7.1 root (hd0,2) chainloader (hd1,5)/sda.mbr ``` -------------------------------- ### GPT fdisk MBR conversion final confirmation prompt Source: https://www.rodsbooks.com/gdisk/mbr2gpt.html When you type 'w' at the MBR command prompt, GPT fdisk displays this prompt asking for final confirmation. Typing 'Y' erases the GPT structures and writes the new MBR; typing 'N' leaves the disk untouched but rebuilds the protective MBR. ```text Converted 7 partitions. Finalize and exit? (Y/N): ``` -------------------------------- ### Define MBR hex codes and bootable flags Source: https://www.rodsbooks.com/gdisk/hybrid.html Assign MBR hex codes to the selected partitions, set their bootable flags, and optionally use remaining space to protect additional partitions. ```bash Creating entry for partition #1 Enter an MBR hex code (default AF): **af** Set the bootable flag? (Y/N): **n** Creating entry for partition #3 Enter an MBR hex code (default 07): **0c** Set the bootable flag? (Y/N): **y** Unused partition space(s) found. Use one to protect more partitions? (Y/N): **y** Enter an MBR hex code (EE is EFI GPT, but may confuse MacOS): **0a** ``` -------------------------------- ### Restore partition table backup with sfdisk Source: https://www.rodsbooks.com/gdisk/mbr2gpt.html Restores a partition table backup created with sfdisk -d. Use this to reverse a conversion if needed. ```bash # **sfdisk -f /dev/sda < backup.txt** ``` -------------------------------- ### cgdisk main menu display Source: https://www.rodsbooks.com/gdisk/cgdisk-walkthrough.html The main cgdisk menu after clearing warnings, showing the version, disk device, size, partition list, and option list. The display is divided into summary lines, a partition list, and an option list with a highlighted option and a summary line. ```text cgdisk 1.0.9 Disk Drive: /dev/sdf Size: 30720000, 14.6 GiB Part. # Size Partition Type Partition Name ---------------------------------------------------------------- 1007.0 KiB free space 1 14.6 GiB Microsoft basic data Microsoft basic data [ Align ] [ Backup ] [ Help ] [ Load ] [ New ] [ Quit ] [ Verify ] [ Write ] Create new partition from free space ``` -------------------------------- ### efi_partition Function Source: https://www.rodsbooks.com/gdisk/efi.c The efi_partition function is called from check.c to set up GPT partition entries in the kernel. It returns 1 on success, 0 if it is not a GPT table, and -1 on read errors. ```c int efi_partition(struct parsed_partitions *state, struct block_device *bdev) { gpt_header *gpt = NULL; gpt_entry *ptes = NULL; u32 i; unsigned ssz = bdev_logical_block_size(bdev) / 512; if (!find_valid_gpt(bdev, &gpt, &ptes) || !gpt || !ptes) { kfree(gpt); kfree(ptes); return 0; } pr_debug("GUID Partition Table is valid! Yea!\n"); for (i = 0; i < le32_to_cpu(gpt->num_partition_entries) && i < state->limit-1; i++) { u64 start = le64_to_cpu(ptes[i].starting_lba); u64 size = le64_to_cpu(ptes[i].ending_lba) - le64_to_cpu(ptes[i].starting_lba) + 1ULL; if (!is_pte_valid(&ptes[i], last_lba(bdev))) continue; put_partition(state, i+1, start * ssz, size * ssz); /* If this is a RAID volume, tell md */ if (!efi_guidcmp(ptes[i].partition_type_guid, PARTITION_LINUX_RAID_GUID)) state->parts[i+1].flags = 1; } kfree(ptes); kfree(gpt); printk("\n"); return 1; } ``` -------------------------------- ### Identify GPT data with gdisk Source: https://www.rodsbooks.com/gdisk/wipegpt.html The gdisk utility explicitly detects both MBR and GPT data and prompts the user to choose which table to use. ```bash # **gdisk -l /dev/sdf** GPT fdisk (gdisk) version 1.0.9 Partition table scan: MBR: MBR only BSD: not present APM: not present GPT: present Found valid MBR and GPT. Which do you want to use? 1 - MBR 2 - GPT 3 - Create blank GPT Your answer: ``` -------------------------------- ### alloc_read_gpt_entries() Source: https://www.rodsbooks.com/gdisk/efi.c Allocates memory and reads GPT partition entries from the disk. ```c static gpt_entry * alloc_read_gpt_entries(struct block_device *bdev, gpt_header *gpt) { size_t count; gpt_entry *pte; if (!bdev || !gpt) return NULL; count = le32_to_cpu(gpt->num_partition_entries) * le32_to_cpu(gpt->sizeof_partition_entry); if (!count) return NULL; pte = kzalloc(count, GFP_KERNEL); if (!pte) return NULL; if (read_lba(bdev, le64_to_cpu(gpt->partition_entry_lba), (u8 *) pte, count) < count) { kfree(pte); pte=NULL; return NULL; } return pte; } ```