Linux – Removing extended partition without deleting logical in it

gpartedlinuxpartitioning

I'm running a Linux-based laptop, and in order to multi-boot several distros in it, I created an extended partition which contains a bunch of logical ones with GParted. Now, after quite a long time with this setup, I've changed my mind because of the consequent lack of storing space for my data partition. Now I want to keep one distro alone like it's normal, and eventually have some other operating systems stored in external supports to plug in and use if I want.

Obviously, also this partition I want to keep (and to enlarge a little too) is just a logical inside the extended I want to keep. For what concerns the number I'm ok, meaning I currently have this big distro dedicated extended, the swap and the data partitions, so there's space for another primary before I delete the extended, but I don't know how to delete it without touching the logical in it, I don't want to reinstall the system losing all changes and settings, and I don't want to keep an extended partition for a logical alone.

How can I do? Do I have to create a new primary, copy the logical content in it and then delete everything? Will the system boot and maintain exactly all the features it has now? Or is there a way to convert an extended into a primary once it contains just one logical? Or can I directly move a logical out of an extended turning it into a primary? Or, again, am I screwed?

Best Answer

Thanks for the answers, I eventually found some others methods too, I was just searching the wrong way: didn't know all I had to write was "turn logical into primary". The solution I found is the one knjers is suggesting, I suppose, but said like that in one sentence I didn't even understand wht he was talking about until I read that.

The post is this: Convert via terminal

If I had found the way to search solutions for this problem earlier I would have spared one useless post, but maybe some people would be in my same situation, so I'll resume the solution. Long story short, we need sfdisk. First run

sudo sfdisk -d /dev/sdX

to get your drive's detailed map (obviously, X stands for the device letter, run sudo fdisk -l to get your drive list if you don't know your drive's code).
Just copy that into a text file and manually edit the partition table into the one you want. Be careful, the chance of screwing everything up is pretty high. I tested on an USB stick before and it just works.

Anyway, all you have to do is figure out the map you want to create and express it in terms of sectors. Everything is easier if you are in a situation like mine (want to get rid of extended partition), as that means you can delete any logical but the one you wanna turn into a primary and have just one logical inside the extended, as big as it. You can get to this situation with an easier automatic tool like GParted. It's easier because you can just use the starting and ending point of the extended for the new primary, while if you want to keep the extended with several logicals in it and just move one of them outside of it (obviously, only possible if it is the last or first one -just move it if it isn't - and if you have three primary/extended partitions or less) the editing is a little more complex, as you have to correctly specify the new starting/ending points of the extended to contain all the other primaries etc.

Let's say for example you have this simple map: one single logical into an extended located at the very beginning, a swap and a huge data partition.

sudo sfdisk -d /dev/sda

partition table of /dev/sda
unit: sectors

/dev/sda1 : start= 2048, size=80000000, Id= f
/dev/sda2 : start=80002048, size= 800000, Id=82
/dev/sda3 : start= 80802048, size= 800000000, Id=83
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 4096, size=79997952, Id=83

Numbers are just made up by me to make it easier to see how it does work. As you see, the maximum number of "true" partitions possible is four, that means that every logical will start numbering from five, even if you have less than four primaries. Also, /dev/sda4 would be displayed as type 0 starting from 0 and big 0 also if you had just three partitions and no logicals. The threshold of four primaries is always displayed. Id 83 is Linux ext partition, and 82 is Linux swap (List of partition types with IDs). Now, if we want to keep this exact map except for logical partition replacing the extended as a logical one, we just paste the exact output of the command we got (from "partition table") and modify it this way:

partition table of /dev/sda
unit: sectors

/dev/sda1 : start= 4096, size=79997952, Id=83
/dev/sda2 : start=80002048, size= 800000, Id=82
/dev/sda3 : start= 80802048, size= 800000000, Id=83
/dev/sda4 : start= 0, size= 0, Id= 0

All we did was putting our logical, with his original limits (it starts a bit after and ends a bit before the extended, you have to put its own ones) in place of our extended (it now occupies a position with number < 5, reserved to extended or primaries) with ID 83 (Linux native filesystem, finally defining it as primary). This way, when we will import the new table, the chunk containing all files of our logical will be recognized as part of a primary, but none of them will be deleted, so it's just perfect. To import our new configuration, save our text file and do the following command:

sudo sfdisk --force /dev/sdX < path/to/file/yournewpartitiontable.txt

IMPORTANT: do this from a live session, it is fundamental your hard disk is not in use and all partitions are unmounted. If the drive you are modifying is not your system one (like a secondary hard drive or an external one or a USB pen drive/SD card), then fine, just make sure all of his partitions are unmounted.

GParted method:

While booting into GParted live to delete all other logical partitions and get the extended to contain all of and just the logical I wanted to switch to primary, I actually ran into a new method, just by chance (the one I used, so I can guarantee you it works, you just must have less than four partitions as you are gonna need a free backup block and actually create your new primary before being able to delete the extended).

GParted has a copy function which creates an exact copy of a partition into a free block big enough. All I did was copy my logical outside of the extended and then, after having assured everything worked just like in my original partition, deleting logical and extended and moving everything in place of those, extending my data partition. This method is safer as it is not 100% manual, but also muuuch longer (I did made my data partition occupy left free space, enlarging it from 380 to 440 GiB, that requested 5 hours) and maybe more hard disk stressful, as you are gonna do more actions in it instead of directly modifying partition table and symbolic information. But again, is also neater as you are doing all in one operation, occupying left free spaces and making partitions stick together without wasted space between them (with sfdisk you will give new primary the old logical's boundaries, which means starting after and finishing before, leaving unused space in between).

Hope this helps, cheers!

Related Question