How to merge two partitions by using gdisk

How to merge two partitions, one is system, the lower offset, and the other destroyable content, the adjacent higher offset, by using gdisk (preferably if none else is better) ?
Seems more like a job for parted to be honest.
What type of partition is the main partition? Are the two partitions next to each other?
If you have a separate hard drive/storage device then back up all data first.
Below are separate instructions for fdisk and gparted;
https://askubuntu.com/questions/66000/how-to-merge-partitions

I would recommend the gparted route for ease of use, but if you are doing this with a device that you're ssh'd or otherwise only has a TUI then fdisk should work.

https://forum.manjaro.org/t/how-do-i-move-a-partition-in-gparted/56724

Edit:
Ha, funny how quick google is with those stack overflow questions: https://unix.stackexchange.com/questions/725405/how-to-merge-two-partitions-by-using-gdisk
https://www.linuxquestions.org/questions/linux-software-2/how-to-merge-two-partitions-by-using-gdisk-4175718886/

The answer on the second link has a good point, you don't want to try this from a currently running partition. Use a liveCD or a run from a thumbdrive.
Last edited on
Lets try analogous C++ case:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
char disk[5000]; // The disk is an array of "sectors"
std::string_view partition_table[4]; // Traditional MBR table

partition_table[0] = std::string_view( disk+10, disk+110 );
partition_table[1] = std::string_view( disk+110, disk+2110 );

// Now first partition shows 100 characters disk[10]..disk[109]
// Second partition shows 2000 characters disk[110]..disk[2109]
// Partitions do not affect the data on the disk
// It is an error, if the views are overlapping

// Lets "initialize" the parts of disk shown by partitions:
for ( char& s : partition_table[0] ) s = 'a';
for ( char& s : partition_table[1] ) s = 'b';

// With disks the "initialization" is called "formatting"
// and it writes structures of filesystem into the sectors

// What you can do with "gdisk" is:
partition_table[1] = std::string_view(); // "delete" partition
partition_table[0] = std::string_view( disk+10, disk+2110 ); // modify begin and end

// These do not change the data on sectors disk[10]..disk[2109]

// When OS looks for filesystem, it looks from partition_table[0]
// and finds the one that starts at disk[10] 

One still has to "expand" the filesystem to use the entire view it is in,
but that is done with a tool that the filesystem must provide.
Registered users can post here. Sign in or register to post.