1

After restoring a cloned OS image on a disk of arbitrary size (larger than that of the original image was captured from), I would like the extended partition in the MBR table to include all the available disk space so that new logical partitions could be added. The questions is how to accomplish this with a command line utility — without using interactive programs (especially those that have a GUI, like GParted) and, possibly, without inventing the wheel via sophisticated shell script that parses the output of fdisk and alike.

The simpliest method I found available in Linux text console is this:

  1. Use cfdisk TUI to interactively create a logical partition (automatically snapped to free space beyond existing extended partition), write the changes and exit.

  2. Use fdisk shell to interactively delete that newly created logical partition, write the changes and exit.

(It is only at the “write and exit” step of fdisk that the extended partition is resized — in order to fix the discrepancy, I guess: if the logical partition is deleted by cfdisk itself, then extended partition turns out to have unaltered size.)

The tools I tried that are not a direct answer to my question, and why:

  • cfdisk — not scriptable, does not actually adjust the extended partition size (at least when temporary logical partition is deleted; see above);

  • fdisk — does not allow to create logical partitions outside of the extended partition;

  • parted — scripting-friendly, but requires specifying exact start and end sectors (well, the last sector may be entered as -1s), and seems to behave like fdisk — at least I could not figure out valid start sector number, always receiving “Can't have overlapping partitions” error, even when passing in the values previously selected by cfdisk in default mode;

  • gdisk — just converts the MBR partition table to GPT, which is not an option for me;

  • sfdisk — dedicated to re-creating the entire partition table from scratch, so no different than invoking fdisk for the same exsessive set of actions.

I think this is a common task to adjust the size of extended partition, but surprisinlgy enough could not find any pre-existing solution.

Your Answer

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

Browse other questions tagged or ask your own question.