Wednesday, May 04, 2016

Converting a primary partition to a logical partition

MBR partitioning only allows for 4 partitions in the MBR itself. One of those can be an extended partition, which then allows for an unlimited number of extended partitions. If you already have 4 partitions in the MBR and no extended partition, and you want to create a partition, then you must convert one of the partitions into an extended partition. This can be done manually:
  1. Shrink the partition before the partition being converted. This frees up space for the extended partition table. Shrink by at least 2 megabytes.
  2. Note the start and end sectors of the partition you are converting, and then delete it.
  3. Create the extended partition. The start sector needs to be before the start sector of the partition being converted. With Linux fdisk it normally needs to be 2048 sectors (one megabyte) before, but with the -c=dos option it can be only 63 sectors before. There is no point in aligning the partition for an SSD, as what matters is alignment of partitions with data inside them.
  4. Create a new logical partition with the exact same start and end sectors of the partition you deleted. Set it to the type the old partition had.
  5. If the partition was bootable, you may need to fix bootability by reconfiguring the boot manager.
  6. Grow the partition you shrunk at the beginning, using up all the space up to the extended partition.
When loading GRUB from a partition, it will fail to load if that partition's number changes, and you'll get the GRUB rescue prompt. It's best to just type in commands there to make GRUB work, and then reinstall GRUB once you get to the command prompt. This example is for when Linux is moved to the first logical partition, numbered 5.
set root=(hd0,msdos5)
set prefix=(hd0,msdos5)/boot/grub
insmod normal
normal 

I wrote the bootloader for chain booting Linux into the first sector of the extended partition /dev/sda4. Make sure you only write the code, which is 400 bytes in that case, and don't overwrite the partition table there. This makes Linux boot if the extended partition is set to be active. Some software doesn't like when an extended partition is active, so this is just an idea, not a recommendation.

No comments: