This page looks best with JavaScript enabled

Moving a Primary Partition To an Extended Partition

 ·  ☕ 3 min read

Background

This article explains how to move a primary (root) partition to an extended partition. There are several ways this task can be performed; however, I chose to keep my system generally intact, introduce minimal changes, and use no external storage (other than a SD card for the LiveCD).

I had a disk layout as follows:

  • swap (2gb) - /dev/sda1
  • /boot (120mb) - /dev/sda2
  • / (root) (6gb) - /dev/sda3
  • extended partition - /dev/sda4
    • /tmp (1gb) - /dev/sda5
    • /home (15gb) - /dev/sda6
    • /var (1gb) - /dev/sda7

As you can see from the above, there is not a lot of space on this disk, so partition sizes were minimal. Subsequently, /var was separated out from the root partition to give a bit more room for root post install. After putting up with small root and /var space for several months I finally ran into a dist-upgrade that took more than the space available in /var so I was forced to deal with the issue.

I decided that /home could shrink (and later be supplemented with an SD card) to grow root, the only issue is I had the above layout so growing root meant moving the extended partition which was not possible.

Details

  • move all of the data on /var to a temp location on the /home partition
  • shrink /home partition by 7gb
  • delete /var partition
  • create a new (to be root) partition out of the 7gb from /home and 1gb from /var
  • move all data from / (old root) to new root partition
  • update /etc/fstab to point to new root partition and to no longer mount the /var partition

The above was easy enough with a GParted LiveCD. The only issue is that the original root, identified in GRUB was not on the same disk, consequently, grub needed to be updated to find the new root partition. There are several ways to do this but I chose to chroot and run update-grub. The process is as follows:

  • boot from a LiveCD (make sure the architecture [32-bit vs 64-bit] on the LiveCD matches the architecture of your root partition)
  • create a mountpoint for your chroot: $ mkdir -p /mnt/newroot
  • mount the new root partition to the mountpoint (change sdaX with the partition/device id of your new root partition): $ mount /dev/sdaX /mnt/newroot
  • mount the boot partition to the new root: $ mount /dev/sda2 /mnt/newroot/boot
  • mount critical virtual filesystems: $ for i in /dev /dev/pts /proc /sys /run; do sudo mount -B $i /mnt/newroot$i; done
  • chroot: $ chroot /mnt/newroot
  • update grub: $ update-grub

Reboot and test changes, all went well for me so:

  • boot back into the GParted LiveCD
  • delete the original root partition (/dev/sda3)
  • move/grew the extended partition to encompass the original root partition
  • adjust the size of my new root partition (needed to grow it another 1.5gb)
  • add the remaining space back to my /home partition

It sounds like a lot of (risky) steps but it was fairly straightforward and took roughly an hour (most of the time was time to shrink the original /home partition). I now have enough space and, more importantly, a better laid out disk!

I now have the following disk layout:

  • swap (2gb) - /dev/sda1
  • /boot (120mb) - /dev/sda2
  • extended partition - /dev/sda4
    • /tmp (1gb) - /dev/sda5
    • /home (14gb) - /dev/sda6
    • / (root) (8gb) - /dev/sda7

Notes

Everyone has different needs for their disk layouts, primarily because we all use our systems differently. I generally do little work to disks after my initial layout unless I run out of space; however, I will stick to the following guidelines when laying out general purpose disks in the future due to this experience:

  • create your boot and swap partitions as primary partitions
  • create all remaining partitions as extended partitions

If your swap has the potential for change (in size) then create it as an extended as well.

Share on

drad
WRITTEN BY
drad
Sr. Consultant