8

I have an Ubuntu Mate Trusty installation on VB which I want to migrate to a physical laptop. By following this guide i was able to get it up and running. Only issue now is I want to expand the current partition to fill up the rest of the disk, and it is encrypted and using LVM. Gparted doesn't support this type of operation, so I have to resort to the CLI. However, I don't understand anything about manually resizing partitions and all the guides I can find are either focused on shrinking the partition or are horribly outdated.

Can some partition guru provide a guide for me to follow?

Thanks

5

After fiddling around for a long time, and failing multiple times (thank god for backups) I was able to resize it. Here goes my guide.

This assumes you have a partition table like mine: sda1 is swap and around 250MB, sda2 is a container with sda5 (our crypt volume) and a lot of free space in front of sda2, like so

|sda1|sda2 (sda5)|free space|

Boot to a LiveCD of your favourite distro. Run gparted and shrink your sda1 partition by around 20MB. This is because fdisk requires a buffer of unallocated space between the start of sda2 and sda5, dunno why. While you're at it increase the size of sda2 as far as it can go, all the way to the right. You should end up with

|sda1|free space|sda2 (sda5)|

Go ahead and launch fdisk

sudo fdisk /dev/sda

press p to print out the current partition scheme. Write down where sda5 starts.

press d to delete first sda5 then sda2. Scary.

Now press n to create a new volume. Press e to select type extended and number it 2. The defaults for start and end should be ok.

Now create a new partition, type is Linux (should be default) and number it 5. The start value should be the same as the one you wrote down for sda5 before, the end value is the default one (as far as it can go).

Press p again to be sure that everything is OK and press w to write your changes to disk.

Reboot into your live CD again.

If you open up gparted you should now have

|sda1|sda2 (free space) (sda5)|

Follow these steps:

Decrypt your file system.

sudo cryptsetup luksOpen /dev/sda5 crypt1

Get the live CD to recognize (activate) your LVM.

sudo vgscan --mknodes
sudo vgchange -ay

Resize the Crypt.

sudo cryptsetup resize crypt1

Resize the (LVM) Physical Volume.

sudo pvresize /dev/mapper/crypt1

Resize your root (LVM) Logical Volume.

Unlock the (LVM) Physical Volume.

 sudo pvchange -x y /dev/mapper/crypt1

Resize the (LVM) Physical Volume.

 sudo lvresize -l +100%FREE /dev/ubuntu-vg/root

Re-lock the physical volume.

 sudo pvchange -x n /dev/mapper/crypt1

Resize the filesystem.

sudo e2fsck -f /dev/mapper/ubuntu--vg-root
sudo resize2fs -p /dev/mapper/ubuntu--vg-root

Now you should be OK to reboot and if everything went well you should have a bigger encrypted partition.

Note: This method has the downside of robbing you of around 20MB of swap. If the swap resize isn't done, fdisk won't let you write sda5 in the required position (which may lead to corruption, I suspect). This caveat is necessary until someone explains exactly what fdisk is doing behind the scenes and if there is any way to get it to do our bidding.

  • 1
    Perfect! Worked great for me - I cloned my laptop hard-drive to a larger one, and wanted to use the extra space. Only difference was mine boots with UEFI, so the partition is /dev/sda3 not sda5, so made changes accordingly. Also I did not seem to need to take the 20 MB out of sda2, it worked fine for me just changing /dev/sda3. Thanks! – seanlano Aug 28 '17 at 12:28
  • Thanks! just 2 notes. Before starting, I had to resize the extended partition (sda2) that contains the encrypted one (sda5). I also skipped the 3rd before last command, did the last 2 resize command and just then locked using the command I skipped. – ozma Mar 4 at 20:44
0

I'm trying to follow these instructions and I do have a concern. For me - /dev/sda5 is the extended volume and contains the OS and everything. Once you fdisk, delete and re-create the partitions, then write those changes to disk - does this not erase all that information?

I tried the method as listed, and when I reboot back into the live cd, and then get to the part of "sudo cryptsetup resize crypt1" - I get a message saying "Device crypt1 is not active" and nothing works after that.

I am trying this on a vbox instance, so at least I've been making backups but I have not been able to get it work properly yet.

What am I missing?

0

After "cryptsetup resize" just didn't work I've got to this solution:

Make sure the /dev/sdX fits for your disk! Note that I am not sure yet if it will impact grub. Run from ubuntu live cd.

  • open gparted
  • resize /dev/sda2 to the desired size using gparted
  • decrypt:

    sudo cryptsetup luksOpen /dev/sda5 crypt1

  • resize /dev/sda5 to the desired size using gparted

You can make sure everything went okay by mounting and browsing your drive

sudo mount /dev/mapper/ubuntu--vg-root

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.